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 |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 553 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 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 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 849 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 850 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 851 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 852 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 853 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 854 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 855 | return 0; |
| 856 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 857 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 858 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 859 | _write_size64(char *out, size_t value) |
| 860 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 861 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 862 | |
| 863 | assert(sizeof(size_t) <= 8); |
| 864 | |
| 865 | for (i = 0; i < sizeof(size_t); i++) { |
| 866 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 867 | } |
| 868 | for (i = sizeof(size_t); i < 8; i++) { |
| 869 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 870 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 871 | } |
| 872 | |
| 873 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 874 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 875 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 876 | qdata[0] = FRAME; |
| 877 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 878 | } |
| 879 | |
| 880 | static int |
| 881 | _Pickler_CommitFrame(PicklerObject *self) |
| 882 | { |
| 883 | size_t frame_len; |
| 884 | char *qdata; |
| 885 | |
| 886 | if (!self->framing || self->frame_start == -1) |
| 887 | return 0; |
| 888 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 889 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 890 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 891 | self->frame_start = -1; |
| 892 | return 0; |
| 893 | } |
| 894 | |
| 895 | static int |
| 896 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 897 | { |
| 898 | Py_ssize_t frame_len; |
| 899 | |
| 900 | if (!self->framing || self->frame_start == -1) |
| 901 | return 0; |
| 902 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 903 | if (frame_len >= FRAME_SIZE_TARGET) |
| 904 | return _Pickler_CommitFrame(self); |
| 905 | else |
| 906 | return 0; |
| 907 | } |
| 908 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 909 | static PyObject * |
| 910 | _Pickler_GetString(PicklerObject *self) |
| 911 | { |
| 912 | PyObject *output_buffer = self->output_buffer; |
| 913 | |
| 914 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 915 | |
| 916 | if (_Pickler_CommitFrame(self)) |
| 917 | return NULL; |
| 918 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 919 | self->output_buffer = NULL; |
| 920 | /* Resize down to exact size */ |
| 921 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 922 | return NULL; |
| 923 | return output_buffer; |
| 924 | } |
| 925 | |
| 926 | static int |
| 927 | _Pickler_FlushToFile(PicklerObject *self) |
| 928 | { |
| 929 | PyObject *output, *result; |
| 930 | |
| 931 | assert(self->write != NULL); |
| 932 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 933 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | output = _Pickler_GetString(self); |
| 935 | if (output == NULL) |
| 936 | return -1; |
| 937 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 938 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 939 | Py_XDECREF(result); |
| 940 | return (result == NULL) ? -1 : 0; |
| 941 | } |
| 942 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 943 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 944 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 945 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 946 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 947 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 948 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 949 | |
| 950 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 951 | need_new_frame = (self->framing && self->frame_start == -1); |
| 952 | |
| 953 | if (need_new_frame) |
| 954 | n = data_len + FRAME_HEADER_SIZE; |
| 955 | else |
| 956 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 957 | |
| 958 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 959 | if (required > self->max_output_len) { |
| 960 | /* Make place in buffer for the pickle chunk */ |
| 961 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 962 | PyErr_NoMemory(); |
| 963 | return -1; |
| 964 | } |
| 965 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 966 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 967 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 968 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 969 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 970 | if (need_new_frame) { |
| 971 | /* Setup new frame */ |
| 972 | Py_ssize_t frame_start = self->output_len; |
| 973 | self->frame_start = frame_start; |
| 974 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 975 | /* Write an invalid value, for debugging */ |
| 976 | buffer[frame_start + i] = 0xFE; |
| 977 | } |
| 978 | self->output_len += FRAME_HEADER_SIZE; |
| 979 | } |
| 980 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 981 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 982 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 983 | buffer[self->output_len + i] = s[i]; |
| 984 | } |
| 985 | } |
| 986 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 987 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 988 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 989 | self->output_len += data_len; |
| 990 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 993 | static PicklerObject * |
| 994 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 995 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 996 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 997 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 998 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 999 | if (self == NULL) |
| 1000 | return NULL; |
| 1001 | |
| 1002 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1003 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1004 | self->write = NULL; |
| 1005 | self->proto = 0; |
| 1006 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1007 | self->framing = 0; |
| 1008 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1009 | self->fast = 0; |
| 1010 | self->fast_nesting = 0; |
| 1011 | self->fix_imports = 0; |
| 1012 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1013 | self->max_output_len = WRITE_BUF_SIZE; |
| 1014 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1015 | |
| 1016 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1017 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1018 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1019 | |
| 1020 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1021 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1022 | return NULL; |
| 1023 | } |
| 1024 | return self; |
| 1025 | } |
| 1026 | |
| 1027 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1028 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1029 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1030 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1031 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1032 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1033 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1034 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1035 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1036 | proto = PyLong_AsLong(protocol); |
| 1037 | if (proto < 0) { |
| 1038 | if (proto == -1 && PyErr_Occurred()) |
| 1039 | return -1; |
| 1040 | proto = HIGHEST_PROTOCOL; |
| 1041 | } |
| 1042 | else if (proto > HIGHEST_PROTOCOL) { |
| 1043 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1044 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1045 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1046 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1047 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1048 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1049 | self->bin = proto > 0; |
| 1050 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1051 | return 0; |
| 1052 | } |
| 1053 | |
| 1054 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1055 | be called once on a freshly created Pickler. */ |
| 1056 | static int |
| 1057 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1058 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1059 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1060 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1061 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1062 | if (self->write == NULL) { |
| 1063 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1064 | PyErr_SetString(PyExc_TypeError, |
| 1065 | "file must have a 'write' attribute"); |
| 1066 | return -1; |
| 1067 | } |
| 1068 | |
| 1069 | return 0; |
| 1070 | } |
| 1071 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1072 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1073 | own reference to `input`. */ |
| 1074 | static Py_ssize_t |
| 1075 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1076 | { |
| 1077 | if (self->buffer.buf != NULL) |
| 1078 | PyBuffer_Release(&self->buffer); |
| 1079 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1080 | return -1; |
| 1081 | self->input_buffer = self->buffer.buf; |
| 1082 | self->input_len = self->buffer.len; |
| 1083 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1084 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1085 | return self->input_len; |
| 1086 | } |
| 1087 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1088 | static int |
| 1089 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1090 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1091 | Py_ssize_t consumed; |
| 1092 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1093 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1094 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1095 | if (consumed <= 0) |
| 1096 | return 0; |
| 1097 | |
| 1098 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1099 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1100 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1101 | if (r == NULL) |
| 1102 | return -1; |
| 1103 | Py_DECREF(r); |
| 1104 | |
| 1105 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1106 | return 0; |
| 1107 | } |
| 1108 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1109 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1110 | |
| 1111 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1112 | may be multiple pickle objects arranged contiguously in the same input |
| 1113 | buffer. |
| 1114 | |
| 1115 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1116 | bytes from the input stream/buffer. |
| 1117 | |
| 1118 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1119 | failure; on success, returns the number of bytes read from the file. |
| 1120 | |
| 1121 | On success, self->input_len will be 0; this is intentional so that when |
| 1122 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1123 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1124 | size to tell you how much data you can process. */ |
| 1125 | static Py_ssize_t |
| 1126 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1127 | { |
| 1128 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1129 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1130 | |
| 1131 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1132 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1133 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1134 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1135 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1136 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1137 | PyObject *empty_tuple = PyTuple_New(0); |
| 1138 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1139 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1140 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1141 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1142 | PyObject *len; |
| 1143 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1144 | if (self->peek && n < PREFETCH) { |
| 1145 | len = PyLong_FromSsize_t(PREFETCH); |
| 1146 | if (len == NULL) |
| 1147 | return -1; |
| 1148 | data = _Pickle_FastCall(self->peek, len); |
| 1149 | if (data == NULL) { |
| 1150 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1151 | return -1; |
| 1152 | /* peek() is probably not supported by the given file object */ |
| 1153 | PyErr_Clear(); |
| 1154 | Py_CLEAR(self->peek); |
| 1155 | } |
| 1156 | else { |
| 1157 | read_size = _Unpickler_SetStringInput(self, data); |
| 1158 | Py_DECREF(data); |
| 1159 | self->prefetched_idx = 0; |
| 1160 | if (n <= read_size) |
| 1161 | return n; |
| 1162 | } |
| 1163 | } |
| 1164 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1165 | if (len == NULL) |
| 1166 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1167 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1168 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1169 | if (data == NULL) |
| 1170 | return -1; |
| 1171 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1172 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1173 | Py_DECREF(data); |
| 1174 | return read_size; |
| 1175 | } |
| 1176 | |
| 1177 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1178 | |
| 1179 | This should be used for all data reads, rather than accessing the unpickler's |
| 1180 | input buffer directly. This method deals correctly with reading from input |
| 1181 | streams, which the input buffer doesn't deal with. |
| 1182 | |
| 1183 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1184 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1185 | should use this function's return value to know how many bytes you can |
| 1186 | consume. |
| 1187 | |
| 1188 | Returns -1 (with an exception set) on failure. On success, return the |
| 1189 | number of chars read. */ |
| 1190 | static Py_ssize_t |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1191 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1192 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1193 | Py_ssize_t num_read; |
| 1194 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1195 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1196 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1197 | PickleState *st = _Pickle_GetGlobalState(); |
| 1198 | PyErr_SetString(st->UnpicklingError, |
| 1199 | "read would overflow (invalid bytecode)"); |
| 1200 | return -1; |
| 1201 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1202 | if (self->next_read_idx + n <= self->input_len) { |
| 1203 | *s = self->input_buffer + self->next_read_idx; |
| 1204 | self->next_read_idx += n; |
| 1205 | return n; |
| 1206 | } |
| 1207 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1208 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1209 | return -1; |
| 1210 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1211 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1212 | if (num_read < 0) |
| 1213 | return -1; |
| 1214 | if (num_read < n) { |
| 1215 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1216 | return -1; |
| 1217 | } |
| 1218 | *s = self->input_buffer; |
| 1219 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1220 | return n; |
| 1221 | } |
| 1222 | |
| 1223 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1224 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1225 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1226 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1227 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1228 | if (input_line == NULL) { |
| 1229 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1230 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1231 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1232 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1233 | memcpy(input_line, line, len); |
| 1234 | input_line[len] = '\0'; |
| 1235 | self->input_line = input_line; |
| 1236 | *result = self->input_line; |
| 1237 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1238 | } |
| 1239 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1240 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1241 | before hitting \n, return the data we found. |
| 1242 | |
| 1243 | Returns the number of chars read, or -1 on failure. */ |
| 1244 | static Py_ssize_t |
| 1245 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1246 | { |
| 1247 | Py_ssize_t i, num_read; |
| 1248 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1249 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1250 | if (self->input_buffer[i] == '\n') { |
| 1251 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1252 | num_read = i - self->next_read_idx + 1; |
| 1253 | self->next_read_idx = i + 1; |
| 1254 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1255 | } |
| 1256 | } |
| 1257 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1258 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1259 | if (num_read < 0) |
| 1260 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1261 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1262 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1263 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1264 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1265 | /* If we get here, we've run off the end of the input string. Return the |
| 1266 | remaining string and let the caller figure it out. */ |
| 1267 | *result = self->input_buffer + self->next_read_idx; |
| 1268 | num_read = i - self->next_read_idx; |
| 1269 | self->next_read_idx = i; |
| 1270 | return num_read; |
| 1271 | } |
| 1272 | |
| 1273 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1274 | will be modified in place. */ |
| 1275 | static int |
| 1276 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1277 | { |
| 1278 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1279 | |
| 1280 | assert(new_size > self->memo_size); |
| 1281 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1282 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1283 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1284 | PyErr_NoMemory(); |
| 1285 | return -1; |
| 1286 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1287 | for (i = self->memo_size; i < new_size; i++) |
| 1288 | self->memo[i] = NULL; |
| 1289 | self->memo_size = new_size; |
| 1290 | return 0; |
| 1291 | } |
| 1292 | |
| 1293 | /* Returns NULL if idx is out of bounds. */ |
| 1294 | static PyObject * |
| 1295 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1296 | { |
| 1297 | if (idx < 0 || idx >= self->memo_size) |
| 1298 | return NULL; |
| 1299 | |
| 1300 | return self->memo[idx]; |
| 1301 | } |
| 1302 | |
| 1303 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1304 | This takes its own reference to `value`. */ |
| 1305 | static int |
| 1306 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1307 | { |
| 1308 | PyObject *old_item; |
| 1309 | |
| 1310 | if (idx >= self->memo_size) { |
| 1311 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1312 | return -1; |
| 1313 | assert(idx < self->memo_size); |
| 1314 | } |
| 1315 | Py_INCREF(value); |
| 1316 | old_item = self->memo[idx]; |
| 1317 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1318 | if (old_item != NULL) { |
| 1319 | Py_DECREF(old_item); |
| 1320 | } |
| 1321 | else { |
| 1322 | self->memo_len++; |
| 1323 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1324 | return 0; |
| 1325 | } |
| 1326 | |
| 1327 | static PyObject ** |
| 1328 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1329 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1330 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1331 | if (memo == NULL) { |
| 1332 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1333 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1334 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1335 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1336 | return memo; |
| 1337 | } |
| 1338 | |
| 1339 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1340 | static void |
| 1341 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1342 | { |
| 1343 | Py_ssize_t i; |
| 1344 | PyObject **memo = self->memo; |
| 1345 | |
| 1346 | if (self->memo == NULL) |
| 1347 | return; |
| 1348 | self->memo = NULL; |
| 1349 | i = self->memo_size; |
| 1350 | while (--i >= 0) { |
| 1351 | Py_XDECREF(memo[i]); |
| 1352 | } |
| 1353 | PyMem_FREE(memo); |
| 1354 | } |
| 1355 | |
| 1356 | static UnpicklerObject * |
| 1357 | _Unpickler_New(void) |
| 1358 | { |
| 1359 | UnpicklerObject *self; |
| 1360 | |
| 1361 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1362 | if (self == NULL) |
| 1363 | return NULL; |
| 1364 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1365 | self->pers_func = NULL; |
| 1366 | self->input_buffer = NULL; |
| 1367 | self->input_line = NULL; |
| 1368 | self->input_len = 0; |
| 1369 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1370 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1371 | self->read = NULL; |
| 1372 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1373 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1374 | self->encoding = NULL; |
| 1375 | self->errors = NULL; |
| 1376 | self->marks = NULL; |
| 1377 | self->num_marks = 0; |
| 1378 | self->marks_size = 0; |
| 1379 | self->proto = 0; |
| 1380 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1381 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1382 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1383 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1384 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1385 | self->stack = (Pdata *)Pdata_New(); |
| 1386 | |
| 1387 | if (self->memo == NULL || self->stack == NULL) { |
| 1388 | Py_DECREF(self); |
| 1389 | return NULL; |
| 1390 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1391 | |
| 1392 | return self; |
| 1393 | } |
| 1394 | |
| 1395 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1396 | be called once on a freshly created Pickler. */ |
| 1397 | static int |
| 1398 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1399 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1400 | _Py_IDENTIFIER(peek); |
| 1401 | _Py_IDENTIFIER(read); |
| 1402 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1403 | |
| 1404 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1405 | if (self->peek == NULL) { |
| 1406 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1407 | PyErr_Clear(); |
| 1408 | else |
| 1409 | return -1; |
| 1410 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1411 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1412 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1413 | if (self->readline == NULL || self->read == NULL) { |
| 1414 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1415 | PyErr_SetString(PyExc_TypeError, |
| 1416 | "file must have 'read' and 'readline' attributes"); |
| 1417 | Py_CLEAR(self->read); |
| 1418 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1419 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1420 | return -1; |
| 1421 | } |
| 1422 | return 0; |
| 1423 | } |
| 1424 | |
| 1425 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1426 | be called once on a freshly created Pickler. */ |
| 1427 | static int |
| 1428 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1429 | const char *encoding, |
| 1430 | const char *errors) |
| 1431 | { |
| 1432 | if (encoding == NULL) |
| 1433 | encoding = "ASCII"; |
| 1434 | if (errors == NULL) |
| 1435 | errors = "strict"; |
| 1436 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1437 | self->encoding = _PyMem_Strdup(encoding); |
| 1438 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1439 | if (self->encoding == NULL || self->errors == NULL) { |
| 1440 | PyErr_NoMemory(); |
| 1441 | return -1; |
| 1442 | } |
| 1443 | return 0; |
| 1444 | } |
| 1445 | |
| 1446 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1447 | static int |
| 1448 | memo_get(PicklerObject *self, PyObject *key) |
| 1449 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1450 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1451 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1452 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1453 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1454 | value = PyMemoTable_Get(self->memo, key); |
| 1455 | if (value == NULL) { |
| 1456 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1457 | return -1; |
| 1458 | } |
| 1459 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1460 | if (!self->bin) { |
| 1461 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1462 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1463 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1464 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1465 | } |
| 1466 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1467 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1468 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1469 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1470 | len = 2; |
| 1471 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1472 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1473 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1474 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1475 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1476 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1477 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1478 | len = 5; |
| 1479 | } |
| 1480 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1481 | PickleState *st = _Pickle_GetGlobalState(); |
| 1482 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1483 | "memo id too large for LONG_BINGET"); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | } |
| 1487 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1488 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1489 | return -1; |
| 1490 | |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1495 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1496 | static int |
| 1497 | memo_put(PicklerObject *self, PyObject *obj) |
| 1498 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1499 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1500 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1501 | Py_ssize_t idx; |
| 1502 | |
| 1503 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1504 | |
| 1505 | if (self->fast) |
| 1506 | return 0; |
| 1507 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1508 | idx = PyMemoTable_Size(self->memo); |
| 1509 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1510 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1511 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1512 | if (self->proto >= 4) { |
| 1513 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1514 | return -1; |
| 1515 | return 0; |
| 1516 | } |
| 1517 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1518 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1519 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1520 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1521 | len = strlen(pdata); |
| 1522 | } |
| 1523 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1524 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1525 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1526 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1527 | len = 2; |
| 1528 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1529 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1530 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1531 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1532 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1533 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1534 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1535 | len = 5; |
| 1536 | } |
| 1537 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1538 | PickleState *st = _Pickle_GetGlobalState(); |
| 1539 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1540 | "memo id too large for LONG_BINPUT"); |
| 1541 | return -1; |
| 1542 | } |
| 1543 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1544 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1545 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1546 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1547 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1551 | get_dotted_path(PyObject *obj, PyObject *name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1552 | _Py_static_string(PyId_dot, "."); |
| 1553 | _Py_static_string(PyId_locals, "<locals>"); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1554 | PyObject *dotted_path; |
| 1555 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1556 | |
| 1557 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1558 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1559 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1560 | n = PyList_GET_SIZE(dotted_path); |
| 1561 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1562 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1563 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1564 | PyObject *result = PyUnicode_RichCompare( |
| 1565 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1566 | int is_equal = (result == Py_True); |
| 1567 | assert(PyBool_Check(result)); |
| 1568 | Py_DECREF(result); |
| 1569 | if (is_equal) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1570 | if (obj == NULL) |
| 1571 | PyErr_Format(PyExc_AttributeError, |
| 1572 | "Can't pickle local object %R", name); |
| 1573 | else |
| 1574 | PyErr_Format(PyExc_AttributeError, |
| 1575 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1576 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1577 | return NULL; |
| 1578 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1579 | } |
| 1580 | return dotted_path; |
| 1581 | } |
| 1582 | |
| 1583 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1584 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1585 | { |
| 1586 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1587 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1588 | |
| 1589 | assert(PyList_CheckExact(names)); |
| 1590 | Py_INCREF(obj); |
| 1591 | n = PyList_GET_SIZE(names); |
| 1592 | for (i = 0; i < n; i++) { |
| 1593 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1594 | Py_XDECREF(parent); |
| 1595 | parent = obj; |
| 1596 | obj = PyObject_GetAttr(parent, name); |
| 1597 | if (obj == NULL) { |
| 1598 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1599 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1600 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1601 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1602 | if (pparent != NULL) |
| 1603 | *pparent = parent; |
| 1604 | else |
| 1605 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1606 | return obj; |
| 1607 | } |
| 1608 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1609 | static void |
| 1610 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1611 | { |
| 1612 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1613 | PyErr_Clear(); |
| 1614 | PyErr_Format(PyExc_AttributeError, |
| 1615 | "Can't get attribute %R on %R", name, obj); |
| 1616 | } |
| 1617 | } |
| 1618 | |
| 1619 | |
| 1620 | static PyObject * |
| 1621 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1622 | { |
| 1623 | PyObject *dotted_path, *attr; |
| 1624 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1625 | if (allow_qualname) { |
| 1626 | dotted_path = get_dotted_path(obj, name); |
| 1627 | if (dotted_path == NULL) |
| 1628 | return NULL; |
| 1629 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1630 | Py_DECREF(dotted_path); |
| 1631 | } |
| 1632 | else |
| 1633 | attr = PyObject_GetAttr(obj, name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1634 | if (attr == NULL) |
| 1635 | reformat_attribute_error(obj, name); |
| 1636 | return attr; |
| 1637 | } |
| 1638 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1639 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1640 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1641 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1642 | PyObject *module_name; |
| 1643 | PyObject *modules_dict; |
| 1644 | PyObject *module; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1645 | Py_ssize_t i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1646 | _Py_IDENTIFIER(__module__); |
| 1647 | _Py_IDENTIFIER(modules); |
| 1648 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1649 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1650 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1651 | |
| 1652 | if (module_name == NULL) { |
| 1653 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1654 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1655 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1656 | } |
| 1657 | else { |
| 1658 | /* In some rare cases (e.g., bound methods of extension types), |
| 1659 | __module__ can be None. If it is so, then search sys.modules for |
| 1660 | the module of global. */ |
| 1661 | if (module_name != Py_None) |
| 1662 | return module_name; |
| 1663 | Py_CLEAR(module_name); |
| 1664 | } |
| 1665 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1666 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1667 | /* Fallback on walking sys.modules */ |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1668 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1669 | if (modules_dict == NULL) { |
| 1670 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1671 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1672 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1673 | |
| 1674 | i = 0; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1675 | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
| 1676 | PyObject *candidate; |
| 1677 | if (PyUnicode_Check(module_name) && |
| 1678 | !PyUnicode_CompareWithASCIIString(module_name, "__main__")) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1679 | continue; |
| 1680 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1681 | continue; |
| 1682 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1683 | candidate = get_deep_attribute(module, dotted_path, NULL); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1684 | if (candidate == NULL) { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1685 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1686 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1687 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1688 | continue; |
| 1689 | } |
| 1690 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1691 | if (candidate == global) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1692 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1693 | Py_DECREF(candidate); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1694 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1695 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1696 | Py_DECREF(candidate); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1700 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1701 | Py_INCREF(module_name); |
| 1702 | return module_name; |
| 1703 | } |
| 1704 | |
| 1705 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1706 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1707 | memoization disabled). If the nesting of a list or dict object exceed |
| 1708 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1709 | reference to the seen list or dict objects and check whether these objects |
| 1710 | are recursive. These are not strictly necessary, since save() has a |
| 1711 | hard-coded recursion limit, but they give a nicer error message than the |
| 1712 | typical RuntimeError. */ |
| 1713 | static int |
| 1714 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1715 | { |
| 1716 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1717 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1718 | PyObject *key = NULL; |
| 1719 | if (self->fast_memo == NULL) { |
| 1720 | self->fast_memo = PyDict_New(); |
| 1721 | if (self->fast_memo == NULL) { |
| 1722 | self->fast_nesting = -1; |
| 1723 | return 0; |
| 1724 | } |
| 1725 | } |
| 1726 | key = PyLong_FromVoidPtr(obj); |
| 1727 | if (key == NULL) |
| 1728 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1729 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1730 | Py_DECREF(key); |
| 1731 | PyErr_Format(PyExc_ValueError, |
| 1732 | "fast mode: can't pickle cyclic objects " |
| 1733 | "including object type %.200s at %p", |
| 1734 | obj->ob_type->tp_name, obj); |
| 1735 | self->fast_nesting = -1; |
| 1736 | return 0; |
| 1737 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1738 | if (PyErr_Occurred()) { |
| 1739 | return 0; |
| 1740 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1741 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1742 | Py_DECREF(key); |
| 1743 | self->fast_nesting = -1; |
| 1744 | return 0; |
| 1745 | } |
| 1746 | Py_DECREF(key); |
| 1747 | } |
| 1748 | return 1; |
| 1749 | } |
| 1750 | |
| 1751 | static int |
| 1752 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1753 | { |
| 1754 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1755 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1756 | if (key == NULL) |
| 1757 | return 0; |
| 1758 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1759 | Py_DECREF(key); |
| 1760 | return 0; |
| 1761 | } |
| 1762 | Py_DECREF(key); |
| 1763 | } |
| 1764 | return 1; |
| 1765 | } |
| 1766 | |
| 1767 | static int |
| 1768 | save_none(PicklerObject *self, PyObject *obj) |
| 1769 | { |
| 1770 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1771 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1772 | return -1; |
| 1773 | |
| 1774 | return 0; |
| 1775 | } |
| 1776 | |
| 1777 | static int |
| 1778 | save_bool(PicklerObject *self, PyObject *obj) |
| 1779 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1780 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1781 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1782 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1783 | return -1; |
| 1784 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1785 | else { |
| 1786 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1787 | * so that unpicklers written before bools were introduced unpickle them |
| 1788 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1789 | * Note that protocol 2 added direct ways to pickle bools. |
| 1790 | */ |
| 1791 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1792 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1793 | return -1; |
| 1794 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1795 | return 0; |
| 1796 | } |
| 1797 | |
| 1798 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1799 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1800 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1801 | PyObject *repr = NULL; |
| 1802 | Py_ssize_t size; |
| 1803 | long val; |
| 1804 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1805 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1806 | const char long_op = LONG; |
| 1807 | |
| 1808 | val= PyLong_AsLong(obj); |
| 1809 | if (val == -1 && PyErr_Occurred()) { |
| 1810 | /* out of range for int pickling */ |
| 1811 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1812 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1813 | else if (self->bin && |
| 1814 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1815 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1816 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1817 | |
| 1818 | Note: we can't use -0x80000000L in the above condition because some |
| 1819 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1820 | before applying the unary minus when sizeof(long) <= 4. The |
| 1821 | resulting value stays unsigned which is commonly not what we want, |
| 1822 | so MSVC happily warns us about it. However, that result would have |
| 1823 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1824 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1825 | char pdata[32]; |
| 1826 | Py_ssize_t len = 0; |
| 1827 | |
| 1828 | pdata[1] = (unsigned char)(val & 0xff); |
| 1829 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1830 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1831 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1832 | |
| 1833 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1834 | if (pdata[2] == 0) { |
| 1835 | pdata[0] = BININT1; |
| 1836 | len = 2; |
| 1837 | } |
| 1838 | else { |
| 1839 | pdata[0] = BININT2; |
| 1840 | len = 3; |
| 1841 | } |
| 1842 | } |
| 1843 | else { |
| 1844 | pdata[0] = BININT; |
| 1845 | len = 5; |
| 1846 | } |
| 1847 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1848 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1849 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1850 | |
| 1851 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1852 | } |
| 1853 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1854 | if (self->proto >= 2) { |
| 1855 | /* Linear-time pickling. */ |
| 1856 | size_t nbits; |
| 1857 | size_t nbytes; |
| 1858 | unsigned char *pdata; |
| 1859 | char header[5]; |
| 1860 | int i; |
| 1861 | int sign = _PyLong_Sign(obj); |
| 1862 | |
| 1863 | if (sign == 0) { |
| 1864 | header[0] = LONG1; |
| 1865 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1866 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1867 | goto error; |
| 1868 | return 0; |
| 1869 | } |
| 1870 | nbits = _PyLong_NumBits(obj); |
| 1871 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1872 | goto error; |
| 1873 | /* How many bytes do we need? There are nbits >> 3 full |
| 1874 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1875 | * are any leftover bits, then we clearly need another |
| 1876 | * byte. Wnat's not so obvious is that we *probably* |
| 1877 | * need another byte even if there aren't any leftovers: |
| 1878 | * the most-significant bit of the most-significant byte |
| 1879 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1880 | * opposite of the one we need. The exception is ints |
| 1881 | * 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] | 1882 | * its own 256's-complement, so has the right sign bit |
| 1883 | * even without the extra byte. That's a pain to check |
| 1884 | * for in advance, though, so we always grab an extra |
| 1885 | * byte at the start, and cut it back later if possible. |
| 1886 | */ |
| 1887 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1888 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1889 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1890 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1891 | goto error; |
| 1892 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1893 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1894 | if (repr == NULL) |
| 1895 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1896 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1897 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1898 | pdata, nbytes, |
| 1899 | 1 /* little endian */ , 1 /* signed */ ); |
| 1900 | if (i < 0) |
| 1901 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1902 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1903 | * needed. This is so iff the MSB is all redundant sign |
| 1904 | * bits. |
| 1905 | */ |
| 1906 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1907 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1908 | pdata[nbytes - 1] == 0xff && |
| 1909 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1910 | nbytes--; |
| 1911 | } |
| 1912 | |
| 1913 | if (nbytes < 256) { |
| 1914 | header[0] = LONG1; |
| 1915 | header[1] = (unsigned char)nbytes; |
| 1916 | size = 2; |
| 1917 | } |
| 1918 | else { |
| 1919 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1920 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1921 | for (i = 1; i < 5; i++) { |
| 1922 | header[i] = (unsigned char)(size & 0xff); |
| 1923 | size >>= 8; |
| 1924 | } |
| 1925 | size = 5; |
| 1926 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1927 | if (_Pickler_Write(self, header, size) < 0 || |
| 1928 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1929 | goto error; |
| 1930 | } |
| 1931 | else { |
| 1932 | char *string; |
| 1933 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1934 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1935 | the number of digits), in both directions. We add a trailing 'L' |
| 1936 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1937 | |
| 1938 | repr = PyObject_Repr(obj); |
| 1939 | if (repr == NULL) |
| 1940 | goto error; |
| 1941 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1942 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1943 | if (string == NULL) |
| 1944 | goto error; |
| 1945 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1946 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1947 | _Pickler_Write(self, string, size) < 0 || |
| 1948 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1949 | goto error; |
| 1950 | } |
| 1951 | |
| 1952 | if (0) { |
| 1953 | error: |
| 1954 | status = -1; |
| 1955 | } |
| 1956 | Py_XDECREF(repr); |
| 1957 | |
| 1958 | return status; |
| 1959 | } |
| 1960 | |
| 1961 | static int |
| 1962 | save_float(PicklerObject *self, PyObject *obj) |
| 1963 | { |
| 1964 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1965 | |
| 1966 | if (self->bin) { |
| 1967 | char pdata[9]; |
| 1968 | pdata[0] = BINFLOAT; |
| 1969 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1970 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1971 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1972 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1973 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1974 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1975 | int result = -1; |
| 1976 | char *buf = NULL; |
| 1977 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1978 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1979 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1980 | goto done; |
| 1981 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 1982 | 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] | 1983 | if (!buf) { |
| 1984 | PyErr_NoMemory(); |
| 1985 | goto done; |
| 1986 | } |
| 1987 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1988 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1989 | goto done; |
| 1990 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1991 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1992 | goto done; |
| 1993 | |
| 1994 | result = 0; |
| 1995 | done: |
| 1996 | PyMem_Free(buf); |
| 1997 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1998 | } |
| 1999 | |
| 2000 | return 0; |
| 2001 | } |
| 2002 | |
| 2003 | static int |
| 2004 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2005 | { |
| 2006 | if (self->proto < 3) { |
| 2007 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2008 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2009 | the __reduce__ method) to permit bytes object unpickling. |
| 2010 | |
| 2011 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2012 | 2 'bytes' is just an alias for 'str' (which has different |
| 2013 | parameters than the actual bytes object), we use codecs.encode |
| 2014 | to create the appropriate 'str' object when unpickled using |
| 2015 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2016 | using Python 3. Again this is a hack and we don't need to do this |
| 2017 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2018 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2019 | int status; |
| 2020 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2021 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2022 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2023 | } |
| 2024 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2025 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2026 | PyObject *unicode_str = |
| 2027 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2028 | PyBytes_GET_SIZE(obj), |
| 2029 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2030 | _Py_IDENTIFIER(latin1); |
| 2031 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2032 | if (unicode_str == NULL) |
| 2033 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2034 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2035 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2036 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2037 | Py_DECREF(unicode_str); |
| 2038 | } |
| 2039 | |
| 2040 | if (reduce_value == NULL) |
| 2041 | return -1; |
| 2042 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2043 | /* save_reduce() will memoize the object automatically. */ |
| 2044 | status = save_reduce(self, reduce_value, obj); |
| 2045 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2046 | return status; |
| 2047 | } |
| 2048 | else { |
| 2049 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2050 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2051 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2052 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2053 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2054 | if (size < 0) |
| 2055 | return -1; |
| 2056 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2057 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2058 | header[0] = SHORT_BINBYTES; |
| 2059 | header[1] = (unsigned char)size; |
| 2060 | len = 2; |
| 2061 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2062 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2063 | header[0] = BINBYTES; |
| 2064 | header[1] = (unsigned char)(size & 0xff); |
| 2065 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2066 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2067 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2068 | len = 5; |
| 2069 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2070 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2071 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2072 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2073 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2074 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2075 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2076 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2077 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2078 | return -1; /* string too large */ |
| 2079 | } |
| 2080 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2081 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2082 | return -1; |
| 2083 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2084 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2085 | return -1; |
| 2086 | |
| 2087 | if (memo_put(self, obj) < 0) |
| 2088 | return -1; |
| 2089 | |
| 2090 | return 0; |
| 2091 | } |
| 2092 | } |
| 2093 | |
| 2094 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2095 | backslash and newline characters to \uXXXX escapes. */ |
| 2096 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2097 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2098 | { |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2099 | PyObject *repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2100 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2101 | Py_ssize_t i, size; |
| 2102 | size_t expandsize; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2103 | void *data; |
| 2104 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2105 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2106 | if (PyUnicode_READY(obj)) |
| 2107 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2108 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2109 | size = PyUnicode_GET_LENGTH(obj); |
| 2110 | data = PyUnicode_DATA(obj); |
| 2111 | kind = PyUnicode_KIND(obj); |
| 2112 | if (kind == PyUnicode_4BYTE_KIND) |
| 2113 | expandsize = 10; |
| 2114 | else |
| 2115 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2116 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2117 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize) |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2118 | return PyErr_NoMemory(); |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2119 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2120 | if (repr == NULL) |
| 2121 | return NULL; |
| 2122 | if (size == 0) |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2123 | return repr; |
| 2124 | assert(Py_REFCNT(repr) == 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2125 | |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2126 | p = PyBytes_AS_STRING(repr); |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2127 | for (i=0; i < size; i++) { |
| 2128 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2129 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2130 | if (ch >= 0x10000) { |
| 2131 | *p++ = '\\'; |
| 2132 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2133 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2134 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2135 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2136 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2137 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2138 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2139 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2140 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2141 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2142 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2143 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2144 | *p++ = '\\'; |
| 2145 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2146 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2147 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2148 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2149 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2150 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2151 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2152 | else |
| 2153 | *p++ = (char) ch; |
| 2154 | } |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2155 | size = p - PyBytes_AS_STRING(repr); |
| 2156 | if (_PyBytes_Resize(&repr, size) < 0) |
| 2157 | return NULL; |
| 2158 | return repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2159 | } |
| 2160 | |
| 2161 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2162 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2163 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2164 | char header[9]; |
| 2165 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2166 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2167 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2168 | if (size <= 0xff && self->proto >= 4) { |
| 2169 | header[0] = SHORT_BINUNICODE; |
| 2170 | header[1] = (unsigned char)(size & 0xff); |
| 2171 | len = 2; |
| 2172 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2173 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2174 | header[0] = BINUNICODE; |
| 2175 | header[1] = (unsigned char)(size & 0xff); |
| 2176 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2177 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2178 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2179 | len = 5; |
| 2180 | } |
| 2181 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2182 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2183 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2184 | len = 9; |
| 2185 | } |
| 2186 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2187 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2188 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2189 | return -1; |
| 2190 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2191 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2192 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2193 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2194 | if (_Pickler_Write(self, data, size) < 0) |
| 2195 | return -1; |
| 2196 | |
| 2197 | return 0; |
| 2198 | } |
| 2199 | |
| 2200 | static int |
| 2201 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2202 | { |
| 2203 | PyObject *encoded = NULL; |
| 2204 | Py_ssize_t size; |
| 2205 | char *data; |
| 2206 | int r; |
| 2207 | |
| 2208 | if (PyUnicode_READY(obj)) |
| 2209 | return -1; |
| 2210 | |
| 2211 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2212 | if (data != NULL) |
| 2213 | return write_utf8(self, data, size); |
| 2214 | |
| 2215 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2216 | "surrogatepass" error handler. */ |
| 2217 | PyErr_Clear(); |
| 2218 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2219 | if (encoded == NULL) |
| 2220 | return -1; |
| 2221 | |
| 2222 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2223 | PyBytes_GET_SIZE(encoded)); |
| 2224 | Py_DECREF(encoded); |
| 2225 | return r; |
| 2226 | } |
| 2227 | |
| 2228 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2229 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2230 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2231 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2232 | if (write_unicode_binary(self, obj) < 0) |
| 2233 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2234 | } |
| 2235 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2236 | PyObject *encoded; |
| 2237 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2238 | const char unicode_op = UNICODE; |
| 2239 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2240 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2241 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2242 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2243 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2244 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2245 | Py_DECREF(encoded); |
| 2246 | return -1; |
| 2247 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2248 | |
| 2249 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2250 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2251 | Py_DECREF(encoded); |
| 2252 | return -1; |
| 2253 | } |
| 2254 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2255 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2256 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2257 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2258 | } |
| 2259 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2260 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2261 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2262 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2266 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2267 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2268 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2269 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2270 | |
| 2271 | assert(PyTuple_Size(t) == len); |
| 2272 | |
| 2273 | for (i = 0; i < len; i++) { |
| 2274 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2275 | |
| 2276 | if (element == NULL) |
| 2277 | return -1; |
| 2278 | if (save(self, element, 0) < 0) |
| 2279 | return -1; |
| 2280 | } |
| 2281 | |
| 2282 | return 0; |
| 2283 | } |
| 2284 | |
| 2285 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2286 | * used across protocols to minimize the space needed to pickle them. |
| 2287 | * Tuples are also the only builtin immutable type that can be recursive |
| 2288 | * (a tuple can be reached from itself), and that requires some subtle |
| 2289 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2290 | */ |
| 2291 | static int |
| 2292 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2293 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2294 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2295 | |
| 2296 | const char mark_op = MARK; |
| 2297 | const char tuple_op = TUPLE; |
| 2298 | const char pop_op = POP; |
| 2299 | const char pop_mark_op = POP_MARK; |
| 2300 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2301 | |
| 2302 | if ((len = PyTuple_Size(obj)) < 0) |
| 2303 | return -1; |
| 2304 | |
| 2305 | if (len == 0) { |
| 2306 | char pdata[2]; |
| 2307 | |
| 2308 | if (self->proto) { |
| 2309 | pdata[0] = EMPTY_TUPLE; |
| 2310 | len = 1; |
| 2311 | } |
| 2312 | else { |
| 2313 | pdata[0] = MARK; |
| 2314 | pdata[1] = TUPLE; |
| 2315 | len = 2; |
| 2316 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2317 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2318 | return -1; |
| 2319 | return 0; |
| 2320 | } |
| 2321 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2322 | /* 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] | 2323 | * saving the tuple elements, the tuple must be recursive, in |
| 2324 | * which case we'll pop everything we put on the stack, and fetch |
| 2325 | * its value from the memo. |
| 2326 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2327 | if (len <= 3 && self->proto >= 2) { |
| 2328 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2329 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2330 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2331 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2332 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2333 | /* pop the len elements */ |
| 2334 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2335 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2336 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2337 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2338 | if (memo_get(self, obj) < 0) |
| 2339 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2340 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2341 | return 0; |
| 2342 | } |
| 2343 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2344 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2345 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2346 | } |
| 2347 | goto memoize; |
| 2348 | } |
| 2349 | |
| 2350 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2351 | * Generate MARK e1 e2 ... TUPLE |
| 2352 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2353 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2354 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2355 | |
| 2356 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2357 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2358 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2359 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2360 | /* pop the stack stuff we pushed */ |
| 2361 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2362 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2363 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2364 | } |
| 2365 | else { |
| 2366 | /* Note that we pop one more than len, to remove |
| 2367 | * the MARK too. |
| 2368 | */ |
| 2369 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2370 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2371 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2372 | } |
| 2373 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2374 | if (memo_get(self, obj) < 0) |
| 2375 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2376 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2377 | return 0; |
| 2378 | } |
| 2379 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2380 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2381 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2382 | } |
| 2383 | |
| 2384 | memoize: |
| 2385 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2386 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2387 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2388 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* iter is an iterator giving items, and we batch up chunks of |
| 2392 | * MARK item item ... item APPENDS |
| 2393 | * opcode sequences. Calling code should have arranged to first create an |
| 2394 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2395 | * Returns 0 on success, <0 on error. |
| 2396 | */ |
| 2397 | static int |
| 2398 | batch_list(PicklerObject *self, PyObject *iter) |
| 2399 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2400 | PyObject *obj = NULL; |
| 2401 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2402 | int i, n; |
| 2403 | |
| 2404 | const char mark_op = MARK; |
| 2405 | const char append_op = APPEND; |
| 2406 | const char appends_op = APPENDS; |
| 2407 | |
| 2408 | assert(iter != NULL); |
| 2409 | |
| 2410 | /* XXX: I think this function could be made faster by avoiding the |
| 2411 | iterator interface and fetching objects directly from list using |
| 2412 | PyList_GET_ITEM. |
| 2413 | */ |
| 2414 | |
| 2415 | if (self->proto == 0) { |
| 2416 | /* APPENDS isn't available; do one at a time. */ |
| 2417 | for (;;) { |
| 2418 | obj = PyIter_Next(iter); |
| 2419 | if (obj == NULL) { |
| 2420 | if (PyErr_Occurred()) |
| 2421 | return -1; |
| 2422 | break; |
| 2423 | } |
| 2424 | i = save(self, obj, 0); |
| 2425 | Py_DECREF(obj); |
| 2426 | if (i < 0) |
| 2427 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2428 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2429 | return -1; |
| 2430 | } |
| 2431 | return 0; |
| 2432 | } |
| 2433 | |
| 2434 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2435 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2436 | /* Get first item */ |
| 2437 | firstitem = PyIter_Next(iter); |
| 2438 | if (firstitem == NULL) { |
| 2439 | if (PyErr_Occurred()) |
| 2440 | goto error; |
| 2441 | |
| 2442 | /* nothing more to add */ |
| 2443 | break; |
| 2444 | } |
| 2445 | |
| 2446 | /* Try to get a second item */ |
| 2447 | obj = PyIter_Next(iter); |
| 2448 | if (obj == NULL) { |
| 2449 | if (PyErr_Occurred()) |
| 2450 | goto error; |
| 2451 | |
| 2452 | /* Only one item to write */ |
| 2453 | if (save(self, firstitem, 0) < 0) |
| 2454 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2455 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2456 | goto error; |
| 2457 | Py_CLEAR(firstitem); |
| 2458 | break; |
| 2459 | } |
| 2460 | |
| 2461 | /* More than one item to write */ |
| 2462 | |
| 2463 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2464 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2465 | goto error; |
| 2466 | |
| 2467 | if (save(self, firstitem, 0) < 0) |
| 2468 | goto error; |
| 2469 | Py_CLEAR(firstitem); |
| 2470 | n = 1; |
| 2471 | |
| 2472 | /* Fetch and save up to BATCHSIZE items */ |
| 2473 | while (obj) { |
| 2474 | if (save(self, obj, 0) < 0) |
| 2475 | goto error; |
| 2476 | Py_CLEAR(obj); |
| 2477 | n += 1; |
| 2478 | |
| 2479 | if (n == BATCHSIZE) |
| 2480 | break; |
| 2481 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2482 | obj = PyIter_Next(iter); |
| 2483 | if (obj == NULL) { |
| 2484 | if (PyErr_Occurred()) |
| 2485 | goto error; |
| 2486 | break; |
| 2487 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2490 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2491 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2492 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2493 | } while (n == BATCHSIZE); |
| 2494 | return 0; |
| 2495 | |
| 2496 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2497 | Py_XDECREF(firstitem); |
| 2498 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2499 | return -1; |
| 2500 | } |
| 2501 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2502 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2503 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2504 | * MARK item item ... item APPENDS |
| 2505 | * opcode sequences. Calling code should have arranged to first create an |
| 2506 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2507 | * Returns 0 on success, -1 on error. |
| 2508 | * |
| 2509 | * This version is considerably faster than batch_list(), if less general. |
| 2510 | * |
| 2511 | * Note that this only works for protocols > 0. |
| 2512 | */ |
| 2513 | static int |
| 2514 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2515 | { |
| 2516 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2517 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2518 | |
| 2519 | const char append_op = APPEND; |
| 2520 | const char appends_op = APPENDS; |
| 2521 | const char mark_op = MARK; |
| 2522 | |
| 2523 | assert(obj != NULL); |
| 2524 | assert(self->proto > 0); |
| 2525 | assert(PyList_CheckExact(obj)); |
| 2526 | |
| 2527 | if (PyList_GET_SIZE(obj) == 1) { |
| 2528 | item = PyList_GET_ITEM(obj, 0); |
| 2529 | if (save(self, item, 0) < 0) |
| 2530 | return -1; |
| 2531 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2532 | return -1; |
| 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | /* Write in batches of BATCHSIZE. */ |
| 2537 | total = 0; |
| 2538 | do { |
| 2539 | this_batch = 0; |
| 2540 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2541 | return -1; |
| 2542 | while (total < PyList_GET_SIZE(obj)) { |
| 2543 | item = PyList_GET_ITEM(obj, total); |
| 2544 | if (save(self, item, 0) < 0) |
| 2545 | return -1; |
| 2546 | total++; |
| 2547 | if (++this_batch == BATCHSIZE) |
| 2548 | break; |
| 2549 | } |
| 2550 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2551 | return -1; |
| 2552 | |
| 2553 | } while (total < PyList_GET_SIZE(obj)); |
| 2554 | |
| 2555 | return 0; |
| 2556 | } |
| 2557 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2558 | static int |
| 2559 | save_list(PicklerObject *self, PyObject *obj) |
| 2560 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2561 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2562 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2563 | int status = 0; |
| 2564 | |
| 2565 | if (self->fast && !fast_save_enter(self, obj)) |
| 2566 | goto error; |
| 2567 | |
| 2568 | /* Create an empty list. */ |
| 2569 | if (self->bin) { |
| 2570 | header[0] = EMPTY_LIST; |
| 2571 | len = 1; |
| 2572 | } |
| 2573 | else { |
| 2574 | header[0] = MARK; |
| 2575 | header[1] = LIST; |
| 2576 | len = 2; |
| 2577 | } |
| 2578 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2579 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2580 | goto error; |
| 2581 | |
| 2582 | /* Get list length, and bow out early if empty. */ |
| 2583 | if ((len = PyList_Size(obj)) < 0) |
| 2584 | goto error; |
| 2585 | |
| 2586 | if (memo_put(self, obj) < 0) |
| 2587 | goto error; |
| 2588 | |
| 2589 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2590 | /* Materialize the list elements. */ |
| 2591 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2592 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2593 | goto error; |
| 2594 | status = batch_list_exact(self, obj); |
| 2595 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2596 | } else { |
| 2597 | PyObject *iter = PyObject_GetIter(obj); |
| 2598 | if (iter == NULL) |
| 2599 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2600 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2601 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2602 | Py_DECREF(iter); |
| 2603 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2604 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2605 | status = batch_list(self, iter); |
| 2606 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2607 | Py_DECREF(iter); |
| 2608 | } |
| 2609 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2610 | if (0) { |
| 2611 | error: |
| 2612 | status = -1; |
| 2613 | } |
| 2614 | |
| 2615 | if (self->fast && !fast_save_leave(self, obj)) |
| 2616 | status = -1; |
| 2617 | |
| 2618 | return status; |
| 2619 | } |
| 2620 | |
| 2621 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2622 | * MARK key value ... key value SETITEMS |
| 2623 | * opcode sequences. Calling code should have arranged to first create an |
| 2624 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2625 | * Returns 0 on success, <0 on error. |
| 2626 | * |
| 2627 | * This is very much like batch_list(). The difference between saving |
| 2628 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2629 | * the C level, though, that attempts to combine these routines were too |
| 2630 | * ugly to bear. |
| 2631 | */ |
| 2632 | static int |
| 2633 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2634 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2635 | PyObject *obj = NULL; |
| 2636 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2637 | int i, n; |
| 2638 | |
| 2639 | const char mark_op = MARK; |
| 2640 | const char setitem_op = SETITEM; |
| 2641 | const char setitems_op = SETITEMS; |
| 2642 | |
| 2643 | assert(iter != NULL); |
| 2644 | |
| 2645 | if (self->proto == 0) { |
| 2646 | /* SETITEMS isn't available; do one at a time. */ |
| 2647 | for (;;) { |
| 2648 | obj = PyIter_Next(iter); |
| 2649 | if (obj == NULL) { |
| 2650 | if (PyErr_Occurred()) |
| 2651 | return -1; |
| 2652 | break; |
| 2653 | } |
| 2654 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2655 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2656 | "iterator must return 2-tuples"); |
| 2657 | return -1; |
| 2658 | } |
| 2659 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2660 | if (i >= 0) |
| 2661 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2662 | Py_DECREF(obj); |
| 2663 | if (i < 0) |
| 2664 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2665 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2666 | return -1; |
| 2667 | } |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
| 2671 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2672 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2673 | /* Get first item */ |
| 2674 | firstitem = PyIter_Next(iter); |
| 2675 | if (firstitem == NULL) { |
| 2676 | if (PyErr_Occurred()) |
| 2677 | goto error; |
| 2678 | |
| 2679 | /* nothing more to add */ |
| 2680 | break; |
| 2681 | } |
| 2682 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2683 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2684 | "iterator must return 2-tuples"); |
| 2685 | goto error; |
| 2686 | } |
| 2687 | |
| 2688 | /* Try to get a second item */ |
| 2689 | obj = PyIter_Next(iter); |
| 2690 | if (obj == NULL) { |
| 2691 | if (PyErr_Occurred()) |
| 2692 | goto error; |
| 2693 | |
| 2694 | /* Only one item to write */ |
| 2695 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2696 | goto error; |
| 2697 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2698 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2699 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2700 | goto error; |
| 2701 | Py_CLEAR(firstitem); |
| 2702 | break; |
| 2703 | } |
| 2704 | |
| 2705 | /* More than one item to write */ |
| 2706 | |
| 2707 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2708 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2709 | goto error; |
| 2710 | |
| 2711 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2712 | goto error; |
| 2713 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2714 | goto error; |
| 2715 | Py_CLEAR(firstitem); |
| 2716 | n = 1; |
| 2717 | |
| 2718 | /* Fetch and save up to BATCHSIZE items */ |
| 2719 | while (obj) { |
| 2720 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2721 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2722 | "iterator must return 2-tuples"); |
| 2723 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2724 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2725 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2726 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2727 | goto error; |
| 2728 | Py_CLEAR(obj); |
| 2729 | n += 1; |
| 2730 | |
| 2731 | if (n == BATCHSIZE) |
| 2732 | break; |
| 2733 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2734 | obj = PyIter_Next(iter); |
| 2735 | if (obj == NULL) { |
| 2736 | if (PyErr_Occurred()) |
| 2737 | goto error; |
| 2738 | break; |
| 2739 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2740 | } |
| 2741 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2742 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2743 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2744 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2745 | } while (n == BATCHSIZE); |
| 2746 | return 0; |
| 2747 | |
| 2748 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2749 | Py_XDECREF(firstitem); |
| 2750 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2751 | return -1; |
| 2752 | } |
| 2753 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2754 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2755 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2756 | * MARK key value ... key value SETITEMS |
| 2757 | * opcode sequences. Calling code should have arranged to first create an |
| 2758 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2759 | * Returns 0 on success, -1 on error. |
| 2760 | * |
| 2761 | * Note that this currently doesn't work for protocol 0. |
| 2762 | */ |
| 2763 | static int |
| 2764 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2765 | { |
| 2766 | PyObject *key = NULL, *value = NULL; |
| 2767 | int i; |
| 2768 | Py_ssize_t dict_size, ppos = 0; |
| 2769 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2770 | const char mark_op = MARK; |
| 2771 | const char setitem_op = SETITEM; |
| 2772 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2773 | |
| 2774 | assert(obj != NULL); |
| 2775 | assert(self->proto > 0); |
| 2776 | |
| 2777 | dict_size = PyDict_Size(obj); |
| 2778 | |
| 2779 | /* Special-case len(d) == 1 to save space. */ |
| 2780 | if (dict_size == 1) { |
| 2781 | PyDict_Next(obj, &ppos, &key, &value); |
| 2782 | if (save(self, key, 0) < 0) |
| 2783 | return -1; |
| 2784 | if (save(self, value, 0) < 0) |
| 2785 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2786 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2787 | return -1; |
| 2788 | return 0; |
| 2789 | } |
| 2790 | |
| 2791 | /* Write in batches of BATCHSIZE. */ |
| 2792 | do { |
| 2793 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2794 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2795 | return -1; |
| 2796 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2797 | if (save(self, key, 0) < 0) |
| 2798 | return -1; |
| 2799 | if (save(self, value, 0) < 0) |
| 2800 | return -1; |
| 2801 | if (++i == BATCHSIZE) |
| 2802 | break; |
| 2803 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2804 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2805 | return -1; |
| 2806 | if (PyDict_Size(obj) != dict_size) { |
| 2807 | PyErr_Format( |
| 2808 | PyExc_RuntimeError, |
| 2809 | "dictionary changed size during iteration"); |
| 2810 | return -1; |
| 2811 | } |
| 2812 | |
| 2813 | } while (i == BATCHSIZE); |
| 2814 | return 0; |
| 2815 | } |
| 2816 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2817 | static int |
| 2818 | save_dict(PicklerObject *self, PyObject *obj) |
| 2819 | { |
| 2820 | PyObject *items, *iter; |
| 2821 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2822 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2823 | int status = 0; |
| 2824 | |
| 2825 | if (self->fast && !fast_save_enter(self, obj)) |
| 2826 | goto error; |
| 2827 | |
| 2828 | /* Create an empty dict. */ |
| 2829 | if (self->bin) { |
| 2830 | header[0] = EMPTY_DICT; |
| 2831 | len = 1; |
| 2832 | } |
| 2833 | else { |
| 2834 | header[0] = MARK; |
| 2835 | header[1] = DICT; |
| 2836 | len = 2; |
| 2837 | } |
| 2838 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2839 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2840 | goto error; |
| 2841 | |
| 2842 | /* Get dict size, and bow out early if empty. */ |
| 2843 | if ((len = PyDict_Size(obj)) < 0) |
| 2844 | goto error; |
| 2845 | |
| 2846 | if (memo_put(self, obj) < 0) |
| 2847 | goto error; |
| 2848 | |
| 2849 | if (len != 0) { |
| 2850 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2851 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2852 | /* We can take certain shortcuts if we know this is a dict and |
| 2853 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2854 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2855 | goto error; |
| 2856 | status = batch_dict_exact(self, obj); |
| 2857 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2858 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2859 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2860 | |
| 2861 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2862 | if (items == NULL) |
| 2863 | goto error; |
| 2864 | iter = PyObject_GetIter(items); |
| 2865 | Py_DECREF(items); |
| 2866 | if (iter == NULL) |
| 2867 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2868 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2869 | Py_DECREF(iter); |
| 2870 | goto error; |
| 2871 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2872 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2873 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2874 | Py_DECREF(iter); |
| 2875 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2876 | } |
| 2877 | |
| 2878 | if (0) { |
| 2879 | error: |
| 2880 | status = -1; |
| 2881 | } |
| 2882 | |
| 2883 | if (self->fast && !fast_save_leave(self, obj)) |
| 2884 | status = -1; |
| 2885 | |
| 2886 | return status; |
| 2887 | } |
| 2888 | |
| 2889 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2890 | save_set(PicklerObject *self, PyObject *obj) |
| 2891 | { |
| 2892 | PyObject *item; |
| 2893 | int i; |
| 2894 | Py_ssize_t set_size, ppos = 0; |
| 2895 | Py_hash_t hash; |
| 2896 | |
| 2897 | const char empty_set_op = EMPTY_SET; |
| 2898 | const char mark_op = MARK; |
| 2899 | const char additems_op = ADDITEMS; |
| 2900 | |
| 2901 | if (self->proto < 4) { |
| 2902 | PyObject *items; |
| 2903 | PyObject *reduce_value; |
| 2904 | int status; |
| 2905 | |
| 2906 | items = PySequence_List(obj); |
| 2907 | if (items == NULL) { |
| 2908 | return -1; |
| 2909 | } |
| 2910 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2911 | Py_DECREF(items); |
| 2912 | if (reduce_value == NULL) { |
| 2913 | return -1; |
| 2914 | } |
| 2915 | /* save_reduce() will memoize the object automatically. */ |
| 2916 | status = save_reduce(self, reduce_value, obj); |
| 2917 | Py_DECREF(reduce_value); |
| 2918 | return status; |
| 2919 | } |
| 2920 | |
| 2921 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2922 | return -1; |
| 2923 | |
| 2924 | if (memo_put(self, obj) < 0) |
| 2925 | return -1; |
| 2926 | |
| 2927 | set_size = PySet_GET_SIZE(obj); |
| 2928 | if (set_size == 0) |
| 2929 | return 0; /* nothing to do */ |
| 2930 | |
| 2931 | /* Write in batches of BATCHSIZE. */ |
| 2932 | do { |
| 2933 | i = 0; |
| 2934 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2935 | return -1; |
| 2936 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2937 | if (save(self, item, 0) < 0) |
| 2938 | return -1; |
| 2939 | if (++i == BATCHSIZE) |
| 2940 | break; |
| 2941 | } |
| 2942 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2943 | return -1; |
| 2944 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2945 | PyErr_Format( |
| 2946 | PyExc_RuntimeError, |
| 2947 | "set changed size during iteration"); |
| 2948 | return -1; |
| 2949 | } |
| 2950 | } while (i == BATCHSIZE); |
| 2951 | |
| 2952 | return 0; |
| 2953 | } |
| 2954 | |
| 2955 | static int |
| 2956 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2957 | { |
| 2958 | PyObject *iter; |
| 2959 | |
| 2960 | const char mark_op = MARK; |
| 2961 | const char frozenset_op = FROZENSET; |
| 2962 | |
| 2963 | if (self->fast && !fast_save_enter(self, obj)) |
| 2964 | return -1; |
| 2965 | |
| 2966 | if (self->proto < 4) { |
| 2967 | PyObject *items; |
| 2968 | PyObject *reduce_value; |
| 2969 | int status; |
| 2970 | |
| 2971 | items = PySequence_List(obj); |
| 2972 | if (items == NULL) { |
| 2973 | return -1; |
| 2974 | } |
| 2975 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2976 | items); |
| 2977 | Py_DECREF(items); |
| 2978 | if (reduce_value == NULL) { |
| 2979 | return -1; |
| 2980 | } |
| 2981 | /* save_reduce() will memoize the object automatically. */ |
| 2982 | status = save_reduce(self, reduce_value, obj); |
| 2983 | Py_DECREF(reduce_value); |
| 2984 | return status; |
| 2985 | } |
| 2986 | |
| 2987 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2988 | return -1; |
| 2989 | |
| 2990 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2991 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2992 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2993 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2994 | for (;;) { |
| 2995 | PyObject *item; |
| 2996 | |
| 2997 | item = PyIter_Next(iter); |
| 2998 | if (item == NULL) { |
| 2999 | if (PyErr_Occurred()) { |
| 3000 | Py_DECREF(iter); |
| 3001 | return -1; |
| 3002 | } |
| 3003 | break; |
| 3004 | } |
| 3005 | if (save(self, item, 0) < 0) { |
| 3006 | Py_DECREF(item); |
| 3007 | Py_DECREF(iter); |
| 3008 | return -1; |
| 3009 | } |
| 3010 | Py_DECREF(item); |
| 3011 | } |
| 3012 | Py_DECREF(iter); |
| 3013 | |
| 3014 | /* If the object is already in the memo, this means it is |
| 3015 | recursive. In this case, throw away everything we put on the |
| 3016 | stack, and fetch the object back from the memo. */ |
| 3017 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3018 | const char pop_mark_op = POP_MARK; |
| 3019 | |
| 3020 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3021 | return -1; |
| 3022 | if (memo_get(self, obj) < 0) |
| 3023 | return -1; |
| 3024 | return 0; |
| 3025 | } |
| 3026 | |
| 3027 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3028 | return -1; |
| 3029 | if (memo_put(self, obj) < 0) |
| 3030 | return -1; |
| 3031 | |
| 3032 | return 0; |
| 3033 | } |
| 3034 | |
| 3035 | static int |
| 3036 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3037 | { |
| 3038 | PyObject *key; |
| 3039 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3040 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3041 | |
| 3042 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3043 | if (key == NULL) |
| 3044 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3045 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3046 | Py_DECREF(key); |
| 3047 | if (item) { |
| 3048 | PyObject *fixed_module_name; |
| 3049 | PyObject *fixed_global_name; |
| 3050 | |
| 3051 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3052 | PyErr_Format(PyExc_RuntimeError, |
| 3053 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3054 | "should be 2-tuples, not %.200s", |
| 3055 | Py_TYPE(item)->tp_name); |
| 3056 | return -1; |
| 3057 | } |
| 3058 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3059 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3060 | if (!PyUnicode_Check(fixed_module_name) || |
| 3061 | !PyUnicode_Check(fixed_global_name)) { |
| 3062 | PyErr_Format(PyExc_RuntimeError, |
| 3063 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3064 | "should be pairs of str, not (%.200s, %.200s)", |
| 3065 | Py_TYPE(fixed_module_name)->tp_name, |
| 3066 | Py_TYPE(fixed_global_name)->tp_name); |
| 3067 | return -1; |
| 3068 | } |
| 3069 | |
| 3070 | Py_CLEAR(*module_name); |
| 3071 | Py_CLEAR(*global_name); |
| 3072 | Py_INCREF(fixed_module_name); |
| 3073 | Py_INCREF(fixed_global_name); |
| 3074 | *module_name = fixed_module_name; |
| 3075 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3076 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3077 | } |
| 3078 | else if (PyErr_Occurred()) { |
| 3079 | return -1; |
| 3080 | } |
| 3081 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3082 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3083 | if (item) { |
| 3084 | if (!PyUnicode_Check(item)) { |
| 3085 | PyErr_Format(PyExc_RuntimeError, |
| 3086 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3087 | "should be strings, not %.200s", |
| 3088 | Py_TYPE(item)->tp_name); |
| 3089 | return -1; |
| 3090 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3091 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3092 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3093 | } |
| 3094 | else if (PyErr_Occurred()) { |
| 3095 | return -1; |
| 3096 | } |
| 3097 | |
| 3098 | return 0; |
| 3099 | } |
| 3100 | |
| 3101 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3102 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3103 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3104 | PyObject *global_name = NULL; |
| 3105 | PyObject *module_name = NULL; |
| 3106 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3107 | PyObject *parent = NULL; |
| 3108 | PyObject *dotted_path = NULL; |
| 3109 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3110 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3111 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3112 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3113 | _Py_IDENTIFIER(__name__); |
| 3114 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3115 | |
| 3116 | const char global_op = GLOBAL; |
| 3117 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3118 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3119 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3120 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3121 | } |
| 3122 | else { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3123 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3124 | if (global_name == NULL) { |
| 3125 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3126 | goto error; |
| 3127 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3128 | } |
| 3129 | if (global_name == NULL) { |
| 3130 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3131 | if (global_name == NULL) |
| 3132 | goto error; |
| 3133 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3134 | } |
| 3135 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3136 | dotted_path = get_dotted_path(module, global_name); |
| 3137 | if (dotted_path == NULL) |
| 3138 | goto error; |
| 3139 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3140 | if (module_name == NULL) |
| 3141 | goto error; |
| 3142 | |
| 3143 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3144 | relative imports. |
| 3145 | |
| 3146 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3147 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3148 | custom import functions (IMHO, this would be a nice security |
| 3149 | feature). The import C API would need to be extended to support the |
| 3150 | extra parameters of __import__ to fix that. */ |
| 3151 | module = PyImport_Import(module_name); |
| 3152 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3153 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3154 | "Can't pickle %R: import of module %R failed", |
| 3155 | obj, module_name); |
| 3156 | goto error; |
| 3157 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3158 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3159 | Py_INCREF(lastname); |
| 3160 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3161 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3162 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3163 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3164 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3165 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3166 | goto error; |
| 3167 | } |
| 3168 | if (cls != obj) { |
| 3169 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3170 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3171 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3172 | obj, module_name, global_name); |
| 3173 | goto error; |
| 3174 | } |
| 3175 | Py_DECREF(cls); |
| 3176 | |
| 3177 | if (self->proto >= 2) { |
| 3178 | /* See whether this is in the extension registry, and if |
| 3179 | * so generate an EXT opcode. |
| 3180 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3181 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3182 | PyObject *code_obj; /* extension code as Python object */ |
| 3183 | long code; /* extension code as C value */ |
| 3184 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3185 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3186 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3187 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3188 | if (extension_key == NULL) { |
| 3189 | goto error; |
| 3190 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3191 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3192 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3193 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3194 | /* The object is not registered in the extension registry. |
| 3195 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3196 | if (code_obj == NULL) { |
| 3197 | if (PyErr_Occurred()) { |
| 3198 | goto error; |
| 3199 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3200 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3201 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3202 | |
| 3203 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3204 | of the value returned by the extension_registry. It should for |
| 3205 | consistency. */ |
| 3206 | |
| 3207 | /* Verify code_obj has the right type and value. */ |
| 3208 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3209 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3210 | "Can't pickle %R: extension code %R isn't an integer", |
| 3211 | obj, code_obj); |
| 3212 | goto error; |
| 3213 | } |
| 3214 | code = PyLong_AS_LONG(code_obj); |
| 3215 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3216 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3217 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3218 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3219 | goto error; |
| 3220 | } |
| 3221 | |
| 3222 | /* Generate an EXT opcode. */ |
| 3223 | if (code <= 0xff) { |
| 3224 | pdata[0] = EXT1; |
| 3225 | pdata[1] = (unsigned char)code; |
| 3226 | n = 2; |
| 3227 | } |
| 3228 | else if (code <= 0xffff) { |
| 3229 | pdata[0] = EXT2; |
| 3230 | pdata[1] = (unsigned char)(code & 0xff); |
| 3231 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3232 | n = 3; |
| 3233 | } |
| 3234 | else { |
| 3235 | pdata[0] = EXT4; |
| 3236 | pdata[1] = (unsigned char)(code & 0xff); |
| 3237 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3238 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3239 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3240 | n = 5; |
| 3241 | } |
| 3242 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3243 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3244 | goto error; |
| 3245 | } |
| 3246 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3247 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3248 | if (parent == module) { |
| 3249 | Py_INCREF(lastname); |
| 3250 | Py_DECREF(global_name); |
| 3251 | global_name = lastname; |
| 3252 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3253 | if (self->proto >= 4) { |
| 3254 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3255 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3256 | if (save(self, module_name, 0) < 0) |
| 3257 | goto error; |
| 3258 | if (save(self, global_name, 0) < 0) |
| 3259 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3260 | |
| 3261 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3262 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3263 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3264 | else if (parent != module) { |
| 3265 | PickleState *st = _Pickle_GetGlobalState(); |
| 3266 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3267 | st->getattr, parent, lastname); |
| 3268 | status = save_reduce(self, reduce_value, NULL); |
| 3269 | Py_DECREF(reduce_value); |
| 3270 | if (status < 0) |
| 3271 | goto error; |
| 3272 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3273 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3274 | /* Generate a normal global opcode if we are using a pickle |
| 3275 | protocol < 4, or if the object is not registered in the |
| 3276 | extension registry. */ |
| 3277 | PyObject *encoded; |
| 3278 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3279 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3280 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3281 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3282 | |
| 3283 | /* For protocol < 3 and if the user didn't request against doing |
| 3284 | so, we convert module names to the old 2.x module names. */ |
| 3285 | if (self->proto < 3 && self->fix_imports) { |
| 3286 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3287 | goto error; |
| 3288 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3289 | } |
| 3290 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3291 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3292 | both the module name and the global name using UTF-8. We do so |
| 3293 | only when we are using the pickle protocol newer than version |
| 3294 | 3. This is to ensure compatibility with older Unpickler running |
| 3295 | on Python 2.x. */ |
| 3296 | if (self->proto == 3) { |
| 3297 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3298 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3299 | else { |
| 3300 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3301 | } |
| 3302 | encoded = unicode_encoder(module_name); |
| 3303 | if (encoded == NULL) { |
| 3304 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3305 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3306 | "can't pickle module identifier '%S' using " |
| 3307 | "pickle protocol %i", |
| 3308 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3309 | goto error; |
| 3310 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3311 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3312 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3313 | Py_DECREF(encoded); |
| 3314 | goto error; |
| 3315 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3316 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3317 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3318 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3319 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3320 | /* Save the name of the module. */ |
| 3321 | encoded = unicode_encoder(global_name); |
| 3322 | if (encoded == NULL) { |
| 3323 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3324 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3325 | "can't pickle global identifier '%S' using " |
| 3326 | "pickle protocol %i", |
| 3327 | global_name, self->proto); |
| 3328 | goto error; |
| 3329 | } |
| 3330 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3331 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3332 | Py_DECREF(encoded); |
| 3333 | goto error; |
| 3334 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3335 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3336 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3337 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3338 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3339 | /* Memoize the object. */ |
| 3340 | if (memo_put(self, obj) < 0) |
| 3341 | goto error; |
| 3342 | } |
| 3343 | |
| 3344 | if (0) { |
| 3345 | error: |
| 3346 | status = -1; |
| 3347 | } |
| 3348 | Py_XDECREF(module_name); |
| 3349 | Py_XDECREF(global_name); |
| 3350 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3351 | Py_XDECREF(parent); |
| 3352 | Py_XDECREF(dotted_path); |
| 3353 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3354 | |
| 3355 | return status; |
| 3356 | } |
| 3357 | |
| 3358 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3359 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3360 | { |
| 3361 | PyObject *reduce_value; |
| 3362 | int status; |
| 3363 | |
| 3364 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3365 | if (reduce_value == NULL) { |
| 3366 | return -1; |
| 3367 | } |
| 3368 | status = save_reduce(self, reduce_value, obj); |
| 3369 | Py_DECREF(reduce_value); |
| 3370 | return status; |
| 3371 | } |
| 3372 | |
| 3373 | static int |
| 3374 | save_type(PicklerObject *self, PyObject *obj) |
| 3375 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3376 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3377 | return save_singleton_type(self, obj, Py_None); |
| 3378 | } |
| 3379 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3380 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3381 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3382 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3383 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3384 | } |
| 3385 | return save_global(self, obj, NULL); |
| 3386 | } |
| 3387 | |
| 3388 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3389 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3390 | { |
| 3391 | PyObject *pid = NULL; |
| 3392 | int status = 0; |
| 3393 | |
| 3394 | const char persid_op = PERSID; |
| 3395 | const char binpersid_op = BINPERSID; |
| 3396 | |
| 3397 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3398 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3399 | if (pid == NULL) |
| 3400 | return -1; |
| 3401 | |
| 3402 | if (pid != Py_None) { |
| 3403 | if (self->bin) { |
| 3404 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3405 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3406 | goto error; |
| 3407 | } |
| 3408 | else { |
| 3409 | PyObject *pid_str = NULL; |
| 3410 | char *pid_ascii_bytes; |
| 3411 | Py_ssize_t size; |
| 3412 | |
| 3413 | pid_str = PyObject_Str(pid); |
| 3414 | if (pid_str == NULL) |
| 3415 | goto error; |
| 3416 | |
| 3417 | /* XXX: Should it check whether the persistent id only contains |
| 3418 | ASCII characters? And what if the pid contains embedded |
| 3419 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3420 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3421 | Py_DECREF(pid_str); |
| 3422 | if (pid_ascii_bytes == NULL) |
| 3423 | goto error; |
| 3424 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3425 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3426 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3427 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3428 | goto error; |
| 3429 | } |
| 3430 | status = 1; |
| 3431 | } |
| 3432 | |
| 3433 | if (0) { |
| 3434 | error: |
| 3435 | status = -1; |
| 3436 | } |
| 3437 | Py_XDECREF(pid); |
| 3438 | |
| 3439 | return status; |
| 3440 | } |
| 3441 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3442 | static PyObject * |
| 3443 | get_class(PyObject *obj) |
| 3444 | { |
| 3445 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3446 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3447 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3448 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3449 | if (cls == NULL) { |
| 3450 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3451 | PyErr_Clear(); |
| 3452 | cls = (PyObject *) Py_TYPE(obj); |
| 3453 | Py_INCREF(cls); |
| 3454 | } |
| 3455 | } |
| 3456 | return cls; |
| 3457 | } |
| 3458 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3459 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3460 | * appropriate __reduce__ method for obj. |
| 3461 | */ |
| 3462 | static int |
| 3463 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3464 | { |
| 3465 | PyObject *callable; |
| 3466 | PyObject *argtup; |
| 3467 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3468 | PyObject *listitems = Py_None; |
| 3469 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3470 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3471 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3472 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3473 | |
| 3474 | const char reduce_op = REDUCE; |
| 3475 | const char build_op = BUILD; |
| 3476 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3477 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3478 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3479 | size = PyTuple_Size(args); |
| 3480 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3481 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3482 | "__reduce__ must contain 2 through 5 elements"); |
| 3483 | return -1; |
| 3484 | } |
| 3485 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3486 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3487 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3488 | return -1; |
| 3489 | |
| 3490 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3491 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3492 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3493 | return -1; |
| 3494 | } |
| 3495 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3496 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3497 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3498 | return -1; |
| 3499 | } |
| 3500 | |
| 3501 | if (state == Py_None) |
| 3502 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3503 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3504 | if (listitems == Py_None) |
| 3505 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3506 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3507 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3508 | "returned by __reduce__ must be an iterator, not %s", |
| 3509 | Py_TYPE(listitems)->tp_name); |
| 3510 | return -1; |
| 3511 | } |
| 3512 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3513 | if (dictitems == Py_None) |
| 3514 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3515 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3516 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3517 | "returned by __reduce__ must be an iterator, not %s", |
| 3518 | Py_TYPE(dictitems)->tp_name); |
| 3519 | return -1; |
| 3520 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3521 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3522 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3523 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3524 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3525 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3526 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3527 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3528 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3529 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3530 | } |
| 3531 | PyErr_Clear(); |
| 3532 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3533 | else if (PyUnicode_Check(name)) { |
| 3534 | if (self->proto >= 4) { |
| 3535 | _Py_IDENTIFIER(__newobj_ex__); |
| 3536 | use_newobj_ex = PyUnicode_Compare( |
| 3537 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3538 | } |
| 3539 | if (!use_newobj_ex) { |
| 3540 | _Py_IDENTIFIER(__newobj__); |
| 3541 | use_newobj = PyUnicode_Compare( |
| 3542 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
| 3543 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3544 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3545 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3546 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3547 | |
| 3548 | if (use_newobj_ex) { |
| 3549 | PyObject *cls; |
| 3550 | PyObject *args; |
| 3551 | PyObject *kwargs; |
| 3552 | |
| 3553 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3554 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3555 | "length of the NEWOBJ_EX argument tuple must be " |
| 3556 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3557 | return -1; |
| 3558 | } |
| 3559 | |
| 3560 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3561 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3562 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3563 | "first item from NEWOBJ_EX argument tuple must " |
| 3564 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3565 | return -1; |
| 3566 | } |
| 3567 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3568 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3569 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3570 | "second item from NEWOBJ_EX argument tuple must " |
| 3571 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3572 | return -1; |
| 3573 | } |
| 3574 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3575 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3576 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3577 | "third item from NEWOBJ_EX argument tuple must " |
| 3578 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3579 | return -1; |
| 3580 | } |
| 3581 | |
| 3582 | if (save(self, cls, 0) < 0 || |
| 3583 | save(self, args, 0) < 0 || |
| 3584 | save(self, kwargs, 0) < 0 || |
| 3585 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3586 | return -1; |
| 3587 | } |
| 3588 | } |
| 3589 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3590 | PyObject *cls; |
| 3591 | PyObject *newargtup; |
| 3592 | PyObject *obj_class; |
| 3593 | int p; |
| 3594 | |
| 3595 | /* Sanity checks. */ |
| 3596 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3597 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3598 | return -1; |
| 3599 | } |
| 3600 | |
| 3601 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3602 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3603 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3604 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3605 | return -1; |
| 3606 | } |
| 3607 | |
| 3608 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3609 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3610 | p = obj_class != cls; /* true iff a problem */ |
| 3611 | Py_DECREF(obj_class); |
| 3612 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3613 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3614 | "__newobj__ args has the wrong class"); |
| 3615 | return -1; |
| 3616 | } |
| 3617 | } |
| 3618 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3619 | what happen if the value returned by the __reduce__() method of |
| 3620 | some extension type contains another object of the same type. Ouch! |
| 3621 | |
| 3622 | Here is a quick example, that I ran into, to illustrate what I |
| 3623 | mean: |
| 3624 | |
| 3625 | >>> import pickle, copyreg |
| 3626 | >>> copyreg.dispatch_table.pop(complex) |
| 3627 | >>> pickle.dumps(1+2j) |
| 3628 | Traceback (most recent call last): |
| 3629 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3630 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3631 | |
| 3632 | Removing the complex class from copyreg.dispatch_table made the |
| 3633 | __reduce_ex__() method emit another complex object: |
| 3634 | |
| 3635 | >>> (1+1j).__reduce_ex__(2) |
| 3636 | (<function __newobj__ at 0xb7b71c3c>, |
| 3637 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3638 | |
| 3639 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3640 | ensued. Of course, the bug was in the complex class which had a |
| 3641 | broken __getnewargs__() that emitted another complex object. But, |
| 3642 | the point, here, is it is quite easy to end up with a broken reduce |
| 3643 | function. */ |
| 3644 | |
| 3645 | /* Save the class and its __new__ arguments. */ |
| 3646 | if (save(self, cls, 0) < 0) |
| 3647 | return -1; |
| 3648 | |
| 3649 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3650 | if (newargtup == NULL) |
| 3651 | return -1; |
| 3652 | |
| 3653 | p = save(self, newargtup, 0); |
| 3654 | Py_DECREF(newargtup); |
| 3655 | if (p < 0) |
| 3656 | return -1; |
| 3657 | |
| 3658 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3659 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3660 | return -1; |
| 3661 | } |
| 3662 | else { /* Not using NEWOBJ. */ |
| 3663 | if (save(self, callable, 0) < 0 || |
| 3664 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3665 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3666 | return -1; |
| 3667 | } |
| 3668 | |
| 3669 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3670 | the caller do not want to memoize the object. Not particularly useful, |
| 3671 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3672 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3673 | if (obj != NULL) { |
| 3674 | /* If the object is already in the memo, this means it is |
| 3675 | recursive. In this case, throw away everything we put on the |
| 3676 | stack, and fetch the object back from the memo. */ |
| 3677 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3678 | const char pop_op = POP; |
| 3679 | |
| 3680 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3681 | return -1; |
| 3682 | if (memo_get(self, obj) < 0) |
| 3683 | return -1; |
| 3684 | |
| 3685 | return 0; |
| 3686 | } |
| 3687 | else if (memo_put(self, obj) < 0) |
| 3688 | return -1; |
| 3689 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3690 | |
| 3691 | if (listitems && batch_list(self, listitems) < 0) |
| 3692 | return -1; |
| 3693 | |
| 3694 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3695 | return -1; |
| 3696 | |
| 3697 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3698 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3699 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3700 | return -1; |
| 3701 | } |
| 3702 | |
| 3703 | return 0; |
| 3704 | } |
| 3705 | |
| 3706 | static int |
| 3707 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3708 | { |
| 3709 | PyTypeObject *type; |
| 3710 | PyObject *reduce_func = NULL; |
| 3711 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3712 | int status = 0; |
| 3713 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3714 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3715 | return -1; |
| 3716 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3717 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3718 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3719 | |
| 3720 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3721 | on its returned object. */ |
| 3722 | if (!pers_save && self->pers_func) { |
| 3723 | /* save_pers() returns: |
| 3724 | -1 to signal an error; |
| 3725 | 0 if it did nothing successfully; |
| 3726 | 1 if a persistent id was saved. |
| 3727 | */ |
| 3728 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3729 | goto done; |
| 3730 | } |
| 3731 | |
| 3732 | type = Py_TYPE(obj); |
| 3733 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3734 | /* The old cPickle had an optimization that used switch-case statement |
| 3735 | dispatching on the first letter of the type name. This has was removed |
| 3736 | since benchmarks shown that this optimization was actually slowing |
| 3737 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3738 | |
| 3739 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3740 | |
| 3741 | if (obj == Py_None) { |
| 3742 | status = save_none(self, obj); |
| 3743 | goto done; |
| 3744 | } |
| 3745 | else if (obj == Py_False || obj == Py_True) { |
| 3746 | status = save_bool(self, obj); |
| 3747 | goto done; |
| 3748 | } |
| 3749 | else if (type == &PyLong_Type) { |
| 3750 | status = save_long(self, obj); |
| 3751 | goto done; |
| 3752 | } |
| 3753 | else if (type == &PyFloat_Type) { |
| 3754 | status = save_float(self, obj); |
| 3755 | goto done; |
| 3756 | } |
| 3757 | |
| 3758 | /* Check the memo to see if it has the object. If so, generate |
| 3759 | a GET (or BINGET) opcode, instead of pickling the object |
| 3760 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3761 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3762 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3763 | goto error; |
| 3764 | goto done; |
| 3765 | } |
| 3766 | |
| 3767 | if (type == &PyBytes_Type) { |
| 3768 | status = save_bytes(self, obj); |
| 3769 | goto done; |
| 3770 | } |
| 3771 | else if (type == &PyUnicode_Type) { |
| 3772 | status = save_unicode(self, obj); |
| 3773 | goto done; |
| 3774 | } |
| 3775 | else if (type == &PyDict_Type) { |
| 3776 | status = save_dict(self, obj); |
| 3777 | goto done; |
| 3778 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3779 | else if (type == &PySet_Type) { |
| 3780 | status = save_set(self, obj); |
| 3781 | goto done; |
| 3782 | } |
| 3783 | else if (type == &PyFrozenSet_Type) { |
| 3784 | status = save_frozenset(self, obj); |
| 3785 | goto done; |
| 3786 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3787 | else if (type == &PyList_Type) { |
| 3788 | status = save_list(self, obj); |
| 3789 | goto done; |
| 3790 | } |
| 3791 | else if (type == &PyTuple_Type) { |
| 3792 | status = save_tuple(self, obj); |
| 3793 | goto done; |
| 3794 | } |
| 3795 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3796 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3797 | goto done; |
| 3798 | } |
| 3799 | else if (type == &PyFunction_Type) { |
| 3800 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3801 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3802 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3803 | |
| 3804 | /* XXX: This part needs some unit tests. */ |
| 3805 | |
| 3806 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3807 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3808 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3809 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3810 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3811 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3812 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3813 | (PyObject *)type); |
| 3814 | if (reduce_func == NULL) { |
| 3815 | if (PyErr_Occurred()) { |
| 3816 | goto error; |
| 3817 | } |
| 3818 | } else { |
| 3819 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3820 | Increase the reference count to be consistent with |
| 3821 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3822 | Py_INCREF(reduce_func); |
| 3823 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3824 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3825 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3826 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3827 | if (reduce_func == NULL) { |
| 3828 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3829 | PyErr_Clear(); |
| 3830 | else |
| 3831 | goto error; |
| 3832 | } |
| 3833 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3834 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3835 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3836 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3837 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3838 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3839 | status = save_global(self, obj, NULL); |
| 3840 | goto done; |
| 3841 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3842 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3843 | _Py_IDENTIFIER(__reduce__); |
| 3844 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3845 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3846 | |
| 3847 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3848 | automatically defined as __reduce__. While this is convenient, this |
| 3849 | make it impossible to know which method was actually called. Of |
| 3850 | course, this is not a big deal. But still, it would be nice to let |
| 3851 | the user know which method was called when something go |
| 3852 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3853 | don't actually have to check for a __reduce__ method. */ |
| 3854 | |
| 3855 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3856 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3857 | if (reduce_func != NULL) { |
| 3858 | PyObject *proto; |
| 3859 | proto = PyLong_FromLong(self->proto); |
| 3860 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3861 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3862 | } |
| 3863 | } |
| 3864 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3865 | PickleState *st = _Pickle_GetGlobalState(); |
| 3866 | |
| 3867 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3868 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3869 | } |
| 3870 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3871 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3872 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3873 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3874 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3875 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3876 | PyObject *empty_tuple = PyTuple_New(0); |
| 3877 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3878 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3879 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3880 | } |
| 3881 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3882 | PyErr_Format(st->PicklingError, |
| 3883 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3884 | type->tp_name, obj); |
| 3885 | goto error; |
| 3886 | } |
| 3887 | } |
| 3888 | } |
| 3889 | |
| 3890 | if (reduce_value == NULL) |
| 3891 | goto error; |
| 3892 | |
| 3893 | if (PyUnicode_Check(reduce_value)) { |
| 3894 | status = save_global(self, obj, reduce_value); |
| 3895 | goto done; |
| 3896 | } |
| 3897 | |
| 3898 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3899 | PickleState *st = _Pickle_GetGlobalState(); |
| 3900 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3901 | "__reduce__ must return a string or tuple"); |
| 3902 | goto error; |
| 3903 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3904 | |
| 3905 | status = save_reduce(self, reduce_value, obj); |
| 3906 | |
| 3907 | if (0) { |
| 3908 | error: |
| 3909 | status = -1; |
| 3910 | } |
| 3911 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3912 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3913 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3914 | Py_XDECREF(reduce_func); |
| 3915 | Py_XDECREF(reduce_value); |
| 3916 | |
| 3917 | return status; |
| 3918 | } |
| 3919 | |
| 3920 | static int |
| 3921 | dump(PicklerObject *self, PyObject *obj) |
| 3922 | { |
| 3923 | const char stop_op = STOP; |
| 3924 | |
| 3925 | if (self->proto >= 2) { |
| 3926 | char header[2]; |
| 3927 | |
| 3928 | header[0] = PROTO; |
| 3929 | assert(self->proto >= 0 && self->proto < 256); |
| 3930 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3931 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3932 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3933 | if (self->proto >= 4) |
| 3934 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3935 | } |
| 3936 | |
| 3937 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3938 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3939 | return -1; |
| 3940 | |
| 3941 | return 0; |
| 3942 | } |
| 3943 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3944 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3945 | |
| 3946 | _pickle.Pickler.clear_memo |
| 3947 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3948 | Clears the pickler's "memo". |
| 3949 | |
| 3950 | The memo is the data structure that remembers which objects the |
| 3951 | pickler has already seen, so that shared or recursive objects are |
| 3952 | pickled by reference and not by value. This method is useful when |
| 3953 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3954 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3955 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 3956 | static PyObject * |
| 3957 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3958 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3959 | { |
| 3960 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3961 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3962 | |
| 3963 | Py_RETURN_NONE; |
| 3964 | } |
| 3965 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3966 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3967 | |
| 3968 | _pickle.Pickler.dump |
| 3969 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3970 | obj: object |
| 3971 | / |
| 3972 | |
| 3973 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3974 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3975 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3976 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3977 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3978 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3979 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3980 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3981 | Developers often forget to call __init__() in their subclasses, which |
| 3982 | would trigger a segfault without this check. */ |
| 3983 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3984 | PickleState *st = _Pickle_GetGlobalState(); |
| 3985 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3986 | "Pickler.__init__() was not called by %s.__init__()", |
| 3987 | Py_TYPE(self)->tp_name); |
| 3988 | return NULL; |
| 3989 | } |
| 3990 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3991 | if (_Pickler_ClearBuffer(self) < 0) |
| 3992 | return NULL; |
| 3993 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3994 | if (dump(self, obj) < 0) |
| 3995 | return NULL; |
| 3996 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3997 | if (_Pickler_FlushToFile(self) < 0) |
| 3998 | return NULL; |
| 3999 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4000 | Py_RETURN_NONE; |
| 4001 | } |
| 4002 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4003 | /*[clinic input] |
| 4004 | |
| 4005 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4006 | |
| 4007 | Returns size in memory, in bytes. |
| 4008 | [clinic start generated code]*/ |
| 4009 | |
| 4010 | static Py_ssize_t |
| 4011 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4012 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4013 | { |
| 4014 | Py_ssize_t res, s; |
| 4015 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4016 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4017 | if (self->memo != NULL) { |
| 4018 | res += sizeof(PyMemoTable); |
| 4019 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4020 | } |
| 4021 | if (self->output_buffer != NULL) { |
| 4022 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4023 | if (s == -1) |
| 4024 | return -1; |
| 4025 | res += s; |
| 4026 | } |
| 4027 | return res; |
| 4028 | } |
| 4029 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4030 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4031 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4032 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4033 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4034 | {NULL, NULL} /* sentinel */ |
| 4035 | }; |
| 4036 | |
| 4037 | static void |
| 4038 | Pickler_dealloc(PicklerObject *self) |
| 4039 | { |
| 4040 | PyObject_GC_UnTrack(self); |
| 4041 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4042 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4043 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4044 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4045 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4046 | Py_XDECREF(self->fast_memo); |
| 4047 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4048 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4049 | |
| 4050 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4051 | } |
| 4052 | |
| 4053 | static int |
| 4054 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4055 | { |
| 4056 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4057 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4058 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4059 | Py_VISIT(self->fast_memo); |
| 4060 | return 0; |
| 4061 | } |
| 4062 | |
| 4063 | static int |
| 4064 | Pickler_clear(PicklerObject *self) |
| 4065 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4066 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4067 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4068 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4069 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4070 | Py_CLEAR(self->fast_memo); |
| 4071 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4072 | if (self->memo != NULL) { |
| 4073 | PyMemoTable *memo = self->memo; |
| 4074 | self->memo = NULL; |
| 4075 | PyMemoTable_Del(memo); |
| 4076 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4077 | return 0; |
| 4078 | } |
| 4079 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4080 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4081 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4082 | |
| 4083 | _pickle.Pickler.__init__ |
| 4084 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4085 | file: object |
| 4086 | protocol: object = NULL |
| 4087 | fix_imports: bool = True |
| 4088 | |
| 4089 | This takes a binary file for writing a pickle data stream. |
| 4090 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4091 | The optional *protocol* argument tells the pickler to use the given |
| 4092 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4093 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4094 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4095 | Specifying a negative protocol version selects the highest protocol |
| 4096 | version supported. The higher the protocol used, the more recent the |
| 4097 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4098 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4099 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4100 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4101 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4102 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4103 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4104 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4105 | to map the new Python 3 names to the old module names used in Python |
| 4106 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4107 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4108 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4109 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4110 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4111 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4112 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4113 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4114 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4115 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4116 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4117 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4118 | if (self->write != NULL) |
| 4119 | (void)Pickler_clear(self); |
| 4120 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4121 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4122 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4123 | |
| 4124 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4125 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4126 | |
| 4127 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4128 | if (self->memo == NULL) { |
| 4129 | self->memo = PyMemoTable_New(); |
| 4130 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4131 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4132 | } |
| 4133 | self->output_len = 0; |
| 4134 | if (self->output_buffer == NULL) { |
| 4135 | self->max_output_len = WRITE_BUF_SIZE; |
| 4136 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4137 | self->max_output_len); |
| 4138 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4139 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4140 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4141 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4142 | self->fast = 0; |
| 4143 | self->fast_nesting = 0; |
| 4144 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4145 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4146 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4147 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4148 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4149 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4150 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4151 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4152 | self->dispatch_table = NULL; |
| 4153 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4154 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4155 | &PyId_dispatch_table); |
| 4156 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4157 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4158 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4159 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4160 | return 0; |
| 4161 | } |
| 4162 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4163 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4164 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4165 | * avoid breaking code like: |
| 4166 | * pickler.memo.clear() |
| 4167 | * and |
| 4168 | * pickler.memo = saved_memo |
| 4169 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4170 | * it. Note that we don't implement the entire mapping API here. This is |
| 4171 | * intentional, as these should be treated as black-box implementation details. |
| 4172 | */ |
| 4173 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4174 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4175 | _pickle.PicklerMemoProxy.clear |
| 4176 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4177 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4178 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4179 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4180 | static PyObject * |
| 4181 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4182 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4183 | { |
| 4184 | if (self->pickler->memo) |
| 4185 | PyMemoTable_Clear(self->pickler->memo); |
| 4186 | Py_RETURN_NONE; |
| 4187 | } |
| 4188 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4189 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4190 | _pickle.PicklerMemoProxy.copy |
| 4191 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4192 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4193 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4194 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4195 | static PyObject * |
| 4196 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4197 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4198 | { |
| 4199 | Py_ssize_t i; |
| 4200 | PyMemoTable *memo; |
| 4201 | PyObject *new_memo = PyDict_New(); |
| 4202 | if (new_memo == NULL) |
| 4203 | return NULL; |
| 4204 | |
| 4205 | memo = self->pickler->memo; |
| 4206 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4207 | PyMemoEntry entry = memo->mt_table[i]; |
| 4208 | if (entry.me_key != NULL) { |
| 4209 | int status; |
| 4210 | PyObject *key, *value; |
| 4211 | |
| 4212 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4213 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4214 | |
| 4215 | if (key == NULL || value == NULL) { |
| 4216 | Py_XDECREF(key); |
| 4217 | Py_XDECREF(value); |
| 4218 | goto error; |
| 4219 | } |
| 4220 | status = PyDict_SetItem(new_memo, key, value); |
| 4221 | Py_DECREF(key); |
| 4222 | Py_DECREF(value); |
| 4223 | if (status < 0) |
| 4224 | goto error; |
| 4225 | } |
| 4226 | } |
| 4227 | return new_memo; |
| 4228 | |
| 4229 | error: |
| 4230 | Py_XDECREF(new_memo); |
| 4231 | return NULL; |
| 4232 | } |
| 4233 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4234 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4235 | _pickle.PicklerMemoProxy.__reduce__ |
| 4236 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4237 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4238 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4239 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4240 | static PyObject * |
| 4241 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4242 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4243 | { |
| 4244 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4245 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4246 | if (contents == NULL) |
| 4247 | return NULL; |
| 4248 | |
| 4249 | reduce_value = PyTuple_New(2); |
| 4250 | if (reduce_value == NULL) { |
| 4251 | Py_DECREF(contents); |
| 4252 | return NULL; |
| 4253 | } |
| 4254 | dict_args = PyTuple_New(1); |
| 4255 | if (dict_args == NULL) { |
| 4256 | Py_DECREF(contents); |
| 4257 | Py_DECREF(reduce_value); |
| 4258 | return NULL; |
| 4259 | } |
| 4260 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4261 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4262 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4263 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4264 | return reduce_value; |
| 4265 | } |
| 4266 | |
| 4267 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4268 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4269 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4270 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4271 | {NULL, NULL} /* sentinel */ |
| 4272 | }; |
| 4273 | |
| 4274 | static void |
| 4275 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4276 | { |
| 4277 | PyObject_GC_UnTrack(self); |
| 4278 | Py_XDECREF(self->pickler); |
| 4279 | PyObject_GC_Del((PyObject *)self); |
| 4280 | } |
| 4281 | |
| 4282 | static int |
| 4283 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4284 | visitproc visit, void *arg) |
| 4285 | { |
| 4286 | Py_VISIT(self->pickler); |
| 4287 | return 0; |
| 4288 | } |
| 4289 | |
| 4290 | static int |
| 4291 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4292 | { |
| 4293 | Py_CLEAR(self->pickler); |
| 4294 | return 0; |
| 4295 | } |
| 4296 | |
| 4297 | static PyTypeObject PicklerMemoProxyType = { |
| 4298 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4299 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4300 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4301 | 0, |
| 4302 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4303 | 0, /* tp_print */ |
| 4304 | 0, /* tp_getattr */ |
| 4305 | 0, /* tp_setattr */ |
| 4306 | 0, /* tp_compare */ |
| 4307 | 0, /* tp_repr */ |
| 4308 | 0, /* tp_as_number */ |
| 4309 | 0, /* tp_as_sequence */ |
| 4310 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4311 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4312 | 0, /* tp_call */ |
| 4313 | 0, /* tp_str */ |
| 4314 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4315 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4316 | 0, /* tp_as_buffer */ |
| 4317 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4318 | 0, /* tp_doc */ |
| 4319 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4320 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4321 | 0, /* tp_richcompare */ |
| 4322 | 0, /* tp_weaklistoffset */ |
| 4323 | 0, /* tp_iter */ |
| 4324 | 0, /* tp_iternext */ |
| 4325 | picklerproxy_methods, /* tp_methods */ |
| 4326 | }; |
| 4327 | |
| 4328 | static PyObject * |
| 4329 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4330 | { |
| 4331 | PicklerMemoProxyObject *self; |
| 4332 | |
| 4333 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4334 | if (self == NULL) |
| 4335 | return NULL; |
| 4336 | Py_INCREF(pickler); |
| 4337 | self->pickler = pickler; |
| 4338 | PyObject_GC_Track(self); |
| 4339 | return (PyObject *)self; |
| 4340 | } |
| 4341 | |
| 4342 | /*****************************************************************************/ |
| 4343 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4344 | static PyObject * |
| 4345 | Pickler_get_memo(PicklerObject *self) |
| 4346 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4347 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4348 | } |
| 4349 | |
| 4350 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4351 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4352 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4353 | PyMemoTable *new_memo = NULL; |
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 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4356 | PyErr_SetString(PyExc_TypeError, |
| 4357 | "attribute deletion is not supported"); |
| 4358 | return -1; |
| 4359 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4360 | |
| 4361 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4362 | PicklerObject *pickler = |
| 4363 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4364 | |
| 4365 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4366 | if (new_memo == NULL) |
| 4367 | return -1; |
| 4368 | } |
| 4369 | else if (PyDict_Check(obj)) { |
| 4370 | Py_ssize_t i = 0; |
| 4371 | PyObject *key, *value; |
| 4372 | |
| 4373 | new_memo = PyMemoTable_New(); |
| 4374 | if (new_memo == NULL) |
| 4375 | return -1; |
| 4376 | |
| 4377 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4378 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4379 | PyObject *memo_obj; |
| 4380 | |
| 4381 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4382 | PyErr_SetString(PyExc_TypeError, |
| 4383 | "'memo' values must be 2-item tuples"); |
| 4384 | goto error; |
| 4385 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4386 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4387 | if (memo_id == -1 && PyErr_Occurred()) |
| 4388 | goto error; |
| 4389 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4390 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4391 | goto error; |
| 4392 | } |
| 4393 | } |
| 4394 | else { |
| 4395 | PyErr_Format(PyExc_TypeError, |
| 4396 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4397 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4398 | return -1; |
| 4399 | } |
| 4400 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4401 | PyMemoTable_Del(self->memo); |
| 4402 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4403 | |
| 4404 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4405 | |
| 4406 | error: |
| 4407 | if (new_memo) |
| 4408 | PyMemoTable_Del(new_memo); |
| 4409 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | static PyObject * |
| 4413 | Pickler_get_persid(PicklerObject *self) |
| 4414 | { |
| 4415 | if (self->pers_func == NULL) |
| 4416 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4417 | else |
| 4418 | Py_INCREF(self->pers_func); |
| 4419 | return self->pers_func; |
| 4420 | } |
| 4421 | |
| 4422 | static int |
| 4423 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4424 | { |
| 4425 | PyObject *tmp; |
| 4426 | |
| 4427 | if (value == NULL) { |
| 4428 | PyErr_SetString(PyExc_TypeError, |
| 4429 | "attribute deletion is not supported"); |
| 4430 | return -1; |
| 4431 | } |
| 4432 | if (!PyCallable_Check(value)) { |
| 4433 | PyErr_SetString(PyExc_TypeError, |
| 4434 | "persistent_id must be a callable taking one argument"); |
| 4435 | return -1; |
| 4436 | } |
| 4437 | |
| 4438 | tmp = self->pers_func; |
| 4439 | Py_INCREF(value); |
| 4440 | self->pers_func = value; |
| 4441 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4442 | |
| 4443 | return 0; |
| 4444 | } |
| 4445 | |
| 4446 | static PyMemberDef Pickler_members[] = { |
| 4447 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4448 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4449 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4450 | {NULL} |
| 4451 | }; |
| 4452 | |
| 4453 | static PyGetSetDef Pickler_getsets[] = { |
| 4454 | {"memo", (getter)Pickler_get_memo, |
| 4455 | (setter)Pickler_set_memo}, |
| 4456 | {"persistent_id", (getter)Pickler_get_persid, |
| 4457 | (setter)Pickler_set_persid}, |
| 4458 | {NULL} |
| 4459 | }; |
| 4460 | |
| 4461 | static PyTypeObject Pickler_Type = { |
| 4462 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4463 | "_pickle.Pickler" , /*tp_name*/ |
| 4464 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4465 | 0, /*tp_itemsize*/ |
| 4466 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4467 | 0, /*tp_print*/ |
| 4468 | 0, /*tp_getattr*/ |
| 4469 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4470 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4471 | 0, /*tp_repr*/ |
| 4472 | 0, /*tp_as_number*/ |
| 4473 | 0, /*tp_as_sequence*/ |
| 4474 | 0, /*tp_as_mapping*/ |
| 4475 | 0, /*tp_hash*/ |
| 4476 | 0, /*tp_call*/ |
| 4477 | 0, /*tp_str*/ |
| 4478 | 0, /*tp_getattro*/ |
| 4479 | 0, /*tp_setattro*/ |
| 4480 | 0, /*tp_as_buffer*/ |
| 4481 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4482 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4483 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4484 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4485 | 0, /*tp_richcompare*/ |
| 4486 | 0, /*tp_weaklistoffset*/ |
| 4487 | 0, /*tp_iter*/ |
| 4488 | 0, /*tp_iternext*/ |
| 4489 | Pickler_methods, /*tp_methods*/ |
| 4490 | Pickler_members, /*tp_members*/ |
| 4491 | Pickler_getsets, /*tp_getset*/ |
| 4492 | 0, /*tp_base*/ |
| 4493 | 0, /*tp_dict*/ |
| 4494 | 0, /*tp_descr_get*/ |
| 4495 | 0, /*tp_descr_set*/ |
| 4496 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4497 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4498 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4499 | PyType_GenericNew, /*tp_new*/ |
| 4500 | PyObject_GC_Del, /*tp_free*/ |
| 4501 | 0, /*tp_is_gc*/ |
| 4502 | }; |
| 4503 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4504 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4505 | |
| 4506 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4507 | using directly the C version of find_class(), when find_class() is not |
| 4508 | overridden by a subclass. Although, this could become rather hackish. A |
| 4509 | simpler optimization would be to call the C function when self is not a |
| 4510 | subclass instance. */ |
| 4511 | static PyObject * |
| 4512 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4513 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4514 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4515 | |
| 4516 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4517 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4520 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4521 | marker(UnpicklerObject *self) |
| 4522 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4523 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4524 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4525 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4526 | return -1; |
| 4527 | } |
| 4528 | |
| 4529 | return self->marks[--self->num_marks]; |
| 4530 | } |
| 4531 | |
| 4532 | static int |
| 4533 | load_none(UnpicklerObject *self) |
| 4534 | { |
| 4535 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4536 | return 0; |
| 4537 | } |
| 4538 | |
| 4539 | static int |
| 4540 | bad_readline(void) |
| 4541 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4542 | PickleState *st = _Pickle_GetGlobalState(); |
| 4543 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4544 | return -1; |
| 4545 | } |
| 4546 | |
| 4547 | static int |
| 4548 | load_int(UnpicklerObject *self) |
| 4549 | { |
| 4550 | PyObject *value; |
| 4551 | char *endptr, *s; |
| 4552 | Py_ssize_t len; |
| 4553 | long x; |
| 4554 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4555 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4556 | return -1; |
| 4557 | if (len < 2) |
| 4558 | return bad_readline(); |
| 4559 | |
| 4560 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4561 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4562 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4563 | x = strtol(s, &endptr, 0); |
| 4564 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4565 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4566 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4567 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4568 | errno = 0; |
| 4569 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4570 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4571 | if (value == NULL) { |
| 4572 | PyErr_SetString(PyExc_ValueError, |
| 4573 | "could not convert string to int"); |
| 4574 | return -1; |
| 4575 | } |
| 4576 | } |
| 4577 | else { |
| 4578 | if (len == 3 && (x == 0 || x == 1)) { |
| 4579 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4580 | return -1; |
| 4581 | } |
| 4582 | else { |
| 4583 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4584 | return -1; |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | PDATA_PUSH(self->stack, value, -1); |
| 4589 | return 0; |
| 4590 | } |
| 4591 | |
| 4592 | static int |
| 4593 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4594 | { |
| 4595 | assert(boolean == Py_True || boolean == Py_False); |
| 4596 | PDATA_APPEND(self->stack, boolean, -1); |
| 4597 | return 0; |
| 4598 | } |
| 4599 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4600 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4601 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4602 | */ |
| 4603 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4604 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4605 | { |
| 4606 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4607 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4608 | size_t x = 0; |
| 4609 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4610 | if (nbytes > (int)sizeof(size_t)) { |
| 4611 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4612 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4613 | */ |
| 4614 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4615 | if (s[i]) |
| 4616 | return -1; |
| 4617 | } |
| 4618 | nbytes = (int)sizeof(size_t); |
| 4619 | } |
| 4620 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4621 | x |= (size_t) s[i] << (8 * i); |
| 4622 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4623 | |
| 4624 | if (x > PY_SSIZE_T_MAX) |
| 4625 | return -1; |
| 4626 | else |
| 4627 | return (Py_ssize_t) x; |
| 4628 | } |
| 4629 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4630 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4631 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
Serhiy Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame] | 4632 | * int, but when x is 4 it's a signed one. This is a historical source |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4633 | * of x-platform bugs. |
| 4634 | */ |
| 4635 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4636 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4637 | { |
| 4638 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4639 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4640 | long x = 0; |
| 4641 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4642 | for (i = 0; i < nbytes; i++) { |
| 4643 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4647 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4648 | * to extend a BININT's sign bit to the full width. |
| 4649 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4650 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4651 | x |= -(x & (1L << 31)); |
| 4652 | } |
| 4653 | |
| 4654 | return x; |
| 4655 | } |
| 4656 | |
| 4657 | static int |
| 4658 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4659 | { |
| 4660 | PyObject *value; |
| 4661 | long x; |
| 4662 | |
| 4663 | x = calc_binint(s, size); |
| 4664 | |
| 4665 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4666 | return -1; |
| 4667 | |
| 4668 | PDATA_PUSH(self->stack, value, -1); |
| 4669 | return 0; |
| 4670 | } |
| 4671 | |
| 4672 | static int |
| 4673 | load_binint(UnpicklerObject *self) |
| 4674 | { |
| 4675 | char *s; |
| 4676 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4677 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4678 | return -1; |
| 4679 | |
| 4680 | return load_binintx(self, s, 4); |
| 4681 | } |
| 4682 | |
| 4683 | static int |
| 4684 | load_binint1(UnpicklerObject *self) |
| 4685 | { |
| 4686 | char *s; |
| 4687 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4688 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4689 | return -1; |
| 4690 | |
| 4691 | return load_binintx(self, s, 1); |
| 4692 | } |
| 4693 | |
| 4694 | static int |
| 4695 | load_binint2(UnpicklerObject *self) |
| 4696 | { |
| 4697 | char *s; |
| 4698 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4699 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4700 | return -1; |
| 4701 | |
| 4702 | return load_binintx(self, s, 2); |
| 4703 | } |
| 4704 | |
| 4705 | static int |
| 4706 | load_long(UnpicklerObject *self) |
| 4707 | { |
| 4708 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4709 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4710 | Py_ssize_t len; |
| 4711 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4712 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4713 | return -1; |
| 4714 | if (len < 2) |
| 4715 | return bad_readline(); |
| 4716 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4717 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4718 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4719 | compatibility with Python 3.0.0, we don't actually *require* |
| 4720 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4721 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4722 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4723 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4724 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4725 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4726 | return -1; |
| 4727 | |
| 4728 | PDATA_PUSH(self->stack, value, -1); |
| 4729 | return 0; |
| 4730 | } |
| 4731 | |
| 4732 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4733 | * data following. |
| 4734 | */ |
| 4735 | static int |
| 4736 | load_counted_long(UnpicklerObject *self, int size) |
| 4737 | { |
| 4738 | PyObject *value; |
| 4739 | char *nbytes; |
| 4740 | char *pdata; |
| 4741 | |
| 4742 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4743 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4744 | return -1; |
| 4745 | |
| 4746 | size = calc_binint(nbytes, size); |
| 4747 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4748 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4749 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4750 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4751 | "LONG pickle has negative byte count"); |
| 4752 | return -1; |
| 4753 | } |
| 4754 | |
| 4755 | if (size == 0) |
| 4756 | value = PyLong_FromLong(0L); |
| 4757 | else { |
| 4758 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4759 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4760 | return -1; |
| 4761 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4762 | 1 /* little endian */ , 1 /* signed */ ); |
| 4763 | } |
| 4764 | if (value == NULL) |
| 4765 | return -1; |
| 4766 | PDATA_PUSH(self->stack, value, -1); |
| 4767 | return 0; |
| 4768 | } |
| 4769 | |
| 4770 | static int |
| 4771 | load_float(UnpicklerObject *self) |
| 4772 | { |
| 4773 | PyObject *value; |
| 4774 | char *endptr, *s; |
| 4775 | Py_ssize_t len; |
| 4776 | double d; |
| 4777 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4778 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4779 | return -1; |
| 4780 | if (len < 2) |
| 4781 | return bad_readline(); |
| 4782 | |
| 4783 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4784 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4785 | if (d == -1.0 && PyErr_Occurred()) |
| 4786 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4787 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4788 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4789 | return -1; |
| 4790 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4791 | value = PyFloat_FromDouble(d); |
| 4792 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4793 | return -1; |
| 4794 | |
| 4795 | PDATA_PUSH(self->stack, value, -1); |
| 4796 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4797 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4798 | |
| 4799 | static int |
| 4800 | load_binfloat(UnpicklerObject *self) |
| 4801 | { |
| 4802 | PyObject *value; |
| 4803 | double x; |
| 4804 | char *s; |
| 4805 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4806 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4807 | return -1; |
| 4808 | |
| 4809 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4810 | if (x == -1.0 && PyErr_Occurred()) |
| 4811 | return -1; |
| 4812 | |
| 4813 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4814 | return -1; |
| 4815 | |
| 4816 | PDATA_PUSH(self->stack, value, -1); |
| 4817 | return 0; |
| 4818 | } |
| 4819 | |
| 4820 | static int |
| 4821 | load_string(UnpicklerObject *self) |
| 4822 | { |
| 4823 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4824 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4825 | Py_ssize_t len; |
| 4826 | char *s, *p; |
| 4827 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4828 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4829 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4830 | /* Strip the newline */ |
| 4831 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4832 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4833 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4834 | p = s + 1; |
| 4835 | len -= 2; |
| 4836 | } |
| 4837 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4838 | PickleState *st = _Pickle_GetGlobalState(); |
| 4839 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4840 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4841 | return -1; |
| 4842 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4843 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4844 | |
| 4845 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4846 | to encode, and then coerce the result to Unicode. */ |
| 4847 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4848 | if (bytes == NULL) |
| 4849 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4850 | |
| 4851 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4852 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4853 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4854 | obj = bytes; |
| 4855 | } |
| 4856 | else { |
| 4857 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4858 | Py_DECREF(bytes); |
| 4859 | if (obj == NULL) { |
| 4860 | return -1; |
| 4861 | } |
| 4862 | } |
| 4863 | |
| 4864 | PDATA_PUSH(self->stack, obj, -1); |
| 4865 | return 0; |
| 4866 | } |
| 4867 | |
| 4868 | static int |
| 4869 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4870 | { |
| 4871 | PyObject *obj; |
| 4872 | Py_ssize_t size; |
| 4873 | char *s; |
| 4874 | |
| 4875 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4876 | return -1; |
| 4877 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4878 | size = calc_binsize(s, nbytes); |
| 4879 | if (size < 0) { |
| 4880 | PickleState *st = _Pickle_GetGlobalState(); |
| 4881 | PyErr_Format(st->UnpicklingError, |
| 4882 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4883 | PY_SSIZE_T_MAX); |
| 4884 | return -1; |
| 4885 | } |
| 4886 | |
| 4887 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4888 | return -1; |
| 4889 | |
| 4890 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4891 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4892 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4893 | obj = PyBytes_FromStringAndSize(s, size); |
| 4894 | } |
| 4895 | else { |
| 4896 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4897 | } |
| 4898 | if (obj == NULL) { |
| 4899 | return -1; |
| 4900 | } |
| 4901 | |
| 4902 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4903 | return 0; |
| 4904 | } |
| 4905 | |
| 4906 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4907 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4908 | { |
| 4909 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4910 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4911 | char *s; |
| 4912 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4913 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4914 | return -1; |
| 4915 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4916 | size = calc_binsize(s, nbytes); |
| 4917 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4918 | PyErr_Format(PyExc_OverflowError, |
| 4919 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4920 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4921 | return -1; |
| 4922 | } |
| 4923 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4924 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4925 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4926 | |
| 4927 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4928 | if (bytes == NULL) |
| 4929 | return -1; |
| 4930 | |
| 4931 | PDATA_PUSH(self->stack, bytes, -1); |
| 4932 | return 0; |
| 4933 | } |
| 4934 | |
| 4935 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4936 | load_unicode(UnpicklerObject *self) |
| 4937 | { |
| 4938 | PyObject *str; |
| 4939 | Py_ssize_t len; |
| 4940 | char *s; |
| 4941 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4942 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4943 | return -1; |
| 4944 | if (len < 1) |
| 4945 | return bad_readline(); |
| 4946 | |
| 4947 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4948 | if (str == NULL) |
| 4949 | return -1; |
| 4950 | |
| 4951 | PDATA_PUSH(self->stack, str, -1); |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
| 4955 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4956 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4957 | { |
| 4958 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4959 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4960 | char *s; |
| 4961 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4962 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4963 | return -1; |
| 4964 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4965 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4966 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4967 | PyErr_Format(PyExc_OverflowError, |
| 4968 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4969 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4970 | return -1; |
| 4971 | } |
| 4972 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4973 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4974 | return -1; |
| 4975 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4976 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4977 | if (str == NULL) |
| 4978 | return -1; |
| 4979 | |
| 4980 | PDATA_PUSH(self->stack, str, -1); |
| 4981 | return 0; |
| 4982 | } |
| 4983 | |
| 4984 | static int |
Victor Stinner | 13f0c61 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 4985 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4986 | { |
| 4987 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4988 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 4989 | if (Py_SIZE(self->stack) < len) |
| 4990 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4991 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 4992 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4993 | if (tuple == NULL) |
| 4994 | return -1; |
| 4995 | PDATA_PUSH(self->stack, tuple, -1); |
| 4996 | return 0; |
| 4997 | } |
| 4998 | |
| 4999 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5000 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5001 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5002 | Py_ssize_t i; |
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 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5005 | return -1; |
| 5006 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5007 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | static int |
| 5011 | load_empty_list(UnpicklerObject *self) |
| 5012 | { |
| 5013 | PyObject *list; |
| 5014 | |
| 5015 | if ((list = PyList_New(0)) == NULL) |
| 5016 | return -1; |
| 5017 | PDATA_PUSH(self->stack, list, -1); |
| 5018 | return 0; |
| 5019 | } |
| 5020 | |
| 5021 | static int |
| 5022 | load_empty_dict(UnpicklerObject *self) |
| 5023 | { |
| 5024 | PyObject *dict; |
| 5025 | |
| 5026 | if ((dict = PyDict_New()) == NULL) |
| 5027 | return -1; |
| 5028 | PDATA_PUSH(self->stack, dict, -1); |
| 5029 | return 0; |
| 5030 | } |
| 5031 | |
| 5032 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5033 | load_empty_set(UnpicklerObject *self) |
| 5034 | { |
| 5035 | PyObject *set; |
| 5036 | |
| 5037 | if ((set = PySet_New(NULL)) == NULL) |
| 5038 | return -1; |
| 5039 | PDATA_PUSH(self->stack, set, -1); |
| 5040 | return 0; |
| 5041 | } |
| 5042 | |
| 5043 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5044 | load_list(UnpicklerObject *self) |
| 5045 | { |
| 5046 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5047 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5048 | |
| 5049 | if ((i = marker(self)) < 0) |
| 5050 | return -1; |
| 5051 | |
| 5052 | list = Pdata_poplist(self->stack, i); |
| 5053 | if (list == NULL) |
| 5054 | return -1; |
| 5055 | PDATA_PUSH(self->stack, list, -1); |
| 5056 | return 0; |
| 5057 | } |
| 5058 | |
| 5059 | static int |
| 5060 | load_dict(UnpicklerObject *self) |
| 5061 | { |
| 5062 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5063 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5064 | |
| 5065 | if ((i = marker(self)) < 0) |
| 5066 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5067 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5068 | |
| 5069 | if ((dict = PyDict_New()) == NULL) |
| 5070 | return -1; |
| 5071 | |
| 5072 | for (k = i + 1; k < j; k += 2) { |
| 5073 | key = self->stack->data[k - 1]; |
| 5074 | value = self->stack->data[k]; |
| 5075 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5076 | Py_DECREF(dict); |
| 5077 | return -1; |
| 5078 | } |
| 5079 | } |
| 5080 | Pdata_clear(self->stack, i); |
| 5081 | PDATA_PUSH(self->stack, dict, -1); |
| 5082 | return 0; |
| 5083 | } |
| 5084 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5085 | static int |
| 5086 | load_frozenset(UnpicklerObject *self) |
| 5087 | { |
| 5088 | PyObject *items; |
| 5089 | PyObject *frozenset; |
| 5090 | Py_ssize_t i; |
| 5091 | |
| 5092 | if ((i = marker(self)) < 0) |
| 5093 | return -1; |
| 5094 | |
| 5095 | items = Pdata_poptuple(self->stack, i); |
| 5096 | if (items == NULL) |
| 5097 | return -1; |
| 5098 | |
| 5099 | frozenset = PyFrozenSet_New(items); |
| 5100 | Py_DECREF(items); |
| 5101 | if (frozenset == NULL) |
| 5102 | return -1; |
| 5103 | |
| 5104 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5105 | return 0; |
| 5106 | } |
| 5107 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5108 | static PyObject * |
| 5109 | instantiate(PyObject *cls, PyObject *args) |
| 5110 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5111 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5112 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5113 | /* Caller must assure args are a tuple. Normally, args come from |
| 5114 | Pdata_poptuple which packs objects from the top of the stack |
| 5115 | into a newly created tuple. */ |
| 5116 | assert(PyTuple_Check(args)); |
| 5117 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5118 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5119 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5120 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5121 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5122 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5123 | |
| 5124 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5125 | } |
| 5126 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5127 | } |
| 5128 | |
| 5129 | static int |
| 5130 | load_obj(UnpicklerObject *self) |
| 5131 | { |
| 5132 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5133 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5134 | |
| 5135 | if ((i = marker(self)) < 0) |
| 5136 | return -1; |
| 5137 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5138 | if (Py_SIZE(self->stack) - i < 1) |
| 5139 | return stack_underflow(); |
| 5140 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5141 | args = Pdata_poptuple(self->stack, i + 1); |
| 5142 | if (args == NULL) |
| 5143 | return -1; |
| 5144 | |
| 5145 | PDATA_POP(self->stack, cls); |
| 5146 | if (cls) { |
| 5147 | obj = instantiate(cls, args); |
| 5148 | Py_DECREF(cls); |
| 5149 | } |
| 5150 | Py_DECREF(args); |
| 5151 | if (obj == NULL) |
| 5152 | return -1; |
| 5153 | |
| 5154 | PDATA_PUSH(self->stack, obj, -1); |
| 5155 | return 0; |
| 5156 | } |
| 5157 | |
| 5158 | static int |
| 5159 | load_inst(UnpicklerObject *self) |
| 5160 | { |
| 5161 | PyObject *cls = NULL; |
| 5162 | PyObject *args = NULL; |
| 5163 | PyObject *obj = NULL; |
| 5164 | PyObject *module_name; |
| 5165 | PyObject *class_name; |
| 5166 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5167 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5168 | char *s; |
| 5169 | |
| 5170 | if ((i = marker(self)) < 0) |
| 5171 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5172 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5173 | return -1; |
| 5174 | if (len < 2) |
| 5175 | return bad_readline(); |
| 5176 | |
| 5177 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5178 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5179 | supported by older protocols on Python 2.x. */ |
| 5180 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5181 | if (module_name == NULL) |
| 5182 | return -1; |
| 5183 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5184 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5185 | if (len < 2) { |
| 5186 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5187 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5188 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5189 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5190 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5191 | cls = find_class(self, module_name, class_name); |
| 5192 | Py_DECREF(class_name); |
| 5193 | } |
| 5194 | } |
| 5195 | Py_DECREF(module_name); |
| 5196 | |
| 5197 | if (cls == NULL) |
| 5198 | return -1; |
| 5199 | |
| 5200 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5201 | obj = instantiate(cls, args); |
| 5202 | Py_DECREF(args); |
| 5203 | } |
| 5204 | Py_DECREF(cls); |
| 5205 | |
| 5206 | if (obj == NULL) |
| 5207 | return -1; |
| 5208 | |
| 5209 | PDATA_PUSH(self->stack, obj, -1); |
| 5210 | return 0; |
| 5211 | } |
| 5212 | |
| 5213 | static int |
| 5214 | load_newobj(UnpicklerObject *self) |
| 5215 | { |
| 5216 | PyObject *args = NULL; |
| 5217 | PyObject *clsraw = NULL; |
| 5218 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5219 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5220 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5221 | |
| 5222 | /* Stack is ... cls argtuple, and we want to call |
| 5223 | * cls.__new__(cls, *argtuple). |
| 5224 | */ |
| 5225 | PDATA_POP(self->stack, args); |
| 5226 | if (args == NULL) |
| 5227 | goto error; |
| 5228 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5229 | PyErr_SetString(st->UnpicklingError, |
| 5230 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5231 | goto error; |
| 5232 | } |
| 5233 | |
| 5234 | PDATA_POP(self->stack, clsraw); |
| 5235 | cls = (PyTypeObject *)clsraw; |
| 5236 | if (cls == NULL) |
| 5237 | goto error; |
| 5238 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5239 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5240 | "isn't a type object"); |
| 5241 | goto error; |
| 5242 | } |
| 5243 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5244 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5245 | "has NULL tp_new"); |
| 5246 | goto error; |
| 5247 | } |
| 5248 | |
| 5249 | /* Call __new__. */ |
| 5250 | obj = cls->tp_new(cls, args, NULL); |
| 5251 | if (obj == NULL) |
| 5252 | goto error; |
| 5253 | |
| 5254 | Py_DECREF(args); |
| 5255 | Py_DECREF(clsraw); |
| 5256 | PDATA_PUSH(self->stack, obj, -1); |
| 5257 | return 0; |
| 5258 | |
| 5259 | error: |
| 5260 | Py_XDECREF(args); |
| 5261 | Py_XDECREF(clsraw); |
| 5262 | return -1; |
| 5263 | } |
| 5264 | |
| 5265 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5266 | load_newobj_ex(UnpicklerObject *self) |
| 5267 | { |
| 5268 | PyObject *cls, *args, *kwargs; |
| 5269 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5270 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5271 | |
| 5272 | PDATA_POP(self->stack, kwargs); |
| 5273 | if (kwargs == NULL) { |
| 5274 | return -1; |
| 5275 | } |
| 5276 | PDATA_POP(self->stack, args); |
| 5277 | if (args == NULL) { |
| 5278 | Py_DECREF(kwargs); |
| 5279 | return -1; |
| 5280 | } |
| 5281 | PDATA_POP(self->stack, cls); |
| 5282 | if (cls == NULL) { |
| 5283 | Py_DECREF(kwargs); |
| 5284 | Py_DECREF(args); |
| 5285 | return -1; |
| 5286 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5287 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5288 | if (!PyType_Check(cls)) { |
| 5289 | Py_DECREF(kwargs); |
| 5290 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5291 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5292 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5293 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5294 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5295 | return -1; |
| 5296 | } |
| 5297 | |
| 5298 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5299 | Py_DECREF(kwargs); |
| 5300 | Py_DECREF(args); |
| 5301 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5302 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5303 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5304 | return -1; |
| 5305 | } |
| 5306 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5307 | Py_DECREF(kwargs); |
| 5308 | Py_DECREF(args); |
| 5309 | Py_DECREF(cls); |
| 5310 | if (obj == NULL) { |
| 5311 | return -1; |
| 5312 | } |
| 5313 | PDATA_PUSH(self->stack, obj, -1); |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
| 5317 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5318 | load_global(UnpicklerObject *self) |
| 5319 | { |
| 5320 | PyObject *global = NULL; |
| 5321 | PyObject *module_name; |
| 5322 | PyObject *global_name; |
| 5323 | Py_ssize_t len; |
| 5324 | char *s; |
| 5325 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5326 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5327 | return -1; |
| 5328 | if (len < 2) |
| 5329 | return bad_readline(); |
| 5330 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5331 | if (!module_name) |
| 5332 | return -1; |
| 5333 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5334 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5335 | if (len < 2) { |
| 5336 | Py_DECREF(module_name); |
| 5337 | return bad_readline(); |
| 5338 | } |
| 5339 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5340 | if (global_name) { |
| 5341 | global = find_class(self, module_name, global_name); |
| 5342 | Py_DECREF(global_name); |
| 5343 | } |
| 5344 | } |
| 5345 | Py_DECREF(module_name); |
| 5346 | |
| 5347 | if (global == NULL) |
| 5348 | return -1; |
| 5349 | PDATA_PUSH(self->stack, global, -1); |
| 5350 | return 0; |
| 5351 | } |
| 5352 | |
| 5353 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5354 | load_stack_global(UnpicklerObject *self) |
| 5355 | { |
| 5356 | PyObject *global; |
| 5357 | PyObject *module_name; |
| 5358 | PyObject *global_name; |
| 5359 | |
| 5360 | PDATA_POP(self->stack, global_name); |
| 5361 | PDATA_POP(self->stack, module_name); |
| 5362 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5363 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5364 | PickleState *st = _Pickle_GetGlobalState(); |
| 5365 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5366 | Py_XDECREF(global_name); |
| 5367 | Py_XDECREF(module_name); |
| 5368 | return -1; |
| 5369 | } |
| 5370 | global = find_class(self, module_name, global_name); |
| 5371 | Py_DECREF(global_name); |
| 5372 | Py_DECREF(module_name); |
| 5373 | if (global == NULL) |
| 5374 | return -1; |
| 5375 | PDATA_PUSH(self->stack, global, -1); |
| 5376 | return 0; |
| 5377 | } |
| 5378 | |
| 5379 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5380 | load_persid(UnpicklerObject *self) |
| 5381 | { |
| 5382 | PyObject *pid; |
| 5383 | Py_ssize_t len; |
| 5384 | char *s; |
| 5385 | |
| 5386 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5387 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5388 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5389 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5390 | return bad_readline(); |
| 5391 | |
| 5392 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5393 | if (pid == NULL) |
| 5394 | return -1; |
| 5395 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5396 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5397 | to pid first. */ |
| 5398 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5399 | if (pid == NULL) |
| 5400 | return -1; |
| 5401 | |
| 5402 | PDATA_PUSH(self->stack, pid, -1); |
| 5403 | return 0; |
| 5404 | } |
| 5405 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5406 | PickleState *st = _Pickle_GetGlobalState(); |
| 5407 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5408 | "A load persistent id instruction was encountered,\n" |
| 5409 | "but no persistent_load function was specified."); |
| 5410 | return -1; |
| 5411 | } |
| 5412 | } |
| 5413 | |
| 5414 | static int |
| 5415 | load_binpersid(UnpicklerObject *self) |
| 5416 | { |
| 5417 | PyObject *pid; |
| 5418 | |
| 5419 | if (self->pers_func) { |
| 5420 | PDATA_POP(self->stack, pid); |
| 5421 | if (pid == NULL) |
| 5422 | return -1; |
| 5423 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5424 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5425 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5426 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5427 | if (pid == NULL) |
| 5428 | return -1; |
| 5429 | |
| 5430 | PDATA_PUSH(self->stack, pid, -1); |
| 5431 | return 0; |
| 5432 | } |
| 5433 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5434 | PickleState *st = _Pickle_GetGlobalState(); |
| 5435 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5436 | "A load persistent id instruction was encountered,\n" |
| 5437 | "but no persistent_load function was specified."); |
| 5438 | return -1; |
| 5439 | } |
| 5440 | } |
| 5441 | |
| 5442 | static int |
| 5443 | load_pop(UnpicklerObject *self) |
| 5444 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5445 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5446 | |
| 5447 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5448 | * an object stack and a mark stack. We have to be clever and |
| 5449 | * 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] | 5450 | * mark stack first, and only signalling a stack underflow if |
| 5451 | * the object stack is empty and the mark stack doesn't match |
| 5452 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5453 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5454 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5455 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5456 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5457 | len--; |
| 5458 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5459 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5460 | } else { |
| 5461 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5462 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5463 | return 0; |
| 5464 | } |
| 5465 | |
| 5466 | static int |
| 5467 | load_pop_mark(UnpicklerObject *self) |
| 5468 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5469 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5470 | |
| 5471 | if ((i = marker(self)) < 0) |
| 5472 | return -1; |
| 5473 | |
| 5474 | Pdata_clear(self->stack, i); |
| 5475 | |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
| 5479 | static int |
| 5480 | load_dup(UnpicklerObject *self) |
| 5481 | { |
| 5482 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5483 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5484 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5485 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5486 | return stack_underflow(); |
| 5487 | last = self->stack->data[len - 1]; |
| 5488 | PDATA_APPEND(self->stack, last, -1); |
| 5489 | return 0; |
| 5490 | } |
| 5491 | |
| 5492 | static int |
| 5493 | load_get(UnpicklerObject *self) |
| 5494 | { |
| 5495 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5496 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5497 | Py_ssize_t len; |
| 5498 | char *s; |
| 5499 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5500 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5501 | return -1; |
| 5502 | if (len < 2) |
| 5503 | return bad_readline(); |
| 5504 | |
| 5505 | key = PyLong_FromString(s, NULL, 10); |
| 5506 | if (key == NULL) |
| 5507 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5508 | idx = PyLong_AsSsize_t(key); |
| 5509 | if (idx == -1 && PyErr_Occurred()) { |
| 5510 | Py_DECREF(key); |
| 5511 | return -1; |
| 5512 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5513 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5514 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5515 | if (value == NULL) { |
| 5516 | if (!PyErr_Occurred()) |
| 5517 | PyErr_SetObject(PyExc_KeyError, key); |
| 5518 | Py_DECREF(key); |
| 5519 | return -1; |
| 5520 | } |
| 5521 | Py_DECREF(key); |
| 5522 | |
| 5523 | PDATA_APPEND(self->stack, value, -1); |
| 5524 | return 0; |
| 5525 | } |
| 5526 | |
| 5527 | static int |
| 5528 | load_binget(UnpicklerObject *self) |
| 5529 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5530 | PyObject *value; |
| 5531 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5532 | char *s; |
| 5533 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5534 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5535 | return -1; |
| 5536 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5537 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5538 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5539 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5540 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5541 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5542 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5543 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5544 | Py_DECREF(key); |
| 5545 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5546 | return -1; |
| 5547 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5548 | |
| 5549 | PDATA_APPEND(self->stack, value, -1); |
| 5550 | return 0; |
| 5551 | } |
| 5552 | |
| 5553 | static int |
| 5554 | load_long_binget(UnpicklerObject *self) |
| 5555 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5556 | PyObject *value; |
| 5557 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5558 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5559 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5560 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5561 | return -1; |
| 5562 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5563 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5564 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5565 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5566 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5567 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5568 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5569 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5570 | Py_DECREF(key); |
| 5571 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5572 | return -1; |
| 5573 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5574 | |
| 5575 | PDATA_APPEND(self->stack, value, -1); |
| 5576 | return 0; |
| 5577 | } |
| 5578 | |
| 5579 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5580 | * the number of bytes following the opcode, holding the index (code) value. |
| 5581 | */ |
| 5582 | static int |
| 5583 | load_extension(UnpicklerObject *self, int nbytes) |
| 5584 | { |
| 5585 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5586 | long code; /* calc_binint returns long */ |
| 5587 | PyObject *py_code; /* code as a Python int */ |
| 5588 | PyObject *obj; /* the object to push */ |
| 5589 | PyObject *pair; /* (module_name, class_name) */ |
| 5590 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5591 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5592 | |
| 5593 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5594 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5595 | return -1; |
| 5596 | code = calc_binint(codebytes, nbytes); |
| 5597 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5598 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5599 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5600 | return -1; |
| 5601 | } |
| 5602 | |
| 5603 | /* Look for the code in the cache. */ |
| 5604 | py_code = PyLong_FromLong(code); |
| 5605 | if (py_code == NULL) |
| 5606 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5607 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5608 | if (obj != NULL) { |
| 5609 | /* Bingo. */ |
| 5610 | Py_DECREF(py_code); |
| 5611 | PDATA_APPEND(self->stack, obj, -1); |
| 5612 | return 0; |
| 5613 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5614 | if (PyErr_Occurred()) { |
| 5615 | Py_DECREF(py_code); |
| 5616 | return -1; |
| 5617 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5618 | |
| 5619 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5620 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5621 | if (pair == NULL) { |
| 5622 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5623 | if (!PyErr_Occurred()) { |
| 5624 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5625 | "code %ld", code); |
| 5626 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5627 | return -1; |
| 5628 | } |
| 5629 | /* Since the extension registry is manipulable via Python code, |
| 5630 | * confirm that pair is really a 2-tuple of strings. |
| 5631 | */ |
| 5632 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5633 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5634 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5635 | Py_DECREF(py_code); |
| 5636 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5637 | "isn't a 2-tuple of strings", code); |
| 5638 | return -1; |
| 5639 | } |
| 5640 | /* Load the object. */ |
| 5641 | obj = find_class(self, module_name, class_name); |
| 5642 | if (obj == NULL) { |
| 5643 | Py_DECREF(py_code); |
| 5644 | return -1; |
| 5645 | } |
| 5646 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5647 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5648 | Py_DECREF(py_code); |
| 5649 | if (code < 0) { |
| 5650 | Py_DECREF(obj); |
| 5651 | return -1; |
| 5652 | } |
| 5653 | PDATA_PUSH(self->stack, obj, -1); |
| 5654 | return 0; |
| 5655 | } |
| 5656 | |
| 5657 | static int |
| 5658 | load_put(UnpicklerObject *self) |
| 5659 | { |
| 5660 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5661 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5662 | Py_ssize_t len; |
| 5663 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5664 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5665 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5666 | return -1; |
| 5667 | if (len < 2) |
| 5668 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5669 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5670 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5671 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5672 | |
| 5673 | key = PyLong_FromString(s, NULL, 10); |
| 5674 | if (key == NULL) |
| 5675 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5676 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5677 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5678 | if (idx < 0) { |
| 5679 | if (!PyErr_Occurred()) |
| 5680 | PyErr_SetString(PyExc_ValueError, |
| 5681 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5682 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5683 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5684 | |
| 5685 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5686 | } |
| 5687 | |
| 5688 | static int |
| 5689 | load_binput(UnpicklerObject *self) |
| 5690 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5691 | PyObject *value; |
| 5692 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5693 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5695 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5696 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5697 | |
| 5698 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5699 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5700 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5701 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5702 | idx = Py_CHARMASK(s[0]); |
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 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5705 | } |
| 5706 | |
| 5707 | static int |
| 5708 | load_long_binput(UnpicklerObject *self) |
| 5709 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5710 | PyObject *value; |
| 5711 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5712 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5713 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5714 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5715 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5716 | |
| 5717 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5718 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5719 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5720 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5721 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5722 | if (idx < 0) { |
| 5723 | PyErr_SetString(PyExc_ValueError, |
| 5724 | "negative LONG_BINPUT argument"); |
| 5725 | return -1; |
| 5726 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5727 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5728 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5729 | } |
| 5730 | |
| 5731 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5732 | load_memoize(UnpicklerObject *self) |
| 5733 | { |
| 5734 | PyObject *value; |
| 5735 | |
| 5736 | if (Py_SIZE(self->stack) <= 0) |
| 5737 | return stack_underflow(); |
| 5738 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5739 | |
| 5740 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5741 | } |
| 5742 | |
| 5743 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5744 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5745 | { |
| 5746 | PyObject *value; |
| 5747 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5748 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5749 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5750 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5751 | if (x > len || x <= 0) |
| 5752 | return stack_underflow(); |
| 5753 | if (len == x) /* nothing to do */ |
| 5754 | return 0; |
| 5755 | |
| 5756 | list = self->stack->data[x - 1]; |
| 5757 | |
| 5758 | if (PyList_Check(list)) { |
| 5759 | PyObject *slice; |
| 5760 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5761 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5762 | |
| 5763 | slice = Pdata_poplist(self->stack, x); |
| 5764 | if (!slice) |
| 5765 | return -1; |
| 5766 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5767 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5768 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5769 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5770 | } |
| 5771 | else { |
| 5772 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5773 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5774 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5775 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5776 | if (append_func == NULL) |
| 5777 | return -1; |
| 5778 | for (i = x; i < len; i++) { |
| 5779 | PyObject *result; |
| 5780 | |
| 5781 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5782 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5783 | if (result == NULL) { |
| 5784 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5785 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5786 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5787 | return -1; |
| 5788 | } |
| 5789 | Py_DECREF(result); |
| 5790 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5791 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5792 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | return 0; |
| 5796 | } |
| 5797 | |
| 5798 | static int |
| 5799 | load_append(UnpicklerObject *self) |
| 5800 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5801 | if (Py_SIZE(self->stack) - 1 <= 0) |
| 5802 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5803 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5804 | } |
| 5805 | |
| 5806 | static int |
| 5807 | load_appends(UnpicklerObject *self) |
| 5808 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5809 | Py_ssize_t i = marker(self); |
| 5810 | if (i < 0) |
| 5811 | return -1; |
| 5812 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5813 | } |
| 5814 | |
| 5815 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5816 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5817 | { |
| 5818 | PyObject *value, *key; |
| 5819 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5820 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5821 | int status = 0; |
| 5822 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5823 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5824 | if (x > len || x <= 0) |
| 5825 | return stack_underflow(); |
| 5826 | if (len == x) /* nothing to do */ |
| 5827 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5828 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5829 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5830 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5831 | PyErr_SetString(st->UnpicklingError, |
| 5832 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5833 | return -1; |
| 5834 | } |
| 5835 | |
| 5836 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5837 | that supports the __setitem__ attribute. */ |
| 5838 | dict = self->stack->data[x - 1]; |
| 5839 | |
| 5840 | for (i = x + 1; i < len; i += 2) { |
| 5841 | key = self->stack->data[i - 1]; |
| 5842 | value = self->stack->data[i]; |
| 5843 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5844 | status = -1; |
| 5845 | break; |
| 5846 | } |
| 5847 | } |
| 5848 | |
| 5849 | Pdata_clear(self->stack, x); |
| 5850 | return status; |
| 5851 | } |
| 5852 | |
| 5853 | static int |
| 5854 | load_setitem(UnpicklerObject *self) |
| 5855 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5856 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5857 | } |
| 5858 | |
| 5859 | static int |
| 5860 | load_setitems(UnpicklerObject *self) |
| 5861 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5862 | Py_ssize_t i = marker(self); |
| 5863 | if (i < 0) |
| 5864 | return -1; |
| 5865 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5866 | } |
| 5867 | |
| 5868 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5869 | load_additems(UnpicklerObject *self) |
| 5870 | { |
| 5871 | PyObject *set; |
| 5872 | Py_ssize_t mark, len, i; |
| 5873 | |
| 5874 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5875 | if (mark < 0) |
| 5876 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5877 | len = Py_SIZE(self->stack); |
| 5878 | if (mark > len || mark <= 0) |
| 5879 | return stack_underflow(); |
| 5880 | if (len == mark) /* nothing to do */ |
| 5881 | return 0; |
| 5882 | |
| 5883 | set = self->stack->data[mark - 1]; |
| 5884 | |
| 5885 | if (PySet_Check(set)) { |
| 5886 | PyObject *items; |
| 5887 | int status; |
| 5888 | |
| 5889 | items = Pdata_poptuple(self->stack, mark); |
| 5890 | if (items == NULL) |
| 5891 | return -1; |
| 5892 | |
| 5893 | status = _PySet_Update(set, items); |
| 5894 | Py_DECREF(items); |
| 5895 | return status; |
| 5896 | } |
| 5897 | else { |
| 5898 | PyObject *add_func; |
| 5899 | _Py_IDENTIFIER(add); |
| 5900 | |
| 5901 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5902 | if (add_func == NULL) |
| 5903 | return -1; |
| 5904 | for (i = mark; i < len; i++) { |
| 5905 | PyObject *result; |
| 5906 | PyObject *item; |
| 5907 | |
| 5908 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5909 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5910 | if (result == NULL) { |
| 5911 | Pdata_clear(self->stack, i + 1); |
| 5912 | Py_SIZE(self->stack) = mark; |
| 5913 | return -1; |
| 5914 | } |
| 5915 | Py_DECREF(result); |
| 5916 | } |
| 5917 | Py_SIZE(self->stack) = mark; |
| 5918 | } |
| 5919 | |
| 5920 | return 0; |
| 5921 | } |
| 5922 | |
| 5923 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5924 | load_build(UnpicklerObject *self) |
| 5925 | { |
| 5926 | PyObject *state, *inst, *slotstate; |
| 5927 | PyObject *setstate; |
| 5928 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5929 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5930 | |
| 5931 | /* Stack is ... instance, state. We want to leave instance at |
| 5932 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5933 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5934 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5935 | return stack_underflow(); |
| 5936 | |
| 5937 | PDATA_POP(self->stack, state); |
| 5938 | if (state == NULL) |
| 5939 | return -1; |
| 5940 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5941 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5942 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5943 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5944 | if (setstate == NULL) { |
| 5945 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5946 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5947 | else { |
| 5948 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5949 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5950 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5951 | } |
| 5952 | else { |
| 5953 | PyObject *result; |
| 5954 | |
| 5955 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5956 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5957 | Py_DECREF(setstate); |
| 5958 | if (result == NULL) |
| 5959 | return -1; |
| 5960 | Py_DECREF(result); |
| 5961 | return 0; |
| 5962 | } |
| 5963 | |
| 5964 | /* A default __setstate__. First see whether state embeds a |
| 5965 | * slot state dict too (a proto 2 addition). |
| 5966 | */ |
| 5967 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5968 | PyObject *tmp = state; |
| 5969 | |
| 5970 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5971 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5972 | Py_INCREF(state); |
| 5973 | Py_INCREF(slotstate); |
| 5974 | Py_DECREF(tmp); |
| 5975 | } |
| 5976 | else |
| 5977 | slotstate = NULL; |
| 5978 | |
| 5979 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5980 | if (state != Py_None) { |
| 5981 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5982 | PyObject *d_key, *d_value; |
| 5983 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5984 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5985 | |
| 5986 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5987 | PickleState *st = _Pickle_GetGlobalState(); |
| 5988 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5989 | goto error; |
| 5990 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5991 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5992 | if (dict == NULL) |
| 5993 | goto error; |
| 5994 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5995 | i = 0; |
| 5996 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5997 | /* normally the keys for instance attributes are |
| 5998 | interned. we should try to do that here. */ |
| 5999 | Py_INCREF(d_key); |
| 6000 | if (PyUnicode_CheckExact(d_key)) |
| 6001 | PyUnicode_InternInPlace(&d_key); |
| 6002 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6003 | Py_DECREF(d_key); |
| 6004 | goto error; |
| 6005 | } |
| 6006 | Py_DECREF(d_key); |
| 6007 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6008 | Py_DECREF(dict); |
| 6009 | } |
| 6010 | |
| 6011 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6012 | if (slotstate != NULL) { |
| 6013 | PyObject *d_key, *d_value; |
| 6014 | Py_ssize_t i; |
| 6015 | |
| 6016 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6017 | PickleState *st = _Pickle_GetGlobalState(); |
| 6018 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6019 | "slot state is not a dictionary"); |
| 6020 | goto error; |
| 6021 | } |
| 6022 | i = 0; |
| 6023 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6024 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6025 | goto error; |
| 6026 | } |
| 6027 | } |
| 6028 | |
| 6029 | if (0) { |
| 6030 | error: |
| 6031 | status = -1; |
| 6032 | } |
| 6033 | |
| 6034 | Py_DECREF(state); |
| 6035 | Py_XDECREF(slotstate); |
| 6036 | return status; |
| 6037 | } |
| 6038 | |
| 6039 | static int |
| 6040 | load_mark(UnpicklerObject *self) |
| 6041 | { |
| 6042 | |
| 6043 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6044 | * object stack and a mark stack. Here we push a mark onto the |
| 6045 | * mark stack. |
| 6046 | */ |
| 6047 | |
| 6048 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6049 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6050 | |
| 6051 | /* Use the size_t type to check for overflow. */ |
| 6052 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6053 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6054 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6055 | PyErr_NoMemory(); |
| 6056 | return -1; |
| 6057 | } |
| 6058 | |
| 6059 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6060 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6061 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6062 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6063 | if (self->marks == NULL) { |
| 6064 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6065 | PyErr_NoMemory(); |
| 6066 | return -1; |
| 6067 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6068 | self->marks_size = (Py_ssize_t)alloc; |
| 6069 | } |
| 6070 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6071 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6072 | |
| 6073 | return 0; |
| 6074 | } |
| 6075 | |
| 6076 | static int |
| 6077 | load_reduce(UnpicklerObject *self) |
| 6078 | { |
| 6079 | PyObject *callable = NULL; |
| 6080 | PyObject *argtup = NULL; |
| 6081 | PyObject *obj = NULL; |
| 6082 | |
| 6083 | PDATA_POP(self->stack, argtup); |
| 6084 | if (argtup == NULL) |
| 6085 | return -1; |
| 6086 | PDATA_POP(self->stack, callable); |
| 6087 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6088 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6089 | Py_DECREF(callable); |
| 6090 | } |
| 6091 | Py_DECREF(argtup); |
| 6092 | |
| 6093 | if (obj == NULL) |
| 6094 | return -1; |
| 6095 | |
| 6096 | PDATA_PUSH(self->stack, obj, -1); |
| 6097 | return 0; |
| 6098 | } |
| 6099 | |
| 6100 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6101 | * is the first opcode for protocols >= 2. |
| 6102 | */ |
| 6103 | static int |
| 6104 | load_proto(UnpicklerObject *self) |
| 6105 | { |
| 6106 | char *s; |
| 6107 | int i; |
| 6108 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6109 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6110 | return -1; |
| 6111 | |
| 6112 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6113 | if (i <= HIGHEST_PROTOCOL) { |
| 6114 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6115 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6116 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6117 | |
| 6118 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6119 | return -1; |
| 6120 | } |
| 6121 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6122 | static int |
| 6123 | load_frame(UnpicklerObject *self) |
| 6124 | { |
| 6125 | char *s; |
| 6126 | Py_ssize_t frame_len; |
| 6127 | |
| 6128 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6129 | return -1; |
| 6130 | |
| 6131 | frame_len = calc_binsize(s, 8); |
| 6132 | if (frame_len < 0) { |
| 6133 | PyErr_Format(PyExc_OverflowError, |
| 6134 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6135 | PY_SSIZE_T_MAX); |
| 6136 | return -1; |
| 6137 | } |
| 6138 | |
| 6139 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6140 | return -1; |
| 6141 | |
| 6142 | /* Rewind to start of frame */ |
| 6143 | self->next_read_idx -= frame_len; |
| 6144 | return 0; |
| 6145 | } |
| 6146 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6147 | static PyObject * |
| 6148 | load(UnpicklerObject *self) |
| 6149 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6150 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6151 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6152 | |
| 6153 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6154 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6155 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6156 | Pdata_clear(self->stack, 0); |
| 6157 | |
| 6158 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6159 | #define OP(opcode, load_func) \ |
| 6160 | case opcode: if (load_func(self) < 0) break; continue; |
| 6161 | |
| 6162 | #define OP_ARG(opcode, load_func, arg) \ |
| 6163 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6164 | |
| 6165 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6166 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6167 | break; |
| 6168 | |
| 6169 | switch ((enum opcode)s[0]) { |
| 6170 | OP(NONE, load_none) |
| 6171 | OP(BININT, load_binint) |
| 6172 | OP(BININT1, load_binint1) |
| 6173 | OP(BININT2, load_binint2) |
| 6174 | OP(INT, load_int) |
| 6175 | OP(LONG, load_long) |
| 6176 | OP_ARG(LONG1, load_counted_long, 1) |
| 6177 | OP_ARG(LONG4, load_counted_long, 4) |
| 6178 | OP(FLOAT, load_float) |
| 6179 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6180 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6181 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6182 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6183 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6184 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6185 | OP(STRING, load_string) |
| 6186 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6187 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6188 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6189 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6190 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6191 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6192 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6193 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6194 | OP(TUPLE, load_tuple) |
| 6195 | OP(EMPTY_LIST, load_empty_list) |
| 6196 | OP(LIST, load_list) |
| 6197 | OP(EMPTY_DICT, load_empty_dict) |
| 6198 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6199 | OP(EMPTY_SET, load_empty_set) |
| 6200 | OP(ADDITEMS, load_additems) |
| 6201 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6202 | OP(OBJ, load_obj) |
| 6203 | OP(INST, load_inst) |
| 6204 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6205 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6206 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6207 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6208 | OP(APPEND, load_append) |
| 6209 | OP(APPENDS, load_appends) |
| 6210 | OP(BUILD, load_build) |
| 6211 | OP(DUP, load_dup) |
| 6212 | OP(BINGET, load_binget) |
| 6213 | OP(LONG_BINGET, load_long_binget) |
| 6214 | OP(GET, load_get) |
| 6215 | OP(MARK, load_mark) |
| 6216 | OP(BINPUT, load_binput) |
| 6217 | OP(LONG_BINPUT, load_long_binput) |
| 6218 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6219 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6220 | OP(POP, load_pop) |
| 6221 | OP(POP_MARK, load_pop_mark) |
| 6222 | OP(SETITEM, load_setitem) |
| 6223 | OP(SETITEMS, load_setitems) |
| 6224 | OP(PERSID, load_persid) |
| 6225 | OP(BINPERSID, load_binpersid) |
| 6226 | OP(REDUCE, load_reduce) |
| 6227 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6228 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6229 | OP_ARG(EXT1, load_extension, 1) |
| 6230 | OP_ARG(EXT2, load_extension, 2) |
| 6231 | OP_ARG(EXT4, load_extension, 4) |
| 6232 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6233 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6234 | |
| 6235 | case STOP: |
| 6236 | break; |
| 6237 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6238 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6239 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6240 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6241 | } |
| 6242 | else { |
| 6243 | PickleState *st = _Pickle_GetGlobalState(); |
| 6244 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6245 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6246 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6247 | return NULL; |
| 6248 | } |
| 6249 | |
| 6250 | break; /* and we are done! */ |
| 6251 | } |
| 6252 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6253 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6254 | return NULL; |
| 6255 | } |
| 6256 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6257 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6258 | return NULL; |
| 6259 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6260 | PDATA_POP(self->stack, value); |
| 6261 | return value; |
| 6262 | } |
| 6263 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6264 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6265 | |
| 6266 | _pickle.Unpickler.load |
| 6267 | |
| 6268 | Load a pickle. |
| 6269 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6270 | Read a pickled object representation from the open file object given |
| 6271 | in the constructor, and return the reconstituted object hierarchy |
| 6272 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6273 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6274 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6275 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6276 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6277 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6278 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6279 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6280 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6281 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6282 | segfaulting if a subclass overridden __init__ with a function that does |
| 6283 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6284 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6285 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6286 | PickleState *st = _Pickle_GetGlobalState(); |
| 6287 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6288 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6289 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6290 | return NULL; |
| 6291 | } |
| 6292 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6293 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6294 | } |
| 6295 | |
| 6296 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6297 | function is used for loading any global (i.e., functions), not just |
| 6298 | classes. The name is kept only for backward compatibility. */ |
| 6299 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6300 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6301 | |
| 6302 | _pickle.Unpickler.find_class |
| 6303 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6304 | module_name: object |
| 6305 | global_name: object |
| 6306 | / |
| 6307 | |
| 6308 | Return an object from a specified module. |
| 6309 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6310 | If necessary, the module will be imported. Subclasses may override |
| 6311 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6312 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6313 | |
| 6314 | This method is called whenever a class or a function object is |
| 6315 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6316 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6317 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6318 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6319 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6320 | PyObject *module_name, |
| 6321 | PyObject *global_name) |
| 6322 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6323 | { |
| 6324 | PyObject *global; |
| 6325 | PyObject *modules_dict; |
| 6326 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6327 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6328 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6329 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6330 | Python 3.x. We do this only with old pickle protocols and when the |
| 6331 | user has not disabled the feature. */ |
| 6332 | if (self->proto < 3 && self->fix_imports) { |
| 6333 | PyObject *key; |
| 6334 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6335 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6336 | |
| 6337 | /* Check if the global (i.e., a function or a class) was renamed |
| 6338 | or moved to another module. */ |
| 6339 | key = PyTuple_Pack(2, module_name, global_name); |
| 6340 | if (key == NULL) |
| 6341 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6342 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6343 | Py_DECREF(key); |
| 6344 | if (item) { |
| 6345 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6346 | PyErr_Format(PyExc_RuntimeError, |
| 6347 | "_compat_pickle.NAME_MAPPING values should be " |
| 6348 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6349 | return NULL; |
| 6350 | } |
| 6351 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6352 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6353 | if (!PyUnicode_Check(module_name) || |
| 6354 | !PyUnicode_Check(global_name)) { |
| 6355 | PyErr_Format(PyExc_RuntimeError, |
| 6356 | "_compat_pickle.NAME_MAPPING values should be " |
| 6357 | "pairs of str, not (%.200s, %.200s)", |
| 6358 | Py_TYPE(module_name)->tp_name, |
| 6359 | Py_TYPE(global_name)->tp_name); |
| 6360 | return NULL; |
| 6361 | } |
| 6362 | } |
| 6363 | else if (PyErr_Occurred()) { |
| 6364 | return NULL; |
| 6365 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6366 | else { |
| 6367 | /* Check if the module was renamed. */ |
| 6368 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6369 | if (item) { |
| 6370 | if (!PyUnicode_Check(item)) { |
| 6371 | PyErr_Format(PyExc_RuntimeError, |
| 6372 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6373 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6374 | return NULL; |
| 6375 | } |
| 6376 | module_name = item; |
| 6377 | } |
| 6378 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6379 | return NULL; |
| 6380 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6381 | } |
| 6382 | } |
| 6383 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6384 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6385 | if (modules_dict == NULL) { |
| 6386 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6387 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6388 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6389 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6390 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6391 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6392 | if (PyErr_Occurred()) |
| 6393 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6394 | module = PyImport_Import(module_name); |
| 6395 | if (module == NULL) |
| 6396 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6397 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6398 | Py_DECREF(module); |
| 6399 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6400 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6401 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6402 | } |
| 6403 | return global; |
| 6404 | } |
| 6405 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6406 | /*[clinic input] |
| 6407 | |
| 6408 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6409 | |
| 6410 | Returns size in memory, in bytes. |
| 6411 | [clinic start generated code]*/ |
| 6412 | |
| 6413 | static Py_ssize_t |
| 6414 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6415 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6416 | { |
| 6417 | Py_ssize_t res; |
| 6418 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 6419 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6420 | if (self->memo != NULL) |
| 6421 | res += self->memo_size * sizeof(PyObject *); |
| 6422 | if (self->marks != NULL) |
| 6423 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6424 | if (self->input_line != NULL) |
| 6425 | res += strlen(self->input_line) + 1; |
| 6426 | if (self->encoding != NULL) |
| 6427 | res += strlen(self->encoding) + 1; |
| 6428 | if (self->errors != NULL) |
| 6429 | res += strlen(self->errors) + 1; |
| 6430 | return res; |
| 6431 | } |
| 6432 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6433 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6434 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6435 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6436 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6437 | {NULL, NULL} /* sentinel */ |
| 6438 | }; |
| 6439 | |
| 6440 | static void |
| 6441 | Unpickler_dealloc(UnpicklerObject *self) |
| 6442 | { |
| 6443 | PyObject_GC_UnTrack((PyObject *)self); |
| 6444 | Py_XDECREF(self->readline); |
| 6445 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6446 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6447 | Py_XDECREF(self->stack); |
| 6448 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6449 | if (self->buffer.buf != NULL) { |
| 6450 | PyBuffer_Release(&self->buffer); |
| 6451 | self->buffer.buf = NULL; |
| 6452 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6453 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6454 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6455 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6456 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6457 | PyMem_Free(self->encoding); |
| 6458 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6459 | |
| 6460 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6461 | } |
| 6462 | |
| 6463 | static int |
| 6464 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6465 | { |
| 6466 | Py_VISIT(self->readline); |
| 6467 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6468 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6469 | Py_VISIT(self->stack); |
| 6470 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6471 | return 0; |
| 6472 | } |
| 6473 | |
| 6474 | static int |
| 6475 | Unpickler_clear(UnpicklerObject *self) |
| 6476 | { |
| 6477 | Py_CLEAR(self->readline); |
| 6478 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6479 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6480 | Py_CLEAR(self->stack); |
| 6481 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6482 | if (self->buffer.buf != NULL) { |
| 6483 | PyBuffer_Release(&self->buffer); |
| 6484 | self->buffer.buf = NULL; |
| 6485 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6486 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6487 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6488 | PyMem_Free(self->marks); |
| 6489 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6490 | PyMem_Free(self->input_line); |
| 6491 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6492 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6493 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6494 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6495 | self->errors = NULL; |
| 6496 | |
| 6497 | return 0; |
| 6498 | } |
| 6499 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6500 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6501 | |
| 6502 | _pickle.Unpickler.__init__ |
| 6503 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6504 | file: object |
| 6505 | * |
| 6506 | fix_imports: bool = True |
| 6507 | encoding: str = 'ASCII' |
| 6508 | errors: str = 'strict' |
| 6509 | |
| 6510 | This takes a binary file for reading a pickle data stream. |
| 6511 | |
| 6512 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6513 | protocol argument is needed. Bytes past the pickled object's |
| 6514 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6515 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6516 | The argument *file* must have two methods, a read() method that takes |
| 6517 | an integer argument, and a readline() method that requires no |
| 6518 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6519 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6520 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6521 | |
| 6522 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6523 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6524 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6525 | 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] | 6526 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6527 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6528 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6529 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6530 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6531 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6532 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6533 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6534 | int fix_imports, const char *encoding, |
| 6535 | const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6536 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6537 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6538 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6539 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6540 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6541 | if (self->read != NULL) |
| 6542 | (void)Unpickler_clear(self); |
| 6543 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6544 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6545 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6546 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6547 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6548 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6549 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6550 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6551 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6552 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6553 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6554 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6555 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6556 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6557 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6558 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6559 | } |
| 6560 | else { |
| 6561 | self->pers_func = NULL; |
| 6562 | } |
| 6563 | |
| 6564 | self->stack = (Pdata *)Pdata_New(); |
| 6565 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6566 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6567 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6568 | self->memo_size = 32; |
| 6569 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6570 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6571 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6572 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6573 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6574 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6575 | return 0; |
| 6576 | } |
| 6577 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6578 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6579 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6580 | * avoid breaking code like: |
| 6581 | * unpickler.memo.clear() |
| 6582 | * and |
| 6583 | * unpickler.memo = saved_memo |
| 6584 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6585 | * it. Note that we don't implement the entire mapping API here. This is |
| 6586 | * intentional, as these should be treated as black-box implementation details. |
| 6587 | * |
| 6588 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6589 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6590 | */ |
| 6591 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6592 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6593 | _pickle.UnpicklerMemoProxy.clear |
| 6594 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6595 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6596 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6597 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6598 | static PyObject * |
| 6599 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6600 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6601 | { |
| 6602 | _Unpickler_MemoCleanup(self->unpickler); |
| 6603 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6604 | if (self->unpickler->memo == NULL) |
| 6605 | return NULL; |
| 6606 | Py_RETURN_NONE; |
| 6607 | } |
| 6608 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6609 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6610 | _pickle.UnpicklerMemoProxy.copy |
| 6611 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6612 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6613 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6614 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6615 | static PyObject * |
| 6616 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6617 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6618 | { |
| 6619 | Py_ssize_t i; |
| 6620 | PyObject *new_memo = PyDict_New(); |
| 6621 | if (new_memo == NULL) |
| 6622 | return NULL; |
| 6623 | |
| 6624 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6625 | int status; |
| 6626 | PyObject *key, *value; |
| 6627 | |
| 6628 | value = self->unpickler->memo[i]; |
| 6629 | if (value == NULL) |
| 6630 | continue; |
| 6631 | |
| 6632 | key = PyLong_FromSsize_t(i); |
| 6633 | if (key == NULL) |
| 6634 | goto error; |
| 6635 | status = PyDict_SetItem(new_memo, key, value); |
| 6636 | Py_DECREF(key); |
| 6637 | if (status < 0) |
| 6638 | goto error; |
| 6639 | } |
| 6640 | return new_memo; |
| 6641 | |
| 6642 | error: |
| 6643 | Py_DECREF(new_memo); |
| 6644 | return NULL; |
| 6645 | } |
| 6646 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6647 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6648 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6649 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6650 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6651 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6652 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6653 | static PyObject * |
| 6654 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6655 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6656 | { |
| 6657 | PyObject *reduce_value; |
| 6658 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6659 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6660 | if (contents == NULL) |
| 6661 | return NULL; |
| 6662 | |
| 6663 | reduce_value = PyTuple_New(2); |
| 6664 | if (reduce_value == NULL) { |
| 6665 | Py_DECREF(contents); |
| 6666 | return NULL; |
| 6667 | } |
| 6668 | constructor_args = PyTuple_New(1); |
| 6669 | if (constructor_args == NULL) { |
| 6670 | Py_DECREF(contents); |
| 6671 | Py_DECREF(reduce_value); |
| 6672 | return NULL; |
| 6673 | } |
| 6674 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6675 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6676 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6677 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6678 | return reduce_value; |
| 6679 | } |
| 6680 | |
| 6681 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6682 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6683 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6684 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6685 | {NULL, NULL} /* sentinel */ |
| 6686 | }; |
| 6687 | |
| 6688 | static void |
| 6689 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6690 | { |
| 6691 | PyObject_GC_UnTrack(self); |
| 6692 | Py_XDECREF(self->unpickler); |
| 6693 | PyObject_GC_Del((PyObject *)self); |
| 6694 | } |
| 6695 | |
| 6696 | static int |
| 6697 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6698 | visitproc visit, void *arg) |
| 6699 | { |
| 6700 | Py_VISIT(self->unpickler); |
| 6701 | return 0; |
| 6702 | } |
| 6703 | |
| 6704 | static int |
| 6705 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6706 | { |
| 6707 | Py_CLEAR(self->unpickler); |
| 6708 | return 0; |
| 6709 | } |
| 6710 | |
| 6711 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6712 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6713 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6714 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6715 | 0, |
| 6716 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6717 | 0, /* tp_print */ |
| 6718 | 0, /* tp_getattr */ |
| 6719 | 0, /* tp_setattr */ |
| 6720 | 0, /* tp_compare */ |
| 6721 | 0, /* tp_repr */ |
| 6722 | 0, /* tp_as_number */ |
| 6723 | 0, /* tp_as_sequence */ |
| 6724 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6725 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6726 | 0, /* tp_call */ |
| 6727 | 0, /* tp_str */ |
| 6728 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6729 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6730 | 0, /* tp_as_buffer */ |
| 6731 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6732 | 0, /* tp_doc */ |
| 6733 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6734 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6735 | 0, /* tp_richcompare */ |
| 6736 | 0, /* tp_weaklistoffset */ |
| 6737 | 0, /* tp_iter */ |
| 6738 | 0, /* tp_iternext */ |
| 6739 | unpicklerproxy_methods, /* tp_methods */ |
| 6740 | }; |
| 6741 | |
| 6742 | static PyObject * |
| 6743 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6744 | { |
| 6745 | UnpicklerMemoProxyObject *self; |
| 6746 | |
| 6747 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6748 | &UnpicklerMemoProxyType); |
| 6749 | if (self == NULL) |
| 6750 | return NULL; |
| 6751 | Py_INCREF(unpickler); |
| 6752 | self->unpickler = unpickler; |
| 6753 | PyObject_GC_Track(self); |
| 6754 | return (PyObject *)self; |
| 6755 | } |
| 6756 | |
| 6757 | /*****************************************************************************/ |
| 6758 | |
| 6759 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6760 | static PyObject * |
| 6761 | Unpickler_get_memo(UnpicklerObject *self) |
| 6762 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6763 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6764 | } |
| 6765 | |
| 6766 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6767 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6768 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6769 | PyObject **new_memo; |
| 6770 | Py_ssize_t new_memo_size = 0; |
| 6771 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6772 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6773 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6774 | PyErr_SetString(PyExc_TypeError, |
| 6775 | "attribute deletion is not supported"); |
| 6776 | return -1; |
| 6777 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6778 | |
| 6779 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6780 | UnpicklerObject *unpickler = |
| 6781 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6782 | |
| 6783 | new_memo_size = unpickler->memo_size; |
| 6784 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6785 | if (new_memo == NULL) |
| 6786 | return -1; |
| 6787 | |
| 6788 | for (i = 0; i < new_memo_size; i++) { |
| 6789 | Py_XINCREF(unpickler->memo[i]); |
| 6790 | new_memo[i] = unpickler->memo[i]; |
| 6791 | } |
| 6792 | } |
| 6793 | else if (PyDict_Check(obj)) { |
| 6794 | Py_ssize_t i = 0; |
| 6795 | PyObject *key, *value; |
| 6796 | |
| 6797 | new_memo_size = PyDict_Size(obj); |
| 6798 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6799 | if (new_memo == NULL) |
| 6800 | return -1; |
| 6801 | |
| 6802 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6803 | Py_ssize_t idx; |
| 6804 | if (!PyLong_Check(key)) { |
| 6805 | PyErr_SetString(PyExc_TypeError, |
| 6806 | "memo key must be integers"); |
| 6807 | goto error; |
| 6808 | } |
| 6809 | idx = PyLong_AsSsize_t(key); |
| 6810 | if (idx == -1 && PyErr_Occurred()) |
| 6811 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6812 | if (idx < 0) { |
| 6813 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6814 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6815 | goto error; |
| 6816 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6817 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6818 | goto error; |
| 6819 | } |
| 6820 | } |
| 6821 | else { |
| 6822 | PyErr_Format(PyExc_TypeError, |
| 6823 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6824 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6825 | return -1; |
| 6826 | } |
| 6827 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6828 | _Unpickler_MemoCleanup(self); |
| 6829 | self->memo_size = new_memo_size; |
| 6830 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6831 | |
| 6832 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6833 | |
| 6834 | error: |
| 6835 | if (new_memo_size) { |
| 6836 | i = new_memo_size; |
| 6837 | while (--i >= 0) { |
| 6838 | Py_XDECREF(new_memo[i]); |
| 6839 | } |
| 6840 | PyMem_FREE(new_memo); |
| 6841 | } |
| 6842 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6843 | } |
| 6844 | |
| 6845 | static PyObject * |
| 6846 | Unpickler_get_persload(UnpicklerObject *self) |
| 6847 | { |
| 6848 | if (self->pers_func == NULL) |
| 6849 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6850 | else |
| 6851 | Py_INCREF(self->pers_func); |
| 6852 | return self->pers_func; |
| 6853 | } |
| 6854 | |
| 6855 | static int |
| 6856 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6857 | { |
| 6858 | PyObject *tmp; |
| 6859 | |
| 6860 | if (value == NULL) { |
| 6861 | PyErr_SetString(PyExc_TypeError, |
| 6862 | "attribute deletion is not supported"); |
| 6863 | return -1; |
| 6864 | } |
| 6865 | if (!PyCallable_Check(value)) { |
| 6866 | PyErr_SetString(PyExc_TypeError, |
| 6867 | "persistent_load must be a callable taking " |
| 6868 | "one argument"); |
| 6869 | return -1; |
| 6870 | } |
| 6871 | |
| 6872 | tmp = self->pers_func; |
| 6873 | Py_INCREF(value); |
| 6874 | self->pers_func = value; |
| 6875 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6876 | |
| 6877 | return 0; |
| 6878 | } |
| 6879 | |
| 6880 | static PyGetSetDef Unpickler_getsets[] = { |
| 6881 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6882 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6883 | (setter)Unpickler_set_persload}, |
| 6884 | {NULL} |
| 6885 | }; |
| 6886 | |
| 6887 | static PyTypeObject Unpickler_Type = { |
| 6888 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6889 | "_pickle.Unpickler", /*tp_name*/ |
| 6890 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6891 | 0, /*tp_itemsize*/ |
| 6892 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6893 | 0, /*tp_print*/ |
| 6894 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6895 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6896 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6897 | 0, /*tp_repr*/ |
| 6898 | 0, /*tp_as_number*/ |
| 6899 | 0, /*tp_as_sequence*/ |
| 6900 | 0, /*tp_as_mapping*/ |
| 6901 | 0, /*tp_hash*/ |
| 6902 | 0, /*tp_call*/ |
| 6903 | 0, /*tp_str*/ |
| 6904 | 0, /*tp_getattro*/ |
| 6905 | 0, /*tp_setattro*/ |
| 6906 | 0, /*tp_as_buffer*/ |
| 6907 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6908 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6909 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6910 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6911 | 0, /*tp_richcompare*/ |
| 6912 | 0, /*tp_weaklistoffset*/ |
| 6913 | 0, /*tp_iter*/ |
| 6914 | 0, /*tp_iternext*/ |
| 6915 | Unpickler_methods, /*tp_methods*/ |
| 6916 | 0, /*tp_members*/ |
| 6917 | Unpickler_getsets, /*tp_getset*/ |
| 6918 | 0, /*tp_base*/ |
| 6919 | 0, /*tp_dict*/ |
| 6920 | 0, /*tp_descr_get*/ |
| 6921 | 0, /*tp_descr_set*/ |
| 6922 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6923 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6924 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6925 | PyType_GenericNew, /*tp_new*/ |
| 6926 | PyObject_GC_Del, /*tp_free*/ |
| 6927 | 0, /*tp_is_gc*/ |
| 6928 | }; |
| 6929 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6930 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6931 | |
| 6932 | _pickle.dump |
| 6933 | |
| 6934 | obj: object |
| 6935 | file: object |
| 6936 | protocol: object = NULL |
| 6937 | * |
| 6938 | fix_imports: bool = True |
| 6939 | |
| 6940 | Write a pickled representation of obj to the open file object file. |
| 6941 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6942 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 6943 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6944 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6945 | The optional *protocol* argument tells the pickler to use the given |
| 6946 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 6947 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6948 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6949 | Specifying a negative protocol version selects the highest protocol |
| 6950 | version supported. The higher the protocol used, the more recent the |
| 6951 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6952 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6953 | The *file* argument must have a write() method that accepts a single |
| 6954 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6955 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6956 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6957 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6958 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 6959 | to map the new Python 3 names to the old module names used in Python |
| 6960 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6961 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6962 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6963 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6964 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, |
| 6965 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 6966 | /*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6967 | { |
| 6968 | PicklerObject *pickler = _Pickler_New(); |
| 6969 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6970 | if (pickler == NULL) |
| 6971 | return NULL; |
| 6972 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6973 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6974 | goto error; |
| 6975 | |
| 6976 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6977 | goto error; |
| 6978 | |
| 6979 | if (dump(pickler, obj) < 0) |
| 6980 | goto error; |
| 6981 | |
| 6982 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6983 | goto error; |
| 6984 | |
| 6985 | Py_DECREF(pickler); |
| 6986 | Py_RETURN_NONE; |
| 6987 | |
| 6988 | error: |
| 6989 | Py_XDECREF(pickler); |
| 6990 | return NULL; |
| 6991 | } |
| 6992 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6993 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6994 | |
| 6995 | _pickle.dumps |
| 6996 | |
| 6997 | obj: object |
| 6998 | protocol: object = NULL |
| 6999 | * |
| 7000 | fix_imports: bool = True |
| 7001 | |
| 7002 | Return the pickled representation of the object as a bytes object. |
| 7003 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7004 | The optional *protocol* argument tells the pickler to use the given |
| 7005 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7006 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7007 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7008 | Specifying a negative protocol version selects the highest protocol |
| 7009 | version supported. The higher the protocol used, the more recent the |
| 7010 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7011 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7012 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7013 | try to map the new Python 3 names to the old module names used in |
| 7014 | 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] | 7015 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7016 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7017 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7018 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, |
| 7019 | int fix_imports) |
| 7020 | /*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7021 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7022 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7023 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7024 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7025 | if (pickler == NULL) |
| 7026 | return NULL; |
| 7027 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7028 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7029 | goto error; |
| 7030 | |
| 7031 | if (dump(pickler, obj) < 0) |
| 7032 | goto error; |
| 7033 | |
| 7034 | result = _Pickler_GetString(pickler); |
| 7035 | Py_DECREF(pickler); |
| 7036 | return result; |
| 7037 | |
| 7038 | error: |
| 7039 | Py_XDECREF(pickler); |
| 7040 | return NULL; |
| 7041 | } |
| 7042 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7043 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7044 | |
| 7045 | _pickle.load |
| 7046 | |
| 7047 | file: object |
| 7048 | * |
| 7049 | fix_imports: bool = True |
| 7050 | encoding: str = 'ASCII' |
| 7051 | errors: str = 'strict' |
| 7052 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7053 | 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] | 7054 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7055 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7056 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7057 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7058 | The protocol version of the pickle is detected automatically, so no |
| 7059 | protocol argument is needed. Bytes past the pickled object's |
| 7060 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7061 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7062 | The argument *file* must have two methods, a read() method that takes |
| 7063 | an integer argument, and a readline() method that requires no |
| 7064 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7065 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7066 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7067 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7068 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7069 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7070 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7071 | map the old Python 2 names to the new names used in Python 3. The |
| 7072 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7073 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7074 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7075 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7076 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7077 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7078 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7079 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, |
| 7080 | const char *encoding, const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7081 | /*[clinic end generated code: output=798f1c57cb2b4eb1 input=01b44dd3fc07afa7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7082 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7083 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7084 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7085 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7086 | if (unpickler == NULL) |
| 7087 | return NULL; |
| 7088 | |
| 7089 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7090 | goto error; |
| 7091 | |
| 7092 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7093 | goto error; |
| 7094 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7095 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7096 | |
| 7097 | result = load(unpickler); |
| 7098 | Py_DECREF(unpickler); |
| 7099 | return result; |
| 7100 | |
| 7101 | error: |
| 7102 | Py_XDECREF(unpickler); |
| 7103 | return NULL; |
| 7104 | } |
| 7105 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7106 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7107 | |
| 7108 | _pickle.loads |
| 7109 | |
| 7110 | data: object |
| 7111 | * |
| 7112 | fix_imports: bool = True |
| 7113 | encoding: str = 'ASCII' |
| 7114 | errors: str = 'strict' |
| 7115 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7116 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7117 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7118 | The protocol version of the pickle is detected automatically, so no |
| 7119 | protocol argument is needed. Bytes past the pickled object's |
| 7120 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7121 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7122 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7123 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7124 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7125 | map the old Python 2 names to the new names used in Python 3. The |
| 7126 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7127 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7128 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7129 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7130 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7131 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7132 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7133 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, |
| 7134 | const char *encoding, const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7135 | /*[clinic end generated code: output=61e9cdb01e36a736 input=70605948a719feb9]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7136 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7137 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7138 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7139 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7140 | if (unpickler == NULL) |
| 7141 | return NULL; |
| 7142 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7143 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7144 | goto error; |
| 7145 | |
| 7146 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7147 | goto error; |
| 7148 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7149 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7150 | |
| 7151 | result = load(unpickler); |
| 7152 | Py_DECREF(unpickler); |
| 7153 | return result; |
| 7154 | |
| 7155 | error: |
| 7156 | Py_XDECREF(unpickler); |
| 7157 | return NULL; |
| 7158 | } |
| 7159 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7160 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7161 | _PICKLE_DUMP_METHODDEF |
| 7162 | _PICKLE_DUMPS_METHODDEF |
| 7163 | _PICKLE_LOAD_METHODDEF |
| 7164 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7165 | {NULL, NULL} /* sentinel */ |
| 7166 | }; |
| 7167 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7168 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7169 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7170 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7171 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7172 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7173 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7174 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7175 | static void |
| 7176 | pickle_free(PyObject *m) |
| 7177 | { |
| 7178 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7179 | } |
| 7180 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7181 | static int |
| 7182 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7183 | { |
| 7184 | PickleState *st = _Pickle_GetState(m); |
| 7185 | Py_VISIT(st->PickleError); |
| 7186 | Py_VISIT(st->PicklingError); |
| 7187 | Py_VISIT(st->UnpicklingError); |
| 7188 | Py_VISIT(st->dispatch_table); |
| 7189 | Py_VISIT(st->extension_registry); |
| 7190 | Py_VISIT(st->extension_cache); |
| 7191 | Py_VISIT(st->inverted_registry); |
| 7192 | Py_VISIT(st->name_mapping_2to3); |
| 7193 | Py_VISIT(st->import_mapping_2to3); |
| 7194 | Py_VISIT(st->name_mapping_3to2); |
| 7195 | Py_VISIT(st->import_mapping_3to2); |
| 7196 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7197 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7198 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7199 | } |
| 7200 | |
| 7201 | static struct PyModuleDef _picklemodule = { |
| 7202 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7203 | "_pickle", /* m_name */ |
| 7204 | pickle_module_doc, /* m_doc */ |
| 7205 | sizeof(PickleState), /* m_size */ |
| 7206 | pickle_methods, /* m_methods */ |
| 7207 | NULL, /* m_reload */ |
| 7208 | pickle_traverse, /* m_traverse */ |
| 7209 | pickle_clear, /* m_clear */ |
| 7210 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7211 | }; |
| 7212 | |
| 7213 | PyMODINIT_FUNC |
| 7214 | PyInit__pickle(void) |
| 7215 | { |
| 7216 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7217 | PickleState *st; |
| 7218 | |
| 7219 | m = PyState_FindModule(&_picklemodule); |
| 7220 | if (m) { |
| 7221 | Py_INCREF(m); |
| 7222 | return m; |
| 7223 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7224 | |
| 7225 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7226 | return NULL; |
| 7227 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7228 | return NULL; |
| 7229 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7230 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7231 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7232 | return NULL; |
| 7233 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7234 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7235 | |
| 7236 | /* Create the module and add the functions. */ |
| 7237 | m = PyModule_Create(&_picklemodule); |
| 7238 | if (m == NULL) |
| 7239 | return NULL; |
| 7240 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7241 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7242 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7243 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7244 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7245 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7246 | return NULL; |
| 7247 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7248 | st = _Pickle_GetState(m); |
| 7249 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7250 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7251 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7252 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7253 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7254 | st->PicklingError = \ |
| 7255 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7256 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7257 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7258 | st->UnpicklingError = \ |
| 7259 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7260 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7261 | return NULL; |
| 7262 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7263 | Py_INCREF(st->PickleError); |
| 7264 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7265 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7266 | Py_INCREF(st->PicklingError); |
| 7267 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7268 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7269 | Py_INCREF(st->UnpicklingError); |
| 7270 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7271 | return NULL; |
| 7272 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7273 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7274 | return NULL; |
| 7275 | |
| 7276 | return m; |
| 7277 | } |