Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "structmember.h" |
| 3 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4 | PyDoc_STRVAR(pickle_module_doc, |
| 5 | "Optimized C implementation for the Python pickle module."); |
| 6 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 8 | module _pickle |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 9 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 10 | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
| 11 | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
| 12 | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 13 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 14 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 15 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 16 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 17 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 18 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 19 | DEFAULT_PROTOCOL = 3 |
| 20 | }; |
| 21 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 22 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 23 | Extensive docs are in pickletools.py. */ |
| 24 | enum opcode { |
| 25 | MARK = '(', |
| 26 | STOP = '.', |
| 27 | POP = '0', |
| 28 | POP_MARK = '1', |
| 29 | DUP = '2', |
| 30 | FLOAT = 'F', |
| 31 | INT = 'I', |
| 32 | BININT = 'J', |
| 33 | BININT1 = 'K', |
| 34 | LONG = 'L', |
| 35 | BININT2 = 'M', |
| 36 | NONE = 'N', |
| 37 | PERSID = 'P', |
| 38 | BINPERSID = 'Q', |
| 39 | REDUCE = 'R', |
| 40 | STRING = 'S', |
| 41 | BINSTRING = 'T', |
| 42 | SHORT_BINSTRING = 'U', |
| 43 | UNICODE = 'V', |
| 44 | BINUNICODE = 'X', |
| 45 | APPEND = 'a', |
| 46 | BUILD = 'b', |
| 47 | GLOBAL = 'c', |
| 48 | DICT = 'd', |
| 49 | EMPTY_DICT = '}', |
| 50 | APPENDS = 'e', |
| 51 | GET = 'g', |
| 52 | BINGET = 'h', |
| 53 | INST = 'i', |
| 54 | LONG_BINGET = 'j', |
| 55 | LIST = 'l', |
| 56 | EMPTY_LIST = ']', |
| 57 | OBJ = 'o', |
| 58 | PUT = 'p', |
| 59 | BINPUT = 'q', |
| 60 | LONG_BINPUT = 'r', |
| 61 | SETITEM = 's', |
| 62 | TUPLE = 't', |
| 63 | EMPTY_TUPLE = ')', |
| 64 | SETITEMS = 'u', |
| 65 | BINFLOAT = 'G', |
| 66 | |
| 67 | /* Protocol 2. */ |
| 68 | PROTO = '\x80', |
| 69 | NEWOBJ = '\x81', |
| 70 | EXT1 = '\x82', |
| 71 | EXT2 = '\x83', |
| 72 | EXT4 = '\x84', |
| 73 | TUPLE1 = '\x85', |
| 74 | TUPLE2 = '\x86', |
| 75 | TUPLE3 = '\x87', |
| 76 | NEWTRUE = '\x88', |
| 77 | NEWFALSE = '\x89', |
| 78 | LONG1 = '\x8a', |
| 79 | LONG4 = '\x8b', |
| 80 | |
| 81 | /* Protocol 3 (Python 3.x) */ |
| 82 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 83 | SHORT_BINBYTES = 'C', |
| 84 | |
| 85 | /* Protocol 4 */ |
| 86 | SHORT_BINUNICODE = '\x8c', |
| 87 | BINUNICODE8 = '\x8d', |
| 88 | BINBYTES8 = '\x8e', |
| 89 | EMPTY_SET = '\x8f', |
| 90 | ADDITEMS = '\x90', |
| 91 | FROZENSET = '\x91', |
| 92 | NEWOBJ_EX = '\x92', |
| 93 | STACK_GLOBAL = '\x93', |
| 94 | MEMOIZE = '\x94', |
| 95 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 98 | enum { |
| 99 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 100 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 101 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 102 | help anything either. */ |
| 103 | BATCHSIZE = 1000, |
| 104 | |
| 105 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 106 | checking for self-referential data-structures. */ |
| 107 | FAST_NESTING_LIMIT = 50, |
| 108 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 109 | /* Initial size of the write buffer of Pickler. */ |
| 110 | WRITE_BUF_SIZE = 4096, |
| 111 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 112 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 113 | PREFETCH = 8192 * 16, |
| 114 | |
| 115 | FRAME_SIZE_TARGET = 64 * 1024, |
| 116 | |
| 117 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 120 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 121 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 122 | /* State of the pickle module, per PEP 3121. */ |
| 123 | typedef struct { |
| 124 | /* Exception classes for pickle. */ |
| 125 | PyObject *PickleError; |
| 126 | PyObject *PicklingError; |
| 127 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 128 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 129 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 130 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 131 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 132 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 133 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 134 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 135 | PyObject *extension_registry; |
| 136 | /* copyreg._extension_cache, {code: object} */ |
| 137 | PyObject *extension_cache; |
| 138 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 139 | PyObject *inverted_registry; |
| 140 | |
| 141 | /* Import mappings for compatibility with Python 2.x */ |
| 142 | |
| 143 | /* _compat_pickle.NAME_MAPPING, |
| 144 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 145 | PyObject *name_mapping_2to3; |
| 146 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 147 | PyObject *import_mapping_2to3; |
| 148 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 149 | PyObject *name_mapping_3to2; |
| 150 | PyObject *import_mapping_3to2; |
| 151 | |
| 152 | /* codecs.encode, used for saving bytes in older protocols */ |
| 153 | PyObject *codecs_encode; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 154 | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
| 155 | PyObject *getattr; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 156 | } PickleState; |
| 157 | |
| 158 | /* Forward declaration of the _pickle module definition. */ |
| 159 | static struct PyModuleDef _picklemodule; |
| 160 | |
| 161 | /* Given a module object, get its per-module state. */ |
| 162 | static PickleState * |
| 163 | _Pickle_GetState(PyObject *module) |
| 164 | { |
| 165 | return (PickleState *)PyModule_GetState(module); |
| 166 | } |
| 167 | |
| 168 | /* Find the module instance imported in the currently running sub-interpreter |
| 169 | and get its state. */ |
| 170 | static PickleState * |
| 171 | _Pickle_GetGlobalState(void) |
| 172 | { |
| 173 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 174 | } |
| 175 | |
| 176 | /* Clear the given pickle module state. */ |
| 177 | static void |
| 178 | _Pickle_ClearState(PickleState *st) |
| 179 | { |
| 180 | Py_CLEAR(st->PickleError); |
| 181 | Py_CLEAR(st->PicklingError); |
| 182 | Py_CLEAR(st->UnpicklingError); |
| 183 | Py_CLEAR(st->dispatch_table); |
| 184 | Py_CLEAR(st->extension_registry); |
| 185 | Py_CLEAR(st->extension_cache); |
| 186 | Py_CLEAR(st->inverted_registry); |
| 187 | Py_CLEAR(st->name_mapping_2to3); |
| 188 | Py_CLEAR(st->import_mapping_2to3); |
| 189 | Py_CLEAR(st->name_mapping_3to2); |
| 190 | Py_CLEAR(st->import_mapping_3to2); |
| 191 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 192 | Py_CLEAR(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Initialize the given pickle module state. */ |
| 196 | static int |
| 197 | _Pickle_InitState(PickleState *st) |
| 198 | { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 199 | PyObject *builtins; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 200 | PyObject *copyreg = NULL; |
| 201 | PyObject *compat_pickle = NULL; |
| 202 | PyObject *codecs = NULL; |
| 203 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 204 | builtins = PyEval_GetBuiltins(); |
| 205 | if (builtins == NULL) |
| 206 | goto error; |
| 207 | st->getattr = PyDict_GetItemString(builtins, "getattr"); |
| 208 | if (st->getattr == NULL) |
| 209 | goto error; |
| 210 | Py_INCREF(st->getattr); |
| 211 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 212 | copyreg = PyImport_ImportModule("copyreg"); |
| 213 | if (!copyreg) |
| 214 | goto error; |
| 215 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 216 | if (!st->dispatch_table) |
| 217 | goto error; |
| 218 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 219 | PyErr_Format(PyExc_RuntimeError, |
| 220 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 221 | Py_TYPE(st->dispatch_table)->tp_name); |
| 222 | goto error; |
| 223 | } |
| 224 | st->extension_registry = \ |
| 225 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 226 | if (!st->extension_registry) |
| 227 | goto error; |
| 228 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 229 | PyErr_Format(PyExc_RuntimeError, |
| 230 | "copyreg._extension_registry should be a dict, " |
| 231 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 232 | goto error; |
| 233 | } |
| 234 | st->inverted_registry = \ |
| 235 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 236 | if (!st->inverted_registry) |
| 237 | goto error; |
| 238 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 239 | PyErr_Format(PyExc_RuntimeError, |
| 240 | "copyreg._inverted_registry should be a dict, " |
| 241 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 242 | goto error; |
| 243 | } |
| 244 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 245 | if (!st->extension_cache) |
| 246 | goto error; |
| 247 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 248 | PyErr_Format(PyExc_RuntimeError, |
| 249 | "copyreg._extension_cache should be a dict, " |
| 250 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 251 | goto error; |
| 252 | } |
| 253 | Py_CLEAR(copyreg); |
| 254 | |
| 255 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 256 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 257 | if (!compat_pickle) |
| 258 | goto error; |
| 259 | st->name_mapping_2to3 = \ |
| 260 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 261 | if (!st->name_mapping_2to3) |
| 262 | goto error; |
| 263 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 264 | PyErr_Format(PyExc_RuntimeError, |
| 265 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 266 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 267 | goto error; |
| 268 | } |
| 269 | st->import_mapping_2to3 = \ |
| 270 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 271 | if (!st->import_mapping_2to3) |
| 272 | goto error; |
| 273 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 274 | PyErr_Format(PyExc_RuntimeError, |
| 275 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 276 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 277 | goto error; |
| 278 | } |
| 279 | /* ... and the 3.x -> 2.x mapping tables */ |
| 280 | st->name_mapping_3to2 = \ |
| 281 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 282 | if (!st->name_mapping_3to2) |
| 283 | goto error; |
| 284 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 285 | PyErr_Format(PyExc_RuntimeError, |
| 286 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 287 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 288 | goto error; |
| 289 | } |
| 290 | st->import_mapping_3to2 = \ |
| 291 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 292 | if (!st->import_mapping_3to2) |
| 293 | goto error; |
| 294 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 295 | PyErr_Format(PyExc_RuntimeError, |
| 296 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 297 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 298 | goto error; |
| 299 | } |
| 300 | Py_CLEAR(compat_pickle); |
| 301 | |
| 302 | codecs = PyImport_ImportModule("codecs"); |
| 303 | if (codecs == NULL) |
| 304 | goto error; |
| 305 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 306 | if (st->codecs_encode == NULL) { |
| 307 | goto error; |
| 308 | } |
| 309 | if (!PyCallable_Check(st->codecs_encode)) { |
| 310 | PyErr_Format(PyExc_RuntimeError, |
| 311 | "codecs.encode should be a callable, not %.200s", |
| 312 | Py_TYPE(st->codecs_encode)->tp_name); |
| 313 | goto error; |
| 314 | } |
| 315 | Py_CLEAR(codecs); |
| 316 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 317 | return 0; |
| 318 | |
| 319 | error: |
| 320 | Py_CLEAR(copyreg); |
| 321 | Py_CLEAR(compat_pickle); |
| 322 | Py_CLEAR(codecs); |
| 323 | _Pickle_ClearState(st); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | /* Helper for calling a function with a single argument quickly. |
| 328 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 329 | This function steals the reference of the given argument. */ |
| 330 | static PyObject * |
| 331 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 332 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 333 | PyObject *result; |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 334 | PyObject *arg_tuple = PyTuple_New(1); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 335 | |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 336 | /* Note: this function used to reuse the argument tuple. This used to give |
| 337 | a slight performance boost with older pickle implementations where many |
| 338 | unbuffered reads occurred (thus needing many function calls). |
| 339 | |
| 340 | However, this optimization was removed because it was too complicated |
| 341 | to get right. It abused the C API for tuples to mutate them which led |
| 342 | to subtle reference counting and concurrency bugs. Furthermore, the |
| 343 | introduction of protocol 4 and the prefetching optimization via peek() |
| 344 | significantly reduced the number of function calls we do. Thus, the |
| 345 | benefits became marginal at best. */ |
| 346 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 347 | if (arg_tuple == NULL) { |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 348 | Py_DECREF(obj); |
| 349 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 350 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 351 | PyTuple_SET_ITEM(arg_tuple, 0, obj); |
| 352 | result = PyObject_Call(func, arg_tuple, NULL); |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 353 | Py_CLEAR(arg_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 354 | return result; |
| 355 | } |
| 356 | |
| 357 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 358 | |
| 359 | static int |
| 360 | stack_underflow(void) |
| 361 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 362 | PickleState *st = _Pickle_GetGlobalState(); |
| 363 | PyErr_SetString(st->UnpicklingError, "unpickling stack underflow"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | /* Internal data type used as the unpickling stack. */ |
| 368 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 369 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 370 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 371 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 372 | } Pdata; |
| 373 | |
| 374 | static void |
| 375 | Pdata_dealloc(Pdata *self) |
| 376 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 377 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 378 | while (--i >= 0) { |
| 379 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 380 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 381 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 382 | PyObject_Del(self); |
| 383 | } |
| 384 | |
| 385 | static PyTypeObject Pdata_Type = { |
| 386 | PyVarObject_HEAD_INIT(NULL, 0) |
| 387 | "_pickle.Pdata", /*tp_name*/ |
| 388 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 389 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 390 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 391 | }; |
| 392 | |
| 393 | static PyObject * |
| 394 | Pdata_New(void) |
| 395 | { |
| 396 | Pdata *self; |
| 397 | |
| 398 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 399 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 400 | Py_SIZE(self) = 0; |
| 401 | self->allocated = 8; |
| 402 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 403 | if (self->data) |
| 404 | return (PyObject *)self; |
| 405 | Py_DECREF(self); |
| 406 | return PyErr_NoMemory(); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Retain only the initial clearto items. If clearto >= the current |
| 411 | * number of items, this is a (non-erroneous) NOP. |
| 412 | */ |
| 413 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 414 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 415 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 416 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 417 | |
| 418 | if (clearto < 0) |
| 419 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 420 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 423 | while (--i >= clearto) { |
| 424 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 425 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 426 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int |
| 431 | Pdata_grow(Pdata *self) |
| 432 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 433 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 434 | size_t allocated = (size_t)self->allocated; |
| 435 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 437 | new_allocated = (allocated >> 3) + 6; |
| 438 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 439 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 440 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 441 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 442 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 443 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 444 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 445 | |
| 446 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 447 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 448 | return 0; |
| 449 | |
| 450 | nomemory: |
| 451 | PyErr_NoMemory(); |
| 452 | return -1; |
| 453 | } |
| 454 | |
| 455 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 456 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 457 | * is raised and V is set to NULL. |
| 458 | */ |
| 459 | static PyObject * |
| 460 | Pdata_pop(Pdata *self) |
| 461 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 462 | if (Py_SIZE(self) == 0) { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 463 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 464 | PyErr_SetString(st->UnpicklingError, "bad pickle data"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 467 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 468 | } |
| 469 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 470 | |
| 471 | static int |
| 472 | Pdata_push(Pdata *self, PyObject *obj) |
| 473 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 474 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 475 | return -1; |
| 476 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 477 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 482 | #define PDATA_PUSH(D, O, ER) do { \ |
| 483 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 484 | |
| 485 | /* Push an object on stack, adding a new reference to the object. */ |
| 486 | #define PDATA_APPEND(D, O, ER) do { \ |
| 487 | Py_INCREF((O)); \ |
| 488 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 489 | |
| 490 | static PyObject * |
| 491 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 492 | { |
| 493 | PyObject *tuple; |
| 494 | Py_ssize_t len, i, j; |
| 495 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 496 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 497 | tuple = PyTuple_New(len); |
| 498 | if (tuple == NULL) |
| 499 | return NULL; |
| 500 | for (i = start, j = 0; j < len; i++, j++) |
| 501 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 502 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 503 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 504 | return tuple; |
| 505 | } |
| 506 | |
| 507 | static PyObject * |
| 508 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 509 | { |
| 510 | PyObject *list; |
| 511 | Py_ssize_t len, i, j; |
| 512 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 513 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 514 | list = PyList_New(len); |
| 515 | if (list == NULL) |
| 516 | return NULL; |
| 517 | for (i = start, j = 0; j < len; i++, j++) |
| 518 | PyList_SET_ITEM(list, j, self->data[i]); |
| 519 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 520 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 521 | return list; |
| 522 | } |
| 523 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 524 | typedef struct { |
| 525 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 526 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 527 | } PyMemoEntry; |
| 528 | |
| 529 | typedef struct { |
| 530 | Py_ssize_t mt_mask; |
| 531 | Py_ssize_t mt_used; |
| 532 | Py_ssize_t mt_allocated; |
| 533 | PyMemoEntry *mt_table; |
| 534 | } PyMemoTable; |
| 535 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 536 | typedef struct PicklerObject { |
| 537 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 538 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 539 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 540 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 541 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 542 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 543 | |
| 544 | PyObject *write; /* write() method of the output stream. */ |
| 545 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 546 | flushing to the stream. */ |
| 547 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 548 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 549 | int proto; /* Pickle protocol number, >= 0 */ |
| 550 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 551 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 552 | Py_ssize_t frame_start; /* Position in output_buffer where the |
| 553 | where the current frame begins. -1 if there |
| 554 | is no frame currently open. */ |
| 555 | |
| 556 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 557 | int fast; /* Enable fast mode if set to a true value. |
| 558 | The fast mode disable the usage of memo, |
| 559 | therefore speeding the pickling process by |
| 560 | not generating superfluous PUT opcodes. It |
| 561 | should not be used if with self-referential |
| 562 | objects. */ |
| 563 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 564 | int fix_imports; /* Indicate whether Pickler should fix |
| 565 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 566 | PyObject *fast_memo; |
| 567 | } PicklerObject; |
| 568 | |
| 569 | typedef struct UnpicklerObject { |
| 570 | PyObject_HEAD |
| 571 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 572 | |
| 573 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 574 | is unnecessary, since the keys are contiguous ints. */ |
| 575 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 576 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 577 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 578 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 579 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 580 | |
| 581 | Py_buffer buffer; |
| 582 | char *input_buffer; |
| 583 | char *input_line; |
| 584 | Py_ssize_t input_len; |
| 585 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 586 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 587 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 588 | PyObject *read; /* read() method of the input stream. */ |
| 589 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 590 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 591 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 592 | char *encoding; /* Name of the encoding to be used for |
| 593 | decoding strings pickled using Python |
| 594 | 2.x. The default value is "ASCII" */ |
| 595 | char *errors; /* Name of errors handling scheme to used when |
| 596 | decoding strings. The default value is |
| 597 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 598 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 599 | objects. */ |
| 600 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 601 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 602 | int proto; /* Protocol of the pickle loaded. */ |
| 603 | int fix_imports; /* Indicate whether Unpickler should fix |
| 604 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 605 | } UnpicklerObject; |
| 606 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 607 | typedef struct { |
| 608 | PyObject_HEAD |
| 609 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 610 | } PicklerMemoProxyObject; |
| 611 | |
| 612 | typedef struct { |
| 613 | PyObject_HEAD |
| 614 | UnpicklerObject *unpickler; |
| 615 | } UnpicklerMemoProxyObject; |
| 616 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 617 | /* Forward declarations */ |
| 618 | static int save(PicklerObject *, PyObject *, int); |
| 619 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 620 | static PyTypeObject Pickler_Type; |
| 621 | static PyTypeObject Unpickler_Type; |
| 622 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 623 | #include "clinic/_pickle.c.h" |
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 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 626 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 627 | 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] | 628 | a bunch of unnecessary object creation. This makes a huge performance |
| 629 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 631 | #define MT_MINSIZE 8 |
| 632 | #define PERTURB_SHIFT 5 |
| 633 | |
| 634 | |
| 635 | static PyMemoTable * |
| 636 | PyMemoTable_New(void) |
| 637 | { |
| 638 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 639 | if (memo == NULL) { |
| 640 | PyErr_NoMemory(); |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | memo->mt_used = 0; |
| 645 | memo->mt_allocated = MT_MINSIZE; |
| 646 | memo->mt_mask = MT_MINSIZE - 1; |
| 647 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 648 | if (memo->mt_table == NULL) { |
| 649 | PyMem_FREE(memo); |
| 650 | PyErr_NoMemory(); |
| 651 | return NULL; |
| 652 | } |
| 653 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 654 | |
| 655 | return memo; |
| 656 | } |
| 657 | |
| 658 | static PyMemoTable * |
| 659 | PyMemoTable_Copy(PyMemoTable *self) |
| 660 | { |
| 661 | Py_ssize_t i; |
| 662 | PyMemoTable *new = PyMemoTable_New(); |
| 663 | if (new == NULL) |
| 664 | return NULL; |
| 665 | |
| 666 | new->mt_used = self->mt_used; |
| 667 | new->mt_allocated = self->mt_allocated; |
| 668 | new->mt_mask = self->mt_mask; |
| 669 | /* The table we get from _New() is probably smaller than we wanted. |
| 670 | Free it and allocate one that's the right size. */ |
| 671 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 672 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 673 | if (new->mt_table == NULL) { |
| 674 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 675 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 676 | return NULL; |
| 677 | } |
| 678 | for (i = 0; i < self->mt_allocated; i++) { |
| 679 | Py_XINCREF(self->mt_table[i].me_key); |
| 680 | } |
| 681 | memcpy(new->mt_table, self->mt_table, |
| 682 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 683 | |
| 684 | return new; |
| 685 | } |
| 686 | |
| 687 | static Py_ssize_t |
| 688 | PyMemoTable_Size(PyMemoTable *self) |
| 689 | { |
| 690 | return self->mt_used; |
| 691 | } |
| 692 | |
| 693 | static int |
| 694 | PyMemoTable_Clear(PyMemoTable *self) |
| 695 | { |
| 696 | Py_ssize_t i = self->mt_allocated; |
| 697 | |
| 698 | while (--i >= 0) { |
| 699 | Py_XDECREF(self->mt_table[i].me_key); |
| 700 | } |
| 701 | self->mt_used = 0; |
| 702 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static void |
| 707 | PyMemoTable_Del(PyMemoTable *self) |
| 708 | { |
| 709 | if (self == NULL) |
| 710 | return; |
| 711 | PyMemoTable_Clear(self); |
| 712 | |
| 713 | PyMem_FREE(self->mt_table); |
| 714 | PyMem_FREE(self); |
| 715 | } |
| 716 | |
| 717 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 718 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 719 | static PyMemoEntry * |
| 720 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 721 | { |
| 722 | size_t i; |
| 723 | size_t perturb; |
| 724 | size_t mask = (size_t)self->mt_mask; |
| 725 | PyMemoEntry *table = self->mt_table; |
| 726 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 727 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 728 | |
| 729 | i = hash & mask; |
| 730 | entry = &table[i]; |
| 731 | if (entry->me_key == NULL || entry->me_key == key) |
| 732 | return entry; |
| 733 | |
| 734 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 735 | i = (i << 2) + i + perturb + 1; |
| 736 | entry = &table[i & mask]; |
| 737 | if (entry->me_key == NULL || entry->me_key == key) |
| 738 | return entry; |
| 739 | } |
| 740 | assert(0); /* Never reached */ |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | /* Returns -1 on failure, 0 on success. */ |
| 745 | static int |
| 746 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 747 | { |
| 748 | PyMemoEntry *oldtable = NULL; |
| 749 | PyMemoEntry *oldentry, *newentry; |
| 750 | Py_ssize_t new_size = MT_MINSIZE; |
| 751 | Py_ssize_t to_process; |
| 752 | |
| 753 | assert(min_size > 0); |
| 754 | |
| 755 | /* Find the smallest valid table size >= min_size. */ |
| 756 | while (new_size < min_size && new_size > 0) |
| 757 | new_size <<= 1; |
| 758 | if (new_size <= 0) { |
| 759 | PyErr_NoMemory(); |
| 760 | return -1; |
| 761 | } |
| 762 | /* new_size needs to be a power of two. */ |
| 763 | assert((new_size & (new_size - 1)) == 0); |
| 764 | |
| 765 | /* Allocate new table. */ |
| 766 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 767 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 768 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 769 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 770 | PyErr_NoMemory(); |
| 771 | return -1; |
| 772 | } |
| 773 | self->mt_allocated = new_size; |
| 774 | self->mt_mask = new_size - 1; |
| 775 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 776 | |
| 777 | /* Copy entries from the old table. */ |
| 778 | to_process = self->mt_used; |
| 779 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 780 | if (oldentry->me_key != NULL) { |
| 781 | to_process--; |
| 782 | /* newentry is a pointer to a chunk of the new |
| 783 | mt_table, so we're setting the key:value pair |
| 784 | in-place. */ |
| 785 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 786 | newentry->me_key = oldentry->me_key; |
| 787 | newentry->me_value = oldentry->me_value; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /* Deallocate the old table. */ |
| 792 | PyMem_FREE(oldtable); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 797 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 798 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 799 | { |
| 800 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 801 | if (entry->me_key == NULL) |
| 802 | return NULL; |
| 803 | return &entry->me_value; |
| 804 | } |
| 805 | |
| 806 | /* Returns -1 on failure, 0 on success. */ |
| 807 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 808 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 809 | { |
| 810 | PyMemoEntry *entry; |
| 811 | |
| 812 | assert(key != NULL); |
| 813 | |
| 814 | entry = _PyMemoTable_Lookup(self, key); |
| 815 | if (entry->me_key != NULL) { |
| 816 | entry->me_value = value; |
| 817 | return 0; |
| 818 | } |
| 819 | Py_INCREF(key); |
| 820 | entry->me_key = key; |
| 821 | entry->me_value = value; |
| 822 | self->mt_used++; |
| 823 | |
| 824 | /* If we added a key, we can safely resize. Otherwise just return! |
| 825 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 826 | * |
| 827 | * Quadrupling the size improves average table sparseness |
| 828 | * (reducing collisions) at the cost of some memory. It also halves |
| 829 | * the number of expensive resize operations in a growing memo table. |
| 830 | * |
| 831 | * Very large memo tables (over 50K items) use doubling instead. |
| 832 | * This may help applications with severe memory constraints. |
| 833 | */ |
| 834 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 835 | return 0; |
| 836 | return _PyMemoTable_ResizeTable(self, |
| 837 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 838 | } |
| 839 | |
| 840 | #undef MT_MINSIZE |
| 841 | #undef PERTURB_SHIFT |
| 842 | |
| 843 | /*************************************************************************/ |
| 844 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 846 | static int |
| 847 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 848 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 849 | Py_CLEAR(self->output_buffer); |
| 850 | self->output_buffer = |
| 851 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 852 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 853 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 854 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 855 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 856 | return 0; |
| 857 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 859 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 860 | _write_size64(char *out, size_t value) |
| 861 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 862 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 863 | |
| 864 | assert(sizeof(size_t) <= 8); |
| 865 | |
| 866 | for (i = 0; i < sizeof(size_t); i++) { |
| 867 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 868 | } |
| 869 | for (i = sizeof(size_t); i < 8; i++) { |
| 870 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 871 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 875 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 876 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 877 | qdata[0] = FRAME; |
| 878 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static int |
| 882 | _Pickler_CommitFrame(PicklerObject *self) |
| 883 | { |
| 884 | size_t frame_len; |
| 885 | char *qdata; |
| 886 | |
| 887 | if (!self->framing || self->frame_start == -1) |
| 888 | return 0; |
| 889 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 890 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 891 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 892 | self->frame_start = -1; |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static int |
| 897 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 898 | { |
| 899 | Py_ssize_t frame_len; |
| 900 | |
| 901 | if (!self->framing || self->frame_start == -1) |
| 902 | return 0; |
| 903 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 904 | if (frame_len >= FRAME_SIZE_TARGET) |
| 905 | return _Pickler_CommitFrame(self); |
| 906 | else |
| 907 | return 0; |
| 908 | } |
| 909 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 910 | static PyObject * |
| 911 | _Pickler_GetString(PicklerObject *self) |
| 912 | { |
| 913 | PyObject *output_buffer = self->output_buffer; |
| 914 | |
| 915 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 916 | |
| 917 | if (_Pickler_CommitFrame(self)) |
| 918 | return NULL; |
| 919 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 920 | self->output_buffer = NULL; |
| 921 | /* Resize down to exact size */ |
| 922 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 923 | return NULL; |
| 924 | return output_buffer; |
| 925 | } |
| 926 | |
| 927 | static int |
| 928 | _Pickler_FlushToFile(PicklerObject *self) |
| 929 | { |
| 930 | PyObject *output, *result; |
| 931 | |
| 932 | assert(self->write != NULL); |
| 933 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 934 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 935 | output = _Pickler_GetString(self); |
| 936 | if (output == NULL) |
| 937 | return -1; |
| 938 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 939 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 940 | Py_XDECREF(result); |
| 941 | return (result == NULL) ? -1 : 0; |
| 942 | } |
| 943 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 944 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 945 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 946 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 947 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 948 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 949 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 950 | |
| 951 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 952 | need_new_frame = (self->framing && self->frame_start == -1); |
| 953 | |
| 954 | if (need_new_frame) |
| 955 | n = data_len + FRAME_HEADER_SIZE; |
| 956 | else |
| 957 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 958 | |
| 959 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 960 | if (required > self->max_output_len) { |
| 961 | /* Make place in buffer for the pickle chunk */ |
| 962 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 963 | PyErr_NoMemory(); |
| 964 | return -1; |
| 965 | } |
| 966 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 967 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 968 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 969 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 970 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 971 | if (need_new_frame) { |
| 972 | /* Setup new frame */ |
| 973 | Py_ssize_t frame_start = self->output_len; |
| 974 | self->frame_start = frame_start; |
| 975 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 976 | /* Write an invalid value, for debugging */ |
| 977 | buffer[frame_start + i] = 0xFE; |
| 978 | } |
| 979 | self->output_len += FRAME_HEADER_SIZE; |
| 980 | } |
| 981 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 982 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 983 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 984 | buffer[self->output_len + i] = s[i]; |
| 985 | } |
| 986 | } |
| 987 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 988 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 989 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 990 | self->output_len += data_len; |
| 991 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 994 | static PicklerObject * |
| 995 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 996 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 997 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 999 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1000 | if (self == NULL) |
| 1001 | return NULL; |
| 1002 | |
| 1003 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1004 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1005 | self->write = NULL; |
| 1006 | self->proto = 0; |
| 1007 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1008 | self->framing = 0; |
| 1009 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1010 | self->fast = 0; |
| 1011 | self->fast_nesting = 0; |
| 1012 | self->fix_imports = 0; |
| 1013 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1014 | self->max_output_len = WRITE_BUF_SIZE; |
| 1015 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1016 | |
| 1017 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1018 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1019 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1020 | |
| 1021 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1022 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1023 | return NULL; |
| 1024 | } |
| 1025 | return self; |
| 1026 | } |
| 1027 | |
| 1028 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1029 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1030 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1031 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1032 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1033 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1034 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1035 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1036 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1037 | proto = PyLong_AsLong(protocol); |
| 1038 | if (proto < 0) { |
| 1039 | if (proto == -1 && PyErr_Occurred()) |
| 1040 | return -1; |
| 1041 | proto = HIGHEST_PROTOCOL; |
| 1042 | } |
| 1043 | else if (proto > HIGHEST_PROTOCOL) { |
| 1044 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1045 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1046 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1047 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1048 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1049 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1050 | self->bin = proto > 0; |
| 1051 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1056 | be called once on a freshly created Pickler. */ |
| 1057 | static int |
| 1058 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1059 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1060 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1061 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1062 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1063 | if (self->write == NULL) { |
| 1064 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1065 | PyErr_SetString(PyExc_TypeError, |
| 1066 | "file must have a 'write' attribute"); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1073 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1074 | own reference to `input`. */ |
| 1075 | static Py_ssize_t |
| 1076 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1077 | { |
| 1078 | if (self->buffer.buf != NULL) |
| 1079 | PyBuffer_Release(&self->buffer); |
| 1080 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1081 | return -1; |
| 1082 | self->input_buffer = self->buffer.buf; |
| 1083 | self->input_len = self->buffer.len; |
| 1084 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1085 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1086 | return self->input_len; |
| 1087 | } |
| 1088 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1089 | static int |
| 1090 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1091 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1092 | Py_ssize_t consumed; |
| 1093 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1094 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1095 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1096 | if (consumed <= 0) |
| 1097 | return 0; |
| 1098 | |
| 1099 | assert(self->peek); /* otherwise we did something wrong */ |
| 1100 | /* This makes an useless copy... */ |
| 1101 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1102 | if (r == NULL) |
| 1103 | return -1; |
| 1104 | Py_DECREF(r); |
| 1105 | |
| 1106 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1110 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1111 | |
| 1112 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1113 | may be multiple pickle objects arranged contiguously in the same input |
| 1114 | buffer. |
| 1115 | |
| 1116 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1117 | bytes from the input stream/buffer. |
| 1118 | |
| 1119 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1120 | failure; on success, returns the number of bytes read from the file. |
| 1121 | |
| 1122 | On success, self->input_len will be 0; this is intentional so that when |
| 1123 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1124 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1125 | size to tell you how much data you can process. */ |
| 1126 | static Py_ssize_t |
| 1127 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1128 | { |
| 1129 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1130 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1131 | |
| 1132 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1133 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1134 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1135 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1136 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1137 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1138 | PyObject *empty_tuple = PyTuple_New(0); |
| 1139 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1140 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1141 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1142 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1143 | PyObject *len; |
| 1144 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1145 | if (self->peek && n < PREFETCH) { |
| 1146 | len = PyLong_FromSsize_t(PREFETCH); |
| 1147 | if (len == NULL) |
| 1148 | return -1; |
| 1149 | data = _Pickle_FastCall(self->peek, len); |
| 1150 | if (data == NULL) { |
| 1151 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1152 | return -1; |
| 1153 | /* peek() is probably not supported by the given file object */ |
| 1154 | PyErr_Clear(); |
| 1155 | Py_CLEAR(self->peek); |
| 1156 | } |
| 1157 | else { |
| 1158 | read_size = _Unpickler_SetStringInput(self, data); |
| 1159 | Py_DECREF(data); |
| 1160 | self->prefetched_idx = 0; |
| 1161 | if (n <= read_size) |
| 1162 | return n; |
| 1163 | } |
| 1164 | } |
| 1165 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1166 | if (len == NULL) |
| 1167 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1168 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1169 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1170 | if (data == NULL) |
| 1171 | return -1; |
| 1172 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1173 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1174 | Py_DECREF(data); |
| 1175 | return read_size; |
| 1176 | } |
| 1177 | |
| 1178 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1179 | |
| 1180 | This should be used for all data reads, rather than accessing the unpickler's |
| 1181 | input buffer directly. This method deals correctly with reading from input |
| 1182 | streams, which the input buffer doesn't deal with. |
| 1183 | |
| 1184 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1185 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1186 | should use this function's return value to know how many bytes you can |
| 1187 | consume. |
| 1188 | |
| 1189 | Returns -1 (with an exception set) on failure. On success, return the |
| 1190 | number of chars read. */ |
| 1191 | static Py_ssize_t |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1192 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1193 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1194 | Py_ssize_t num_read; |
| 1195 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1196 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1197 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1198 | PickleState *st = _Pickle_GetGlobalState(); |
| 1199 | PyErr_SetString(st->UnpicklingError, |
| 1200 | "read would overflow (invalid bytecode)"); |
| 1201 | return -1; |
| 1202 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1203 | if (self->next_read_idx + n <= self->input_len) { |
| 1204 | *s = self->input_buffer + self->next_read_idx; |
| 1205 | self->next_read_idx += n; |
| 1206 | return n; |
| 1207 | } |
| 1208 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1209 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1210 | return -1; |
| 1211 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1212 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1213 | if (num_read < 0) |
| 1214 | return -1; |
| 1215 | if (num_read < n) { |
| 1216 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1217 | return -1; |
| 1218 | } |
| 1219 | *s = self->input_buffer; |
| 1220 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1221 | return n; |
| 1222 | } |
| 1223 | |
| 1224 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1225 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1226 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1227 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1228 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1229 | if (input_line == NULL) { |
| 1230 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1231 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1232 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1233 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1234 | memcpy(input_line, line, len); |
| 1235 | input_line[len] = '\0'; |
| 1236 | self->input_line = input_line; |
| 1237 | *result = self->input_line; |
| 1238 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1239 | } |
| 1240 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1241 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1242 | before hitting \n, return the data we found. |
| 1243 | |
| 1244 | Returns the number of chars read, or -1 on failure. */ |
| 1245 | static Py_ssize_t |
| 1246 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1247 | { |
| 1248 | Py_ssize_t i, num_read; |
| 1249 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1250 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1251 | if (self->input_buffer[i] == '\n') { |
| 1252 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1253 | num_read = i - self->next_read_idx + 1; |
| 1254 | self->next_read_idx = i + 1; |
| 1255 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1256 | } |
| 1257 | } |
| 1258 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1259 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1260 | if (num_read < 0) |
| 1261 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1262 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1263 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1264 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1265 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1266 | /* If we get here, we've run off the end of the input string. Return the |
| 1267 | remaining string and let the caller figure it out. */ |
| 1268 | *result = self->input_buffer + self->next_read_idx; |
| 1269 | num_read = i - self->next_read_idx; |
| 1270 | self->next_read_idx = i; |
| 1271 | return num_read; |
| 1272 | } |
| 1273 | |
| 1274 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1275 | will be modified in place. */ |
| 1276 | static int |
| 1277 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1278 | { |
| 1279 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1280 | |
| 1281 | assert(new_size > self->memo_size); |
| 1282 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1283 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1284 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1285 | PyErr_NoMemory(); |
| 1286 | return -1; |
| 1287 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1288 | for (i = self->memo_size; i < new_size; i++) |
| 1289 | self->memo[i] = NULL; |
| 1290 | self->memo_size = new_size; |
| 1291 | return 0; |
| 1292 | } |
| 1293 | |
| 1294 | /* Returns NULL if idx is out of bounds. */ |
| 1295 | static PyObject * |
| 1296 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1297 | { |
| 1298 | if (idx < 0 || idx >= self->memo_size) |
| 1299 | return NULL; |
| 1300 | |
| 1301 | return self->memo[idx]; |
| 1302 | } |
| 1303 | |
| 1304 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1305 | This takes its own reference to `value`. */ |
| 1306 | static int |
| 1307 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1308 | { |
| 1309 | PyObject *old_item; |
| 1310 | |
| 1311 | if (idx >= self->memo_size) { |
| 1312 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1313 | return -1; |
| 1314 | assert(idx < self->memo_size); |
| 1315 | } |
| 1316 | Py_INCREF(value); |
| 1317 | old_item = self->memo[idx]; |
| 1318 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1319 | if (old_item != NULL) { |
| 1320 | Py_DECREF(old_item); |
| 1321 | } |
| 1322 | else { |
| 1323 | self->memo_len++; |
| 1324 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1325 | return 0; |
| 1326 | } |
| 1327 | |
| 1328 | static PyObject ** |
| 1329 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1330 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1331 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1332 | if (memo == NULL) { |
| 1333 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1334 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1335 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1336 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1337 | return memo; |
| 1338 | } |
| 1339 | |
| 1340 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1341 | static void |
| 1342 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1343 | { |
| 1344 | Py_ssize_t i; |
| 1345 | PyObject **memo = self->memo; |
| 1346 | |
| 1347 | if (self->memo == NULL) |
| 1348 | return; |
| 1349 | self->memo = NULL; |
| 1350 | i = self->memo_size; |
| 1351 | while (--i >= 0) { |
| 1352 | Py_XDECREF(memo[i]); |
| 1353 | } |
| 1354 | PyMem_FREE(memo); |
| 1355 | } |
| 1356 | |
| 1357 | static UnpicklerObject * |
| 1358 | _Unpickler_New(void) |
| 1359 | { |
| 1360 | UnpicklerObject *self; |
| 1361 | |
| 1362 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1363 | if (self == NULL) |
| 1364 | return NULL; |
| 1365 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1366 | self->pers_func = NULL; |
| 1367 | self->input_buffer = NULL; |
| 1368 | self->input_line = NULL; |
| 1369 | self->input_len = 0; |
| 1370 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1371 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1372 | self->read = NULL; |
| 1373 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1374 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1375 | self->encoding = NULL; |
| 1376 | self->errors = NULL; |
| 1377 | self->marks = NULL; |
| 1378 | self->num_marks = 0; |
| 1379 | self->marks_size = 0; |
| 1380 | self->proto = 0; |
| 1381 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1382 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1383 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1384 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1385 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1386 | self->stack = (Pdata *)Pdata_New(); |
| 1387 | |
| 1388 | if (self->memo == NULL || self->stack == NULL) { |
| 1389 | Py_DECREF(self); |
| 1390 | return NULL; |
| 1391 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1392 | |
| 1393 | return self; |
| 1394 | } |
| 1395 | |
| 1396 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1397 | be called once on a freshly created Pickler. */ |
| 1398 | static int |
| 1399 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1400 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1401 | _Py_IDENTIFIER(peek); |
| 1402 | _Py_IDENTIFIER(read); |
| 1403 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1404 | |
| 1405 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1406 | if (self->peek == NULL) { |
| 1407 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1408 | PyErr_Clear(); |
| 1409 | else |
| 1410 | return -1; |
| 1411 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1412 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1413 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1414 | if (self->readline == NULL || self->read == NULL) { |
| 1415 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1416 | PyErr_SetString(PyExc_TypeError, |
| 1417 | "file must have 'read' and 'readline' attributes"); |
| 1418 | Py_CLEAR(self->read); |
| 1419 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1420 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1421 | return -1; |
| 1422 | } |
| 1423 | return 0; |
| 1424 | } |
| 1425 | |
| 1426 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1427 | be called once on a freshly created Pickler. */ |
| 1428 | static int |
| 1429 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1430 | const char *encoding, |
| 1431 | const char *errors) |
| 1432 | { |
| 1433 | if (encoding == NULL) |
| 1434 | encoding = "ASCII"; |
| 1435 | if (errors == NULL) |
| 1436 | errors = "strict"; |
| 1437 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1438 | self->encoding = _PyMem_Strdup(encoding); |
| 1439 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1440 | if (self->encoding == NULL || self->errors == NULL) { |
| 1441 | PyErr_NoMemory(); |
| 1442 | return -1; |
| 1443 | } |
| 1444 | return 0; |
| 1445 | } |
| 1446 | |
| 1447 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1448 | static int |
| 1449 | memo_get(PicklerObject *self, PyObject *key) |
| 1450 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1451 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1452 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1453 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1454 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1455 | value = PyMemoTable_Get(self->memo, key); |
| 1456 | if (value == NULL) { |
| 1457 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1458 | return -1; |
| 1459 | } |
| 1460 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1461 | if (!self->bin) { |
| 1462 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1463 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1464 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1465 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1466 | } |
| 1467 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1468 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1469 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1470 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1471 | len = 2; |
| 1472 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1473 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1474 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1475 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1476 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1477 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1478 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1479 | len = 5; |
| 1480 | } |
| 1481 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1482 | PickleState *st = _Pickle_GetGlobalState(); |
| 1483 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1484 | "memo id too large for LONG_BINGET"); |
| 1485 | return -1; |
| 1486 | } |
| 1487 | } |
| 1488 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1489 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1490 | return -1; |
| 1491 | |
| 1492 | return 0; |
| 1493 | } |
| 1494 | |
| 1495 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1496 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1497 | static int |
| 1498 | memo_put(PicklerObject *self, PyObject *obj) |
| 1499 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1500 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1501 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1502 | Py_ssize_t idx; |
| 1503 | |
| 1504 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1505 | |
| 1506 | if (self->fast) |
| 1507 | return 0; |
| 1508 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1509 | idx = PyMemoTable_Size(self->memo); |
| 1510 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1511 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1512 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1513 | if (self->proto >= 4) { |
| 1514 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1515 | return -1; |
| 1516 | return 0; |
| 1517 | } |
| 1518 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1519 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1520 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1521 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1522 | len = strlen(pdata); |
| 1523 | } |
| 1524 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1525 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1526 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1527 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1528 | len = 2; |
| 1529 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1530 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1531 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1532 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1533 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1534 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1535 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1536 | len = 5; |
| 1537 | } |
| 1538 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1539 | PickleState *st = _Pickle_GetGlobalState(); |
| 1540 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1541 | "memo id too large for LONG_BINPUT"); |
| 1542 | return -1; |
| 1543 | } |
| 1544 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1545 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1546 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1547 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1548 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
| 1551 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1552 | get_dotted_path(PyObject *obj, PyObject *name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1553 | _Py_static_string(PyId_dot, "."); |
| 1554 | _Py_static_string(PyId_locals, "<locals>"); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1555 | PyObject *dotted_path; |
| 1556 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1557 | |
| 1558 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1559 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1560 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1561 | n = PyList_GET_SIZE(dotted_path); |
| 1562 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1563 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1564 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1565 | PyObject *result = PyUnicode_RichCompare( |
| 1566 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1567 | int is_equal = (result == Py_True); |
| 1568 | assert(PyBool_Check(result)); |
| 1569 | Py_DECREF(result); |
| 1570 | if (is_equal) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1571 | if (obj == NULL) |
| 1572 | PyErr_Format(PyExc_AttributeError, |
| 1573 | "Can't pickle local object %R", name); |
| 1574 | else |
| 1575 | PyErr_Format(PyExc_AttributeError, |
| 1576 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1577 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1578 | return NULL; |
| 1579 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1580 | } |
| 1581 | return dotted_path; |
| 1582 | } |
| 1583 | |
| 1584 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1585 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1586 | { |
| 1587 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1588 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1589 | |
| 1590 | assert(PyList_CheckExact(names)); |
| 1591 | Py_INCREF(obj); |
| 1592 | n = PyList_GET_SIZE(names); |
| 1593 | for (i = 0; i < n; i++) { |
| 1594 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1595 | Py_XDECREF(parent); |
| 1596 | parent = obj; |
| 1597 | obj = PyObject_GetAttr(parent, name); |
| 1598 | if (obj == NULL) { |
| 1599 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1600 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1601 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1602 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1603 | if (pparent != NULL) |
| 1604 | *pparent = parent; |
| 1605 | else |
| 1606 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1607 | return obj; |
| 1608 | } |
| 1609 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1610 | static void |
| 1611 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1612 | { |
| 1613 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1614 | PyErr_Clear(); |
| 1615 | PyErr_Format(PyExc_AttributeError, |
| 1616 | "Can't get attribute %R on %R", name, obj); |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | |
| 1621 | static PyObject * |
| 1622 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1623 | { |
| 1624 | PyObject *dotted_path, *attr; |
| 1625 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1626 | if (allow_qualname) { |
| 1627 | dotted_path = get_dotted_path(obj, name); |
| 1628 | if (dotted_path == NULL) |
| 1629 | return NULL; |
| 1630 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1631 | Py_DECREF(dotted_path); |
| 1632 | } |
| 1633 | else |
| 1634 | attr = PyObject_GetAttr(obj, name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1635 | if (attr == NULL) |
| 1636 | reformat_attribute_error(obj, name); |
| 1637 | return attr; |
| 1638 | } |
| 1639 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1640 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1641 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1642 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1643 | PyObject *module_name; |
| 1644 | PyObject *modules_dict; |
| 1645 | PyObject *module; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1646 | Py_ssize_t i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1647 | _Py_IDENTIFIER(__module__); |
| 1648 | _Py_IDENTIFIER(modules); |
| 1649 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1650 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1651 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1652 | |
| 1653 | if (module_name == NULL) { |
| 1654 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1655 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1656 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1657 | } |
| 1658 | else { |
| 1659 | /* In some rare cases (e.g., bound methods of extension types), |
| 1660 | __module__ can be None. If it is so, then search sys.modules for |
| 1661 | the module of global. */ |
| 1662 | if (module_name != Py_None) |
| 1663 | return module_name; |
| 1664 | Py_CLEAR(module_name); |
| 1665 | } |
| 1666 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1667 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1668 | /* Fallback on walking sys.modules */ |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1669 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1670 | if (modules_dict == NULL) { |
| 1671 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1672 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1673 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1674 | |
| 1675 | i = 0; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1676 | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
| 1677 | PyObject *candidate; |
| 1678 | if (PyUnicode_Check(module_name) && |
| 1679 | !PyUnicode_CompareWithASCIIString(module_name, "__main__")) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1680 | continue; |
| 1681 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1682 | continue; |
| 1683 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1684 | candidate = get_deep_attribute(module, dotted_path, NULL); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1685 | if (candidate == NULL) { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1686 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1687 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1688 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1689 | continue; |
| 1690 | } |
| 1691 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1692 | if (candidate == global) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1693 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1694 | Py_DECREF(candidate); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1695 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1696 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1697 | Py_DECREF(candidate); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1701 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1702 | Py_INCREF(module_name); |
| 1703 | return module_name; |
| 1704 | } |
| 1705 | |
| 1706 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1707 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1708 | memoization disabled). If the nesting of a list or dict object exceed |
| 1709 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1710 | reference to the seen list or dict objects and check whether these objects |
| 1711 | are recursive. These are not strictly necessary, since save() has a |
| 1712 | hard-coded recursion limit, but they give a nicer error message than the |
| 1713 | typical RuntimeError. */ |
| 1714 | static int |
| 1715 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1716 | { |
| 1717 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1718 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1719 | PyObject *key = NULL; |
| 1720 | if (self->fast_memo == NULL) { |
| 1721 | self->fast_memo = PyDict_New(); |
| 1722 | if (self->fast_memo == NULL) { |
| 1723 | self->fast_nesting = -1; |
| 1724 | return 0; |
| 1725 | } |
| 1726 | } |
| 1727 | key = PyLong_FromVoidPtr(obj); |
| 1728 | if (key == NULL) |
| 1729 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1730 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1731 | Py_DECREF(key); |
| 1732 | PyErr_Format(PyExc_ValueError, |
| 1733 | "fast mode: can't pickle cyclic objects " |
| 1734 | "including object type %.200s at %p", |
| 1735 | obj->ob_type->tp_name, obj); |
| 1736 | self->fast_nesting = -1; |
| 1737 | return 0; |
| 1738 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1739 | if (PyErr_Occurred()) { |
| 1740 | return 0; |
| 1741 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1742 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1743 | Py_DECREF(key); |
| 1744 | self->fast_nesting = -1; |
| 1745 | return 0; |
| 1746 | } |
| 1747 | Py_DECREF(key); |
| 1748 | } |
| 1749 | return 1; |
| 1750 | } |
| 1751 | |
| 1752 | static int |
| 1753 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1754 | { |
| 1755 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1756 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1757 | if (key == NULL) |
| 1758 | return 0; |
| 1759 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1760 | Py_DECREF(key); |
| 1761 | return 0; |
| 1762 | } |
| 1763 | Py_DECREF(key); |
| 1764 | } |
| 1765 | return 1; |
| 1766 | } |
| 1767 | |
| 1768 | static int |
| 1769 | save_none(PicklerObject *self, PyObject *obj) |
| 1770 | { |
| 1771 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1772 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1773 | return -1; |
| 1774 | |
| 1775 | return 0; |
| 1776 | } |
| 1777 | |
| 1778 | static int |
| 1779 | save_bool(PicklerObject *self, PyObject *obj) |
| 1780 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1781 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1782 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1783 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1784 | return -1; |
| 1785 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1786 | else { |
| 1787 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1788 | * so that unpicklers written before bools were introduced unpickle them |
| 1789 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1790 | * Note that protocol 2 added direct ways to pickle bools. |
| 1791 | */ |
| 1792 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1793 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1794 | return -1; |
| 1795 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
| 1799 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1800 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1801 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1802 | PyObject *repr = NULL; |
| 1803 | Py_ssize_t size; |
| 1804 | long val; |
| 1805 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1806 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1807 | const char long_op = LONG; |
| 1808 | |
| 1809 | val= PyLong_AsLong(obj); |
| 1810 | if (val == -1 && PyErr_Occurred()) { |
| 1811 | /* out of range for int pickling */ |
| 1812 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1813 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1814 | else if (self->bin && |
| 1815 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1816 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1817 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1818 | |
| 1819 | Note: we can't use -0x80000000L in the above condition because some |
| 1820 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1821 | before applying the unary minus when sizeof(long) <= 4. The |
| 1822 | resulting value stays unsigned which is commonly not what we want, |
| 1823 | so MSVC happily warns us about it. However, that result would have |
| 1824 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1825 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1826 | char pdata[32]; |
| 1827 | Py_ssize_t len = 0; |
| 1828 | |
| 1829 | pdata[1] = (unsigned char)(val & 0xff); |
| 1830 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1831 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1832 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1833 | |
| 1834 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1835 | if (pdata[2] == 0) { |
| 1836 | pdata[0] = BININT1; |
| 1837 | len = 2; |
| 1838 | } |
| 1839 | else { |
| 1840 | pdata[0] = BININT2; |
| 1841 | len = 3; |
| 1842 | } |
| 1843 | } |
| 1844 | else { |
| 1845 | pdata[0] = BININT; |
| 1846 | len = 5; |
| 1847 | } |
| 1848 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1849 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1850 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1851 | |
| 1852 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1855 | if (self->proto >= 2) { |
| 1856 | /* Linear-time pickling. */ |
| 1857 | size_t nbits; |
| 1858 | size_t nbytes; |
| 1859 | unsigned char *pdata; |
| 1860 | char header[5]; |
| 1861 | int i; |
| 1862 | int sign = _PyLong_Sign(obj); |
| 1863 | |
| 1864 | if (sign == 0) { |
| 1865 | header[0] = LONG1; |
| 1866 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1867 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1868 | goto error; |
| 1869 | return 0; |
| 1870 | } |
| 1871 | nbits = _PyLong_NumBits(obj); |
| 1872 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1873 | goto error; |
| 1874 | /* How many bytes do we need? There are nbits >> 3 full |
| 1875 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1876 | * are any leftover bits, then we clearly need another |
| 1877 | * byte. Wnat's not so obvious is that we *probably* |
| 1878 | * need another byte even if there aren't any leftovers: |
| 1879 | * the most-significant bit of the most-significant byte |
| 1880 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1881 | * opposite of the one we need. The exception is ints |
| 1882 | * 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] | 1883 | * its own 256's-complement, so has the right sign bit |
| 1884 | * even without the extra byte. That's a pain to check |
| 1885 | * for in advance, though, so we always grab an extra |
| 1886 | * byte at the start, and cut it back later if possible. |
| 1887 | */ |
| 1888 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1889 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1890 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1891 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1892 | goto error; |
| 1893 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1894 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1895 | if (repr == NULL) |
| 1896 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1897 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1898 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1899 | pdata, nbytes, |
| 1900 | 1 /* little endian */ , 1 /* signed */ ); |
| 1901 | if (i < 0) |
| 1902 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1903 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1904 | * needed. This is so iff the MSB is all redundant sign |
| 1905 | * bits. |
| 1906 | */ |
| 1907 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1908 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1909 | pdata[nbytes - 1] == 0xff && |
| 1910 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1911 | nbytes--; |
| 1912 | } |
| 1913 | |
| 1914 | if (nbytes < 256) { |
| 1915 | header[0] = LONG1; |
| 1916 | header[1] = (unsigned char)nbytes; |
| 1917 | size = 2; |
| 1918 | } |
| 1919 | else { |
| 1920 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1921 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1922 | for (i = 1; i < 5; i++) { |
| 1923 | header[i] = (unsigned char)(size & 0xff); |
| 1924 | size >>= 8; |
| 1925 | } |
| 1926 | size = 5; |
| 1927 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1928 | if (_Pickler_Write(self, header, size) < 0 || |
| 1929 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1930 | goto error; |
| 1931 | } |
| 1932 | else { |
| 1933 | char *string; |
| 1934 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1935 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1936 | the number of digits), in both directions. We add a trailing 'L' |
| 1937 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1938 | |
| 1939 | repr = PyObject_Repr(obj); |
| 1940 | if (repr == NULL) |
| 1941 | goto error; |
| 1942 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1943 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1944 | if (string == NULL) |
| 1945 | goto error; |
| 1946 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1947 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1948 | _Pickler_Write(self, string, size) < 0 || |
| 1949 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1950 | goto error; |
| 1951 | } |
| 1952 | |
| 1953 | if (0) { |
| 1954 | error: |
| 1955 | status = -1; |
| 1956 | } |
| 1957 | Py_XDECREF(repr); |
| 1958 | |
| 1959 | return status; |
| 1960 | } |
| 1961 | |
| 1962 | static int |
| 1963 | save_float(PicklerObject *self, PyObject *obj) |
| 1964 | { |
| 1965 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1966 | |
| 1967 | if (self->bin) { |
| 1968 | char pdata[9]; |
| 1969 | pdata[0] = BINFLOAT; |
| 1970 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1971 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1972 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1973 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1974 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1975 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1976 | int result = -1; |
| 1977 | char *buf = NULL; |
| 1978 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1979 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1980 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1981 | goto done; |
| 1982 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 1983 | buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL); |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1984 | if (!buf) { |
| 1985 | PyErr_NoMemory(); |
| 1986 | goto done; |
| 1987 | } |
| 1988 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1989 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1990 | goto done; |
| 1991 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1992 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1993 | goto done; |
| 1994 | |
| 1995 | result = 0; |
| 1996 | done: |
| 1997 | PyMem_Free(buf); |
| 1998 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1999 | } |
| 2000 | |
| 2001 | return 0; |
| 2002 | } |
| 2003 | |
| 2004 | static int |
| 2005 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2006 | { |
| 2007 | if (self->proto < 3) { |
| 2008 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2009 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2010 | the __reduce__ method) to permit bytes object unpickling. |
| 2011 | |
| 2012 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2013 | 2 'bytes' is just an alias for 'str' (which has different |
| 2014 | parameters than the actual bytes object), we use codecs.encode |
| 2015 | to create the appropriate 'str' object when unpickled using |
| 2016 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2017 | using Python 3. Again this is a hack and we don't need to do this |
| 2018 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2019 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2020 | int status; |
| 2021 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2022 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2023 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2024 | } |
| 2025 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2026 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2027 | PyObject *unicode_str = |
| 2028 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2029 | PyBytes_GET_SIZE(obj), |
| 2030 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2031 | _Py_IDENTIFIER(latin1); |
| 2032 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2033 | if (unicode_str == NULL) |
| 2034 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2035 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2036 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2037 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2038 | Py_DECREF(unicode_str); |
| 2039 | } |
| 2040 | |
| 2041 | if (reduce_value == NULL) |
| 2042 | return -1; |
| 2043 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2044 | /* save_reduce() will memoize the object automatically. */ |
| 2045 | status = save_reduce(self, reduce_value, obj); |
| 2046 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2047 | return status; |
| 2048 | } |
| 2049 | else { |
| 2050 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2051 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2052 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2053 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2054 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2055 | if (size < 0) |
| 2056 | return -1; |
| 2057 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2058 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2059 | header[0] = SHORT_BINBYTES; |
| 2060 | header[1] = (unsigned char)size; |
| 2061 | len = 2; |
| 2062 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2063 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2064 | header[0] = BINBYTES; |
| 2065 | header[1] = (unsigned char)(size & 0xff); |
| 2066 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2067 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2068 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2069 | len = 5; |
| 2070 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2071 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2072 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2073 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2074 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2075 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2076 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2077 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2078 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2079 | return -1; /* string too large */ |
| 2080 | } |
| 2081 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2082 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2083 | return -1; |
| 2084 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2085 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2086 | return -1; |
| 2087 | |
| 2088 | if (memo_put(self, obj) < 0) |
| 2089 | return -1; |
| 2090 | |
| 2091 | return 0; |
| 2092 | } |
| 2093 | } |
| 2094 | |
| 2095 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2096 | backslash and newline characters to \uXXXX escapes. */ |
| 2097 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2098 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2099 | { |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2100 | PyObject *repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2101 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2102 | Py_ssize_t i, size; |
| 2103 | size_t expandsize; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2104 | void *data; |
| 2105 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2106 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2107 | if (PyUnicode_READY(obj)) |
| 2108 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2109 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2110 | size = PyUnicode_GET_LENGTH(obj); |
| 2111 | data = PyUnicode_DATA(obj); |
| 2112 | kind = PyUnicode_KIND(obj); |
| 2113 | if (kind == PyUnicode_4BYTE_KIND) |
| 2114 | expandsize = 10; |
| 2115 | else |
| 2116 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2117 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2118 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize) |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2119 | return PyErr_NoMemory(); |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2120 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2121 | if (repr == NULL) |
| 2122 | return NULL; |
| 2123 | if (size == 0) |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2124 | return repr; |
| 2125 | assert(Py_REFCNT(repr) == 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2126 | |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2127 | p = PyBytes_AS_STRING(repr); |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2128 | for (i=0; i < size; i++) { |
| 2129 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2130 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2131 | if (ch >= 0x10000) { |
| 2132 | *p++ = '\\'; |
| 2133 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2134 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2135 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2136 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2137 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2138 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2139 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2140 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2141 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2142 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2143 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2144 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2145 | *p++ = '\\'; |
| 2146 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2147 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2148 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2149 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2150 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2151 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2152 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2153 | else |
| 2154 | *p++ = (char) ch; |
| 2155 | } |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2156 | size = p - PyBytes_AS_STRING(repr); |
| 2157 | if (_PyBytes_Resize(&repr, size) < 0) |
| 2158 | return NULL; |
| 2159 | return repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2160 | } |
| 2161 | |
| 2162 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2163 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2164 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2165 | char header[9]; |
| 2166 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2167 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2168 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2169 | if (size <= 0xff && self->proto >= 4) { |
| 2170 | header[0] = SHORT_BINUNICODE; |
| 2171 | header[1] = (unsigned char)(size & 0xff); |
| 2172 | len = 2; |
| 2173 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2174 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2175 | header[0] = BINUNICODE; |
| 2176 | header[1] = (unsigned char)(size & 0xff); |
| 2177 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2178 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2179 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2180 | len = 5; |
| 2181 | } |
| 2182 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2183 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2184 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2185 | len = 9; |
| 2186 | } |
| 2187 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2188 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2189 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2190 | return -1; |
| 2191 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2192 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2193 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2194 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2195 | if (_Pickler_Write(self, data, size) < 0) |
| 2196 | return -1; |
| 2197 | |
| 2198 | return 0; |
| 2199 | } |
| 2200 | |
| 2201 | static int |
| 2202 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2203 | { |
| 2204 | PyObject *encoded = NULL; |
| 2205 | Py_ssize_t size; |
| 2206 | char *data; |
| 2207 | int r; |
| 2208 | |
| 2209 | if (PyUnicode_READY(obj)) |
| 2210 | return -1; |
| 2211 | |
| 2212 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2213 | if (data != NULL) |
| 2214 | return write_utf8(self, data, size); |
| 2215 | |
| 2216 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2217 | "surrogatepass" error handler. */ |
| 2218 | PyErr_Clear(); |
| 2219 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2220 | if (encoded == NULL) |
| 2221 | return -1; |
| 2222 | |
| 2223 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2224 | PyBytes_GET_SIZE(encoded)); |
| 2225 | Py_DECREF(encoded); |
| 2226 | return r; |
| 2227 | } |
| 2228 | |
| 2229 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2230 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2231 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2232 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2233 | if (write_unicode_binary(self, obj) < 0) |
| 2234 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2235 | } |
| 2236 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2237 | PyObject *encoded; |
| 2238 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2239 | const char unicode_op = UNICODE; |
| 2240 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2241 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2242 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2243 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2244 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2245 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2246 | Py_DECREF(encoded); |
| 2247 | return -1; |
| 2248 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2249 | |
| 2250 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2251 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2252 | Py_DECREF(encoded); |
| 2253 | return -1; |
| 2254 | } |
| 2255 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2256 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2257 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2258 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2259 | } |
| 2260 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2261 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2262 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2263 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
| 2266 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2267 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2268 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2269 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2270 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2271 | |
| 2272 | assert(PyTuple_Size(t) == len); |
| 2273 | |
| 2274 | for (i = 0; i < len; i++) { |
| 2275 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2276 | |
| 2277 | if (element == NULL) |
| 2278 | return -1; |
| 2279 | if (save(self, element, 0) < 0) |
| 2280 | return -1; |
| 2281 | } |
| 2282 | |
| 2283 | return 0; |
| 2284 | } |
| 2285 | |
| 2286 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2287 | * used across protocols to minimize the space needed to pickle them. |
| 2288 | * Tuples are also the only builtin immutable type that can be recursive |
| 2289 | * (a tuple can be reached from itself), and that requires some subtle |
| 2290 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2291 | */ |
| 2292 | static int |
| 2293 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2294 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2295 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2296 | |
| 2297 | const char mark_op = MARK; |
| 2298 | const char tuple_op = TUPLE; |
| 2299 | const char pop_op = POP; |
| 2300 | const char pop_mark_op = POP_MARK; |
| 2301 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2302 | |
| 2303 | if ((len = PyTuple_Size(obj)) < 0) |
| 2304 | return -1; |
| 2305 | |
| 2306 | if (len == 0) { |
| 2307 | char pdata[2]; |
| 2308 | |
| 2309 | if (self->proto) { |
| 2310 | pdata[0] = EMPTY_TUPLE; |
| 2311 | len = 1; |
| 2312 | } |
| 2313 | else { |
| 2314 | pdata[0] = MARK; |
| 2315 | pdata[1] = TUPLE; |
| 2316 | len = 2; |
| 2317 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2318 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2319 | return -1; |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2323 | /* 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] | 2324 | * saving the tuple elements, the tuple must be recursive, in |
| 2325 | * which case we'll pop everything we put on the stack, and fetch |
| 2326 | * its value from the memo. |
| 2327 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2328 | if (len <= 3 && self->proto >= 2) { |
| 2329 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2330 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2331 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2332 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2333 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2334 | /* pop the len elements */ |
| 2335 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2336 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2337 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2338 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2339 | if (memo_get(self, obj) < 0) |
| 2340 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2341 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2342 | return 0; |
| 2343 | } |
| 2344 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2345 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2346 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2347 | } |
| 2348 | goto memoize; |
| 2349 | } |
| 2350 | |
| 2351 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2352 | * Generate MARK e1 e2 ... TUPLE |
| 2353 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2354 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2355 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2356 | |
| 2357 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2358 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2360 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2361 | /* pop the stack stuff we pushed */ |
| 2362 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2363 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2364 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2365 | } |
| 2366 | else { |
| 2367 | /* Note that we pop one more than len, to remove |
| 2368 | * the MARK too. |
| 2369 | */ |
| 2370 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2371 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2372 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2373 | } |
| 2374 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2375 | if (memo_get(self, obj) < 0) |
| 2376 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2377 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2378 | return 0; |
| 2379 | } |
| 2380 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2381 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2382 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | memoize: |
| 2386 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2387 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2388 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2389 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
| 2392 | /* iter is an iterator giving items, and we batch up chunks of |
| 2393 | * MARK item item ... item APPENDS |
| 2394 | * opcode sequences. Calling code should have arranged to first create an |
| 2395 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2396 | * Returns 0 on success, <0 on error. |
| 2397 | */ |
| 2398 | static int |
| 2399 | batch_list(PicklerObject *self, PyObject *iter) |
| 2400 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2401 | PyObject *obj = NULL; |
| 2402 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2403 | int i, n; |
| 2404 | |
| 2405 | const char mark_op = MARK; |
| 2406 | const char append_op = APPEND; |
| 2407 | const char appends_op = APPENDS; |
| 2408 | |
| 2409 | assert(iter != NULL); |
| 2410 | |
| 2411 | /* XXX: I think this function could be made faster by avoiding the |
| 2412 | iterator interface and fetching objects directly from list using |
| 2413 | PyList_GET_ITEM. |
| 2414 | */ |
| 2415 | |
| 2416 | if (self->proto == 0) { |
| 2417 | /* APPENDS isn't available; do one at a time. */ |
| 2418 | for (;;) { |
| 2419 | obj = PyIter_Next(iter); |
| 2420 | if (obj == NULL) { |
| 2421 | if (PyErr_Occurred()) |
| 2422 | return -1; |
| 2423 | break; |
| 2424 | } |
| 2425 | i = save(self, obj, 0); |
| 2426 | Py_DECREF(obj); |
| 2427 | if (i < 0) |
| 2428 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2429 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2430 | return -1; |
| 2431 | } |
| 2432 | return 0; |
| 2433 | } |
| 2434 | |
| 2435 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2436 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2437 | /* Get first item */ |
| 2438 | firstitem = PyIter_Next(iter); |
| 2439 | if (firstitem == NULL) { |
| 2440 | if (PyErr_Occurred()) |
| 2441 | goto error; |
| 2442 | |
| 2443 | /* nothing more to add */ |
| 2444 | break; |
| 2445 | } |
| 2446 | |
| 2447 | /* Try to get a second item */ |
| 2448 | obj = PyIter_Next(iter); |
| 2449 | if (obj == NULL) { |
| 2450 | if (PyErr_Occurred()) |
| 2451 | goto error; |
| 2452 | |
| 2453 | /* Only one item to write */ |
| 2454 | if (save(self, firstitem, 0) < 0) |
| 2455 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2456 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2457 | goto error; |
| 2458 | Py_CLEAR(firstitem); |
| 2459 | break; |
| 2460 | } |
| 2461 | |
| 2462 | /* More than one item to write */ |
| 2463 | |
| 2464 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2465 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2466 | goto error; |
| 2467 | |
| 2468 | if (save(self, firstitem, 0) < 0) |
| 2469 | goto error; |
| 2470 | Py_CLEAR(firstitem); |
| 2471 | n = 1; |
| 2472 | |
| 2473 | /* Fetch and save up to BATCHSIZE items */ |
| 2474 | while (obj) { |
| 2475 | if (save(self, obj, 0) < 0) |
| 2476 | goto error; |
| 2477 | Py_CLEAR(obj); |
| 2478 | n += 1; |
| 2479 | |
| 2480 | if (n == BATCHSIZE) |
| 2481 | break; |
| 2482 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2483 | obj = PyIter_Next(iter); |
| 2484 | if (obj == NULL) { |
| 2485 | if (PyErr_Occurred()) |
| 2486 | goto error; |
| 2487 | break; |
| 2488 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2489 | } |
| 2490 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2491 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2492 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2493 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2494 | } while (n == BATCHSIZE); |
| 2495 | return 0; |
| 2496 | |
| 2497 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2498 | Py_XDECREF(firstitem); |
| 2499 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2500 | return -1; |
| 2501 | } |
| 2502 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2503 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2504 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2505 | * MARK item item ... item APPENDS |
| 2506 | * opcode sequences. Calling code should have arranged to first create an |
| 2507 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2508 | * Returns 0 on success, -1 on error. |
| 2509 | * |
| 2510 | * This version is considerably faster than batch_list(), if less general. |
| 2511 | * |
| 2512 | * Note that this only works for protocols > 0. |
| 2513 | */ |
| 2514 | static int |
| 2515 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2516 | { |
| 2517 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2518 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2519 | |
| 2520 | const char append_op = APPEND; |
| 2521 | const char appends_op = APPENDS; |
| 2522 | const char mark_op = MARK; |
| 2523 | |
| 2524 | assert(obj != NULL); |
| 2525 | assert(self->proto > 0); |
| 2526 | assert(PyList_CheckExact(obj)); |
| 2527 | |
| 2528 | if (PyList_GET_SIZE(obj) == 1) { |
| 2529 | item = PyList_GET_ITEM(obj, 0); |
| 2530 | if (save(self, item, 0) < 0) |
| 2531 | return -1; |
| 2532 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2533 | return -1; |
| 2534 | return 0; |
| 2535 | } |
| 2536 | |
| 2537 | /* Write in batches of BATCHSIZE. */ |
| 2538 | total = 0; |
| 2539 | do { |
| 2540 | this_batch = 0; |
| 2541 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2542 | return -1; |
| 2543 | while (total < PyList_GET_SIZE(obj)) { |
| 2544 | item = PyList_GET_ITEM(obj, total); |
| 2545 | if (save(self, item, 0) < 0) |
| 2546 | return -1; |
| 2547 | total++; |
| 2548 | if (++this_batch == BATCHSIZE) |
| 2549 | break; |
| 2550 | } |
| 2551 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2552 | return -1; |
| 2553 | |
| 2554 | } while (total < PyList_GET_SIZE(obj)); |
| 2555 | |
| 2556 | return 0; |
| 2557 | } |
| 2558 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2559 | static int |
| 2560 | save_list(PicklerObject *self, PyObject *obj) |
| 2561 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2562 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2563 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2564 | int status = 0; |
| 2565 | |
| 2566 | if (self->fast && !fast_save_enter(self, obj)) |
| 2567 | goto error; |
| 2568 | |
| 2569 | /* Create an empty list. */ |
| 2570 | if (self->bin) { |
| 2571 | header[0] = EMPTY_LIST; |
| 2572 | len = 1; |
| 2573 | } |
| 2574 | else { |
| 2575 | header[0] = MARK; |
| 2576 | header[1] = LIST; |
| 2577 | len = 2; |
| 2578 | } |
| 2579 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2580 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2581 | goto error; |
| 2582 | |
| 2583 | /* Get list length, and bow out early if empty. */ |
| 2584 | if ((len = PyList_Size(obj)) < 0) |
| 2585 | goto error; |
| 2586 | |
| 2587 | if (memo_put(self, obj) < 0) |
| 2588 | goto error; |
| 2589 | |
| 2590 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2591 | /* Materialize the list elements. */ |
| 2592 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2593 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2594 | goto error; |
| 2595 | status = batch_list_exact(self, obj); |
| 2596 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2597 | } else { |
| 2598 | PyObject *iter = PyObject_GetIter(obj); |
| 2599 | if (iter == NULL) |
| 2600 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2601 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2602 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2603 | Py_DECREF(iter); |
| 2604 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2605 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2606 | status = batch_list(self, iter); |
| 2607 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2608 | Py_DECREF(iter); |
| 2609 | } |
| 2610 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2611 | if (0) { |
| 2612 | error: |
| 2613 | status = -1; |
| 2614 | } |
| 2615 | |
| 2616 | if (self->fast && !fast_save_leave(self, obj)) |
| 2617 | status = -1; |
| 2618 | |
| 2619 | return status; |
| 2620 | } |
| 2621 | |
| 2622 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2623 | * MARK key value ... key value SETITEMS |
| 2624 | * opcode sequences. Calling code should have arranged to first create an |
| 2625 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2626 | * Returns 0 on success, <0 on error. |
| 2627 | * |
| 2628 | * This is very much like batch_list(). The difference between saving |
| 2629 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2630 | * the C level, though, that attempts to combine these routines were too |
| 2631 | * ugly to bear. |
| 2632 | */ |
| 2633 | static int |
| 2634 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2635 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2636 | PyObject *obj = NULL; |
| 2637 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2638 | int i, n; |
| 2639 | |
| 2640 | const char mark_op = MARK; |
| 2641 | const char setitem_op = SETITEM; |
| 2642 | const char setitems_op = SETITEMS; |
| 2643 | |
| 2644 | assert(iter != NULL); |
| 2645 | |
| 2646 | if (self->proto == 0) { |
| 2647 | /* SETITEMS isn't available; do one at a time. */ |
| 2648 | for (;;) { |
| 2649 | obj = PyIter_Next(iter); |
| 2650 | if (obj == NULL) { |
| 2651 | if (PyErr_Occurred()) |
| 2652 | return -1; |
| 2653 | break; |
| 2654 | } |
| 2655 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2656 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2657 | "iterator must return 2-tuples"); |
| 2658 | return -1; |
| 2659 | } |
| 2660 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2661 | if (i >= 0) |
| 2662 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2663 | Py_DECREF(obj); |
| 2664 | if (i < 0) |
| 2665 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2666 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2667 | return -1; |
| 2668 | } |
| 2669 | return 0; |
| 2670 | } |
| 2671 | |
| 2672 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2673 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2674 | /* Get first item */ |
| 2675 | firstitem = PyIter_Next(iter); |
| 2676 | if (firstitem == NULL) { |
| 2677 | if (PyErr_Occurred()) |
| 2678 | goto error; |
| 2679 | |
| 2680 | /* nothing more to add */ |
| 2681 | break; |
| 2682 | } |
| 2683 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2684 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2685 | "iterator must return 2-tuples"); |
| 2686 | goto error; |
| 2687 | } |
| 2688 | |
| 2689 | /* Try to get a second item */ |
| 2690 | obj = PyIter_Next(iter); |
| 2691 | if (obj == NULL) { |
| 2692 | if (PyErr_Occurred()) |
| 2693 | goto error; |
| 2694 | |
| 2695 | /* Only one item to write */ |
| 2696 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2697 | goto error; |
| 2698 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2699 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2700 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2701 | goto error; |
| 2702 | Py_CLEAR(firstitem); |
| 2703 | break; |
| 2704 | } |
| 2705 | |
| 2706 | /* More than one item to write */ |
| 2707 | |
| 2708 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2709 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2710 | goto error; |
| 2711 | |
| 2712 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2713 | goto error; |
| 2714 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2715 | goto error; |
| 2716 | Py_CLEAR(firstitem); |
| 2717 | n = 1; |
| 2718 | |
| 2719 | /* Fetch and save up to BATCHSIZE items */ |
| 2720 | while (obj) { |
| 2721 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2722 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2723 | "iterator must return 2-tuples"); |
| 2724 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2725 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2726 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2727 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2728 | goto error; |
| 2729 | Py_CLEAR(obj); |
| 2730 | n += 1; |
| 2731 | |
| 2732 | if (n == BATCHSIZE) |
| 2733 | break; |
| 2734 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2735 | obj = PyIter_Next(iter); |
| 2736 | if (obj == NULL) { |
| 2737 | if (PyErr_Occurred()) |
| 2738 | goto error; |
| 2739 | break; |
| 2740 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2741 | } |
| 2742 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2743 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2744 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2745 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2746 | } while (n == BATCHSIZE); |
| 2747 | return 0; |
| 2748 | |
| 2749 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2750 | Py_XDECREF(firstitem); |
| 2751 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2752 | return -1; |
| 2753 | } |
| 2754 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2755 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2756 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2757 | * MARK key value ... key value SETITEMS |
| 2758 | * opcode sequences. Calling code should have arranged to first create an |
| 2759 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2760 | * Returns 0 on success, -1 on error. |
| 2761 | * |
| 2762 | * Note that this currently doesn't work for protocol 0. |
| 2763 | */ |
| 2764 | static int |
| 2765 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2766 | { |
| 2767 | PyObject *key = NULL, *value = NULL; |
| 2768 | int i; |
| 2769 | Py_ssize_t dict_size, ppos = 0; |
| 2770 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2771 | const char mark_op = MARK; |
| 2772 | const char setitem_op = SETITEM; |
| 2773 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2774 | |
| 2775 | assert(obj != NULL); |
| 2776 | assert(self->proto > 0); |
| 2777 | |
| 2778 | dict_size = PyDict_Size(obj); |
| 2779 | |
| 2780 | /* Special-case len(d) == 1 to save space. */ |
| 2781 | if (dict_size == 1) { |
| 2782 | PyDict_Next(obj, &ppos, &key, &value); |
| 2783 | if (save(self, key, 0) < 0) |
| 2784 | return -1; |
| 2785 | if (save(self, value, 0) < 0) |
| 2786 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2787 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2788 | return -1; |
| 2789 | return 0; |
| 2790 | } |
| 2791 | |
| 2792 | /* Write in batches of BATCHSIZE. */ |
| 2793 | do { |
| 2794 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2795 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2796 | return -1; |
| 2797 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2798 | if (save(self, key, 0) < 0) |
| 2799 | return -1; |
| 2800 | if (save(self, value, 0) < 0) |
| 2801 | return -1; |
| 2802 | if (++i == BATCHSIZE) |
| 2803 | break; |
| 2804 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2805 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2806 | return -1; |
| 2807 | if (PyDict_Size(obj) != dict_size) { |
| 2808 | PyErr_Format( |
| 2809 | PyExc_RuntimeError, |
| 2810 | "dictionary changed size during iteration"); |
| 2811 | return -1; |
| 2812 | } |
| 2813 | |
| 2814 | } while (i == BATCHSIZE); |
| 2815 | return 0; |
| 2816 | } |
| 2817 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2818 | static int |
| 2819 | save_dict(PicklerObject *self, PyObject *obj) |
| 2820 | { |
| 2821 | PyObject *items, *iter; |
| 2822 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2823 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2824 | int status = 0; |
| 2825 | |
| 2826 | if (self->fast && !fast_save_enter(self, obj)) |
| 2827 | goto error; |
| 2828 | |
| 2829 | /* Create an empty dict. */ |
| 2830 | if (self->bin) { |
| 2831 | header[0] = EMPTY_DICT; |
| 2832 | len = 1; |
| 2833 | } |
| 2834 | else { |
| 2835 | header[0] = MARK; |
| 2836 | header[1] = DICT; |
| 2837 | len = 2; |
| 2838 | } |
| 2839 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2840 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2841 | goto error; |
| 2842 | |
| 2843 | /* Get dict size, and bow out early if empty. */ |
| 2844 | if ((len = PyDict_Size(obj)) < 0) |
| 2845 | goto error; |
| 2846 | |
| 2847 | if (memo_put(self, obj) < 0) |
| 2848 | goto error; |
| 2849 | |
| 2850 | if (len != 0) { |
| 2851 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2852 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2853 | /* We can take certain shortcuts if we know this is a dict and |
| 2854 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2855 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2856 | goto error; |
| 2857 | status = batch_dict_exact(self, obj); |
| 2858 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2859 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2860 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2861 | |
| 2862 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2863 | if (items == NULL) |
| 2864 | goto error; |
| 2865 | iter = PyObject_GetIter(items); |
| 2866 | Py_DECREF(items); |
| 2867 | if (iter == NULL) |
| 2868 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2869 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2870 | Py_DECREF(iter); |
| 2871 | goto error; |
| 2872 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2873 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2874 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2875 | Py_DECREF(iter); |
| 2876 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2877 | } |
| 2878 | |
| 2879 | if (0) { |
| 2880 | error: |
| 2881 | status = -1; |
| 2882 | } |
| 2883 | |
| 2884 | if (self->fast && !fast_save_leave(self, obj)) |
| 2885 | status = -1; |
| 2886 | |
| 2887 | return status; |
| 2888 | } |
| 2889 | |
| 2890 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2891 | save_set(PicklerObject *self, PyObject *obj) |
| 2892 | { |
| 2893 | PyObject *item; |
| 2894 | int i; |
| 2895 | Py_ssize_t set_size, ppos = 0; |
| 2896 | Py_hash_t hash; |
| 2897 | |
| 2898 | const char empty_set_op = EMPTY_SET; |
| 2899 | const char mark_op = MARK; |
| 2900 | const char additems_op = ADDITEMS; |
| 2901 | |
| 2902 | if (self->proto < 4) { |
| 2903 | PyObject *items; |
| 2904 | PyObject *reduce_value; |
| 2905 | int status; |
| 2906 | |
| 2907 | items = PySequence_List(obj); |
| 2908 | if (items == NULL) { |
| 2909 | return -1; |
| 2910 | } |
| 2911 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2912 | Py_DECREF(items); |
| 2913 | if (reduce_value == NULL) { |
| 2914 | return -1; |
| 2915 | } |
| 2916 | /* save_reduce() will memoize the object automatically. */ |
| 2917 | status = save_reduce(self, reduce_value, obj); |
| 2918 | Py_DECREF(reduce_value); |
| 2919 | return status; |
| 2920 | } |
| 2921 | |
| 2922 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2923 | return -1; |
| 2924 | |
| 2925 | if (memo_put(self, obj) < 0) |
| 2926 | return -1; |
| 2927 | |
| 2928 | set_size = PySet_GET_SIZE(obj); |
| 2929 | if (set_size == 0) |
| 2930 | return 0; /* nothing to do */ |
| 2931 | |
| 2932 | /* Write in batches of BATCHSIZE. */ |
| 2933 | do { |
| 2934 | i = 0; |
| 2935 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2936 | return -1; |
| 2937 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2938 | if (save(self, item, 0) < 0) |
| 2939 | return -1; |
| 2940 | if (++i == BATCHSIZE) |
| 2941 | break; |
| 2942 | } |
| 2943 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2944 | return -1; |
| 2945 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2946 | PyErr_Format( |
| 2947 | PyExc_RuntimeError, |
| 2948 | "set changed size during iteration"); |
| 2949 | return -1; |
| 2950 | } |
| 2951 | } while (i == BATCHSIZE); |
| 2952 | |
| 2953 | return 0; |
| 2954 | } |
| 2955 | |
| 2956 | static int |
| 2957 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2958 | { |
| 2959 | PyObject *iter; |
| 2960 | |
| 2961 | const char mark_op = MARK; |
| 2962 | const char frozenset_op = FROZENSET; |
| 2963 | |
| 2964 | if (self->fast && !fast_save_enter(self, obj)) |
| 2965 | return -1; |
| 2966 | |
| 2967 | if (self->proto < 4) { |
| 2968 | PyObject *items; |
| 2969 | PyObject *reduce_value; |
| 2970 | int status; |
| 2971 | |
| 2972 | items = PySequence_List(obj); |
| 2973 | if (items == NULL) { |
| 2974 | return -1; |
| 2975 | } |
| 2976 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2977 | items); |
| 2978 | Py_DECREF(items); |
| 2979 | if (reduce_value == NULL) { |
| 2980 | return -1; |
| 2981 | } |
| 2982 | /* save_reduce() will memoize the object automatically. */ |
| 2983 | status = save_reduce(self, reduce_value, obj); |
| 2984 | Py_DECREF(reduce_value); |
| 2985 | return status; |
| 2986 | } |
| 2987 | |
| 2988 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2989 | return -1; |
| 2990 | |
| 2991 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2992 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2993 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2994 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2995 | for (;;) { |
| 2996 | PyObject *item; |
| 2997 | |
| 2998 | item = PyIter_Next(iter); |
| 2999 | if (item == NULL) { |
| 3000 | if (PyErr_Occurred()) { |
| 3001 | Py_DECREF(iter); |
| 3002 | return -1; |
| 3003 | } |
| 3004 | break; |
| 3005 | } |
| 3006 | if (save(self, item, 0) < 0) { |
| 3007 | Py_DECREF(item); |
| 3008 | Py_DECREF(iter); |
| 3009 | return -1; |
| 3010 | } |
| 3011 | Py_DECREF(item); |
| 3012 | } |
| 3013 | Py_DECREF(iter); |
| 3014 | |
| 3015 | /* If the object is already in the memo, this means it is |
| 3016 | recursive. In this case, throw away everything we put on the |
| 3017 | stack, and fetch the object back from the memo. */ |
| 3018 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3019 | const char pop_mark_op = POP_MARK; |
| 3020 | |
| 3021 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3022 | return -1; |
| 3023 | if (memo_get(self, obj) < 0) |
| 3024 | return -1; |
| 3025 | return 0; |
| 3026 | } |
| 3027 | |
| 3028 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3029 | return -1; |
| 3030 | if (memo_put(self, obj) < 0) |
| 3031 | return -1; |
| 3032 | |
| 3033 | return 0; |
| 3034 | } |
| 3035 | |
| 3036 | static int |
| 3037 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3038 | { |
| 3039 | PyObject *key; |
| 3040 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3041 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3042 | |
| 3043 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3044 | if (key == NULL) |
| 3045 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3046 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3047 | Py_DECREF(key); |
| 3048 | if (item) { |
| 3049 | PyObject *fixed_module_name; |
| 3050 | PyObject *fixed_global_name; |
| 3051 | |
| 3052 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3053 | PyErr_Format(PyExc_RuntimeError, |
| 3054 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3055 | "should be 2-tuples, not %.200s", |
| 3056 | Py_TYPE(item)->tp_name); |
| 3057 | return -1; |
| 3058 | } |
| 3059 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3060 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3061 | if (!PyUnicode_Check(fixed_module_name) || |
| 3062 | !PyUnicode_Check(fixed_global_name)) { |
| 3063 | PyErr_Format(PyExc_RuntimeError, |
| 3064 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3065 | "should be pairs of str, not (%.200s, %.200s)", |
| 3066 | Py_TYPE(fixed_module_name)->tp_name, |
| 3067 | Py_TYPE(fixed_global_name)->tp_name); |
| 3068 | return -1; |
| 3069 | } |
| 3070 | |
| 3071 | Py_CLEAR(*module_name); |
| 3072 | Py_CLEAR(*global_name); |
| 3073 | Py_INCREF(fixed_module_name); |
| 3074 | Py_INCREF(fixed_global_name); |
| 3075 | *module_name = fixed_module_name; |
| 3076 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3077 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3078 | } |
| 3079 | else if (PyErr_Occurred()) { |
| 3080 | return -1; |
| 3081 | } |
| 3082 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3083 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3084 | if (item) { |
| 3085 | if (!PyUnicode_Check(item)) { |
| 3086 | PyErr_Format(PyExc_RuntimeError, |
| 3087 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3088 | "should be strings, not %.200s", |
| 3089 | Py_TYPE(item)->tp_name); |
| 3090 | return -1; |
| 3091 | } |
| 3092 | Py_CLEAR(*module_name); |
| 3093 | Py_INCREF(item); |
| 3094 | *module_name = item; |
| 3095 | } |
| 3096 | else if (PyErr_Occurred()) { |
| 3097 | return -1; |
| 3098 | } |
| 3099 | |
| 3100 | return 0; |
| 3101 | } |
| 3102 | |
| 3103 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3104 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3105 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3106 | PyObject *global_name = NULL; |
| 3107 | PyObject *module_name = NULL; |
| 3108 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3109 | PyObject *parent = NULL; |
| 3110 | PyObject *dotted_path = NULL; |
| 3111 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3112 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3113 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3114 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3115 | _Py_IDENTIFIER(__name__); |
| 3116 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3117 | |
| 3118 | const char global_op = GLOBAL; |
| 3119 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3120 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3121 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3122 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3123 | } |
| 3124 | else { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3125 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3126 | if (global_name == NULL) { |
| 3127 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3128 | goto error; |
| 3129 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3130 | } |
| 3131 | if (global_name == NULL) { |
| 3132 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3133 | if (global_name == NULL) |
| 3134 | goto error; |
| 3135 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3136 | } |
| 3137 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3138 | dotted_path = get_dotted_path(module, global_name); |
| 3139 | if (dotted_path == NULL) |
| 3140 | goto error; |
| 3141 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3142 | if (module_name == NULL) |
| 3143 | goto error; |
| 3144 | |
| 3145 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3146 | relative imports. |
| 3147 | |
| 3148 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3149 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3150 | custom import functions (IMHO, this would be a nice security |
| 3151 | feature). The import C API would need to be extended to support the |
| 3152 | extra parameters of __import__ to fix that. */ |
| 3153 | module = PyImport_Import(module_name); |
| 3154 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3155 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3156 | "Can't pickle %R: import of module %R failed", |
| 3157 | obj, module_name); |
| 3158 | goto error; |
| 3159 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3160 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3161 | Py_INCREF(lastname); |
| 3162 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3163 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3164 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3165 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3166 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3167 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3168 | goto error; |
| 3169 | } |
| 3170 | if (cls != obj) { |
| 3171 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3172 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3173 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3174 | obj, module_name, global_name); |
| 3175 | goto error; |
| 3176 | } |
| 3177 | Py_DECREF(cls); |
| 3178 | |
| 3179 | if (self->proto >= 2) { |
| 3180 | /* See whether this is in the extension registry, and if |
| 3181 | * so generate an EXT opcode. |
| 3182 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3183 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3184 | PyObject *code_obj; /* extension code as Python object */ |
| 3185 | long code; /* extension code as C value */ |
| 3186 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3187 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3188 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3189 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3190 | if (extension_key == NULL) { |
| 3191 | goto error; |
| 3192 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3193 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3194 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3195 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3196 | /* The object is not registered in the extension registry. |
| 3197 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3198 | if (code_obj == NULL) { |
| 3199 | if (PyErr_Occurred()) { |
| 3200 | goto error; |
| 3201 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3202 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3203 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3204 | |
| 3205 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3206 | of the value returned by the extension_registry. It should for |
| 3207 | consistency. */ |
| 3208 | |
| 3209 | /* Verify code_obj has the right type and value. */ |
| 3210 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3211 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3212 | "Can't pickle %R: extension code %R isn't an integer", |
| 3213 | obj, code_obj); |
| 3214 | goto error; |
| 3215 | } |
| 3216 | code = PyLong_AS_LONG(code_obj); |
| 3217 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3218 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3219 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3220 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3221 | goto error; |
| 3222 | } |
| 3223 | |
| 3224 | /* Generate an EXT opcode. */ |
| 3225 | if (code <= 0xff) { |
| 3226 | pdata[0] = EXT1; |
| 3227 | pdata[1] = (unsigned char)code; |
| 3228 | n = 2; |
| 3229 | } |
| 3230 | else if (code <= 0xffff) { |
| 3231 | pdata[0] = EXT2; |
| 3232 | pdata[1] = (unsigned char)(code & 0xff); |
| 3233 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3234 | n = 3; |
| 3235 | } |
| 3236 | else { |
| 3237 | pdata[0] = EXT4; |
| 3238 | pdata[1] = (unsigned char)(code & 0xff); |
| 3239 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3240 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3241 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3242 | n = 5; |
| 3243 | } |
| 3244 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3245 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3246 | goto error; |
| 3247 | } |
| 3248 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3249 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3250 | if (parent == module) { |
| 3251 | Py_INCREF(lastname); |
| 3252 | Py_DECREF(global_name); |
| 3253 | global_name = lastname; |
| 3254 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3255 | if (self->proto >= 4) { |
| 3256 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3257 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3258 | if (save(self, module_name, 0) < 0) |
| 3259 | goto error; |
| 3260 | if (save(self, global_name, 0) < 0) |
| 3261 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3262 | |
| 3263 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3264 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3265 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3266 | else if (parent != module) { |
| 3267 | PickleState *st = _Pickle_GetGlobalState(); |
| 3268 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3269 | st->getattr, parent, lastname); |
| 3270 | status = save_reduce(self, reduce_value, NULL); |
| 3271 | Py_DECREF(reduce_value); |
| 3272 | if (status < 0) |
| 3273 | goto error; |
| 3274 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3275 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3276 | /* Generate a normal global opcode if we are using a pickle |
| 3277 | protocol < 4, or if the object is not registered in the |
| 3278 | extension registry. */ |
| 3279 | PyObject *encoded; |
| 3280 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3281 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3282 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3283 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3284 | |
| 3285 | /* For protocol < 3 and if the user didn't request against doing |
| 3286 | so, we convert module names to the old 2.x module names. */ |
| 3287 | if (self->proto < 3 && self->fix_imports) { |
| 3288 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3289 | goto error; |
| 3290 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3291 | } |
| 3292 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3293 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3294 | both the module name and the global name using UTF-8. We do so |
| 3295 | only when we are using the pickle protocol newer than version |
| 3296 | 3. This is to ensure compatibility with older Unpickler running |
| 3297 | on Python 2.x. */ |
| 3298 | if (self->proto == 3) { |
| 3299 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3300 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3301 | else { |
| 3302 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3303 | } |
| 3304 | encoded = unicode_encoder(module_name); |
| 3305 | if (encoded == NULL) { |
| 3306 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3307 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3308 | "can't pickle module identifier '%S' using " |
| 3309 | "pickle protocol %i", |
| 3310 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3311 | goto error; |
| 3312 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3313 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3314 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3315 | Py_DECREF(encoded); |
| 3316 | goto error; |
| 3317 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3318 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3319 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3320 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3322 | /* Save the name of the module. */ |
| 3323 | encoded = unicode_encoder(global_name); |
| 3324 | if (encoded == NULL) { |
| 3325 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3326 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3327 | "can't pickle global identifier '%S' using " |
| 3328 | "pickle protocol %i", |
| 3329 | global_name, self->proto); |
| 3330 | goto error; |
| 3331 | } |
| 3332 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3333 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3334 | Py_DECREF(encoded); |
| 3335 | goto error; |
| 3336 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3337 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3338 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3339 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3340 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3341 | /* Memoize the object. */ |
| 3342 | if (memo_put(self, obj) < 0) |
| 3343 | goto error; |
| 3344 | } |
| 3345 | |
| 3346 | if (0) { |
| 3347 | error: |
| 3348 | status = -1; |
| 3349 | } |
| 3350 | Py_XDECREF(module_name); |
| 3351 | Py_XDECREF(global_name); |
| 3352 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3353 | Py_XDECREF(parent); |
| 3354 | Py_XDECREF(dotted_path); |
| 3355 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3356 | |
| 3357 | return status; |
| 3358 | } |
| 3359 | |
| 3360 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3361 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3362 | { |
| 3363 | PyObject *reduce_value; |
| 3364 | int status; |
| 3365 | |
| 3366 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3367 | if (reduce_value == NULL) { |
| 3368 | return -1; |
| 3369 | } |
| 3370 | status = save_reduce(self, reduce_value, obj); |
| 3371 | Py_DECREF(reduce_value); |
| 3372 | return status; |
| 3373 | } |
| 3374 | |
| 3375 | static int |
| 3376 | save_type(PicklerObject *self, PyObject *obj) |
| 3377 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3378 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3379 | return save_singleton_type(self, obj, Py_None); |
| 3380 | } |
| 3381 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3382 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3383 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3384 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3385 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3386 | } |
| 3387 | return save_global(self, obj, NULL); |
| 3388 | } |
| 3389 | |
| 3390 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3391 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3392 | { |
| 3393 | PyObject *pid = NULL; |
| 3394 | int status = 0; |
| 3395 | |
| 3396 | const char persid_op = PERSID; |
| 3397 | const char binpersid_op = BINPERSID; |
| 3398 | |
| 3399 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3400 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3401 | if (pid == NULL) |
| 3402 | return -1; |
| 3403 | |
| 3404 | if (pid != Py_None) { |
| 3405 | if (self->bin) { |
| 3406 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3407 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3408 | goto error; |
| 3409 | } |
| 3410 | else { |
| 3411 | PyObject *pid_str = NULL; |
| 3412 | char *pid_ascii_bytes; |
| 3413 | Py_ssize_t size; |
| 3414 | |
| 3415 | pid_str = PyObject_Str(pid); |
| 3416 | if (pid_str == NULL) |
| 3417 | goto error; |
| 3418 | |
| 3419 | /* XXX: Should it check whether the persistent id only contains |
| 3420 | ASCII characters? And what if the pid contains embedded |
| 3421 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3422 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3423 | Py_DECREF(pid_str); |
| 3424 | if (pid_ascii_bytes == NULL) |
| 3425 | goto error; |
| 3426 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3427 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3428 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3429 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3430 | goto error; |
| 3431 | } |
| 3432 | status = 1; |
| 3433 | } |
| 3434 | |
| 3435 | if (0) { |
| 3436 | error: |
| 3437 | status = -1; |
| 3438 | } |
| 3439 | Py_XDECREF(pid); |
| 3440 | |
| 3441 | return status; |
| 3442 | } |
| 3443 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3444 | static PyObject * |
| 3445 | get_class(PyObject *obj) |
| 3446 | { |
| 3447 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3448 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3449 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3450 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3451 | if (cls == NULL) { |
| 3452 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3453 | PyErr_Clear(); |
| 3454 | cls = (PyObject *) Py_TYPE(obj); |
| 3455 | Py_INCREF(cls); |
| 3456 | } |
| 3457 | } |
| 3458 | return cls; |
| 3459 | } |
| 3460 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3461 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3462 | * appropriate __reduce__ method for obj. |
| 3463 | */ |
| 3464 | static int |
| 3465 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3466 | { |
| 3467 | PyObject *callable; |
| 3468 | PyObject *argtup; |
| 3469 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3470 | PyObject *listitems = Py_None; |
| 3471 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3472 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3473 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3474 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3475 | |
| 3476 | const char reduce_op = REDUCE; |
| 3477 | const char build_op = BUILD; |
| 3478 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3479 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3480 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3481 | size = PyTuple_Size(args); |
| 3482 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3483 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3484 | "__reduce__ must contain 2 through 5 elements"); |
| 3485 | return -1; |
| 3486 | } |
| 3487 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3488 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3489 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3490 | return -1; |
| 3491 | |
| 3492 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3493 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3494 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3495 | return -1; |
| 3496 | } |
| 3497 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3498 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3499 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3500 | return -1; |
| 3501 | } |
| 3502 | |
| 3503 | if (state == Py_None) |
| 3504 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3505 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3506 | if (listitems == Py_None) |
| 3507 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3508 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3509 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3510 | "returned by __reduce__ must be an iterator, not %s", |
| 3511 | Py_TYPE(listitems)->tp_name); |
| 3512 | return -1; |
| 3513 | } |
| 3514 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3515 | if (dictitems == Py_None) |
| 3516 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3517 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3518 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3519 | "returned by __reduce__ must be an iterator, not %s", |
| 3520 | Py_TYPE(dictitems)->tp_name); |
| 3521 | return -1; |
| 3522 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3523 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3524 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3525 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3526 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3527 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3528 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3529 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3530 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3531 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3532 | } |
| 3533 | PyErr_Clear(); |
| 3534 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3535 | else if (PyUnicode_Check(name)) { |
| 3536 | if (self->proto >= 4) { |
| 3537 | _Py_IDENTIFIER(__newobj_ex__); |
| 3538 | use_newobj_ex = PyUnicode_Compare( |
| 3539 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3540 | } |
| 3541 | if (!use_newobj_ex) { |
| 3542 | _Py_IDENTIFIER(__newobj__); |
| 3543 | use_newobj = PyUnicode_Compare( |
| 3544 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
| 3545 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3546 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3547 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3548 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3549 | |
| 3550 | if (use_newobj_ex) { |
| 3551 | PyObject *cls; |
| 3552 | PyObject *args; |
| 3553 | PyObject *kwargs; |
| 3554 | |
| 3555 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3556 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3557 | "length of the NEWOBJ_EX argument tuple must be " |
| 3558 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3559 | return -1; |
| 3560 | } |
| 3561 | |
| 3562 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3563 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3564 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3565 | "first item from NEWOBJ_EX argument tuple must " |
| 3566 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3567 | return -1; |
| 3568 | } |
| 3569 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3570 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3571 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3572 | "second item from NEWOBJ_EX argument tuple must " |
| 3573 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3574 | return -1; |
| 3575 | } |
| 3576 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3577 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3578 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3579 | "third item from NEWOBJ_EX argument tuple must " |
| 3580 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3581 | return -1; |
| 3582 | } |
| 3583 | |
| 3584 | if (save(self, cls, 0) < 0 || |
| 3585 | save(self, args, 0) < 0 || |
| 3586 | save(self, kwargs, 0) < 0 || |
| 3587 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3588 | return -1; |
| 3589 | } |
| 3590 | } |
| 3591 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3592 | PyObject *cls; |
| 3593 | PyObject *newargtup; |
| 3594 | PyObject *obj_class; |
| 3595 | int p; |
| 3596 | |
| 3597 | /* Sanity checks. */ |
| 3598 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3599 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3600 | return -1; |
| 3601 | } |
| 3602 | |
| 3603 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3604 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3605 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3606 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3607 | return -1; |
| 3608 | } |
| 3609 | |
| 3610 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3611 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3612 | p = obj_class != cls; /* true iff a problem */ |
| 3613 | Py_DECREF(obj_class); |
| 3614 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3615 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3616 | "__newobj__ args has the wrong class"); |
| 3617 | return -1; |
| 3618 | } |
| 3619 | } |
| 3620 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3621 | what happen if the value returned by the __reduce__() method of |
| 3622 | some extension type contains another object of the same type. Ouch! |
| 3623 | |
| 3624 | Here is a quick example, that I ran into, to illustrate what I |
| 3625 | mean: |
| 3626 | |
| 3627 | >>> import pickle, copyreg |
| 3628 | >>> copyreg.dispatch_table.pop(complex) |
| 3629 | >>> pickle.dumps(1+2j) |
| 3630 | Traceback (most recent call last): |
| 3631 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3632 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3633 | |
| 3634 | Removing the complex class from copyreg.dispatch_table made the |
| 3635 | __reduce_ex__() method emit another complex object: |
| 3636 | |
| 3637 | >>> (1+1j).__reduce_ex__(2) |
| 3638 | (<function __newobj__ at 0xb7b71c3c>, |
| 3639 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3640 | |
| 3641 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3642 | ensued. Of course, the bug was in the complex class which had a |
| 3643 | broken __getnewargs__() that emitted another complex object. But, |
| 3644 | the point, here, is it is quite easy to end up with a broken reduce |
| 3645 | function. */ |
| 3646 | |
| 3647 | /* Save the class and its __new__ arguments. */ |
| 3648 | if (save(self, cls, 0) < 0) |
| 3649 | return -1; |
| 3650 | |
| 3651 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3652 | if (newargtup == NULL) |
| 3653 | return -1; |
| 3654 | |
| 3655 | p = save(self, newargtup, 0); |
| 3656 | Py_DECREF(newargtup); |
| 3657 | if (p < 0) |
| 3658 | return -1; |
| 3659 | |
| 3660 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3661 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3662 | return -1; |
| 3663 | } |
| 3664 | else { /* Not using NEWOBJ. */ |
| 3665 | if (save(self, callable, 0) < 0 || |
| 3666 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3667 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3668 | return -1; |
| 3669 | } |
| 3670 | |
| 3671 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3672 | the caller do not want to memoize the object. Not particularly useful, |
| 3673 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3674 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3675 | if (obj != NULL) { |
| 3676 | /* If the object is already in the memo, this means it is |
| 3677 | recursive. In this case, throw away everything we put on the |
| 3678 | stack, and fetch the object back from the memo. */ |
| 3679 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3680 | const char pop_op = POP; |
| 3681 | |
| 3682 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3683 | return -1; |
| 3684 | if (memo_get(self, obj) < 0) |
| 3685 | return -1; |
| 3686 | |
| 3687 | return 0; |
| 3688 | } |
| 3689 | else if (memo_put(self, obj) < 0) |
| 3690 | return -1; |
| 3691 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3692 | |
| 3693 | if (listitems && batch_list(self, listitems) < 0) |
| 3694 | return -1; |
| 3695 | |
| 3696 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3697 | return -1; |
| 3698 | |
| 3699 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3700 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3701 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3702 | return -1; |
| 3703 | } |
| 3704 | |
| 3705 | return 0; |
| 3706 | } |
| 3707 | |
| 3708 | static int |
| 3709 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3710 | { |
| 3711 | PyTypeObject *type; |
| 3712 | PyObject *reduce_func = NULL; |
| 3713 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3714 | int status = 0; |
| 3715 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3716 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3717 | return -1; |
| 3718 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3719 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3720 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3721 | |
| 3722 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3723 | on its returned object. */ |
| 3724 | if (!pers_save && self->pers_func) { |
| 3725 | /* save_pers() returns: |
| 3726 | -1 to signal an error; |
| 3727 | 0 if it did nothing successfully; |
| 3728 | 1 if a persistent id was saved. |
| 3729 | */ |
| 3730 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3731 | goto done; |
| 3732 | } |
| 3733 | |
| 3734 | type = Py_TYPE(obj); |
| 3735 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3736 | /* The old cPickle had an optimization that used switch-case statement |
| 3737 | dispatching on the first letter of the type name. This has was removed |
| 3738 | since benchmarks shown that this optimization was actually slowing |
| 3739 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3740 | |
| 3741 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3742 | |
| 3743 | if (obj == Py_None) { |
| 3744 | status = save_none(self, obj); |
| 3745 | goto done; |
| 3746 | } |
| 3747 | else if (obj == Py_False || obj == Py_True) { |
| 3748 | status = save_bool(self, obj); |
| 3749 | goto done; |
| 3750 | } |
| 3751 | else if (type == &PyLong_Type) { |
| 3752 | status = save_long(self, obj); |
| 3753 | goto done; |
| 3754 | } |
| 3755 | else if (type == &PyFloat_Type) { |
| 3756 | status = save_float(self, obj); |
| 3757 | goto done; |
| 3758 | } |
| 3759 | |
| 3760 | /* Check the memo to see if it has the object. If so, generate |
| 3761 | a GET (or BINGET) opcode, instead of pickling the object |
| 3762 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3763 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3764 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3765 | goto error; |
| 3766 | goto done; |
| 3767 | } |
| 3768 | |
| 3769 | if (type == &PyBytes_Type) { |
| 3770 | status = save_bytes(self, obj); |
| 3771 | goto done; |
| 3772 | } |
| 3773 | else if (type == &PyUnicode_Type) { |
| 3774 | status = save_unicode(self, obj); |
| 3775 | goto done; |
| 3776 | } |
| 3777 | else if (type == &PyDict_Type) { |
| 3778 | status = save_dict(self, obj); |
| 3779 | goto done; |
| 3780 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3781 | else if (type == &PySet_Type) { |
| 3782 | status = save_set(self, obj); |
| 3783 | goto done; |
| 3784 | } |
| 3785 | else if (type == &PyFrozenSet_Type) { |
| 3786 | status = save_frozenset(self, obj); |
| 3787 | goto done; |
| 3788 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3789 | else if (type == &PyList_Type) { |
| 3790 | status = save_list(self, obj); |
| 3791 | goto done; |
| 3792 | } |
| 3793 | else if (type == &PyTuple_Type) { |
| 3794 | status = save_tuple(self, obj); |
| 3795 | goto done; |
| 3796 | } |
| 3797 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3798 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3799 | goto done; |
| 3800 | } |
| 3801 | else if (type == &PyFunction_Type) { |
| 3802 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3803 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3804 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3805 | |
| 3806 | /* XXX: This part needs some unit tests. */ |
| 3807 | |
| 3808 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3809 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3810 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3811 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3812 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3813 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3814 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3815 | (PyObject *)type); |
| 3816 | if (reduce_func == NULL) { |
| 3817 | if (PyErr_Occurred()) { |
| 3818 | goto error; |
| 3819 | } |
| 3820 | } else { |
| 3821 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3822 | Increase the reference count to be consistent with |
| 3823 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3824 | Py_INCREF(reduce_func); |
| 3825 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3826 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3827 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3828 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3829 | if (reduce_func == NULL) { |
| 3830 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3831 | PyErr_Clear(); |
| 3832 | else |
| 3833 | goto error; |
| 3834 | } |
| 3835 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3836 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3837 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3838 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3839 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3840 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3841 | status = save_global(self, obj, NULL); |
| 3842 | goto done; |
| 3843 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3844 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3845 | _Py_IDENTIFIER(__reduce__); |
| 3846 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3847 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3848 | |
| 3849 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3850 | automatically defined as __reduce__. While this is convenient, this |
| 3851 | make it impossible to know which method was actually called. Of |
| 3852 | course, this is not a big deal. But still, it would be nice to let |
| 3853 | the user know which method was called when something go |
| 3854 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3855 | don't actually have to check for a __reduce__ method. */ |
| 3856 | |
| 3857 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3858 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3859 | if (reduce_func != NULL) { |
| 3860 | PyObject *proto; |
| 3861 | proto = PyLong_FromLong(self->proto); |
| 3862 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3863 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3864 | } |
| 3865 | } |
| 3866 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3867 | PickleState *st = _Pickle_GetGlobalState(); |
| 3868 | |
| 3869 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3870 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3871 | } |
| 3872 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3873 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3874 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3875 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3876 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3877 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3878 | PyObject *empty_tuple = PyTuple_New(0); |
| 3879 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3880 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3881 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3882 | } |
| 3883 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3884 | PyErr_Format(st->PicklingError, |
| 3885 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3886 | type->tp_name, obj); |
| 3887 | goto error; |
| 3888 | } |
| 3889 | } |
| 3890 | } |
| 3891 | |
| 3892 | if (reduce_value == NULL) |
| 3893 | goto error; |
| 3894 | |
| 3895 | if (PyUnicode_Check(reduce_value)) { |
| 3896 | status = save_global(self, obj, reduce_value); |
| 3897 | goto done; |
| 3898 | } |
| 3899 | |
| 3900 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3901 | PickleState *st = _Pickle_GetGlobalState(); |
| 3902 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3903 | "__reduce__ must return a string or tuple"); |
| 3904 | goto error; |
| 3905 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3906 | |
| 3907 | status = save_reduce(self, reduce_value, obj); |
| 3908 | |
| 3909 | if (0) { |
| 3910 | error: |
| 3911 | status = -1; |
| 3912 | } |
| 3913 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3914 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3915 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3916 | Py_XDECREF(reduce_func); |
| 3917 | Py_XDECREF(reduce_value); |
| 3918 | |
| 3919 | return status; |
| 3920 | } |
| 3921 | |
| 3922 | static int |
| 3923 | dump(PicklerObject *self, PyObject *obj) |
| 3924 | { |
| 3925 | const char stop_op = STOP; |
| 3926 | |
| 3927 | if (self->proto >= 2) { |
| 3928 | char header[2]; |
| 3929 | |
| 3930 | header[0] = PROTO; |
| 3931 | assert(self->proto >= 0 && self->proto < 256); |
| 3932 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3933 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3934 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3935 | if (self->proto >= 4) |
| 3936 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3937 | } |
| 3938 | |
| 3939 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3940 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3941 | return -1; |
| 3942 | |
| 3943 | return 0; |
| 3944 | } |
| 3945 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3946 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3947 | |
| 3948 | _pickle.Pickler.clear_memo |
| 3949 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3950 | Clears the pickler's "memo". |
| 3951 | |
| 3952 | The memo is the data structure that remembers which objects the |
| 3953 | pickler has already seen, so that shared or recursive objects are |
| 3954 | pickled by reference and not by value. This method is useful when |
| 3955 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3956 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3957 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 3958 | static PyObject * |
| 3959 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3960 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3961 | { |
| 3962 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3963 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3964 | |
| 3965 | Py_RETURN_NONE; |
| 3966 | } |
| 3967 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3968 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3969 | |
| 3970 | _pickle.Pickler.dump |
| 3971 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3972 | obj: object |
| 3973 | / |
| 3974 | |
| 3975 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3976 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3977 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3978 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3979 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3980 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3981 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3982 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3983 | Developers often forget to call __init__() in their subclasses, which |
| 3984 | would trigger a segfault without this check. */ |
| 3985 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3986 | PickleState *st = _Pickle_GetGlobalState(); |
| 3987 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3988 | "Pickler.__init__() was not called by %s.__init__()", |
| 3989 | Py_TYPE(self)->tp_name); |
| 3990 | return NULL; |
| 3991 | } |
| 3992 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3993 | if (_Pickler_ClearBuffer(self) < 0) |
| 3994 | return NULL; |
| 3995 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3996 | if (dump(self, obj) < 0) |
| 3997 | return NULL; |
| 3998 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3999 | if (_Pickler_FlushToFile(self) < 0) |
| 4000 | return NULL; |
| 4001 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4002 | Py_RETURN_NONE; |
| 4003 | } |
| 4004 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4005 | /*[clinic input] |
| 4006 | |
| 4007 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4008 | |
| 4009 | Returns size in memory, in bytes. |
| 4010 | [clinic start generated code]*/ |
| 4011 | |
| 4012 | static Py_ssize_t |
| 4013 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4014 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4015 | { |
| 4016 | Py_ssize_t res, s; |
| 4017 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame^] | 4018 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4019 | if (self->memo != NULL) { |
| 4020 | res += sizeof(PyMemoTable); |
| 4021 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4022 | } |
| 4023 | if (self->output_buffer != NULL) { |
| 4024 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4025 | if (s == -1) |
| 4026 | return -1; |
| 4027 | res += s; |
| 4028 | } |
| 4029 | return res; |
| 4030 | } |
| 4031 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4032 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4033 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4034 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4035 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4036 | {NULL, NULL} /* sentinel */ |
| 4037 | }; |
| 4038 | |
| 4039 | static void |
| 4040 | Pickler_dealloc(PicklerObject *self) |
| 4041 | { |
| 4042 | PyObject_GC_UnTrack(self); |
| 4043 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4044 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4045 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4046 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4047 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4048 | Py_XDECREF(self->fast_memo); |
| 4049 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4050 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4051 | |
| 4052 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4053 | } |
| 4054 | |
| 4055 | static int |
| 4056 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4057 | { |
| 4058 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4059 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4060 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4061 | Py_VISIT(self->fast_memo); |
| 4062 | return 0; |
| 4063 | } |
| 4064 | |
| 4065 | static int |
| 4066 | Pickler_clear(PicklerObject *self) |
| 4067 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4068 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4069 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4070 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4071 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4072 | Py_CLEAR(self->fast_memo); |
| 4073 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4074 | if (self->memo != NULL) { |
| 4075 | PyMemoTable *memo = self->memo; |
| 4076 | self->memo = NULL; |
| 4077 | PyMemoTable_Del(memo); |
| 4078 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4079 | return 0; |
| 4080 | } |
| 4081 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4082 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4083 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4084 | |
| 4085 | _pickle.Pickler.__init__ |
| 4086 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4087 | file: object |
| 4088 | protocol: object = NULL |
| 4089 | fix_imports: bool = True |
| 4090 | |
| 4091 | This takes a binary file for writing a pickle data stream. |
| 4092 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4093 | The optional *protocol* argument tells the pickler to use the given |
| 4094 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4095 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4096 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4097 | Specifying a negative protocol version selects the highest protocol |
| 4098 | version supported. The higher the protocol used, the more recent the |
| 4099 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4100 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4101 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4102 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4103 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4104 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4105 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4106 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4107 | to map the new Python 3 names to the old module names used in Python |
| 4108 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4109 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4110 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4111 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4112 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4113 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4114 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4115 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4116 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4117 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4118 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4119 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4120 | if (self->write != NULL) |
| 4121 | (void)Pickler_clear(self); |
| 4122 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4123 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4124 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4125 | |
| 4126 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4127 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4128 | |
| 4129 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4130 | if (self->memo == NULL) { |
| 4131 | self->memo = PyMemoTable_New(); |
| 4132 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4133 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4134 | } |
| 4135 | self->output_len = 0; |
| 4136 | if (self->output_buffer == NULL) { |
| 4137 | self->max_output_len = WRITE_BUF_SIZE; |
| 4138 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4139 | self->max_output_len); |
| 4140 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4141 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4142 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4143 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4144 | self->fast = 0; |
| 4145 | self->fast_nesting = 0; |
| 4146 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4147 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4148 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4149 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4150 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4151 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4152 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4153 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4154 | self->dispatch_table = NULL; |
| 4155 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4156 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4157 | &PyId_dispatch_table); |
| 4158 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4159 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4160 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4161 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4162 | return 0; |
| 4163 | } |
| 4164 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4165 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4166 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4167 | * avoid breaking code like: |
| 4168 | * pickler.memo.clear() |
| 4169 | * and |
| 4170 | * pickler.memo = saved_memo |
| 4171 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4172 | * it. Note that we don't implement the entire mapping API here. This is |
| 4173 | * intentional, as these should be treated as black-box implementation details. |
| 4174 | */ |
| 4175 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4176 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4177 | _pickle.PicklerMemoProxy.clear |
| 4178 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4179 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4180 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4181 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4182 | static PyObject * |
| 4183 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4184 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4185 | { |
| 4186 | if (self->pickler->memo) |
| 4187 | PyMemoTable_Clear(self->pickler->memo); |
| 4188 | Py_RETURN_NONE; |
| 4189 | } |
| 4190 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4191 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4192 | _pickle.PicklerMemoProxy.copy |
| 4193 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4194 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4195 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4196 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4197 | static PyObject * |
| 4198 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4199 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4200 | { |
| 4201 | Py_ssize_t i; |
| 4202 | PyMemoTable *memo; |
| 4203 | PyObject *new_memo = PyDict_New(); |
| 4204 | if (new_memo == NULL) |
| 4205 | return NULL; |
| 4206 | |
| 4207 | memo = self->pickler->memo; |
| 4208 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4209 | PyMemoEntry entry = memo->mt_table[i]; |
| 4210 | if (entry.me_key != NULL) { |
| 4211 | int status; |
| 4212 | PyObject *key, *value; |
| 4213 | |
| 4214 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4215 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4216 | |
| 4217 | if (key == NULL || value == NULL) { |
| 4218 | Py_XDECREF(key); |
| 4219 | Py_XDECREF(value); |
| 4220 | goto error; |
| 4221 | } |
| 4222 | status = PyDict_SetItem(new_memo, key, value); |
| 4223 | Py_DECREF(key); |
| 4224 | Py_DECREF(value); |
| 4225 | if (status < 0) |
| 4226 | goto error; |
| 4227 | } |
| 4228 | } |
| 4229 | return new_memo; |
| 4230 | |
| 4231 | error: |
| 4232 | Py_XDECREF(new_memo); |
| 4233 | return NULL; |
| 4234 | } |
| 4235 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4236 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4237 | _pickle.PicklerMemoProxy.__reduce__ |
| 4238 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4239 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4240 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4241 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4242 | static PyObject * |
| 4243 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4244 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4245 | { |
| 4246 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4247 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4248 | if (contents == NULL) |
| 4249 | return NULL; |
| 4250 | |
| 4251 | reduce_value = PyTuple_New(2); |
| 4252 | if (reduce_value == NULL) { |
| 4253 | Py_DECREF(contents); |
| 4254 | return NULL; |
| 4255 | } |
| 4256 | dict_args = PyTuple_New(1); |
| 4257 | if (dict_args == NULL) { |
| 4258 | Py_DECREF(contents); |
| 4259 | Py_DECREF(reduce_value); |
| 4260 | return NULL; |
| 4261 | } |
| 4262 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4263 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4264 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4265 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4266 | return reduce_value; |
| 4267 | } |
| 4268 | |
| 4269 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4270 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4271 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4272 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4273 | {NULL, NULL} /* sentinel */ |
| 4274 | }; |
| 4275 | |
| 4276 | static void |
| 4277 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4278 | { |
| 4279 | PyObject_GC_UnTrack(self); |
| 4280 | Py_XDECREF(self->pickler); |
| 4281 | PyObject_GC_Del((PyObject *)self); |
| 4282 | } |
| 4283 | |
| 4284 | static int |
| 4285 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4286 | visitproc visit, void *arg) |
| 4287 | { |
| 4288 | Py_VISIT(self->pickler); |
| 4289 | return 0; |
| 4290 | } |
| 4291 | |
| 4292 | static int |
| 4293 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4294 | { |
| 4295 | Py_CLEAR(self->pickler); |
| 4296 | return 0; |
| 4297 | } |
| 4298 | |
| 4299 | static PyTypeObject PicklerMemoProxyType = { |
| 4300 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4301 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4302 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4303 | 0, |
| 4304 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4305 | 0, /* tp_print */ |
| 4306 | 0, /* tp_getattr */ |
| 4307 | 0, /* tp_setattr */ |
| 4308 | 0, /* tp_compare */ |
| 4309 | 0, /* tp_repr */ |
| 4310 | 0, /* tp_as_number */ |
| 4311 | 0, /* tp_as_sequence */ |
| 4312 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4313 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4314 | 0, /* tp_call */ |
| 4315 | 0, /* tp_str */ |
| 4316 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4317 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4318 | 0, /* tp_as_buffer */ |
| 4319 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4320 | 0, /* tp_doc */ |
| 4321 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4322 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4323 | 0, /* tp_richcompare */ |
| 4324 | 0, /* tp_weaklistoffset */ |
| 4325 | 0, /* tp_iter */ |
| 4326 | 0, /* tp_iternext */ |
| 4327 | picklerproxy_methods, /* tp_methods */ |
| 4328 | }; |
| 4329 | |
| 4330 | static PyObject * |
| 4331 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4332 | { |
| 4333 | PicklerMemoProxyObject *self; |
| 4334 | |
| 4335 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4336 | if (self == NULL) |
| 4337 | return NULL; |
| 4338 | Py_INCREF(pickler); |
| 4339 | self->pickler = pickler; |
| 4340 | PyObject_GC_Track(self); |
| 4341 | return (PyObject *)self; |
| 4342 | } |
| 4343 | |
| 4344 | /*****************************************************************************/ |
| 4345 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4346 | static PyObject * |
| 4347 | Pickler_get_memo(PicklerObject *self) |
| 4348 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4349 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4350 | } |
| 4351 | |
| 4352 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4353 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4354 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4355 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4356 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4357 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4358 | PyErr_SetString(PyExc_TypeError, |
| 4359 | "attribute deletion is not supported"); |
| 4360 | return -1; |
| 4361 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4362 | |
| 4363 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4364 | PicklerObject *pickler = |
| 4365 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4366 | |
| 4367 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4368 | if (new_memo == NULL) |
| 4369 | return -1; |
| 4370 | } |
| 4371 | else if (PyDict_Check(obj)) { |
| 4372 | Py_ssize_t i = 0; |
| 4373 | PyObject *key, *value; |
| 4374 | |
| 4375 | new_memo = PyMemoTable_New(); |
| 4376 | if (new_memo == NULL) |
| 4377 | return -1; |
| 4378 | |
| 4379 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4380 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4381 | PyObject *memo_obj; |
| 4382 | |
| 4383 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4384 | PyErr_SetString(PyExc_TypeError, |
| 4385 | "'memo' values must be 2-item tuples"); |
| 4386 | goto error; |
| 4387 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4388 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4389 | if (memo_id == -1 && PyErr_Occurred()) |
| 4390 | goto error; |
| 4391 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4392 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4393 | goto error; |
| 4394 | } |
| 4395 | } |
| 4396 | else { |
| 4397 | PyErr_Format(PyExc_TypeError, |
| 4398 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4399 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4400 | return -1; |
| 4401 | } |
| 4402 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4403 | PyMemoTable_Del(self->memo); |
| 4404 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4405 | |
| 4406 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4407 | |
| 4408 | error: |
| 4409 | if (new_memo) |
| 4410 | PyMemoTable_Del(new_memo); |
| 4411 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | static PyObject * |
| 4415 | Pickler_get_persid(PicklerObject *self) |
| 4416 | { |
| 4417 | if (self->pers_func == NULL) |
| 4418 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4419 | else |
| 4420 | Py_INCREF(self->pers_func); |
| 4421 | return self->pers_func; |
| 4422 | } |
| 4423 | |
| 4424 | static int |
| 4425 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4426 | { |
| 4427 | PyObject *tmp; |
| 4428 | |
| 4429 | if (value == NULL) { |
| 4430 | PyErr_SetString(PyExc_TypeError, |
| 4431 | "attribute deletion is not supported"); |
| 4432 | return -1; |
| 4433 | } |
| 4434 | if (!PyCallable_Check(value)) { |
| 4435 | PyErr_SetString(PyExc_TypeError, |
| 4436 | "persistent_id must be a callable taking one argument"); |
| 4437 | return -1; |
| 4438 | } |
| 4439 | |
| 4440 | tmp = self->pers_func; |
| 4441 | Py_INCREF(value); |
| 4442 | self->pers_func = value; |
| 4443 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4444 | |
| 4445 | return 0; |
| 4446 | } |
| 4447 | |
| 4448 | static PyMemberDef Pickler_members[] = { |
| 4449 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4450 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4451 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4452 | {NULL} |
| 4453 | }; |
| 4454 | |
| 4455 | static PyGetSetDef Pickler_getsets[] = { |
| 4456 | {"memo", (getter)Pickler_get_memo, |
| 4457 | (setter)Pickler_set_memo}, |
| 4458 | {"persistent_id", (getter)Pickler_get_persid, |
| 4459 | (setter)Pickler_set_persid}, |
| 4460 | {NULL} |
| 4461 | }; |
| 4462 | |
| 4463 | static PyTypeObject Pickler_Type = { |
| 4464 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4465 | "_pickle.Pickler" , /*tp_name*/ |
| 4466 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4467 | 0, /*tp_itemsize*/ |
| 4468 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4469 | 0, /*tp_print*/ |
| 4470 | 0, /*tp_getattr*/ |
| 4471 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4472 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4473 | 0, /*tp_repr*/ |
| 4474 | 0, /*tp_as_number*/ |
| 4475 | 0, /*tp_as_sequence*/ |
| 4476 | 0, /*tp_as_mapping*/ |
| 4477 | 0, /*tp_hash*/ |
| 4478 | 0, /*tp_call*/ |
| 4479 | 0, /*tp_str*/ |
| 4480 | 0, /*tp_getattro*/ |
| 4481 | 0, /*tp_setattro*/ |
| 4482 | 0, /*tp_as_buffer*/ |
| 4483 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4484 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4485 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4486 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4487 | 0, /*tp_richcompare*/ |
| 4488 | 0, /*tp_weaklistoffset*/ |
| 4489 | 0, /*tp_iter*/ |
| 4490 | 0, /*tp_iternext*/ |
| 4491 | Pickler_methods, /*tp_methods*/ |
| 4492 | Pickler_members, /*tp_members*/ |
| 4493 | Pickler_getsets, /*tp_getset*/ |
| 4494 | 0, /*tp_base*/ |
| 4495 | 0, /*tp_dict*/ |
| 4496 | 0, /*tp_descr_get*/ |
| 4497 | 0, /*tp_descr_set*/ |
| 4498 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4499 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4500 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4501 | PyType_GenericNew, /*tp_new*/ |
| 4502 | PyObject_GC_Del, /*tp_free*/ |
| 4503 | 0, /*tp_is_gc*/ |
| 4504 | }; |
| 4505 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4506 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4507 | |
| 4508 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4509 | using directly the C version of find_class(), when find_class() is not |
| 4510 | overridden by a subclass. Although, this could become rather hackish. A |
| 4511 | simpler optimization would be to call the C function when self is not a |
| 4512 | subclass instance. */ |
| 4513 | static PyObject * |
| 4514 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4515 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4516 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4517 | |
| 4518 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4519 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4520 | } |
| 4521 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4522 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4523 | marker(UnpicklerObject *self) |
| 4524 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4525 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4526 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4527 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4528 | return -1; |
| 4529 | } |
| 4530 | |
| 4531 | return self->marks[--self->num_marks]; |
| 4532 | } |
| 4533 | |
| 4534 | static int |
| 4535 | load_none(UnpicklerObject *self) |
| 4536 | { |
| 4537 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4538 | return 0; |
| 4539 | } |
| 4540 | |
| 4541 | static int |
| 4542 | bad_readline(void) |
| 4543 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4544 | PickleState *st = _Pickle_GetGlobalState(); |
| 4545 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4546 | return -1; |
| 4547 | } |
| 4548 | |
| 4549 | static int |
| 4550 | load_int(UnpicklerObject *self) |
| 4551 | { |
| 4552 | PyObject *value; |
| 4553 | char *endptr, *s; |
| 4554 | Py_ssize_t len; |
| 4555 | long x; |
| 4556 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4557 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4558 | return -1; |
| 4559 | if (len < 2) |
| 4560 | return bad_readline(); |
| 4561 | |
| 4562 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4563 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4564 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4565 | x = strtol(s, &endptr, 0); |
| 4566 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4567 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4568 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4569 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4570 | errno = 0; |
| 4571 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4572 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4573 | if (value == NULL) { |
| 4574 | PyErr_SetString(PyExc_ValueError, |
| 4575 | "could not convert string to int"); |
| 4576 | return -1; |
| 4577 | } |
| 4578 | } |
| 4579 | else { |
| 4580 | if (len == 3 && (x == 0 || x == 1)) { |
| 4581 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4582 | return -1; |
| 4583 | } |
| 4584 | else { |
| 4585 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4586 | return -1; |
| 4587 | } |
| 4588 | } |
| 4589 | |
| 4590 | PDATA_PUSH(self->stack, value, -1); |
| 4591 | return 0; |
| 4592 | } |
| 4593 | |
| 4594 | static int |
| 4595 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4596 | { |
| 4597 | assert(boolean == Py_True || boolean == Py_False); |
| 4598 | PDATA_APPEND(self->stack, boolean, -1); |
| 4599 | return 0; |
| 4600 | } |
| 4601 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4602 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4603 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4604 | */ |
| 4605 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4606 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4607 | { |
| 4608 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4609 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4610 | size_t x = 0; |
| 4611 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4612 | if (nbytes > (int)sizeof(size_t)) { |
| 4613 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4614 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4615 | */ |
| 4616 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4617 | if (s[i]) |
| 4618 | return -1; |
| 4619 | } |
| 4620 | nbytes = (int)sizeof(size_t); |
| 4621 | } |
| 4622 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4623 | x |= (size_t) s[i] << (8 * i); |
| 4624 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4625 | |
| 4626 | if (x > PY_SSIZE_T_MAX) |
| 4627 | return -1; |
| 4628 | else |
| 4629 | return (Py_ssize_t) x; |
| 4630 | } |
| 4631 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4632 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4633 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4634 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4635 | * of x-platform bugs. |
| 4636 | */ |
| 4637 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4638 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4639 | { |
| 4640 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4641 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4642 | long x = 0; |
| 4643 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4644 | for (i = 0; i < nbytes; i++) { |
| 4645 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4649 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4650 | * to extend a BININT's sign bit to the full width. |
| 4651 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4652 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4653 | x |= -(x & (1L << 31)); |
| 4654 | } |
| 4655 | |
| 4656 | return x; |
| 4657 | } |
| 4658 | |
| 4659 | static int |
| 4660 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4661 | { |
| 4662 | PyObject *value; |
| 4663 | long x; |
| 4664 | |
| 4665 | x = calc_binint(s, size); |
| 4666 | |
| 4667 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4668 | return -1; |
| 4669 | |
| 4670 | PDATA_PUSH(self->stack, value, -1); |
| 4671 | return 0; |
| 4672 | } |
| 4673 | |
| 4674 | static int |
| 4675 | load_binint(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, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4680 | return -1; |
| 4681 | |
| 4682 | return load_binintx(self, s, 4); |
| 4683 | } |
| 4684 | |
| 4685 | static int |
| 4686 | load_binint1(UnpicklerObject *self) |
| 4687 | { |
| 4688 | char *s; |
| 4689 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4690 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4691 | return -1; |
| 4692 | |
| 4693 | return load_binintx(self, s, 1); |
| 4694 | } |
| 4695 | |
| 4696 | static int |
| 4697 | load_binint2(UnpicklerObject *self) |
| 4698 | { |
| 4699 | char *s; |
| 4700 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4701 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4702 | return -1; |
| 4703 | |
| 4704 | return load_binintx(self, s, 2); |
| 4705 | } |
| 4706 | |
| 4707 | static int |
| 4708 | load_long(UnpicklerObject *self) |
| 4709 | { |
| 4710 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4711 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4712 | Py_ssize_t len; |
| 4713 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4714 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4715 | return -1; |
| 4716 | if (len < 2) |
| 4717 | return bad_readline(); |
| 4718 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4719 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4720 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4721 | compatibility with Python 3.0.0, we don't actually *require* |
| 4722 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4723 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4724 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4725 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4726 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4727 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4728 | return -1; |
| 4729 | |
| 4730 | PDATA_PUSH(self->stack, value, -1); |
| 4731 | return 0; |
| 4732 | } |
| 4733 | |
| 4734 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4735 | * data following. |
| 4736 | */ |
| 4737 | static int |
| 4738 | load_counted_long(UnpicklerObject *self, int size) |
| 4739 | { |
| 4740 | PyObject *value; |
| 4741 | char *nbytes; |
| 4742 | char *pdata; |
| 4743 | |
| 4744 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4745 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4746 | return -1; |
| 4747 | |
| 4748 | size = calc_binint(nbytes, size); |
| 4749 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4750 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4751 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4752 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4753 | "LONG pickle has negative byte count"); |
| 4754 | return -1; |
| 4755 | } |
| 4756 | |
| 4757 | if (size == 0) |
| 4758 | value = PyLong_FromLong(0L); |
| 4759 | else { |
| 4760 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4761 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4762 | return -1; |
| 4763 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4764 | 1 /* little endian */ , 1 /* signed */ ); |
| 4765 | } |
| 4766 | if (value == NULL) |
| 4767 | return -1; |
| 4768 | PDATA_PUSH(self->stack, value, -1); |
| 4769 | return 0; |
| 4770 | } |
| 4771 | |
| 4772 | static int |
| 4773 | load_float(UnpicklerObject *self) |
| 4774 | { |
| 4775 | PyObject *value; |
| 4776 | char *endptr, *s; |
| 4777 | Py_ssize_t len; |
| 4778 | double d; |
| 4779 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4780 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4781 | return -1; |
| 4782 | if (len < 2) |
| 4783 | return bad_readline(); |
| 4784 | |
| 4785 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4786 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4787 | if (d == -1.0 && PyErr_Occurred()) |
| 4788 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4789 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4790 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4791 | return -1; |
| 4792 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4793 | value = PyFloat_FromDouble(d); |
| 4794 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4795 | return -1; |
| 4796 | |
| 4797 | PDATA_PUSH(self->stack, value, -1); |
| 4798 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4799 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4800 | |
| 4801 | static int |
| 4802 | load_binfloat(UnpicklerObject *self) |
| 4803 | { |
| 4804 | PyObject *value; |
| 4805 | double x; |
| 4806 | char *s; |
| 4807 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4808 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4809 | return -1; |
| 4810 | |
| 4811 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4812 | if (x == -1.0 && PyErr_Occurred()) |
| 4813 | return -1; |
| 4814 | |
| 4815 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4816 | return -1; |
| 4817 | |
| 4818 | PDATA_PUSH(self->stack, value, -1); |
| 4819 | return 0; |
| 4820 | } |
| 4821 | |
| 4822 | static int |
| 4823 | load_string(UnpicklerObject *self) |
| 4824 | { |
| 4825 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4826 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4827 | Py_ssize_t len; |
| 4828 | char *s, *p; |
| 4829 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4830 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4831 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4832 | /* Strip the newline */ |
| 4833 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4834 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4835 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4836 | p = s + 1; |
| 4837 | len -= 2; |
| 4838 | } |
| 4839 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4840 | PickleState *st = _Pickle_GetGlobalState(); |
| 4841 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4842 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4843 | return -1; |
| 4844 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4845 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4846 | |
| 4847 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4848 | to encode, and then coerce the result to Unicode. */ |
| 4849 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4850 | if (bytes == NULL) |
| 4851 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4852 | |
| 4853 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4854 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4855 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4856 | obj = bytes; |
| 4857 | } |
| 4858 | else { |
| 4859 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4860 | Py_DECREF(bytes); |
| 4861 | if (obj == NULL) { |
| 4862 | return -1; |
| 4863 | } |
| 4864 | } |
| 4865 | |
| 4866 | PDATA_PUSH(self->stack, obj, -1); |
| 4867 | return 0; |
| 4868 | } |
| 4869 | |
| 4870 | static int |
| 4871 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4872 | { |
| 4873 | PyObject *obj; |
| 4874 | Py_ssize_t size; |
| 4875 | char *s; |
| 4876 | |
| 4877 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4878 | return -1; |
| 4879 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4880 | size = calc_binsize(s, nbytes); |
| 4881 | if (size < 0) { |
| 4882 | PickleState *st = _Pickle_GetGlobalState(); |
| 4883 | PyErr_Format(st->UnpicklingError, |
| 4884 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4885 | PY_SSIZE_T_MAX); |
| 4886 | return -1; |
| 4887 | } |
| 4888 | |
| 4889 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4890 | return -1; |
| 4891 | |
| 4892 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4893 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4894 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4895 | obj = PyBytes_FromStringAndSize(s, size); |
| 4896 | } |
| 4897 | else { |
| 4898 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4899 | } |
| 4900 | if (obj == NULL) { |
| 4901 | return -1; |
| 4902 | } |
| 4903 | |
| 4904 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4905 | return 0; |
| 4906 | } |
| 4907 | |
| 4908 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4909 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4910 | { |
| 4911 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4912 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4913 | char *s; |
| 4914 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4915 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4916 | return -1; |
| 4917 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4918 | size = calc_binsize(s, nbytes); |
| 4919 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4920 | PyErr_Format(PyExc_OverflowError, |
| 4921 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4922 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4923 | return -1; |
| 4924 | } |
| 4925 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4926 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4927 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4928 | |
| 4929 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4930 | if (bytes == NULL) |
| 4931 | return -1; |
| 4932 | |
| 4933 | PDATA_PUSH(self->stack, bytes, -1); |
| 4934 | return 0; |
| 4935 | } |
| 4936 | |
| 4937 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4938 | load_unicode(UnpicklerObject *self) |
| 4939 | { |
| 4940 | PyObject *str; |
| 4941 | Py_ssize_t len; |
| 4942 | char *s; |
| 4943 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4944 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4945 | return -1; |
| 4946 | if (len < 1) |
| 4947 | return bad_readline(); |
| 4948 | |
| 4949 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4950 | if (str == NULL) |
| 4951 | return -1; |
| 4952 | |
| 4953 | PDATA_PUSH(self->stack, str, -1); |
| 4954 | return 0; |
| 4955 | } |
| 4956 | |
| 4957 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4958 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4959 | { |
| 4960 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4961 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4962 | char *s; |
| 4963 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4964 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4965 | return -1; |
| 4966 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4967 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4968 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4969 | PyErr_Format(PyExc_OverflowError, |
| 4970 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4971 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4972 | return -1; |
| 4973 | } |
| 4974 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4975 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4976 | return -1; |
| 4977 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4978 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4979 | if (str == NULL) |
| 4980 | return -1; |
| 4981 | |
| 4982 | PDATA_PUSH(self->stack, str, -1); |
| 4983 | return 0; |
| 4984 | } |
| 4985 | |
| 4986 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 4987 | load_counted_tuple(UnpicklerObject *self, int len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4988 | { |
| 4989 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4990 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 4991 | if (Py_SIZE(self->stack) < len) |
| 4992 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4993 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 4994 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4995 | if (tuple == NULL) |
| 4996 | return -1; |
| 4997 | PDATA_PUSH(self->stack, tuple, -1); |
| 4998 | return 0; |
| 4999 | } |
| 5000 | |
| 5001 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5002 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5003 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5004 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5005 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5006 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5007 | return -1; |
| 5008 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5009 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5010 | } |
| 5011 | |
| 5012 | static int |
| 5013 | load_empty_list(UnpicklerObject *self) |
| 5014 | { |
| 5015 | PyObject *list; |
| 5016 | |
| 5017 | if ((list = PyList_New(0)) == NULL) |
| 5018 | return -1; |
| 5019 | PDATA_PUSH(self->stack, list, -1); |
| 5020 | return 0; |
| 5021 | } |
| 5022 | |
| 5023 | static int |
| 5024 | load_empty_dict(UnpicklerObject *self) |
| 5025 | { |
| 5026 | PyObject *dict; |
| 5027 | |
| 5028 | if ((dict = PyDict_New()) == NULL) |
| 5029 | return -1; |
| 5030 | PDATA_PUSH(self->stack, dict, -1); |
| 5031 | return 0; |
| 5032 | } |
| 5033 | |
| 5034 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5035 | load_empty_set(UnpicklerObject *self) |
| 5036 | { |
| 5037 | PyObject *set; |
| 5038 | |
| 5039 | if ((set = PySet_New(NULL)) == NULL) |
| 5040 | return -1; |
| 5041 | PDATA_PUSH(self->stack, set, -1); |
| 5042 | return 0; |
| 5043 | } |
| 5044 | |
| 5045 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5046 | load_list(UnpicklerObject *self) |
| 5047 | { |
| 5048 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5049 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5050 | |
| 5051 | if ((i = marker(self)) < 0) |
| 5052 | return -1; |
| 5053 | |
| 5054 | list = Pdata_poplist(self->stack, i); |
| 5055 | if (list == NULL) |
| 5056 | return -1; |
| 5057 | PDATA_PUSH(self->stack, list, -1); |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
| 5061 | static int |
| 5062 | load_dict(UnpicklerObject *self) |
| 5063 | { |
| 5064 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5065 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5066 | |
| 5067 | if ((i = marker(self)) < 0) |
| 5068 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5069 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5070 | |
| 5071 | if ((dict = PyDict_New()) == NULL) |
| 5072 | return -1; |
| 5073 | |
| 5074 | for (k = i + 1; k < j; k += 2) { |
| 5075 | key = self->stack->data[k - 1]; |
| 5076 | value = self->stack->data[k]; |
| 5077 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5078 | Py_DECREF(dict); |
| 5079 | return -1; |
| 5080 | } |
| 5081 | } |
| 5082 | Pdata_clear(self->stack, i); |
| 5083 | PDATA_PUSH(self->stack, dict, -1); |
| 5084 | return 0; |
| 5085 | } |
| 5086 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5087 | static int |
| 5088 | load_frozenset(UnpicklerObject *self) |
| 5089 | { |
| 5090 | PyObject *items; |
| 5091 | PyObject *frozenset; |
| 5092 | Py_ssize_t i; |
| 5093 | |
| 5094 | if ((i = marker(self)) < 0) |
| 5095 | return -1; |
| 5096 | |
| 5097 | items = Pdata_poptuple(self->stack, i); |
| 5098 | if (items == NULL) |
| 5099 | return -1; |
| 5100 | |
| 5101 | frozenset = PyFrozenSet_New(items); |
| 5102 | Py_DECREF(items); |
| 5103 | if (frozenset == NULL) |
| 5104 | return -1; |
| 5105 | |
| 5106 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5110 | static PyObject * |
| 5111 | instantiate(PyObject *cls, PyObject *args) |
| 5112 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5113 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5114 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5115 | /* Caller must assure args are a tuple. Normally, args come from |
| 5116 | Pdata_poptuple which packs objects from the top of the stack |
| 5117 | into a newly created tuple. */ |
| 5118 | assert(PyTuple_Check(args)); |
| 5119 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5120 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5121 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5122 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5123 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5124 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5125 | |
| 5126 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5127 | } |
| 5128 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5129 | } |
| 5130 | |
| 5131 | static int |
| 5132 | load_obj(UnpicklerObject *self) |
| 5133 | { |
| 5134 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5135 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5136 | |
| 5137 | if ((i = marker(self)) < 0) |
| 5138 | return -1; |
| 5139 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5140 | if (Py_SIZE(self->stack) - i < 1) |
| 5141 | return stack_underflow(); |
| 5142 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5143 | args = Pdata_poptuple(self->stack, i + 1); |
| 5144 | if (args == NULL) |
| 5145 | return -1; |
| 5146 | |
| 5147 | PDATA_POP(self->stack, cls); |
| 5148 | if (cls) { |
| 5149 | obj = instantiate(cls, args); |
| 5150 | Py_DECREF(cls); |
| 5151 | } |
| 5152 | Py_DECREF(args); |
| 5153 | if (obj == NULL) |
| 5154 | return -1; |
| 5155 | |
| 5156 | PDATA_PUSH(self->stack, obj, -1); |
| 5157 | return 0; |
| 5158 | } |
| 5159 | |
| 5160 | static int |
| 5161 | load_inst(UnpicklerObject *self) |
| 5162 | { |
| 5163 | PyObject *cls = NULL; |
| 5164 | PyObject *args = NULL; |
| 5165 | PyObject *obj = NULL; |
| 5166 | PyObject *module_name; |
| 5167 | PyObject *class_name; |
| 5168 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5169 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5170 | char *s; |
| 5171 | |
| 5172 | if ((i = marker(self)) < 0) |
| 5173 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5174 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5175 | return -1; |
| 5176 | if (len < 2) |
| 5177 | return bad_readline(); |
| 5178 | |
| 5179 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5180 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5181 | supported by older protocols on Python 2.x. */ |
| 5182 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5183 | if (module_name == NULL) |
| 5184 | return -1; |
| 5185 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5186 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5187 | if (len < 2) { |
| 5188 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5189 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5190 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5191 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5192 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5193 | cls = find_class(self, module_name, class_name); |
| 5194 | Py_DECREF(class_name); |
| 5195 | } |
| 5196 | } |
| 5197 | Py_DECREF(module_name); |
| 5198 | |
| 5199 | if (cls == NULL) |
| 5200 | return -1; |
| 5201 | |
| 5202 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5203 | obj = instantiate(cls, args); |
| 5204 | Py_DECREF(args); |
| 5205 | } |
| 5206 | Py_DECREF(cls); |
| 5207 | |
| 5208 | if (obj == NULL) |
| 5209 | return -1; |
| 5210 | |
| 5211 | PDATA_PUSH(self->stack, obj, -1); |
| 5212 | return 0; |
| 5213 | } |
| 5214 | |
| 5215 | static int |
| 5216 | load_newobj(UnpicklerObject *self) |
| 5217 | { |
| 5218 | PyObject *args = NULL; |
| 5219 | PyObject *clsraw = NULL; |
| 5220 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5221 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5222 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5223 | |
| 5224 | /* Stack is ... cls argtuple, and we want to call |
| 5225 | * cls.__new__(cls, *argtuple). |
| 5226 | */ |
| 5227 | PDATA_POP(self->stack, args); |
| 5228 | if (args == NULL) |
| 5229 | goto error; |
| 5230 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5231 | PyErr_SetString(st->UnpicklingError, |
| 5232 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5233 | goto error; |
| 5234 | } |
| 5235 | |
| 5236 | PDATA_POP(self->stack, clsraw); |
| 5237 | cls = (PyTypeObject *)clsraw; |
| 5238 | if (cls == NULL) |
| 5239 | goto error; |
| 5240 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5241 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5242 | "isn't a type object"); |
| 5243 | goto error; |
| 5244 | } |
| 5245 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5246 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5247 | "has NULL tp_new"); |
| 5248 | goto error; |
| 5249 | } |
| 5250 | |
| 5251 | /* Call __new__. */ |
| 5252 | obj = cls->tp_new(cls, args, NULL); |
| 5253 | if (obj == NULL) |
| 5254 | goto error; |
| 5255 | |
| 5256 | Py_DECREF(args); |
| 5257 | Py_DECREF(clsraw); |
| 5258 | PDATA_PUSH(self->stack, obj, -1); |
| 5259 | return 0; |
| 5260 | |
| 5261 | error: |
| 5262 | Py_XDECREF(args); |
| 5263 | Py_XDECREF(clsraw); |
| 5264 | return -1; |
| 5265 | } |
| 5266 | |
| 5267 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5268 | load_newobj_ex(UnpicklerObject *self) |
| 5269 | { |
| 5270 | PyObject *cls, *args, *kwargs; |
| 5271 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5272 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5273 | |
| 5274 | PDATA_POP(self->stack, kwargs); |
| 5275 | if (kwargs == NULL) { |
| 5276 | return -1; |
| 5277 | } |
| 5278 | PDATA_POP(self->stack, args); |
| 5279 | if (args == NULL) { |
| 5280 | Py_DECREF(kwargs); |
| 5281 | return -1; |
| 5282 | } |
| 5283 | PDATA_POP(self->stack, cls); |
| 5284 | if (cls == NULL) { |
| 5285 | Py_DECREF(kwargs); |
| 5286 | Py_DECREF(args); |
| 5287 | return -1; |
| 5288 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5289 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5290 | if (!PyType_Check(cls)) { |
| 5291 | Py_DECREF(kwargs); |
| 5292 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5293 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5294 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5295 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5296 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5297 | return -1; |
| 5298 | } |
| 5299 | |
| 5300 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5301 | Py_DECREF(kwargs); |
| 5302 | Py_DECREF(args); |
| 5303 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5304 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5305 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5306 | return -1; |
| 5307 | } |
| 5308 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5309 | Py_DECREF(kwargs); |
| 5310 | Py_DECREF(args); |
| 5311 | Py_DECREF(cls); |
| 5312 | if (obj == NULL) { |
| 5313 | return -1; |
| 5314 | } |
| 5315 | PDATA_PUSH(self->stack, obj, -1); |
| 5316 | return 0; |
| 5317 | } |
| 5318 | |
| 5319 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5320 | load_global(UnpicklerObject *self) |
| 5321 | { |
| 5322 | PyObject *global = NULL; |
| 5323 | PyObject *module_name; |
| 5324 | PyObject *global_name; |
| 5325 | Py_ssize_t len; |
| 5326 | char *s; |
| 5327 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5328 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5329 | return -1; |
| 5330 | if (len < 2) |
| 5331 | return bad_readline(); |
| 5332 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5333 | if (!module_name) |
| 5334 | return -1; |
| 5335 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5336 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5337 | if (len < 2) { |
| 5338 | Py_DECREF(module_name); |
| 5339 | return bad_readline(); |
| 5340 | } |
| 5341 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5342 | if (global_name) { |
| 5343 | global = find_class(self, module_name, global_name); |
| 5344 | Py_DECREF(global_name); |
| 5345 | } |
| 5346 | } |
| 5347 | Py_DECREF(module_name); |
| 5348 | |
| 5349 | if (global == NULL) |
| 5350 | return -1; |
| 5351 | PDATA_PUSH(self->stack, global, -1); |
| 5352 | return 0; |
| 5353 | } |
| 5354 | |
| 5355 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5356 | load_stack_global(UnpicklerObject *self) |
| 5357 | { |
| 5358 | PyObject *global; |
| 5359 | PyObject *module_name; |
| 5360 | PyObject *global_name; |
| 5361 | |
| 5362 | PDATA_POP(self->stack, global_name); |
| 5363 | PDATA_POP(self->stack, module_name); |
| 5364 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5365 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5366 | PickleState *st = _Pickle_GetGlobalState(); |
| 5367 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5368 | Py_XDECREF(global_name); |
| 5369 | Py_XDECREF(module_name); |
| 5370 | return -1; |
| 5371 | } |
| 5372 | global = find_class(self, module_name, global_name); |
| 5373 | Py_DECREF(global_name); |
| 5374 | Py_DECREF(module_name); |
| 5375 | if (global == NULL) |
| 5376 | return -1; |
| 5377 | PDATA_PUSH(self->stack, global, -1); |
| 5378 | return 0; |
| 5379 | } |
| 5380 | |
| 5381 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5382 | load_persid(UnpicklerObject *self) |
| 5383 | { |
| 5384 | PyObject *pid; |
| 5385 | Py_ssize_t len; |
| 5386 | char *s; |
| 5387 | |
| 5388 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5389 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5390 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5391 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5392 | return bad_readline(); |
| 5393 | |
| 5394 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5395 | if (pid == NULL) |
| 5396 | return -1; |
| 5397 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5398 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5399 | to pid first. */ |
| 5400 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5401 | if (pid == NULL) |
| 5402 | return -1; |
| 5403 | |
| 5404 | PDATA_PUSH(self->stack, pid, -1); |
| 5405 | return 0; |
| 5406 | } |
| 5407 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5408 | PickleState *st = _Pickle_GetGlobalState(); |
| 5409 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5410 | "A load persistent id instruction was encountered,\n" |
| 5411 | "but no persistent_load function was specified."); |
| 5412 | return -1; |
| 5413 | } |
| 5414 | } |
| 5415 | |
| 5416 | static int |
| 5417 | load_binpersid(UnpicklerObject *self) |
| 5418 | { |
| 5419 | PyObject *pid; |
| 5420 | |
| 5421 | if (self->pers_func) { |
| 5422 | PDATA_POP(self->stack, pid); |
| 5423 | if (pid == NULL) |
| 5424 | return -1; |
| 5425 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5426 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5427 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5428 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5429 | if (pid == NULL) |
| 5430 | return -1; |
| 5431 | |
| 5432 | PDATA_PUSH(self->stack, pid, -1); |
| 5433 | return 0; |
| 5434 | } |
| 5435 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5436 | PickleState *st = _Pickle_GetGlobalState(); |
| 5437 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5438 | "A load persistent id instruction was encountered,\n" |
| 5439 | "but no persistent_load function was specified."); |
| 5440 | return -1; |
| 5441 | } |
| 5442 | } |
| 5443 | |
| 5444 | static int |
| 5445 | load_pop(UnpicklerObject *self) |
| 5446 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5447 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5448 | |
| 5449 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5450 | * an object stack and a mark stack. We have to be clever and |
| 5451 | * 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] | 5452 | * mark stack first, and only signalling a stack underflow if |
| 5453 | * the object stack is empty and the mark stack doesn't match |
| 5454 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5455 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5456 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5457 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5458 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5459 | len--; |
| 5460 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5461 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5462 | } else { |
| 5463 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5464 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5465 | return 0; |
| 5466 | } |
| 5467 | |
| 5468 | static int |
| 5469 | load_pop_mark(UnpicklerObject *self) |
| 5470 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5471 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5472 | |
| 5473 | if ((i = marker(self)) < 0) |
| 5474 | return -1; |
| 5475 | |
| 5476 | Pdata_clear(self->stack, i); |
| 5477 | |
| 5478 | return 0; |
| 5479 | } |
| 5480 | |
| 5481 | static int |
| 5482 | load_dup(UnpicklerObject *self) |
| 5483 | { |
| 5484 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5485 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5486 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5487 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5488 | return stack_underflow(); |
| 5489 | last = self->stack->data[len - 1]; |
| 5490 | PDATA_APPEND(self->stack, last, -1); |
| 5491 | return 0; |
| 5492 | } |
| 5493 | |
| 5494 | static int |
| 5495 | load_get(UnpicklerObject *self) |
| 5496 | { |
| 5497 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5498 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5499 | Py_ssize_t len; |
| 5500 | char *s; |
| 5501 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5502 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5503 | return -1; |
| 5504 | if (len < 2) |
| 5505 | return bad_readline(); |
| 5506 | |
| 5507 | key = PyLong_FromString(s, NULL, 10); |
| 5508 | if (key == NULL) |
| 5509 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5510 | idx = PyLong_AsSsize_t(key); |
| 5511 | if (idx == -1 && PyErr_Occurred()) { |
| 5512 | Py_DECREF(key); |
| 5513 | return -1; |
| 5514 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5515 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5516 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5517 | if (value == NULL) { |
| 5518 | if (!PyErr_Occurred()) |
| 5519 | PyErr_SetObject(PyExc_KeyError, key); |
| 5520 | Py_DECREF(key); |
| 5521 | return -1; |
| 5522 | } |
| 5523 | Py_DECREF(key); |
| 5524 | |
| 5525 | PDATA_APPEND(self->stack, value, -1); |
| 5526 | return 0; |
| 5527 | } |
| 5528 | |
| 5529 | static int |
| 5530 | load_binget(UnpicklerObject *self) |
| 5531 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5532 | PyObject *value; |
| 5533 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5534 | char *s; |
| 5535 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5536 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5537 | return -1; |
| 5538 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5539 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5540 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5541 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5542 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5543 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5544 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5545 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5546 | Py_DECREF(key); |
| 5547 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5548 | return -1; |
| 5549 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5550 | |
| 5551 | PDATA_APPEND(self->stack, value, -1); |
| 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | static int |
| 5556 | load_long_binget(UnpicklerObject *self) |
| 5557 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5558 | PyObject *value; |
| 5559 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5560 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5561 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5562 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5563 | return -1; |
| 5564 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5565 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5566 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5567 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5568 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5569 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5570 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5571 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5572 | Py_DECREF(key); |
| 5573 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5574 | return -1; |
| 5575 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5576 | |
| 5577 | PDATA_APPEND(self->stack, value, -1); |
| 5578 | return 0; |
| 5579 | } |
| 5580 | |
| 5581 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5582 | * the number of bytes following the opcode, holding the index (code) value. |
| 5583 | */ |
| 5584 | static int |
| 5585 | load_extension(UnpicklerObject *self, int nbytes) |
| 5586 | { |
| 5587 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5588 | long code; /* calc_binint returns long */ |
| 5589 | PyObject *py_code; /* code as a Python int */ |
| 5590 | PyObject *obj; /* the object to push */ |
| 5591 | PyObject *pair; /* (module_name, class_name) */ |
| 5592 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5593 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5594 | |
| 5595 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5596 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5597 | return -1; |
| 5598 | code = calc_binint(codebytes, nbytes); |
| 5599 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5600 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5601 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5602 | return -1; |
| 5603 | } |
| 5604 | |
| 5605 | /* Look for the code in the cache. */ |
| 5606 | py_code = PyLong_FromLong(code); |
| 5607 | if (py_code == NULL) |
| 5608 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5609 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5610 | if (obj != NULL) { |
| 5611 | /* Bingo. */ |
| 5612 | Py_DECREF(py_code); |
| 5613 | PDATA_APPEND(self->stack, obj, -1); |
| 5614 | return 0; |
| 5615 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5616 | if (PyErr_Occurred()) { |
| 5617 | Py_DECREF(py_code); |
| 5618 | return -1; |
| 5619 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5620 | |
| 5621 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5622 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5623 | if (pair == NULL) { |
| 5624 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5625 | if (!PyErr_Occurred()) { |
| 5626 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5627 | "code %ld", code); |
| 5628 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5629 | return -1; |
| 5630 | } |
| 5631 | /* Since the extension registry is manipulable via Python code, |
| 5632 | * confirm that pair is really a 2-tuple of strings. |
| 5633 | */ |
| 5634 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5635 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5636 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5637 | Py_DECREF(py_code); |
| 5638 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5639 | "isn't a 2-tuple of strings", code); |
| 5640 | return -1; |
| 5641 | } |
| 5642 | /* Load the object. */ |
| 5643 | obj = find_class(self, module_name, class_name); |
| 5644 | if (obj == NULL) { |
| 5645 | Py_DECREF(py_code); |
| 5646 | return -1; |
| 5647 | } |
| 5648 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5649 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5650 | Py_DECREF(py_code); |
| 5651 | if (code < 0) { |
| 5652 | Py_DECREF(obj); |
| 5653 | return -1; |
| 5654 | } |
| 5655 | PDATA_PUSH(self->stack, obj, -1); |
| 5656 | return 0; |
| 5657 | } |
| 5658 | |
| 5659 | static int |
| 5660 | load_put(UnpicklerObject *self) |
| 5661 | { |
| 5662 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5663 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5664 | Py_ssize_t len; |
| 5665 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5666 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5667 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5668 | return -1; |
| 5669 | if (len < 2) |
| 5670 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5671 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5672 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5673 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5674 | |
| 5675 | key = PyLong_FromString(s, NULL, 10); |
| 5676 | if (key == NULL) |
| 5677 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5678 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5679 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5680 | if (idx < 0) { |
| 5681 | if (!PyErr_Occurred()) |
| 5682 | PyErr_SetString(PyExc_ValueError, |
| 5683 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5684 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5685 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5686 | |
| 5687 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
| 5690 | static int |
| 5691 | load_binput(UnpicklerObject *self) |
| 5692 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5693 | PyObject *value; |
| 5694 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5695 | char *s; |
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 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5698 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5699 | |
| 5700 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5701 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5702 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5703 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5704 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5705 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5706 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5707 | } |
| 5708 | |
| 5709 | static int |
| 5710 | load_long_binput(UnpicklerObject *self) |
| 5711 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5712 | PyObject *value; |
| 5713 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5714 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5715 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5716 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5717 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5718 | |
| 5719 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5720 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5721 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5722 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5723 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5724 | if (idx < 0) { |
| 5725 | PyErr_SetString(PyExc_ValueError, |
| 5726 | "negative LONG_BINPUT argument"); |
| 5727 | return -1; |
| 5728 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5729 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5730 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5731 | } |
| 5732 | |
| 5733 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5734 | load_memoize(UnpicklerObject *self) |
| 5735 | { |
| 5736 | PyObject *value; |
| 5737 | |
| 5738 | if (Py_SIZE(self->stack) <= 0) |
| 5739 | return stack_underflow(); |
| 5740 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5741 | |
| 5742 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5743 | } |
| 5744 | |
| 5745 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5746 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5747 | { |
| 5748 | PyObject *value; |
| 5749 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5750 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5751 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5752 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5753 | if (x > len || x <= 0) |
| 5754 | return stack_underflow(); |
| 5755 | if (len == x) /* nothing to do */ |
| 5756 | return 0; |
| 5757 | |
| 5758 | list = self->stack->data[x - 1]; |
| 5759 | |
| 5760 | if (PyList_Check(list)) { |
| 5761 | PyObject *slice; |
| 5762 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5763 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5764 | |
| 5765 | slice = Pdata_poplist(self->stack, x); |
| 5766 | if (!slice) |
| 5767 | return -1; |
| 5768 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5769 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5770 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5771 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5772 | } |
| 5773 | else { |
| 5774 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5775 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5776 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5777 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5778 | if (append_func == NULL) |
| 5779 | return -1; |
| 5780 | for (i = x; i < len; i++) { |
| 5781 | PyObject *result; |
| 5782 | |
| 5783 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5784 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5785 | if (result == NULL) { |
| 5786 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5787 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5788 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5789 | return -1; |
| 5790 | } |
| 5791 | Py_DECREF(result); |
| 5792 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5793 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5794 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
| 5797 | return 0; |
| 5798 | } |
| 5799 | |
| 5800 | static int |
| 5801 | load_append(UnpicklerObject *self) |
| 5802 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5803 | if (Py_SIZE(self->stack) - 1 <= 0) |
| 5804 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5805 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | static int |
| 5809 | load_appends(UnpicklerObject *self) |
| 5810 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5811 | Py_ssize_t i = marker(self); |
| 5812 | if (i < 0) |
| 5813 | return -1; |
| 5814 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5815 | } |
| 5816 | |
| 5817 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5818 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5819 | { |
| 5820 | PyObject *value, *key; |
| 5821 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5822 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5823 | int status = 0; |
| 5824 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5825 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5826 | if (x > len || x <= 0) |
| 5827 | return stack_underflow(); |
| 5828 | if (len == x) /* nothing to do */ |
| 5829 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5830 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5831 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5832 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5833 | PyErr_SetString(st->UnpicklingError, |
| 5834 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5835 | return -1; |
| 5836 | } |
| 5837 | |
| 5838 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5839 | that supports the __setitem__ attribute. */ |
| 5840 | dict = self->stack->data[x - 1]; |
| 5841 | |
| 5842 | for (i = x + 1; i < len; i += 2) { |
| 5843 | key = self->stack->data[i - 1]; |
| 5844 | value = self->stack->data[i]; |
| 5845 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5846 | status = -1; |
| 5847 | break; |
| 5848 | } |
| 5849 | } |
| 5850 | |
| 5851 | Pdata_clear(self->stack, x); |
| 5852 | return status; |
| 5853 | } |
| 5854 | |
| 5855 | static int |
| 5856 | load_setitem(UnpicklerObject *self) |
| 5857 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5858 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5859 | } |
| 5860 | |
| 5861 | static int |
| 5862 | load_setitems(UnpicklerObject *self) |
| 5863 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5864 | Py_ssize_t i = marker(self); |
| 5865 | if (i < 0) |
| 5866 | return -1; |
| 5867 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5868 | } |
| 5869 | |
| 5870 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5871 | load_additems(UnpicklerObject *self) |
| 5872 | { |
| 5873 | PyObject *set; |
| 5874 | Py_ssize_t mark, len, i; |
| 5875 | |
| 5876 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5877 | if (mark < 0) |
| 5878 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5879 | len = Py_SIZE(self->stack); |
| 5880 | if (mark > len || mark <= 0) |
| 5881 | return stack_underflow(); |
| 5882 | if (len == mark) /* nothing to do */ |
| 5883 | return 0; |
| 5884 | |
| 5885 | set = self->stack->data[mark - 1]; |
| 5886 | |
| 5887 | if (PySet_Check(set)) { |
| 5888 | PyObject *items; |
| 5889 | int status; |
| 5890 | |
| 5891 | items = Pdata_poptuple(self->stack, mark); |
| 5892 | if (items == NULL) |
| 5893 | return -1; |
| 5894 | |
| 5895 | status = _PySet_Update(set, items); |
| 5896 | Py_DECREF(items); |
| 5897 | return status; |
| 5898 | } |
| 5899 | else { |
| 5900 | PyObject *add_func; |
| 5901 | _Py_IDENTIFIER(add); |
| 5902 | |
| 5903 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5904 | if (add_func == NULL) |
| 5905 | return -1; |
| 5906 | for (i = mark; i < len; i++) { |
| 5907 | PyObject *result; |
| 5908 | PyObject *item; |
| 5909 | |
| 5910 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5911 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5912 | if (result == NULL) { |
| 5913 | Pdata_clear(self->stack, i + 1); |
| 5914 | Py_SIZE(self->stack) = mark; |
| 5915 | return -1; |
| 5916 | } |
| 5917 | Py_DECREF(result); |
| 5918 | } |
| 5919 | Py_SIZE(self->stack) = mark; |
| 5920 | } |
| 5921 | |
| 5922 | return 0; |
| 5923 | } |
| 5924 | |
| 5925 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5926 | load_build(UnpicklerObject *self) |
| 5927 | { |
| 5928 | PyObject *state, *inst, *slotstate; |
| 5929 | PyObject *setstate; |
| 5930 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5931 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5932 | |
| 5933 | /* Stack is ... instance, state. We want to leave instance at |
| 5934 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5935 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5936 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5937 | return stack_underflow(); |
| 5938 | |
| 5939 | PDATA_POP(self->stack, state); |
| 5940 | if (state == NULL) |
| 5941 | return -1; |
| 5942 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5943 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5944 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5945 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5946 | if (setstate == NULL) { |
| 5947 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5948 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5949 | else { |
| 5950 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5951 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5952 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5953 | } |
| 5954 | else { |
| 5955 | PyObject *result; |
| 5956 | |
| 5957 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5958 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5959 | Py_DECREF(setstate); |
| 5960 | if (result == NULL) |
| 5961 | return -1; |
| 5962 | Py_DECREF(result); |
| 5963 | return 0; |
| 5964 | } |
| 5965 | |
| 5966 | /* A default __setstate__. First see whether state embeds a |
| 5967 | * slot state dict too (a proto 2 addition). |
| 5968 | */ |
| 5969 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5970 | PyObject *tmp = state; |
| 5971 | |
| 5972 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5973 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5974 | Py_INCREF(state); |
| 5975 | Py_INCREF(slotstate); |
| 5976 | Py_DECREF(tmp); |
| 5977 | } |
| 5978 | else |
| 5979 | slotstate = NULL; |
| 5980 | |
| 5981 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5982 | if (state != Py_None) { |
| 5983 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5984 | PyObject *d_key, *d_value; |
| 5985 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5986 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5987 | |
| 5988 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5989 | PickleState *st = _Pickle_GetGlobalState(); |
| 5990 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5991 | goto error; |
| 5992 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5993 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5994 | if (dict == NULL) |
| 5995 | goto error; |
| 5996 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5997 | i = 0; |
| 5998 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5999 | /* normally the keys for instance attributes are |
| 6000 | interned. we should try to do that here. */ |
| 6001 | Py_INCREF(d_key); |
| 6002 | if (PyUnicode_CheckExact(d_key)) |
| 6003 | PyUnicode_InternInPlace(&d_key); |
| 6004 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6005 | Py_DECREF(d_key); |
| 6006 | goto error; |
| 6007 | } |
| 6008 | Py_DECREF(d_key); |
| 6009 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6010 | Py_DECREF(dict); |
| 6011 | } |
| 6012 | |
| 6013 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6014 | if (slotstate != NULL) { |
| 6015 | PyObject *d_key, *d_value; |
| 6016 | Py_ssize_t i; |
| 6017 | |
| 6018 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6019 | PickleState *st = _Pickle_GetGlobalState(); |
| 6020 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6021 | "slot state is not a dictionary"); |
| 6022 | goto error; |
| 6023 | } |
| 6024 | i = 0; |
| 6025 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6026 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6027 | goto error; |
| 6028 | } |
| 6029 | } |
| 6030 | |
| 6031 | if (0) { |
| 6032 | error: |
| 6033 | status = -1; |
| 6034 | } |
| 6035 | |
| 6036 | Py_DECREF(state); |
| 6037 | Py_XDECREF(slotstate); |
| 6038 | return status; |
| 6039 | } |
| 6040 | |
| 6041 | static int |
| 6042 | load_mark(UnpicklerObject *self) |
| 6043 | { |
| 6044 | |
| 6045 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6046 | * object stack and a mark stack. Here we push a mark onto the |
| 6047 | * mark stack. |
| 6048 | */ |
| 6049 | |
| 6050 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6051 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6052 | |
| 6053 | /* Use the size_t type to check for overflow. */ |
| 6054 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6055 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6056 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6057 | PyErr_NoMemory(); |
| 6058 | return -1; |
| 6059 | } |
| 6060 | |
| 6061 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6062 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6063 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6064 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6065 | if (self->marks == NULL) { |
| 6066 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6067 | PyErr_NoMemory(); |
| 6068 | return -1; |
| 6069 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6070 | self->marks_size = (Py_ssize_t)alloc; |
| 6071 | } |
| 6072 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6073 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6074 | |
| 6075 | return 0; |
| 6076 | } |
| 6077 | |
| 6078 | static int |
| 6079 | load_reduce(UnpicklerObject *self) |
| 6080 | { |
| 6081 | PyObject *callable = NULL; |
| 6082 | PyObject *argtup = NULL; |
| 6083 | PyObject *obj = NULL; |
| 6084 | |
| 6085 | PDATA_POP(self->stack, argtup); |
| 6086 | if (argtup == NULL) |
| 6087 | return -1; |
| 6088 | PDATA_POP(self->stack, callable); |
| 6089 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6090 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6091 | Py_DECREF(callable); |
| 6092 | } |
| 6093 | Py_DECREF(argtup); |
| 6094 | |
| 6095 | if (obj == NULL) |
| 6096 | return -1; |
| 6097 | |
| 6098 | PDATA_PUSH(self->stack, obj, -1); |
| 6099 | return 0; |
| 6100 | } |
| 6101 | |
| 6102 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6103 | * is the first opcode for protocols >= 2. |
| 6104 | */ |
| 6105 | static int |
| 6106 | load_proto(UnpicklerObject *self) |
| 6107 | { |
| 6108 | char *s; |
| 6109 | int i; |
| 6110 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6111 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6112 | return -1; |
| 6113 | |
| 6114 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6115 | if (i <= HIGHEST_PROTOCOL) { |
| 6116 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6117 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6118 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6119 | |
| 6120 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6121 | return -1; |
| 6122 | } |
| 6123 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6124 | static int |
| 6125 | load_frame(UnpicklerObject *self) |
| 6126 | { |
| 6127 | char *s; |
| 6128 | Py_ssize_t frame_len; |
| 6129 | |
| 6130 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6131 | return -1; |
| 6132 | |
| 6133 | frame_len = calc_binsize(s, 8); |
| 6134 | if (frame_len < 0) { |
| 6135 | PyErr_Format(PyExc_OverflowError, |
| 6136 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6137 | PY_SSIZE_T_MAX); |
| 6138 | return -1; |
| 6139 | } |
| 6140 | |
| 6141 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6142 | return -1; |
| 6143 | |
| 6144 | /* Rewind to start of frame */ |
| 6145 | self->next_read_idx -= frame_len; |
| 6146 | return 0; |
| 6147 | } |
| 6148 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6149 | static PyObject * |
| 6150 | load(UnpicklerObject *self) |
| 6151 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6152 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6153 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6154 | |
| 6155 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6156 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6157 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6158 | Pdata_clear(self->stack, 0); |
| 6159 | |
| 6160 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6161 | #define OP(opcode, load_func) \ |
| 6162 | case opcode: if (load_func(self) < 0) break; continue; |
| 6163 | |
| 6164 | #define OP_ARG(opcode, load_func, arg) \ |
| 6165 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6166 | |
| 6167 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6168 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6169 | break; |
| 6170 | |
| 6171 | switch ((enum opcode)s[0]) { |
| 6172 | OP(NONE, load_none) |
| 6173 | OP(BININT, load_binint) |
| 6174 | OP(BININT1, load_binint1) |
| 6175 | OP(BININT2, load_binint2) |
| 6176 | OP(INT, load_int) |
| 6177 | OP(LONG, load_long) |
| 6178 | OP_ARG(LONG1, load_counted_long, 1) |
| 6179 | OP_ARG(LONG4, load_counted_long, 4) |
| 6180 | OP(FLOAT, load_float) |
| 6181 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6182 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6183 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6184 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6185 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6186 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6187 | OP(STRING, load_string) |
| 6188 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6189 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6190 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6191 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6192 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6193 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6194 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6195 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6196 | OP(TUPLE, load_tuple) |
| 6197 | OP(EMPTY_LIST, load_empty_list) |
| 6198 | OP(LIST, load_list) |
| 6199 | OP(EMPTY_DICT, load_empty_dict) |
| 6200 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6201 | OP(EMPTY_SET, load_empty_set) |
| 6202 | OP(ADDITEMS, load_additems) |
| 6203 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6204 | OP(OBJ, load_obj) |
| 6205 | OP(INST, load_inst) |
| 6206 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6207 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6208 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6209 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6210 | OP(APPEND, load_append) |
| 6211 | OP(APPENDS, load_appends) |
| 6212 | OP(BUILD, load_build) |
| 6213 | OP(DUP, load_dup) |
| 6214 | OP(BINGET, load_binget) |
| 6215 | OP(LONG_BINGET, load_long_binget) |
| 6216 | OP(GET, load_get) |
| 6217 | OP(MARK, load_mark) |
| 6218 | OP(BINPUT, load_binput) |
| 6219 | OP(LONG_BINPUT, load_long_binput) |
| 6220 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6221 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6222 | OP(POP, load_pop) |
| 6223 | OP(POP_MARK, load_pop_mark) |
| 6224 | OP(SETITEM, load_setitem) |
| 6225 | OP(SETITEMS, load_setitems) |
| 6226 | OP(PERSID, load_persid) |
| 6227 | OP(BINPERSID, load_binpersid) |
| 6228 | OP(REDUCE, load_reduce) |
| 6229 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6230 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6231 | OP_ARG(EXT1, load_extension, 1) |
| 6232 | OP_ARG(EXT2, load_extension, 2) |
| 6233 | OP_ARG(EXT4, load_extension, 4) |
| 6234 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6235 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6236 | |
| 6237 | case STOP: |
| 6238 | break; |
| 6239 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6240 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6241 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6242 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6243 | } |
| 6244 | else { |
| 6245 | PickleState *st = _Pickle_GetGlobalState(); |
| 6246 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6247 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6248 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6249 | return NULL; |
| 6250 | } |
| 6251 | |
| 6252 | break; /* and we are done! */ |
| 6253 | } |
| 6254 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6255 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6256 | return NULL; |
| 6257 | } |
| 6258 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6259 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6260 | return NULL; |
| 6261 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6262 | PDATA_POP(self->stack, value); |
| 6263 | return value; |
| 6264 | } |
| 6265 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6266 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6267 | |
| 6268 | _pickle.Unpickler.load |
| 6269 | |
| 6270 | Load a pickle. |
| 6271 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6272 | Read a pickled object representation from the open file object given |
| 6273 | in the constructor, and return the reconstituted object hierarchy |
| 6274 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6275 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6276 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6277 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6278 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6279 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6280 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6281 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6282 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6283 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6284 | segfaulting if a subclass overridden __init__ with a function that does |
| 6285 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6286 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6287 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6288 | PickleState *st = _Pickle_GetGlobalState(); |
| 6289 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6290 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6291 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6292 | return NULL; |
| 6293 | } |
| 6294 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6295 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6296 | } |
| 6297 | |
| 6298 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6299 | function is used for loading any global (i.e., functions), not just |
| 6300 | classes. The name is kept only for backward compatibility. */ |
| 6301 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6302 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6303 | |
| 6304 | _pickle.Unpickler.find_class |
| 6305 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6306 | module_name: object |
| 6307 | global_name: object |
| 6308 | / |
| 6309 | |
| 6310 | Return an object from a specified module. |
| 6311 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6312 | If necessary, the module will be imported. Subclasses may override |
| 6313 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6314 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6315 | |
| 6316 | This method is called whenever a class or a function object is |
| 6317 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6318 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6319 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6320 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6321 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6322 | PyObject *module_name, |
| 6323 | PyObject *global_name) |
| 6324 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6325 | { |
| 6326 | PyObject *global; |
| 6327 | PyObject *modules_dict; |
| 6328 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6329 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6330 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6331 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6332 | Python 3.x. We do this only with old pickle protocols and when the |
| 6333 | user has not disabled the feature. */ |
| 6334 | if (self->proto < 3 && self->fix_imports) { |
| 6335 | PyObject *key; |
| 6336 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6337 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6338 | |
| 6339 | /* Check if the global (i.e., a function or a class) was renamed |
| 6340 | or moved to another module. */ |
| 6341 | key = PyTuple_Pack(2, module_name, global_name); |
| 6342 | if (key == NULL) |
| 6343 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6344 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6345 | Py_DECREF(key); |
| 6346 | if (item) { |
| 6347 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6348 | PyErr_Format(PyExc_RuntimeError, |
| 6349 | "_compat_pickle.NAME_MAPPING values should be " |
| 6350 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6351 | return NULL; |
| 6352 | } |
| 6353 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6354 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6355 | if (!PyUnicode_Check(module_name) || |
| 6356 | !PyUnicode_Check(global_name)) { |
| 6357 | PyErr_Format(PyExc_RuntimeError, |
| 6358 | "_compat_pickle.NAME_MAPPING values should be " |
| 6359 | "pairs of str, not (%.200s, %.200s)", |
| 6360 | Py_TYPE(module_name)->tp_name, |
| 6361 | Py_TYPE(global_name)->tp_name); |
| 6362 | return NULL; |
| 6363 | } |
| 6364 | } |
| 6365 | else if (PyErr_Occurred()) { |
| 6366 | return NULL; |
| 6367 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6368 | else { |
| 6369 | /* Check if the module was renamed. */ |
| 6370 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6371 | if (item) { |
| 6372 | if (!PyUnicode_Check(item)) { |
| 6373 | PyErr_Format(PyExc_RuntimeError, |
| 6374 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6375 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6376 | return NULL; |
| 6377 | } |
| 6378 | module_name = item; |
| 6379 | } |
| 6380 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6381 | return NULL; |
| 6382 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 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 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6408 | /*[clinic input] |
| 6409 | |
| 6410 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6411 | |
| 6412 | Returns size in memory, in bytes. |
| 6413 | [clinic start generated code]*/ |
| 6414 | |
| 6415 | static Py_ssize_t |
| 6416 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6417 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6418 | { |
| 6419 | Py_ssize_t res; |
| 6420 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame^] | 6421 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6422 | if (self->memo != NULL) |
| 6423 | res += self->memo_size * sizeof(PyObject *); |
| 6424 | if (self->marks != NULL) |
| 6425 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6426 | if (self->input_line != NULL) |
| 6427 | res += strlen(self->input_line) + 1; |
| 6428 | if (self->encoding != NULL) |
| 6429 | res += strlen(self->encoding) + 1; |
| 6430 | if (self->errors != NULL) |
| 6431 | res += strlen(self->errors) + 1; |
| 6432 | return res; |
| 6433 | } |
| 6434 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6435 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6436 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6437 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6438 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6439 | {NULL, NULL} /* sentinel */ |
| 6440 | }; |
| 6441 | |
| 6442 | static void |
| 6443 | Unpickler_dealloc(UnpicklerObject *self) |
| 6444 | { |
| 6445 | PyObject_GC_UnTrack((PyObject *)self); |
| 6446 | Py_XDECREF(self->readline); |
| 6447 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6448 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6449 | Py_XDECREF(self->stack); |
| 6450 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6451 | if (self->buffer.buf != NULL) { |
| 6452 | PyBuffer_Release(&self->buffer); |
| 6453 | self->buffer.buf = NULL; |
| 6454 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6455 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6456 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6457 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6458 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6459 | PyMem_Free(self->encoding); |
| 6460 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6461 | |
| 6462 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6463 | } |
| 6464 | |
| 6465 | static int |
| 6466 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6467 | { |
| 6468 | Py_VISIT(self->readline); |
| 6469 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6470 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6471 | Py_VISIT(self->stack); |
| 6472 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6473 | return 0; |
| 6474 | } |
| 6475 | |
| 6476 | static int |
| 6477 | Unpickler_clear(UnpicklerObject *self) |
| 6478 | { |
| 6479 | Py_CLEAR(self->readline); |
| 6480 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6481 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6482 | Py_CLEAR(self->stack); |
| 6483 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6484 | if (self->buffer.buf != NULL) { |
| 6485 | PyBuffer_Release(&self->buffer); |
| 6486 | self->buffer.buf = NULL; |
| 6487 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6488 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6489 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6490 | PyMem_Free(self->marks); |
| 6491 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6492 | PyMem_Free(self->input_line); |
| 6493 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6494 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6495 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6496 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6497 | self->errors = NULL; |
| 6498 | |
| 6499 | return 0; |
| 6500 | } |
| 6501 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6502 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6503 | |
| 6504 | _pickle.Unpickler.__init__ |
| 6505 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6506 | file: object |
| 6507 | * |
| 6508 | fix_imports: bool = True |
| 6509 | encoding: str = 'ASCII' |
| 6510 | errors: str = 'strict' |
| 6511 | |
| 6512 | This takes a binary file for reading a pickle data stream. |
| 6513 | |
| 6514 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6515 | protocol argument is needed. Bytes past the pickled object's |
| 6516 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6517 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6518 | The argument *file* must have two methods, a read() method that takes |
| 6519 | an integer argument, and a readline() method that requires no |
| 6520 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6521 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6522 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6523 | |
| 6524 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6525 | which are used to control compatiblity support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6526 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6527 | map the old Python 2 names to the new names used in Python 3. The |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6528 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6529 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6530 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6531 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6532 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6533 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6534 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6535 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6536 | int fix_imports, const char *encoding, |
| 6537 | const char *errors) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 6538 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6539 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6540 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6541 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6542 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6543 | if (self->read != NULL) |
| 6544 | (void)Unpickler_clear(self); |
| 6545 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6546 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6547 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6548 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6549 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6550 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6551 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6552 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6553 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6554 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6555 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6556 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6557 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6558 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6559 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6560 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6561 | } |
| 6562 | else { |
| 6563 | self->pers_func = NULL; |
| 6564 | } |
| 6565 | |
| 6566 | self->stack = (Pdata *)Pdata_New(); |
| 6567 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6568 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6569 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6570 | self->memo_size = 32; |
| 6571 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6572 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6573 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6574 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6575 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6576 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6577 | return 0; |
| 6578 | } |
| 6579 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6580 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6581 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6582 | * avoid breaking code like: |
| 6583 | * unpickler.memo.clear() |
| 6584 | * and |
| 6585 | * unpickler.memo = saved_memo |
| 6586 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6587 | * it. Note that we don't implement the entire mapping API here. This is |
| 6588 | * intentional, as these should be treated as black-box implementation details. |
| 6589 | * |
| 6590 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6591 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6592 | */ |
| 6593 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6594 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6595 | _pickle.UnpicklerMemoProxy.clear |
| 6596 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6597 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6598 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6599 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6600 | static PyObject * |
| 6601 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6602 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6603 | { |
| 6604 | _Unpickler_MemoCleanup(self->unpickler); |
| 6605 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6606 | if (self->unpickler->memo == NULL) |
| 6607 | return NULL; |
| 6608 | Py_RETURN_NONE; |
| 6609 | } |
| 6610 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6611 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6612 | _pickle.UnpicklerMemoProxy.copy |
| 6613 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6614 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6615 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6616 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6617 | static PyObject * |
| 6618 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6619 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6620 | { |
| 6621 | Py_ssize_t i; |
| 6622 | PyObject *new_memo = PyDict_New(); |
| 6623 | if (new_memo == NULL) |
| 6624 | return NULL; |
| 6625 | |
| 6626 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6627 | int status; |
| 6628 | PyObject *key, *value; |
| 6629 | |
| 6630 | value = self->unpickler->memo[i]; |
| 6631 | if (value == NULL) |
| 6632 | continue; |
| 6633 | |
| 6634 | key = PyLong_FromSsize_t(i); |
| 6635 | if (key == NULL) |
| 6636 | goto error; |
| 6637 | status = PyDict_SetItem(new_memo, key, value); |
| 6638 | Py_DECREF(key); |
| 6639 | if (status < 0) |
| 6640 | goto error; |
| 6641 | } |
| 6642 | return new_memo; |
| 6643 | |
| 6644 | error: |
| 6645 | Py_DECREF(new_memo); |
| 6646 | return NULL; |
| 6647 | } |
| 6648 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6649 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6650 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6651 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6652 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6653 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6654 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6655 | static PyObject * |
| 6656 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6657 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6658 | { |
| 6659 | PyObject *reduce_value; |
| 6660 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6661 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6662 | if (contents == NULL) |
| 6663 | return NULL; |
| 6664 | |
| 6665 | reduce_value = PyTuple_New(2); |
| 6666 | if (reduce_value == NULL) { |
| 6667 | Py_DECREF(contents); |
| 6668 | return NULL; |
| 6669 | } |
| 6670 | constructor_args = PyTuple_New(1); |
| 6671 | if (constructor_args == NULL) { |
| 6672 | Py_DECREF(contents); |
| 6673 | Py_DECREF(reduce_value); |
| 6674 | return NULL; |
| 6675 | } |
| 6676 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6677 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6678 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6679 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6680 | return reduce_value; |
| 6681 | } |
| 6682 | |
| 6683 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6684 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6685 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6686 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6687 | {NULL, NULL} /* sentinel */ |
| 6688 | }; |
| 6689 | |
| 6690 | static void |
| 6691 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6692 | { |
| 6693 | PyObject_GC_UnTrack(self); |
| 6694 | Py_XDECREF(self->unpickler); |
| 6695 | PyObject_GC_Del((PyObject *)self); |
| 6696 | } |
| 6697 | |
| 6698 | static int |
| 6699 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6700 | visitproc visit, void *arg) |
| 6701 | { |
| 6702 | Py_VISIT(self->unpickler); |
| 6703 | return 0; |
| 6704 | } |
| 6705 | |
| 6706 | static int |
| 6707 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6708 | { |
| 6709 | Py_CLEAR(self->unpickler); |
| 6710 | return 0; |
| 6711 | } |
| 6712 | |
| 6713 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6714 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6715 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6716 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6717 | 0, |
| 6718 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6719 | 0, /* tp_print */ |
| 6720 | 0, /* tp_getattr */ |
| 6721 | 0, /* tp_setattr */ |
| 6722 | 0, /* tp_compare */ |
| 6723 | 0, /* tp_repr */ |
| 6724 | 0, /* tp_as_number */ |
| 6725 | 0, /* tp_as_sequence */ |
| 6726 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6727 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6728 | 0, /* tp_call */ |
| 6729 | 0, /* tp_str */ |
| 6730 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6731 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6732 | 0, /* tp_as_buffer */ |
| 6733 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6734 | 0, /* tp_doc */ |
| 6735 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6736 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6737 | 0, /* tp_richcompare */ |
| 6738 | 0, /* tp_weaklistoffset */ |
| 6739 | 0, /* tp_iter */ |
| 6740 | 0, /* tp_iternext */ |
| 6741 | unpicklerproxy_methods, /* tp_methods */ |
| 6742 | }; |
| 6743 | |
| 6744 | static PyObject * |
| 6745 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6746 | { |
| 6747 | UnpicklerMemoProxyObject *self; |
| 6748 | |
| 6749 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6750 | &UnpicklerMemoProxyType); |
| 6751 | if (self == NULL) |
| 6752 | return NULL; |
| 6753 | Py_INCREF(unpickler); |
| 6754 | self->unpickler = unpickler; |
| 6755 | PyObject_GC_Track(self); |
| 6756 | return (PyObject *)self; |
| 6757 | } |
| 6758 | |
| 6759 | /*****************************************************************************/ |
| 6760 | |
| 6761 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6762 | static PyObject * |
| 6763 | Unpickler_get_memo(UnpicklerObject *self) |
| 6764 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6765 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6766 | } |
| 6767 | |
| 6768 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6769 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6770 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6771 | PyObject **new_memo; |
| 6772 | Py_ssize_t new_memo_size = 0; |
| 6773 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6774 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6775 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6776 | PyErr_SetString(PyExc_TypeError, |
| 6777 | "attribute deletion is not supported"); |
| 6778 | return -1; |
| 6779 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6780 | |
| 6781 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6782 | UnpicklerObject *unpickler = |
| 6783 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6784 | |
| 6785 | new_memo_size = unpickler->memo_size; |
| 6786 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6787 | if (new_memo == NULL) |
| 6788 | return -1; |
| 6789 | |
| 6790 | for (i = 0; i < new_memo_size; i++) { |
| 6791 | Py_XINCREF(unpickler->memo[i]); |
| 6792 | new_memo[i] = unpickler->memo[i]; |
| 6793 | } |
| 6794 | } |
| 6795 | else if (PyDict_Check(obj)) { |
| 6796 | Py_ssize_t i = 0; |
| 6797 | PyObject *key, *value; |
| 6798 | |
| 6799 | new_memo_size = PyDict_Size(obj); |
| 6800 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6801 | if (new_memo == NULL) |
| 6802 | return -1; |
| 6803 | |
| 6804 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6805 | Py_ssize_t idx; |
| 6806 | if (!PyLong_Check(key)) { |
| 6807 | PyErr_SetString(PyExc_TypeError, |
| 6808 | "memo key must be integers"); |
| 6809 | goto error; |
| 6810 | } |
| 6811 | idx = PyLong_AsSsize_t(key); |
| 6812 | if (idx == -1 && PyErr_Occurred()) |
| 6813 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6814 | if (idx < 0) { |
| 6815 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6816 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6817 | goto error; |
| 6818 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6819 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6820 | goto error; |
| 6821 | } |
| 6822 | } |
| 6823 | else { |
| 6824 | PyErr_Format(PyExc_TypeError, |
| 6825 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6826 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6827 | return -1; |
| 6828 | } |
| 6829 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6830 | _Unpickler_MemoCleanup(self); |
| 6831 | self->memo_size = new_memo_size; |
| 6832 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6833 | |
| 6834 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6835 | |
| 6836 | error: |
| 6837 | if (new_memo_size) { |
| 6838 | i = new_memo_size; |
| 6839 | while (--i >= 0) { |
| 6840 | Py_XDECREF(new_memo[i]); |
| 6841 | } |
| 6842 | PyMem_FREE(new_memo); |
| 6843 | } |
| 6844 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6845 | } |
| 6846 | |
| 6847 | static PyObject * |
| 6848 | Unpickler_get_persload(UnpicklerObject *self) |
| 6849 | { |
| 6850 | if (self->pers_func == NULL) |
| 6851 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6852 | else |
| 6853 | Py_INCREF(self->pers_func); |
| 6854 | return self->pers_func; |
| 6855 | } |
| 6856 | |
| 6857 | static int |
| 6858 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6859 | { |
| 6860 | PyObject *tmp; |
| 6861 | |
| 6862 | if (value == NULL) { |
| 6863 | PyErr_SetString(PyExc_TypeError, |
| 6864 | "attribute deletion is not supported"); |
| 6865 | return -1; |
| 6866 | } |
| 6867 | if (!PyCallable_Check(value)) { |
| 6868 | PyErr_SetString(PyExc_TypeError, |
| 6869 | "persistent_load must be a callable taking " |
| 6870 | "one argument"); |
| 6871 | return -1; |
| 6872 | } |
| 6873 | |
| 6874 | tmp = self->pers_func; |
| 6875 | Py_INCREF(value); |
| 6876 | self->pers_func = value; |
| 6877 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6878 | |
| 6879 | return 0; |
| 6880 | } |
| 6881 | |
| 6882 | static PyGetSetDef Unpickler_getsets[] = { |
| 6883 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6884 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6885 | (setter)Unpickler_set_persload}, |
| 6886 | {NULL} |
| 6887 | }; |
| 6888 | |
| 6889 | static PyTypeObject Unpickler_Type = { |
| 6890 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6891 | "_pickle.Unpickler", /*tp_name*/ |
| 6892 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6893 | 0, /*tp_itemsize*/ |
| 6894 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6895 | 0, /*tp_print*/ |
| 6896 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6897 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6898 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6899 | 0, /*tp_repr*/ |
| 6900 | 0, /*tp_as_number*/ |
| 6901 | 0, /*tp_as_sequence*/ |
| 6902 | 0, /*tp_as_mapping*/ |
| 6903 | 0, /*tp_hash*/ |
| 6904 | 0, /*tp_call*/ |
| 6905 | 0, /*tp_str*/ |
| 6906 | 0, /*tp_getattro*/ |
| 6907 | 0, /*tp_setattro*/ |
| 6908 | 0, /*tp_as_buffer*/ |
| 6909 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6910 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6911 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6912 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6913 | 0, /*tp_richcompare*/ |
| 6914 | 0, /*tp_weaklistoffset*/ |
| 6915 | 0, /*tp_iter*/ |
| 6916 | 0, /*tp_iternext*/ |
| 6917 | Unpickler_methods, /*tp_methods*/ |
| 6918 | 0, /*tp_members*/ |
| 6919 | Unpickler_getsets, /*tp_getset*/ |
| 6920 | 0, /*tp_base*/ |
| 6921 | 0, /*tp_dict*/ |
| 6922 | 0, /*tp_descr_get*/ |
| 6923 | 0, /*tp_descr_set*/ |
| 6924 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6925 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6926 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6927 | PyType_GenericNew, /*tp_new*/ |
| 6928 | PyObject_GC_Del, /*tp_free*/ |
| 6929 | 0, /*tp_is_gc*/ |
| 6930 | }; |
| 6931 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6932 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6933 | |
| 6934 | _pickle.dump |
| 6935 | |
| 6936 | obj: object |
| 6937 | file: object |
| 6938 | protocol: object = NULL |
| 6939 | * |
| 6940 | fix_imports: bool = True |
| 6941 | |
| 6942 | Write a pickled representation of obj to the open file object file. |
| 6943 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6944 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 6945 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6946 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6947 | The optional *protocol* argument tells the pickler to use the given |
| 6948 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 6949 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6950 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6951 | Specifying a negative protocol version selects the highest protocol |
| 6952 | version supported. The higher the protocol used, the more recent the |
| 6953 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6954 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6955 | The *file* argument must have a write() method that accepts a single |
| 6956 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6957 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6958 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6959 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6960 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 6961 | to map the new Python 3 names to the old module names used in Python |
| 6962 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6963 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6964 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6965 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6966 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, |
| 6967 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 6968 | /*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6969 | { |
| 6970 | PicklerObject *pickler = _Pickler_New(); |
| 6971 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6972 | if (pickler == NULL) |
| 6973 | return NULL; |
| 6974 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6975 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6976 | goto error; |
| 6977 | |
| 6978 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6979 | goto error; |
| 6980 | |
| 6981 | if (dump(pickler, obj) < 0) |
| 6982 | goto error; |
| 6983 | |
| 6984 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6985 | goto error; |
| 6986 | |
| 6987 | Py_DECREF(pickler); |
| 6988 | Py_RETURN_NONE; |
| 6989 | |
| 6990 | error: |
| 6991 | Py_XDECREF(pickler); |
| 6992 | return NULL; |
| 6993 | } |
| 6994 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6995 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6996 | |
| 6997 | _pickle.dumps |
| 6998 | |
| 6999 | obj: object |
| 7000 | protocol: object = NULL |
| 7001 | * |
| 7002 | fix_imports: bool = True |
| 7003 | |
| 7004 | Return the pickled representation of the object as a bytes object. |
| 7005 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7006 | The optional *protocol* argument tells the pickler to use the given |
| 7007 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7008 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7009 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7010 | Specifying a negative protocol version selects the highest protocol |
| 7011 | version supported. The higher the protocol used, the more recent the |
| 7012 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7013 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7014 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7015 | try to map the new Python 3 names to the old module names used in |
| 7016 | Python 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7017 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7018 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7019 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7020 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, |
| 7021 | int fix_imports) |
| 7022 | /*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7023 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7024 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7025 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7026 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7027 | if (pickler == NULL) |
| 7028 | return NULL; |
| 7029 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7030 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7031 | goto error; |
| 7032 | |
| 7033 | if (dump(pickler, obj) < 0) |
| 7034 | goto error; |
| 7035 | |
| 7036 | result = _Pickler_GetString(pickler); |
| 7037 | Py_DECREF(pickler); |
| 7038 | return result; |
| 7039 | |
| 7040 | error: |
| 7041 | Py_XDECREF(pickler); |
| 7042 | return NULL; |
| 7043 | } |
| 7044 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7045 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7046 | |
| 7047 | _pickle.load |
| 7048 | |
| 7049 | file: object |
| 7050 | * |
| 7051 | fix_imports: bool = True |
| 7052 | encoding: str = 'ASCII' |
| 7053 | errors: str = 'strict' |
| 7054 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7055 | Read and return an object from the pickle data stored in a file. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7056 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7057 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7058 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7059 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7060 | The protocol version of the pickle is detected automatically, so no |
| 7061 | protocol argument is needed. Bytes past the pickled object's |
| 7062 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7063 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7064 | The argument *file* must have two methods, a read() method that takes |
| 7065 | an integer argument, and a readline() method that requires no |
| 7066 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7067 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7068 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7069 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7070 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7071 | which are used to control compatiblity support for pickle stream |
| 7072 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7073 | map the old Python 2 names to the new names used in Python 3. The |
| 7074 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7075 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7076 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7077 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7078 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7079 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7080 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7081 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, |
| 7082 | const char *encoding, const char *errors) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 7083 | /*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7084 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7085 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7086 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7087 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7088 | if (unpickler == NULL) |
| 7089 | return NULL; |
| 7090 | |
| 7091 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7092 | goto error; |
| 7093 | |
| 7094 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7095 | goto error; |
| 7096 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7097 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7098 | |
| 7099 | result = load(unpickler); |
| 7100 | Py_DECREF(unpickler); |
| 7101 | return result; |
| 7102 | |
| 7103 | error: |
| 7104 | Py_XDECREF(unpickler); |
| 7105 | return NULL; |
| 7106 | } |
| 7107 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7108 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7109 | |
| 7110 | _pickle.loads |
| 7111 | |
| 7112 | data: object |
| 7113 | * |
| 7114 | fix_imports: bool = True |
| 7115 | encoding: str = 'ASCII' |
| 7116 | errors: str = 'strict' |
| 7117 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7118 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7119 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7120 | The protocol version of the pickle is detected automatically, so no |
| 7121 | protocol argument is needed. Bytes past the pickled object's |
| 7122 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7123 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7124 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7125 | which are used to control compatiblity support for pickle stream |
| 7126 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7127 | map the old Python 2 names to the new names used in Python 3. The |
| 7128 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7129 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7130 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7131 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7132 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7133 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7134 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7135 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, |
| 7136 | const char *encoding, const char *errors) |
| 7137 | /*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7138 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7139 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7140 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7141 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7142 | if (unpickler == NULL) |
| 7143 | return NULL; |
| 7144 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7145 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7146 | goto error; |
| 7147 | |
| 7148 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7149 | goto error; |
| 7150 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7151 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7152 | |
| 7153 | result = load(unpickler); |
| 7154 | Py_DECREF(unpickler); |
| 7155 | return result; |
| 7156 | |
| 7157 | error: |
| 7158 | Py_XDECREF(unpickler); |
| 7159 | return NULL; |
| 7160 | } |
| 7161 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7162 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7163 | _PICKLE_DUMP_METHODDEF |
| 7164 | _PICKLE_DUMPS_METHODDEF |
| 7165 | _PICKLE_LOAD_METHODDEF |
| 7166 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7167 | {NULL, NULL} /* sentinel */ |
| 7168 | }; |
| 7169 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7170 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7171 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7172 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7173 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7174 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7175 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7176 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7177 | static void |
| 7178 | pickle_free(PyObject *m) |
| 7179 | { |
| 7180 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7181 | } |
| 7182 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7183 | static int |
| 7184 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7185 | { |
| 7186 | PickleState *st = _Pickle_GetState(m); |
| 7187 | Py_VISIT(st->PickleError); |
| 7188 | Py_VISIT(st->PicklingError); |
| 7189 | Py_VISIT(st->UnpicklingError); |
| 7190 | Py_VISIT(st->dispatch_table); |
| 7191 | Py_VISIT(st->extension_registry); |
| 7192 | Py_VISIT(st->extension_cache); |
| 7193 | Py_VISIT(st->inverted_registry); |
| 7194 | Py_VISIT(st->name_mapping_2to3); |
| 7195 | Py_VISIT(st->import_mapping_2to3); |
| 7196 | Py_VISIT(st->name_mapping_3to2); |
| 7197 | Py_VISIT(st->import_mapping_3to2); |
| 7198 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7199 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7200 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7201 | } |
| 7202 | |
| 7203 | static struct PyModuleDef _picklemodule = { |
| 7204 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7205 | "_pickle", /* m_name */ |
| 7206 | pickle_module_doc, /* m_doc */ |
| 7207 | sizeof(PickleState), /* m_size */ |
| 7208 | pickle_methods, /* m_methods */ |
| 7209 | NULL, /* m_reload */ |
| 7210 | pickle_traverse, /* m_traverse */ |
| 7211 | pickle_clear, /* m_clear */ |
| 7212 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7213 | }; |
| 7214 | |
| 7215 | PyMODINIT_FUNC |
| 7216 | PyInit__pickle(void) |
| 7217 | { |
| 7218 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7219 | PickleState *st; |
| 7220 | |
| 7221 | m = PyState_FindModule(&_picklemodule); |
| 7222 | if (m) { |
| 7223 | Py_INCREF(m); |
| 7224 | return m; |
| 7225 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7226 | |
| 7227 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7228 | return NULL; |
| 7229 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7230 | return NULL; |
| 7231 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7232 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7233 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7234 | return NULL; |
| 7235 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7236 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7237 | |
| 7238 | /* Create the module and add the functions. */ |
| 7239 | m = PyModule_Create(&_picklemodule); |
| 7240 | if (m == NULL) |
| 7241 | return NULL; |
| 7242 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7243 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7244 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7245 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7246 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7247 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7248 | return NULL; |
| 7249 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7250 | st = _Pickle_GetState(m); |
| 7251 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7252 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7253 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7254 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7255 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7256 | st->PicklingError = \ |
| 7257 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7258 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7259 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7260 | st->UnpicklingError = \ |
| 7261 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7262 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7263 | return NULL; |
| 7264 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7265 | Py_INCREF(st->PickleError); |
| 7266 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7267 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7268 | Py_INCREF(st->PicklingError); |
| 7269 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7270 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7271 | Py_INCREF(st->UnpicklingError); |
| 7272 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7273 | return NULL; |
| 7274 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7275 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7276 | return NULL; |
| 7277 | |
| 7278 | return m; |
| 7279 | } |