Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1 | |
| 2 | /* Core extension modules are built-in on some platforms (e.g. Windows). */ |
| 3 | #ifdef Py_BUILD_CORE |
Eric Snow | fc1bf87 | 2017-09-11 18:30:43 -0700 | [diff] [blame] | 4 | #define Py_BUILD_CORE_BUILTIN |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 5 | #undef Py_BUILD_CORE |
| 6 | #endif |
| 7 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8 | #include "Python.h" |
| 9 | #include "structmember.h" |
| 10 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 11 | PyDoc_STRVAR(pickle_module_doc, |
| 12 | "Optimized C implementation for the Python pickle module."); |
| 13 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 14 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 15 | module _pickle |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 16 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 17 | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
| 18 | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
| 19 | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 20 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 21 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 22 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 23 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 24 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 25 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 26 | DEFAULT_PROTOCOL = 3 |
| 27 | }; |
| 28 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 29 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 30 | Extensive docs are in pickletools.py. */ |
| 31 | enum opcode { |
| 32 | MARK = '(', |
| 33 | STOP = '.', |
| 34 | POP = '0', |
| 35 | POP_MARK = '1', |
| 36 | DUP = '2', |
| 37 | FLOAT = 'F', |
| 38 | INT = 'I', |
| 39 | BININT = 'J', |
| 40 | BININT1 = 'K', |
| 41 | LONG = 'L', |
| 42 | BININT2 = 'M', |
| 43 | NONE = 'N', |
| 44 | PERSID = 'P', |
| 45 | BINPERSID = 'Q', |
| 46 | REDUCE = 'R', |
| 47 | STRING = 'S', |
| 48 | BINSTRING = 'T', |
| 49 | SHORT_BINSTRING = 'U', |
| 50 | UNICODE = 'V', |
| 51 | BINUNICODE = 'X', |
| 52 | APPEND = 'a', |
| 53 | BUILD = 'b', |
| 54 | GLOBAL = 'c', |
| 55 | DICT = 'd', |
| 56 | EMPTY_DICT = '}', |
| 57 | APPENDS = 'e', |
| 58 | GET = 'g', |
| 59 | BINGET = 'h', |
| 60 | INST = 'i', |
| 61 | LONG_BINGET = 'j', |
| 62 | LIST = 'l', |
| 63 | EMPTY_LIST = ']', |
| 64 | OBJ = 'o', |
| 65 | PUT = 'p', |
| 66 | BINPUT = 'q', |
| 67 | LONG_BINPUT = 'r', |
| 68 | SETITEM = 's', |
| 69 | TUPLE = 't', |
| 70 | EMPTY_TUPLE = ')', |
| 71 | SETITEMS = 'u', |
| 72 | BINFLOAT = 'G', |
| 73 | |
| 74 | /* Protocol 2. */ |
| 75 | PROTO = '\x80', |
| 76 | NEWOBJ = '\x81', |
| 77 | EXT1 = '\x82', |
| 78 | EXT2 = '\x83', |
| 79 | EXT4 = '\x84', |
| 80 | TUPLE1 = '\x85', |
| 81 | TUPLE2 = '\x86', |
| 82 | TUPLE3 = '\x87', |
| 83 | NEWTRUE = '\x88', |
| 84 | NEWFALSE = '\x89', |
| 85 | LONG1 = '\x8a', |
| 86 | LONG4 = '\x8b', |
| 87 | |
| 88 | /* Protocol 3 (Python 3.x) */ |
| 89 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 90 | SHORT_BINBYTES = 'C', |
| 91 | |
| 92 | /* Protocol 4 */ |
| 93 | SHORT_BINUNICODE = '\x8c', |
| 94 | BINUNICODE8 = '\x8d', |
| 95 | BINBYTES8 = '\x8e', |
| 96 | EMPTY_SET = '\x8f', |
| 97 | ADDITEMS = '\x90', |
| 98 | FROZENSET = '\x91', |
| 99 | NEWOBJ_EX = '\x92', |
| 100 | STACK_GLOBAL = '\x93', |
| 101 | MEMOIZE = '\x94', |
| 102 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 105 | enum { |
| 106 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 107 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 108 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 109 | help anything either. */ |
| 110 | BATCHSIZE = 1000, |
| 111 | |
| 112 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 113 | checking for self-referential data-structures. */ |
| 114 | FAST_NESTING_LIMIT = 50, |
| 115 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 116 | /* Initial size of the write buffer of Pickler. */ |
| 117 | WRITE_BUF_SIZE = 4096, |
| 118 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 119 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 120 | PREFETCH = 8192 * 16, |
| 121 | |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 122 | FRAME_SIZE_MIN = 4, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 123 | FRAME_SIZE_TARGET = 64 * 1024, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 124 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 127 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 128 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 129 | /* State of the pickle module, per PEP 3121. */ |
| 130 | typedef struct { |
| 131 | /* Exception classes for pickle. */ |
| 132 | PyObject *PickleError; |
| 133 | PyObject *PicklingError; |
| 134 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 135 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 136 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 137 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 138 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 139 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 140 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 141 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 142 | PyObject *extension_registry; |
| 143 | /* copyreg._extension_cache, {code: object} */ |
| 144 | PyObject *extension_cache; |
| 145 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 146 | PyObject *inverted_registry; |
| 147 | |
| 148 | /* Import mappings for compatibility with Python 2.x */ |
| 149 | |
| 150 | /* _compat_pickle.NAME_MAPPING, |
| 151 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 152 | PyObject *name_mapping_2to3; |
| 153 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 154 | PyObject *import_mapping_2to3; |
| 155 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 156 | PyObject *name_mapping_3to2; |
| 157 | PyObject *import_mapping_3to2; |
| 158 | |
| 159 | /* codecs.encode, used for saving bytes in older protocols */ |
| 160 | PyObject *codecs_encode; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 161 | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
| 162 | PyObject *getattr; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 163 | /* functools.partial, used for implementing __newobj_ex__ with protocols |
| 164 | 2 and 3 */ |
| 165 | PyObject *partial; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 166 | } PickleState; |
| 167 | |
| 168 | /* Forward declaration of the _pickle module definition. */ |
| 169 | static struct PyModuleDef _picklemodule; |
| 170 | |
| 171 | /* Given a module object, get its per-module state. */ |
| 172 | static PickleState * |
| 173 | _Pickle_GetState(PyObject *module) |
| 174 | { |
| 175 | return (PickleState *)PyModule_GetState(module); |
| 176 | } |
| 177 | |
| 178 | /* Find the module instance imported in the currently running sub-interpreter |
| 179 | and get its state. */ |
| 180 | static PickleState * |
| 181 | _Pickle_GetGlobalState(void) |
| 182 | { |
| 183 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 184 | } |
| 185 | |
| 186 | /* Clear the given pickle module state. */ |
| 187 | static void |
| 188 | _Pickle_ClearState(PickleState *st) |
| 189 | { |
| 190 | Py_CLEAR(st->PickleError); |
| 191 | Py_CLEAR(st->PicklingError); |
| 192 | Py_CLEAR(st->UnpicklingError); |
| 193 | Py_CLEAR(st->dispatch_table); |
| 194 | Py_CLEAR(st->extension_registry); |
| 195 | Py_CLEAR(st->extension_cache); |
| 196 | Py_CLEAR(st->inverted_registry); |
| 197 | Py_CLEAR(st->name_mapping_2to3); |
| 198 | Py_CLEAR(st->import_mapping_2to3); |
| 199 | Py_CLEAR(st->name_mapping_3to2); |
| 200 | Py_CLEAR(st->import_mapping_3to2); |
| 201 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 202 | Py_CLEAR(st->getattr); |
Victor Stinner | 9ba97df | 2015-11-17 12:15:07 +0100 | [diff] [blame] | 203 | Py_CLEAR(st->partial); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Initialize the given pickle module state. */ |
| 207 | static int |
| 208 | _Pickle_InitState(PickleState *st) |
| 209 | { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 210 | PyObject *builtins; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 211 | PyObject *copyreg = NULL; |
| 212 | PyObject *compat_pickle = NULL; |
| 213 | PyObject *codecs = NULL; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 214 | PyObject *functools = NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 215 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 216 | builtins = PyEval_GetBuiltins(); |
| 217 | if (builtins == NULL) |
| 218 | goto error; |
| 219 | st->getattr = PyDict_GetItemString(builtins, "getattr"); |
| 220 | if (st->getattr == NULL) |
| 221 | goto error; |
| 222 | Py_INCREF(st->getattr); |
| 223 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 224 | copyreg = PyImport_ImportModule("copyreg"); |
| 225 | if (!copyreg) |
| 226 | goto error; |
| 227 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 228 | if (!st->dispatch_table) |
| 229 | goto error; |
| 230 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 231 | PyErr_Format(PyExc_RuntimeError, |
| 232 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 233 | Py_TYPE(st->dispatch_table)->tp_name); |
| 234 | goto error; |
| 235 | } |
| 236 | st->extension_registry = \ |
| 237 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 238 | if (!st->extension_registry) |
| 239 | goto error; |
| 240 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 241 | PyErr_Format(PyExc_RuntimeError, |
| 242 | "copyreg._extension_registry should be a dict, " |
| 243 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 244 | goto error; |
| 245 | } |
| 246 | st->inverted_registry = \ |
| 247 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 248 | if (!st->inverted_registry) |
| 249 | goto error; |
| 250 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 251 | PyErr_Format(PyExc_RuntimeError, |
| 252 | "copyreg._inverted_registry should be a dict, " |
| 253 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 254 | goto error; |
| 255 | } |
| 256 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 257 | if (!st->extension_cache) |
| 258 | goto error; |
| 259 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 260 | PyErr_Format(PyExc_RuntimeError, |
| 261 | "copyreg._extension_cache should be a dict, " |
| 262 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 263 | goto error; |
| 264 | } |
| 265 | Py_CLEAR(copyreg); |
| 266 | |
| 267 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 268 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 269 | if (!compat_pickle) |
| 270 | goto error; |
| 271 | st->name_mapping_2to3 = \ |
| 272 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 273 | if (!st->name_mapping_2to3) |
| 274 | goto error; |
| 275 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 276 | PyErr_Format(PyExc_RuntimeError, |
| 277 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 278 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 279 | goto error; |
| 280 | } |
| 281 | st->import_mapping_2to3 = \ |
| 282 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 283 | if (!st->import_mapping_2to3) |
| 284 | goto error; |
| 285 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 286 | PyErr_Format(PyExc_RuntimeError, |
| 287 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 288 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 289 | goto error; |
| 290 | } |
| 291 | /* ... and the 3.x -> 2.x mapping tables */ |
| 292 | st->name_mapping_3to2 = \ |
| 293 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 294 | if (!st->name_mapping_3to2) |
| 295 | goto error; |
| 296 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 297 | PyErr_Format(PyExc_RuntimeError, |
| 298 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 299 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 300 | goto error; |
| 301 | } |
| 302 | st->import_mapping_3to2 = \ |
| 303 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 304 | if (!st->import_mapping_3to2) |
| 305 | goto error; |
| 306 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 307 | PyErr_Format(PyExc_RuntimeError, |
| 308 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 309 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 310 | goto error; |
| 311 | } |
| 312 | Py_CLEAR(compat_pickle); |
| 313 | |
| 314 | codecs = PyImport_ImportModule("codecs"); |
| 315 | if (codecs == NULL) |
| 316 | goto error; |
| 317 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 318 | if (st->codecs_encode == NULL) { |
| 319 | goto error; |
| 320 | } |
| 321 | if (!PyCallable_Check(st->codecs_encode)) { |
| 322 | PyErr_Format(PyExc_RuntimeError, |
| 323 | "codecs.encode should be a callable, not %.200s", |
| 324 | Py_TYPE(st->codecs_encode)->tp_name); |
| 325 | goto error; |
| 326 | } |
| 327 | Py_CLEAR(codecs); |
| 328 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 329 | functools = PyImport_ImportModule("functools"); |
| 330 | if (!functools) |
| 331 | goto error; |
| 332 | st->partial = PyObject_GetAttrString(functools, "partial"); |
| 333 | if (!st->partial) |
| 334 | goto error; |
| 335 | Py_CLEAR(functools); |
| 336 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 337 | return 0; |
| 338 | |
| 339 | error: |
| 340 | Py_CLEAR(copyreg); |
| 341 | Py_CLEAR(compat_pickle); |
| 342 | Py_CLEAR(codecs); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 343 | Py_CLEAR(functools); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 344 | _Pickle_ClearState(st); |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | /* Helper for calling a function with a single argument quickly. |
| 349 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 350 | This function steals the reference of the given argument. */ |
| 351 | static PyObject * |
| 352 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 353 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 354 | PyObject *result; |
| 355 | |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 356 | result = PyObject_CallFunctionObjArgs(func, obj, NULL); |
Victor Stinner | 7521069 | 2016-08-19 18:59:15 +0200 | [diff] [blame] | 357 | Py_DECREF(obj); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 358 | return result; |
| 359 | } |
| 360 | |
| 361 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 362 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 363 | /* Retrieve and deconstruct a method for avoiding a reference cycle |
| 364 | (pickler -> bound method of pickler -> pickler) */ |
| 365 | static int |
| 366 | init_method_ref(PyObject *self, _Py_Identifier *name, |
| 367 | PyObject **method_func, PyObject **method_self) |
| 368 | { |
| 369 | PyObject *func, *func2; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 370 | int ret; |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 371 | |
| 372 | /* *method_func and *method_self should be consistent. All refcount decrements |
| 373 | should be occurred after setting *method_self and *method_func. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 374 | ret = _PyObject_LookupAttrId(self, name, &func); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 375 | if (func == NULL) { |
| 376 | *method_self = NULL; |
| 377 | Py_CLEAR(*method_func); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 378 | return ret; |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) { |
| 382 | /* Deconstruct a bound Python method */ |
| 383 | func2 = PyMethod_GET_FUNCTION(func); |
| 384 | Py_INCREF(func2); |
| 385 | *method_self = self; /* borrowed */ |
| 386 | Py_XSETREF(*method_func, func2); |
| 387 | Py_DECREF(func); |
| 388 | return 0; |
| 389 | } |
| 390 | else { |
| 391 | *method_self = NULL; |
| 392 | Py_XSETREF(*method_func, func); |
| 393 | return 0; |
| 394 | } |
| 395 | } |
| 396 | |
| 397 | /* Bind a method if it was deconstructed */ |
| 398 | static PyObject * |
| 399 | reconstruct_method(PyObject *func, PyObject *self) |
| 400 | { |
| 401 | if (self) { |
| 402 | return PyMethod_New(func, self); |
| 403 | } |
| 404 | else { |
| 405 | Py_INCREF(func); |
| 406 | return func; |
| 407 | } |
| 408 | } |
| 409 | |
| 410 | static PyObject * |
| 411 | call_method(PyObject *func, PyObject *self, PyObject *obj) |
| 412 | { |
| 413 | if (self) { |
| 414 | return PyObject_CallFunctionObjArgs(func, self, obj, NULL); |
| 415 | } |
| 416 | else { |
| 417 | return PyObject_CallFunctionObjArgs(func, obj, NULL); |
| 418 | } |
| 419 | } |
| 420 | |
| 421 | /*************************************************************************/ |
| 422 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 423 | /* Internal data type used as the unpickling stack. */ |
| 424 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 425 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 426 | PyObject **data; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 427 | int mark_set; /* is MARK set? */ |
| 428 | Py_ssize_t fence; /* position of top MARK or 0 */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 429 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 430 | } Pdata; |
| 431 | |
| 432 | static void |
| 433 | Pdata_dealloc(Pdata *self) |
| 434 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 435 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 436 | while (--i >= 0) { |
| 437 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 438 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 439 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 440 | PyObject_Del(self); |
| 441 | } |
| 442 | |
| 443 | static PyTypeObject Pdata_Type = { |
| 444 | PyVarObject_HEAD_INIT(NULL, 0) |
| 445 | "_pickle.Pdata", /*tp_name*/ |
| 446 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 447 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 448 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 449 | }; |
| 450 | |
| 451 | static PyObject * |
| 452 | Pdata_New(void) |
| 453 | { |
| 454 | Pdata *self; |
| 455 | |
| 456 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 457 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 458 | Py_SIZE(self) = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 459 | self->mark_set = 0; |
| 460 | self->fence = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 461 | self->allocated = 8; |
| 462 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 463 | if (self->data) |
| 464 | return (PyObject *)self; |
| 465 | Py_DECREF(self); |
| 466 | return PyErr_NoMemory(); |
| 467 | } |
| 468 | |
| 469 | |
| 470 | /* Retain only the initial clearto items. If clearto >= the current |
| 471 | * number of items, this is a (non-erroneous) NOP. |
| 472 | */ |
| 473 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 474 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 475 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 476 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 477 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 478 | assert(clearto >= self->fence); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 479 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 480 | return 0; |
| 481 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 482 | while (--i >= clearto) { |
| 483 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 484 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 485 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int |
| 490 | Pdata_grow(Pdata *self) |
| 491 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 492 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 493 | size_t allocated = (size_t)self->allocated; |
| 494 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 495 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 496 | new_allocated = (allocated >> 3) + 6; |
| 497 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 498 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 499 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 500 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 501 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 502 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 503 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 504 | |
| 505 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 506 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 507 | return 0; |
| 508 | |
| 509 | nomemory: |
| 510 | PyErr_NoMemory(); |
| 511 | return -1; |
| 512 | } |
| 513 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 514 | static int |
| 515 | Pdata_stack_underflow(Pdata *self) |
| 516 | { |
| 517 | PickleState *st = _Pickle_GetGlobalState(); |
| 518 | PyErr_SetString(st->UnpicklingError, |
| 519 | self->mark_set ? |
| 520 | "unexpected MARK found" : |
| 521 | "unpickling stack underflow"); |
| 522 | return -1; |
| 523 | } |
| 524 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 525 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 526 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 527 | * is raised and V is set to NULL. |
| 528 | */ |
| 529 | static PyObject * |
| 530 | Pdata_pop(Pdata *self) |
| 531 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 532 | if (Py_SIZE(self) <= self->fence) { |
| 533 | Pdata_stack_underflow(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 534 | return NULL; |
| 535 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 536 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 537 | } |
| 538 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 539 | |
| 540 | static int |
| 541 | Pdata_push(Pdata *self, PyObject *obj) |
| 542 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 543 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 544 | return -1; |
| 545 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 546 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 547 | return 0; |
| 548 | } |
| 549 | |
| 550 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 551 | #define PDATA_PUSH(D, O, ER) do { \ |
| 552 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 553 | |
| 554 | /* Push an object on stack, adding a new reference to the object. */ |
| 555 | #define PDATA_APPEND(D, O, ER) do { \ |
| 556 | Py_INCREF((O)); \ |
| 557 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 558 | |
| 559 | static PyObject * |
| 560 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 561 | { |
| 562 | PyObject *tuple; |
| 563 | Py_ssize_t len, i, j; |
| 564 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 565 | if (start < self->fence) { |
| 566 | Pdata_stack_underflow(self); |
| 567 | return NULL; |
| 568 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 569 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 570 | tuple = PyTuple_New(len); |
| 571 | if (tuple == NULL) |
| 572 | return NULL; |
| 573 | for (i = start, j = 0; j < len; i++, j++) |
| 574 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 575 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 576 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 577 | return tuple; |
| 578 | } |
| 579 | |
| 580 | static PyObject * |
| 581 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 582 | { |
| 583 | PyObject *list; |
| 584 | Py_ssize_t len, i, j; |
| 585 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 586 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 587 | list = PyList_New(len); |
| 588 | if (list == NULL) |
| 589 | return NULL; |
| 590 | for (i = start, j = 0; j < len; i++, j++) |
| 591 | PyList_SET_ITEM(list, j, self->data[i]); |
| 592 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 593 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 594 | return list; |
| 595 | } |
| 596 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 597 | typedef struct { |
| 598 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 599 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 600 | } PyMemoEntry; |
| 601 | |
| 602 | typedef struct { |
| 603 | Py_ssize_t mt_mask; |
| 604 | Py_ssize_t mt_used; |
| 605 | Py_ssize_t mt_allocated; |
| 606 | PyMemoEntry *mt_table; |
| 607 | } PyMemoTable; |
| 608 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 609 | typedef struct PicklerObject { |
| 610 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 611 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 612 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 613 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 614 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 615 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 616 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 617 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 618 | |
| 619 | PyObject *write; /* write() method of the output stream. */ |
| 620 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 621 | flushing to the stream. */ |
| 622 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 623 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 624 | int proto; /* Pickle protocol number, >= 0 */ |
| 625 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 626 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 627 | Py_ssize_t frame_start; /* Position in output_buffer where the |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 628 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 629 | is no frame currently open. */ |
| 630 | |
| 631 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 632 | int fast; /* Enable fast mode if set to a true value. |
| 633 | The fast mode disable the usage of memo, |
| 634 | therefore speeding the pickling process by |
| 635 | not generating superfluous PUT opcodes. It |
| 636 | should not be used if with self-referential |
| 637 | objects. */ |
| 638 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 639 | int fix_imports; /* Indicate whether Pickler should fix |
| 640 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 641 | PyObject *fast_memo; |
| 642 | } PicklerObject; |
| 643 | |
| 644 | typedef struct UnpicklerObject { |
| 645 | PyObject_HEAD |
| 646 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 647 | |
| 648 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 649 | is unnecessary, since the keys are contiguous ints. */ |
| 650 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 651 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 652 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 653 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 654 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 655 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 656 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 657 | |
| 658 | Py_buffer buffer; |
| 659 | char *input_buffer; |
| 660 | char *input_line; |
| 661 | Py_ssize_t input_len; |
| 662 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 663 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 664 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 665 | PyObject *read; /* read() method of the input stream. */ |
| 666 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 667 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 668 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 669 | char *encoding; /* Name of the encoding to be used for |
| 670 | decoding strings pickled using Python |
| 671 | 2.x. The default value is "ASCII" */ |
| 672 | char *errors; /* Name of errors handling scheme to used when |
| 673 | decoding strings. The default value is |
| 674 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 675 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 676 | objects. */ |
| 677 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 678 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 679 | int proto; /* Protocol of the pickle loaded. */ |
| 680 | int fix_imports; /* Indicate whether Unpickler should fix |
| 681 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 682 | } UnpicklerObject; |
| 683 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 684 | typedef struct { |
| 685 | PyObject_HEAD |
| 686 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 687 | } PicklerMemoProxyObject; |
| 688 | |
| 689 | typedef struct { |
| 690 | PyObject_HEAD |
| 691 | UnpicklerObject *unpickler; |
| 692 | } UnpicklerMemoProxyObject; |
| 693 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 694 | /* Forward declarations */ |
| 695 | static int save(PicklerObject *, PyObject *, int); |
| 696 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 697 | static PyTypeObject Pickler_Type; |
| 698 | static PyTypeObject Unpickler_Type; |
| 699 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 700 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 701 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 702 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 703 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 704 | 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] | 705 | a bunch of unnecessary object creation. This makes a huge performance |
| 706 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 707 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 708 | #define MT_MINSIZE 8 |
| 709 | #define PERTURB_SHIFT 5 |
| 710 | |
| 711 | |
| 712 | static PyMemoTable * |
| 713 | PyMemoTable_New(void) |
| 714 | { |
| 715 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 716 | if (memo == NULL) { |
| 717 | PyErr_NoMemory(); |
| 718 | return NULL; |
| 719 | } |
| 720 | |
| 721 | memo->mt_used = 0; |
| 722 | memo->mt_allocated = MT_MINSIZE; |
| 723 | memo->mt_mask = MT_MINSIZE - 1; |
| 724 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 725 | if (memo->mt_table == NULL) { |
| 726 | PyMem_FREE(memo); |
| 727 | PyErr_NoMemory(); |
| 728 | return NULL; |
| 729 | } |
| 730 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 731 | |
| 732 | return memo; |
| 733 | } |
| 734 | |
| 735 | static PyMemoTable * |
| 736 | PyMemoTable_Copy(PyMemoTable *self) |
| 737 | { |
| 738 | Py_ssize_t i; |
| 739 | PyMemoTable *new = PyMemoTable_New(); |
| 740 | if (new == NULL) |
| 741 | return NULL; |
| 742 | |
| 743 | new->mt_used = self->mt_used; |
| 744 | new->mt_allocated = self->mt_allocated; |
| 745 | new->mt_mask = self->mt_mask; |
| 746 | /* The table we get from _New() is probably smaller than we wanted. |
| 747 | Free it and allocate one that's the right size. */ |
| 748 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 749 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 750 | if (new->mt_table == NULL) { |
| 751 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 752 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 753 | return NULL; |
| 754 | } |
| 755 | for (i = 0; i < self->mt_allocated; i++) { |
| 756 | Py_XINCREF(self->mt_table[i].me_key); |
| 757 | } |
| 758 | memcpy(new->mt_table, self->mt_table, |
| 759 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 760 | |
| 761 | return new; |
| 762 | } |
| 763 | |
| 764 | static Py_ssize_t |
| 765 | PyMemoTable_Size(PyMemoTable *self) |
| 766 | { |
| 767 | return self->mt_used; |
| 768 | } |
| 769 | |
| 770 | static int |
| 771 | PyMemoTable_Clear(PyMemoTable *self) |
| 772 | { |
| 773 | Py_ssize_t i = self->mt_allocated; |
| 774 | |
| 775 | while (--i >= 0) { |
| 776 | Py_XDECREF(self->mt_table[i].me_key); |
| 777 | } |
| 778 | self->mt_used = 0; |
| 779 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | static void |
| 784 | PyMemoTable_Del(PyMemoTable *self) |
| 785 | { |
| 786 | if (self == NULL) |
| 787 | return; |
| 788 | PyMemoTable_Clear(self); |
| 789 | |
| 790 | PyMem_FREE(self->mt_table); |
| 791 | PyMem_FREE(self); |
| 792 | } |
| 793 | |
| 794 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 795 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 796 | static PyMemoEntry * |
| 797 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 798 | { |
| 799 | size_t i; |
| 800 | size_t perturb; |
| 801 | size_t mask = (size_t)self->mt_mask; |
| 802 | PyMemoEntry *table = self->mt_table; |
| 803 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 804 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 805 | |
| 806 | i = hash & mask; |
| 807 | entry = &table[i]; |
| 808 | if (entry->me_key == NULL || entry->me_key == key) |
| 809 | return entry; |
| 810 | |
| 811 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 812 | i = (i << 2) + i + perturb + 1; |
| 813 | entry = &table[i & mask]; |
| 814 | if (entry->me_key == NULL || entry->me_key == key) |
| 815 | return entry; |
| 816 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 817 | Py_UNREACHABLE(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 818 | } |
| 819 | |
| 820 | /* Returns -1 on failure, 0 on success. */ |
| 821 | static int |
| 822 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 823 | { |
| 824 | PyMemoEntry *oldtable = NULL; |
| 825 | PyMemoEntry *oldentry, *newentry; |
| 826 | Py_ssize_t new_size = MT_MINSIZE; |
| 827 | Py_ssize_t to_process; |
| 828 | |
| 829 | assert(min_size > 0); |
| 830 | |
| 831 | /* Find the smallest valid table size >= min_size. */ |
| 832 | while (new_size < min_size && new_size > 0) |
| 833 | new_size <<= 1; |
| 834 | if (new_size <= 0) { |
| 835 | PyErr_NoMemory(); |
| 836 | return -1; |
| 837 | } |
| 838 | /* new_size needs to be a power of two. */ |
| 839 | assert((new_size & (new_size - 1)) == 0); |
| 840 | |
| 841 | /* Allocate new table. */ |
| 842 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 843 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 844 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 845 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 846 | PyErr_NoMemory(); |
| 847 | return -1; |
| 848 | } |
| 849 | self->mt_allocated = new_size; |
| 850 | self->mt_mask = new_size - 1; |
| 851 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 852 | |
| 853 | /* Copy entries from the old table. */ |
| 854 | to_process = self->mt_used; |
| 855 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 856 | if (oldentry->me_key != NULL) { |
| 857 | to_process--; |
| 858 | /* newentry is a pointer to a chunk of the new |
| 859 | mt_table, so we're setting the key:value pair |
| 860 | in-place. */ |
| 861 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 862 | newentry->me_key = oldentry->me_key; |
| 863 | newentry->me_value = oldentry->me_value; |
| 864 | } |
| 865 | } |
| 866 | |
| 867 | /* Deallocate the old table. */ |
| 868 | PyMem_FREE(oldtable); |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 873 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 874 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 875 | { |
| 876 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 877 | if (entry->me_key == NULL) |
| 878 | return NULL; |
| 879 | return &entry->me_value; |
| 880 | } |
| 881 | |
| 882 | /* Returns -1 on failure, 0 on success. */ |
| 883 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 884 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 885 | { |
| 886 | PyMemoEntry *entry; |
| 887 | |
| 888 | assert(key != NULL); |
| 889 | |
| 890 | entry = _PyMemoTable_Lookup(self, key); |
| 891 | if (entry->me_key != NULL) { |
| 892 | entry->me_value = value; |
| 893 | return 0; |
| 894 | } |
| 895 | Py_INCREF(key); |
| 896 | entry->me_key = key; |
| 897 | entry->me_value = value; |
| 898 | self->mt_used++; |
| 899 | |
| 900 | /* If we added a key, we can safely resize. Otherwise just return! |
| 901 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 902 | * |
| 903 | * Quadrupling the size improves average table sparseness |
| 904 | * (reducing collisions) at the cost of some memory. It also halves |
| 905 | * the number of expensive resize operations in a growing memo table. |
| 906 | * |
| 907 | * Very large memo tables (over 50K items) use doubling instead. |
| 908 | * This may help applications with severe memory constraints. |
| 909 | */ |
| 910 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 911 | return 0; |
| 912 | return _PyMemoTable_ResizeTable(self, |
| 913 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 914 | } |
| 915 | |
| 916 | #undef MT_MINSIZE |
| 917 | #undef PERTURB_SHIFT |
| 918 | |
| 919 | /*************************************************************************/ |
| 920 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 921 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 922 | static int |
| 923 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 924 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 925 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 926 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 927 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 928 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 929 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 930 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 931 | return 0; |
| 932 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 933 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 934 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 935 | _write_size64(char *out, size_t value) |
| 936 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 937 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 938 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 939 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 940 | |
| 941 | for (i = 0; i < sizeof(size_t); i++) { |
| 942 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 943 | } |
| 944 | for (i = sizeof(size_t); i < 8; i++) { |
| 945 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 946 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 947 | } |
| 948 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 949 | static int |
| 950 | _Pickler_CommitFrame(PicklerObject *self) |
| 951 | { |
| 952 | size_t frame_len; |
| 953 | char *qdata; |
| 954 | |
| 955 | if (!self->framing || self->frame_start == -1) |
| 956 | return 0; |
| 957 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 958 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 959 | if (frame_len >= FRAME_SIZE_MIN) { |
| 960 | qdata[0] = FRAME; |
| 961 | _write_size64(qdata + 1, frame_len); |
| 962 | } |
| 963 | else { |
| 964 | memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len); |
| 965 | self->output_len -= FRAME_HEADER_SIZE; |
| 966 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 967 | self->frame_start = -1; |
| 968 | return 0; |
| 969 | } |
| 970 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 971 | static PyObject * |
| 972 | _Pickler_GetString(PicklerObject *self) |
| 973 | { |
| 974 | PyObject *output_buffer = self->output_buffer; |
| 975 | |
| 976 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 977 | |
| 978 | if (_Pickler_CommitFrame(self)) |
| 979 | return NULL; |
| 980 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 981 | self->output_buffer = NULL; |
| 982 | /* Resize down to exact size */ |
| 983 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 984 | return NULL; |
| 985 | return output_buffer; |
| 986 | } |
| 987 | |
| 988 | static int |
| 989 | _Pickler_FlushToFile(PicklerObject *self) |
| 990 | { |
| 991 | PyObject *output, *result; |
| 992 | |
| 993 | assert(self->write != NULL); |
| 994 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 995 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 996 | output = _Pickler_GetString(self); |
| 997 | if (output == NULL) |
| 998 | return -1; |
| 999 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1000 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1001 | Py_XDECREF(result); |
| 1002 | return (result == NULL) ? -1 : 0; |
| 1003 | } |
| 1004 | |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1005 | static int |
| 1006 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 1007 | { |
| 1008 | Py_ssize_t frame_len; |
| 1009 | |
| 1010 | if (!self->framing || self->frame_start == -1) { |
| 1011 | return 0; |
| 1012 | } |
| 1013 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 1014 | if (frame_len >= FRAME_SIZE_TARGET) { |
| 1015 | if(_Pickler_CommitFrame(self)) { |
| 1016 | return -1; |
| 1017 | } |
Miss Islington (bot) | e86db34 | 2018-02-03 17:41:43 -0800 | [diff] [blame^] | 1018 | /* Flush the content of the committed frame to the underlying |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1019 | * file and reuse the pickler buffer for the next frame so as |
| 1020 | * to limit memory usage when dumping large complex objects to |
| 1021 | * a file. |
| 1022 | * |
| 1023 | * self->write is NULL when called via dumps. |
| 1024 | */ |
| 1025 | if (self->write != NULL) { |
| 1026 | if (_Pickler_FlushToFile(self) < 0) { |
| 1027 | return -1; |
| 1028 | } |
| 1029 | if (_Pickler_ClearBuffer(self) < 0) { |
| 1030 | return -1; |
| 1031 | } |
| 1032 | } |
| 1033 | } |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1037 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1038 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1039 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1040 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1041 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1042 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1043 | |
| 1044 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1045 | need_new_frame = (self->framing && self->frame_start == -1); |
| 1046 | |
| 1047 | if (need_new_frame) |
| 1048 | n = data_len + FRAME_HEADER_SIZE; |
| 1049 | else |
| 1050 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1051 | |
| 1052 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1053 | if (required > self->max_output_len) { |
| 1054 | /* Make place in buffer for the pickle chunk */ |
| 1055 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 1056 | PyErr_NoMemory(); |
| 1057 | return -1; |
| 1058 | } |
| 1059 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 1060 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 1061 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1062 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1063 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1064 | if (need_new_frame) { |
| 1065 | /* Setup new frame */ |
| 1066 | Py_ssize_t frame_start = self->output_len; |
| 1067 | self->frame_start = frame_start; |
| 1068 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 1069 | /* Write an invalid value, for debugging */ |
| 1070 | buffer[frame_start + i] = 0xFE; |
| 1071 | } |
| 1072 | self->output_len += FRAME_HEADER_SIZE; |
| 1073 | } |
| 1074 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1075 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1076 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1077 | buffer[self->output_len + i] = s[i]; |
| 1078 | } |
| 1079 | } |
| 1080 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1081 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1082 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1083 | self->output_len += data_len; |
| 1084 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1087 | static PicklerObject * |
| 1088 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1089 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1090 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1092 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1093 | if (self == NULL) |
| 1094 | return NULL; |
| 1095 | |
| 1096 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1097 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1098 | self->write = NULL; |
| 1099 | self->proto = 0; |
| 1100 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1101 | self->framing = 0; |
| 1102 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1103 | self->fast = 0; |
| 1104 | self->fast_nesting = 0; |
| 1105 | self->fix_imports = 0; |
| 1106 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1107 | self->max_output_len = WRITE_BUF_SIZE; |
| 1108 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1109 | |
| 1110 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1111 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1112 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1113 | |
| 1114 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1115 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1116 | return NULL; |
| 1117 | } |
| 1118 | return self; |
| 1119 | } |
| 1120 | |
| 1121 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1122 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1123 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1124 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1125 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1126 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1127 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1128 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1129 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1130 | proto = PyLong_AsLong(protocol); |
| 1131 | if (proto < 0) { |
| 1132 | if (proto == -1 && PyErr_Occurred()) |
| 1133 | return -1; |
| 1134 | proto = HIGHEST_PROTOCOL; |
| 1135 | } |
| 1136 | else if (proto > HIGHEST_PROTOCOL) { |
| 1137 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1138 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1139 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1140 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1141 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1142 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1143 | self->bin = proto > 0; |
| 1144 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1145 | return 0; |
| 1146 | } |
| 1147 | |
| 1148 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1149 | be called once on a freshly created Pickler. */ |
| 1150 | static int |
| 1151 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1152 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1153 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1154 | assert(file != NULL); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1155 | if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) { |
| 1156 | return -1; |
| 1157 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1158 | if (self->write == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1159 | PyErr_SetString(PyExc_TypeError, |
| 1160 | "file must have a 'write' attribute"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1161 | return -1; |
| 1162 | } |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1167 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1168 | own reference to `input`. */ |
| 1169 | static Py_ssize_t |
| 1170 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1171 | { |
| 1172 | if (self->buffer.buf != NULL) |
| 1173 | PyBuffer_Release(&self->buffer); |
| 1174 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1175 | return -1; |
| 1176 | self->input_buffer = self->buffer.buf; |
| 1177 | self->input_len = self->buffer.len; |
| 1178 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1179 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1180 | return self->input_len; |
| 1181 | } |
| 1182 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1183 | static int |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1184 | bad_readline(void) |
| 1185 | { |
| 1186 | PickleState *st = _Pickle_GetGlobalState(); |
| 1187 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
| 1188 | return -1; |
| 1189 | } |
| 1190 | |
| 1191 | static int |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1192 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1193 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1194 | Py_ssize_t consumed; |
| 1195 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1196 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1197 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1198 | if (consumed <= 0) |
| 1199 | return 0; |
| 1200 | |
| 1201 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1202 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1203 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1204 | if (r == NULL) |
| 1205 | return -1; |
| 1206 | Py_DECREF(r); |
| 1207 | |
| 1208 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1209 | return 0; |
| 1210 | } |
| 1211 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1212 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1213 | |
| 1214 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1215 | may be multiple pickle objects arranged contiguously in the same input |
| 1216 | buffer. |
| 1217 | |
| 1218 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1219 | bytes from the input stream/buffer. |
| 1220 | |
| 1221 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1222 | failure; on success, returns the number of bytes read from the file. |
| 1223 | |
| 1224 | On success, self->input_len will be 0; this is intentional so that when |
| 1225 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1226 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1227 | size to tell you how much data you can process. */ |
| 1228 | static Py_ssize_t |
| 1229 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1230 | { |
| 1231 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1232 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1233 | |
| 1234 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1235 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1236 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1237 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1238 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1239 | if (n == READ_WHOLE_LINE) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 1240 | data = _PyObject_CallNoArg(self->readline); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1241 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1242 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1243 | PyObject *len; |
| 1244 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1245 | if (self->peek && n < PREFETCH) { |
| 1246 | len = PyLong_FromSsize_t(PREFETCH); |
| 1247 | if (len == NULL) |
| 1248 | return -1; |
| 1249 | data = _Pickle_FastCall(self->peek, len); |
| 1250 | if (data == NULL) { |
| 1251 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1252 | return -1; |
| 1253 | /* peek() is probably not supported by the given file object */ |
| 1254 | PyErr_Clear(); |
| 1255 | Py_CLEAR(self->peek); |
| 1256 | } |
| 1257 | else { |
| 1258 | read_size = _Unpickler_SetStringInput(self, data); |
| 1259 | Py_DECREF(data); |
| 1260 | self->prefetched_idx = 0; |
| 1261 | if (n <= read_size) |
| 1262 | return n; |
| 1263 | } |
| 1264 | } |
| 1265 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1266 | if (len == NULL) |
| 1267 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1268 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1269 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1270 | if (data == NULL) |
| 1271 | return -1; |
| 1272 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1273 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1274 | Py_DECREF(data); |
| 1275 | return read_size; |
| 1276 | } |
| 1277 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1278 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1279 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1280 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1281 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1282 | Py_ssize_t num_read; |
| 1283 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1284 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1285 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1286 | PickleState *st = _Pickle_GetGlobalState(); |
| 1287 | PyErr_SetString(st->UnpicklingError, |
| 1288 | "read would overflow (invalid bytecode)"); |
| 1289 | return -1; |
| 1290 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1291 | |
| 1292 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1293 | assert(self->next_read_idx + n > self->input_len); |
| 1294 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1295 | if (!self->read) |
| 1296 | return bad_readline(); |
| 1297 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1298 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1299 | if (num_read < 0) |
| 1300 | return -1; |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1301 | if (num_read < n) |
| 1302 | return bad_readline(); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1303 | *s = self->input_buffer; |
| 1304 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1305 | return n; |
| 1306 | } |
| 1307 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1308 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1309 | |
| 1310 | This should be used for all data reads, rather than accessing the unpickler's |
| 1311 | input buffer directly. This method deals correctly with reading from input |
| 1312 | streams, which the input buffer doesn't deal with. |
| 1313 | |
| 1314 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1315 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1316 | should use this function's return value to know how many bytes you can |
| 1317 | consume. |
| 1318 | |
| 1319 | Returns -1 (with an exception set) on failure. On success, return the |
| 1320 | number of chars read. */ |
| 1321 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1322 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1323 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1324 | (self)->next_read_idx += (n), \ |
| 1325 | (n)) \ |
| 1326 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1327 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1328 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1329 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1330 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1331 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1332 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1333 | if (input_line == NULL) { |
| 1334 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1335 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1336 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1337 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1338 | memcpy(input_line, line, len); |
| 1339 | input_line[len] = '\0'; |
| 1340 | self->input_line = input_line; |
| 1341 | *result = self->input_line; |
| 1342 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1343 | } |
| 1344 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1345 | /* Read a line from the input stream/buffer. If we run off the end of the input |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1346 | before hitting \n, raise an error. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1347 | |
| 1348 | Returns the number of chars read, or -1 on failure. */ |
| 1349 | static Py_ssize_t |
| 1350 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1351 | { |
| 1352 | Py_ssize_t i, num_read; |
| 1353 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1354 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1355 | if (self->input_buffer[i] == '\n') { |
| 1356 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1357 | num_read = i - self->next_read_idx + 1; |
| 1358 | self->next_read_idx = i + 1; |
| 1359 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1360 | } |
| 1361 | } |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1362 | if (!self->read) |
| 1363 | return bad_readline(); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1364 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1365 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1366 | if (num_read < 0) |
| 1367 | return -1; |
| 1368 | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
| 1369 | return bad_readline(); |
| 1370 | self->next_read_idx = num_read; |
| 1371 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| 1374 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1375 | will be modified in place. */ |
| 1376 | static int |
| 1377 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1378 | { |
| 1379 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1380 | |
| 1381 | assert(new_size > self->memo_size); |
| 1382 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1383 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1384 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1385 | PyErr_NoMemory(); |
| 1386 | return -1; |
| 1387 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1388 | for (i = self->memo_size; i < new_size; i++) |
| 1389 | self->memo[i] = NULL; |
| 1390 | self->memo_size = new_size; |
| 1391 | return 0; |
| 1392 | } |
| 1393 | |
| 1394 | /* Returns NULL if idx is out of bounds. */ |
| 1395 | static PyObject * |
| 1396 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1397 | { |
| 1398 | if (idx < 0 || idx >= self->memo_size) |
| 1399 | return NULL; |
| 1400 | |
| 1401 | return self->memo[idx]; |
| 1402 | } |
| 1403 | |
| 1404 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1405 | This takes its own reference to `value`. */ |
| 1406 | static int |
| 1407 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1408 | { |
| 1409 | PyObject *old_item; |
| 1410 | |
| 1411 | if (idx >= self->memo_size) { |
| 1412 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1413 | return -1; |
| 1414 | assert(idx < self->memo_size); |
| 1415 | } |
| 1416 | Py_INCREF(value); |
| 1417 | old_item = self->memo[idx]; |
| 1418 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1419 | if (old_item != NULL) { |
| 1420 | Py_DECREF(old_item); |
| 1421 | } |
| 1422 | else { |
| 1423 | self->memo_len++; |
| 1424 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | static PyObject ** |
| 1429 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1430 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1431 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1432 | if (memo == NULL) { |
| 1433 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1434 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1435 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1436 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1437 | return memo; |
| 1438 | } |
| 1439 | |
| 1440 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1441 | static void |
| 1442 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1443 | { |
| 1444 | Py_ssize_t i; |
| 1445 | PyObject **memo = self->memo; |
| 1446 | |
| 1447 | if (self->memo == NULL) |
| 1448 | return; |
| 1449 | self->memo = NULL; |
| 1450 | i = self->memo_size; |
| 1451 | while (--i >= 0) { |
| 1452 | Py_XDECREF(memo[i]); |
| 1453 | } |
| 1454 | PyMem_FREE(memo); |
| 1455 | } |
| 1456 | |
| 1457 | static UnpicklerObject * |
| 1458 | _Unpickler_New(void) |
| 1459 | { |
| 1460 | UnpicklerObject *self; |
| 1461 | |
| 1462 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1463 | if (self == NULL) |
| 1464 | return NULL; |
| 1465 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1466 | self->pers_func = NULL; |
| 1467 | self->input_buffer = NULL; |
| 1468 | self->input_line = NULL; |
| 1469 | self->input_len = 0; |
| 1470 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1471 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1472 | self->read = NULL; |
| 1473 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1474 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1475 | self->encoding = NULL; |
| 1476 | self->errors = NULL; |
| 1477 | self->marks = NULL; |
| 1478 | self->num_marks = 0; |
| 1479 | self->marks_size = 0; |
| 1480 | self->proto = 0; |
| 1481 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1482 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1483 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1484 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1485 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1486 | self->stack = (Pdata *)Pdata_New(); |
| 1487 | |
| 1488 | if (self->memo == NULL || self->stack == NULL) { |
| 1489 | Py_DECREF(self); |
| 1490 | return NULL; |
| 1491 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1492 | |
| 1493 | return self; |
| 1494 | } |
| 1495 | |
| 1496 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1497 | be called once on a freshly created Pickler. */ |
| 1498 | static int |
| 1499 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1500 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1501 | _Py_IDENTIFIER(peek); |
| 1502 | _Py_IDENTIFIER(read); |
| 1503 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1504 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1505 | if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) { |
| 1506 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1507 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1508 | (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read); |
| 1509 | (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1510 | if (self->readline == NULL || self->read == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1511 | if (!PyErr_Occurred()) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1512 | PyErr_SetString(PyExc_TypeError, |
| 1513 | "file must have 'read' and 'readline' attributes"); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1514 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1515 | Py_CLEAR(self->read); |
| 1516 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1517 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1518 | return -1; |
| 1519 | } |
| 1520 | return 0; |
| 1521 | } |
| 1522 | |
| 1523 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1524 | be called once on a freshly created Pickler. */ |
| 1525 | static int |
| 1526 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1527 | const char *encoding, |
| 1528 | const char *errors) |
| 1529 | { |
| 1530 | if (encoding == NULL) |
| 1531 | encoding = "ASCII"; |
| 1532 | if (errors == NULL) |
| 1533 | errors = "strict"; |
| 1534 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1535 | self->encoding = _PyMem_Strdup(encoding); |
| 1536 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1537 | if (self->encoding == NULL || self->errors == NULL) { |
| 1538 | PyErr_NoMemory(); |
| 1539 | return -1; |
| 1540 | } |
| 1541 | return 0; |
| 1542 | } |
| 1543 | |
| 1544 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1545 | static int |
| 1546 | memo_get(PicklerObject *self, PyObject *key) |
| 1547 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1548 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1549 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1550 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1551 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1552 | value = PyMemoTable_Get(self->memo, key); |
| 1553 | if (value == NULL) { |
| 1554 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1555 | return -1; |
| 1556 | } |
| 1557 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1558 | if (!self->bin) { |
| 1559 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1560 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1561 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1562 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1563 | } |
| 1564 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1565 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1566 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1567 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1568 | len = 2; |
| 1569 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1570 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1571 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1572 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1573 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1574 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1575 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1576 | len = 5; |
| 1577 | } |
| 1578 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1579 | PickleState *st = _Pickle_GetGlobalState(); |
| 1580 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1581 | "memo id too large for LONG_BINGET"); |
| 1582 | return -1; |
| 1583 | } |
| 1584 | } |
| 1585 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1586 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1587 | return -1; |
| 1588 | |
| 1589 | return 0; |
| 1590 | } |
| 1591 | |
| 1592 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1593 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1594 | static int |
| 1595 | memo_put(PicklerObject *self, PyObject *obj) |
| 1596 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1597 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1598 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1599 | Py_ssize_t idx; |
| 1600 | |
| 1601 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1602 | |
| 1603 | if (self->fast) |
| 1604 | return 0; |
| 1605 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1606 | idx = PyMemoTable_Size(self->memo); |
| 1607 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1608 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1609 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1610 | if (self->proto >= 4) { |
| 1611 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1612 | return -1; |
| 1613 | return 0; |
| 1614 | } |
| 1615 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1616 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1617 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1618 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1619 | len = strlen(pdata); |
| 1620 | } |
| 1621 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1622 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1623 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1624 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1625 | len = 2; |
| 1626 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1627 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1628 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1629 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1630 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1631 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1632 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1633 | len = 5; |
| 1634 | } |
| 1635 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1636 | PickleState *st = _Pickle_GetGlobalState(); |
| 1637 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1638 | "memo id too large for LONG_BINPUT"); |
| 1639 | return -1; |
| 1640 | } |
| 1641 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1642 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1643 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1644 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1645 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1646 | } |
| 1647 | |
| 1648 | static PyObject * |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1649 | get_dotted_path(PyObject *obj, PyObject *name) |
| 1650 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1651 | _Py_static_string(PyId_dot, "."); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1652 | PyObject *dotted_path; |
| 1653 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1654 | |
| 1655 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1656 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1657 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1658 | n = PyList_GET_SIZE(dotted_path); |
| 1659 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1660 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1661 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1662 | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1663 | if (obj == NULL) |
| 1664 | PyErr_Format(PyExc_AttributeError, |
| 1665 | "Can't pickle local object %R", name); |
| 1666 | else |
| 1667 | PyErr_Format(PyExc_AttributeError, |
| 1668 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1669 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1670 | return NULL; |
| 1671 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1672 | } |
| 1673 | return dotted_path; |
| 1674 | } |
| 1675 | |
| 1676 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1677 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1678 | { |
| 1679 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1680 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1681 | |
| 1682 | assert(PyList_CheckExact(names)); |
| 1683 | Py_INCREF(obj); |
| 1684 | n = PyList_GET_SIZE(names); |
| 1685 | for (i = 0; i < n; i++) { |
| 1686 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1687 | Py_XDECREF(parent); |
| 1688 | parent = obj; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1689 | (void)_PyObject_LookupAttr(parent, name, &obj); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1690 | if (obj == NULL) { |
| 1691 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1692 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1693 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1694 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1695 | if (pparent != NULL) |
| 1696 | *pparent = parent; |
| 1697 | else |
| 1698 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1699 | return obj; |
| 1700 | } |
| 1701 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1702 | |
| 1703 | static PyObject * |
| 1704 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1705 | { |
| 1706 | PyObject *dotted_path, *attr; |
| 1707 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1708 | if (allow_qualname) { |
| 1709 | dotted_path = get_dotted_path(obj, name); |
| 1710 | if (dotted_path == NULL) |
| 1711 | return NULL; |
| 1712 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1713 | Py_DECREF(dotted_path); |
| 1714 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1715 | else { |
| 1716 | (void)_PyObject_LookupAttr(obj, name, &attr); |
| 1717 | } |
| 1718 | if (attr == NULL && !PyErr_Occurred()) { |
| 1719 | PyErr_Format(PyExc_AttributeError, |
| 1720 | "Can't get attribute %R on %R", name, obj); |
| 1721 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1722 | return attr; |
| 1723 | } |
| 1724 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1725 | static int |
| 1726 | _checkmodule(PyObject *module_name, PyObject *module, |
| 1727 | PyObject *global, PyObject *dotted_path) |
| 1728 | { |
| 1729 | if (module == Py_None) { |
| 1730 | return -1; |
| 1731 | } |
| 1732 | if (PyUnicode_Check(module_name) && |
| 1733 | _PyUnicode_EqualToASCIIString(module_name, "__main__")) { |
| 1734 | return -1; |
| 1735 | } |
| 1736 | |
| 1737 | PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); |
| 1738 | if (candidate == NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1739 | return -1; |
| 1740 | } |
| 1741 | if (candidate != global) { |
| 1742 | Py_DECREF(candidate); |
| 1743 | return -1; |
| 1744 | } |
| 1745 | Py_DECREF(candidate); |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1749 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1750 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1751 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1752 | PyObject *module_name; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1753 | PyObject *module = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1754 | Py_ssize_t i; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1755 | PyObject *modules; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1756 | _Py_IDENTIFIER(__module__); |
| 1757 | _Py_IDENTIFIER(modules); |
| 1758 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1759 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1760 | if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) { |
| 1761 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1762 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1763 | if (module_name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1764 | /* In some rare cases (e.g., bound methods of extension types), |
| 1765 | __module__ can be None. If it is so, then search sys.modules for |
| 1766 | the module of global. */ |
| 1767 | if (module_name != Py_None) |
| 1768 | return module_name; |
| 1769 | Py_CLEAR(module_name); |
| 1770 | } |
| 1771 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1772 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1773 | /* Fallback on walking sys.modules */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1774 | modules = _PySys_GetObjectId(&PyId_modules); |
| 1775 | if (modules == NULL) { |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1776 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1777 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1778 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1779 | if (PyDict_CheckExact(modules)) { |
| 1780 | i = 0; |
| 1781 | while (PyDict_Next(modules, &i, &module_name, &module)) { |
| 1782 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1783 | Py_INCREF(module_name); |
| 1784 | return module_name; |
| 1785 | } |
| 1786 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1787 | return NULL; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1788 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1789 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1790 | } |
| 1791 | else { |
| 1792 | PyObject *iterator = PyObject_GetIter(modules); |
| 1793 | if (iterator == NULL) { |
| 1794 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1795 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1796 | while ((module_name = PyIter_Next(iterator))) { |
| 1797 | module = PyObject_GetItem(modules, module_name); |
| 1798 | if (module == NULL) { |
| 1799 | Py_DECREF(module_name); |
| 1800 | Py_DECREF(iterator); |
| 1801 | return NULL; |
| 1802 | } |
| 1803 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1804 | Py_DECREF(module); |
| 1805 | Py_DECREF(iterator); |
| 1806 | return module_name; |
| 1807 | } |
| 1808 | Py_DECREF(module); |
| 1809 | Py_DECREF(module_name); |
| 1810 | if (PyErr_Occurred()) { |
| 1811 | Py_DECREF(iterator); |
| 1812 | return NULL; |
| 1813 | } |
| 1814 | } |
| 1815 | Py_DECREF(iterator); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1819 | module_name = _PyUnicode_FromId(&PyId___main__); |
Victor Stinner | af46eb8 | 2017-09-05 23:30:16 +0200 | [diff] [blame] | 1820 | Py_XINCREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1821 | return module_name; |
| 1822 | } |
| 1823 | |
| 1824 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1825 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1826 | memoization disabled). If the nesting of a list or dict object exceed |
| 1827 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1828 | reference to the seen list or dict objects and check whether these objects |
| 1829 | are recursive. These are not strictly necessary, since save() has a |
| 1830 | hard-coded recursion limit, but they give a nicer error message than the |
| 1831 | typical RuntimeError. */ |
| 1832 | static int |
| 1833 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1834 | { |
| 1835 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1836 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1837 | PyObject *key = NULL; |
| 1838 | if (self->fast_memo == NULL) { |
| 1839 | self->fast_memo = PyDict_New(); |
| 1840 | if (self->fast_memo == NULL) { |
| 1841 | self->fast_nesting = -1; |
| 1842 | return 0; |
| 1843 | } |
| 1844 | } |
| 1845 | key = PyLong_FromVoidPtr(obj); |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1846 | if (key == NULL) { |
| 1847 | self->fast_nesting = -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1848 | return 0; |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1849 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1850 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1851 | Py_DECREF(key); |
| 1852 | PyErr_Format(PyExc_ValueError, |
| 1853 | "fast mode: can't pickle cyclic objects " |
| 1854 | "including object type %.200s at %p", |
| 1855 | obj->ob_type->tp_name, obj); |
| 1856 | self->fast_nesting = -1; |
| 1857 | return 0; |
| 1858 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1859 | if (PyErr_Occurred()) { |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1860 | Py_DECREF(key); |
| 1861 | self->fast_nesting = -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1862 | return 0; |
| 1863 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1864 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1865 | Py_DECREF(key); |
| 1866 | self->fast_nesting = -1; |
| 1867 | return 0; |
| 1868 | } |
| 1869 | Py_DECREF(key); |
| 1870 | } |
| 1871 | return 1; |
| 1872 | } |
| 1873 | |
| 1874 | static int |
| 1875 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1876 | { |
| 1877 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1878 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1879 | if (key == NULL) |
| 1880 | return 0; |
| 1881 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1882 | Py_DECREF(key); |
| 1883 | return 0; |
| 1884 | } |
| 1885 | Py_DECREF(key); |
| 1886 | } |
| 1887 | return 1; |
| 1888 | } |
| 1889 | |
| 1890 | static int |
| 1891 | save_none(PicklerObject *self, PyObject *obj) |
| 1892 | { |
| 1893 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1894 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1895 | return -1; |
| 1896 | |
| 1897 | return 0; |
| 1898 | } |
| 1899 | |
| 1900 | static int |
| 1901 | save_bool(PicklerObject *self, PyObject *obj) |
| 1902 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1903 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1904 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1905 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1906 | return -1; |
| 1907 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1908 | else { |
| 1909 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1910 | * so that unpicklers written before bools were introduced unpickle them |
| 1911 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1912 | * Note that protocol 2 added direct ways to pickle bools. |
| 1913 | */ |
| 1914 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1915 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1916 | return -1; |
| 1917 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1918 | return 0; |
| 1919 | } |
| 1920 | |
| 1921 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1922 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1923 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1924 | PyObject *repr = NULL; |
| 1925 | Py_ssize_t size; |
| 1926 | long val; |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1927 | int overflow; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1928 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1929 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1930 | val= PyLong_AsLongAndOverflow(obj, &overflow); |
| 1931 | if (!overflow && (sizeof(long) <= 4 || |
| 1932 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) |
| 1933 | { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1934 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1935 | |
| 1936 | Note: we can't use -0x80000000L in the above condition because some |
| 1937 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1938 | before applying the unary minus when sizeof(long) <= 4. The |
| 1939 | resulting value stays unsigned which is commonly not what we want, |
| 1940 | so MSVC happily warns us about it. However, that result would have |
| 1941 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1942 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1943 | char pdata[32]; |
| 1944 | Py_ssize_t len = 0; |
| 1945 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1946 | if (self->bin) { |
| 1947 | pdata[1] = (unsigned char)(val & 0xff); |
| 1948 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1949 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1950 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1951 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1952 | if ((pdata[4] != 0) || (pdata[3] != 0)) { |
| 1953 | pdata[0] = BININT; |
| 1954 | len = 5; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1955 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1956 | else if (pdata[2] != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1957 | pdata[0] = BININT2; |
| 1958 | len = 3; |
| 1959 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1960 | else { |
| 1961 | pdata[0] = BININT1; |
| 1962 | len = 2; |
| 1963 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1964 | } |
| 1965 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1966 | sprintf(pdata, "%c%ld\n", INT, val); |
| 1967 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1968 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1969 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1970 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1971 | |
| 1972 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1973 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1974 | assert(!PyErr_Occurred()); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1975 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1976 | if (self->proto >= 2) { |
| 1977 | /* Linear-time pickling. */ |
| 1978 | size_t nbits; |
| 1979 | size_t nbytes; |
| 1980 | unsigned char *pdata; |
| 1981 | char header[5]; |
| 1982 | int i; |
| 1983 | int sign = _PyLong_Sign(obj); |
| 1984 | |
| 1985 | if (sign == 0) { |
| 1986 | header[0] = LONG1; |
| 1987 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1988 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1989 | goto error; |
| 1990 | return 0; |
| 1991 | } |
| 1992 | nbits = _PyLong_NumBits(obj); |
| 1993 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1994 | goto error; |
| 1995 | /* How many bytes do we need? There are nbits >> 3 full |
| 1996 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1997 | * are any leftover bits, then we clearly need another |
| 1998 | * byte. Wnat's not so obvious is that we *probably* |
| 1999 | * need another byte even if there aren't any leftovers: |
| 2000 | * the most-significant bit of the most-significant byte |
| 2001 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2002 | * opposite of the one we need. The exception is ints |
| 2003 | * 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] | 2004 | * its own 256's-complement, so has the right sign bit |
| 2005 | * even without the extra byte. That's a pain to check |
| 2006 | * for in advance, though, so we always grab an extra |
| 2007 | * byte at the start, and cut it back later if possible. |
| 2008 | */ |
| 2009 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 2010 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2011 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2012 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2013 | goto error; |
| 2014 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2015 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2016 | if (repr == NULL) |
| 2017 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2018 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2019 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 2020 | pdata, nbytes, |
| 2021 | 1 /* little endian */ , 1 /* signed */ ); |
| 2022 | if (i < 0) |
| 2023 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2024 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2025 | * needed. This is so iff the MSB is all redundant sign |
| 2026 | * bits. |
| 2027 | */ |
| 2028 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2029 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2030 | pdata[nbytes - 1] == 0xff && |
| 2031 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 2032 | nbytes--; |
| 2033 | } |
| 2034 | |
| 2035 | if (nbytes < 256) { |
| 2036 | header[0] = LONG1; |
| 2037 | header[1] = (unsigned char)nbytes; |
| 2038 | size = 2; |
| 2039 | } |
| 2040 | else { |
| 2041 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2042 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2043 | for (i = 1; i < 5; i++) { |
| 2044 | header[i] = (unsigned char)(size & 0xff); |
| 2045 | size >>= 8; |
| 2046 | } |
| 2047 | size = 5; |
| 2048 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2049 | if (_Pickler_Write(self, header, size) < 0 || |
| 2050 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2051 | goto error; |
| 2052 | } |
| 2053 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2054 | const char long_op = LONG; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2055 | const char *string; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2056 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 2057 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 2058 | the number of digits), in both directions. We add a trailing 'L' |
| 2059 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2060 | |
| 2061 | repr = PyObject_Repr(obj); |
| 2062 | if (repr == NULL) |
| 2063 | goto error; |
| 2064 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2065 | string = PyUnicode_AsUTF8AndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2066 | if (string == NULL) |
| 2067 | goto error; |
| 2068 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2069 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 2070 | _Pickler_Write(self, string, size) < 0 || |
| 2071 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2072 | goto error; |
| 2073 | } |
| 2074 | |
| 2075 | if (0) { |
| 2076 | error: |
| 2077 | status = -1; |
| 2078 | } |
| 2079 | Py_XDECREF(repr); |
| 2080 | |
| 2081 | return status; |
| 2082 | } |
| 2083 | |
| 2084 | static int |
| 2085 | save_float(PicklerObject *self, PyObject *obj) |
| 2086 | { |
| 2087 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 2088 | |
| 2089 | if (self->bin) { |
| 2090 | char pdata[9]; |
| 2091 | pdata[0] = BINFLOAT; |
| 2092 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2093 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2094 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2095 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2096 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2097 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2098 | int result = -1; |
| 2099 | char *buf = NULL; |
| 2100 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2101 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2102 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2103 | goto done; |
| 2104 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2105 | 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] | 2106 | if (!buf) { |
| 2107 | PyErr_NoMemory(); |
| 2108 | goto done; |
| 2109 | } |
| 2110 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2111 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2112 | goto done; |
| 2113 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2114 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2115 | goto done; |
| 2116 | |
| 2117 | result = 0; |
| 2118 | done: |
| 2119 | PyMem_Free(buf); |
| 2120 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2121 | } |
| 2122 | |
| 2123 | return 0; |
| 2124 | } |
| 2125 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2126 | /* Perform direct write of the header and payload of the binary object. |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2127 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2128 | The large contiguous data is written directly into the underlying file |
| 2129 | object, bypassing the output_buffer of the Pickler. We intentionally |
| 2130 | do not insert a protocol 4 frame opcode to make it possible to optimize |
| 2131 | file.read calls in the loader. |
| 2132 | */ |
| 2133 | static int |
| 2134 | _Pickler_write_bytes(PicklerObject *self, |
| 2135 | const char *header, Py_ssize_t header_size, |
| 2136 | const char *data, Py_ssize_t data_size, |
| 2137 | PyObject *payload) |
| 2138 | { |
| 2139 | int bypass_buffer = (data_size >= FRAME_SIZE_TARGET); |
| 2140 | int framing = self->framing; |
| 2141 | |
| 2142 | if (bypass_buffer) { |
| 2143 | assert(self->output_buffer != NULL); |
| 2144 | /* Commit the previous frame. */ |
| 2145 | if (_Pickler_CommitFrame(self)) { |
| 2146 | return -1; |
| 2147 | } |
| 2148 | /* Disable framing temporarily */ |
| 2149 | self->framing = 0; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2150 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2151 | |
| 2152 | if (_Pickler_Write(self, header, header_size) < 0) { |
| 2153 | return -1; |
| 2154 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2155 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2156 | if (bypass_buffer && self->write != NULL) { |
| 2157 | /* Bypass the in-memory buffer to directly stream large data |
| 2158 | into the underlying file object. */ |
| 2159 | PyObject *result, *mem = NULL; |
| 2160 | /* Dump the output buffer to the file. */ |
| 2161 | if (_Pickler_FlushToFile(self) < 0) { |
| 2162 | return -1; |
| 2163 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2164 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2165 | /* Stream write the payload into the file without going through the |
| 2166 | output buffer. */ |
| 2167 | if (payload == NULL) { |
Serhiy Storchaka | 5b76bdb | 2018-01-13 00:28:31 +0200 | [diff] [blame] | 2168 | /* TODO: It would be better to use a memoryview with a linked |
| 2169 | original string if this is possible. */ |
| 2170 | payload = mem = PyBytes_FromStringAndSize(data, data_size); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2171 | if (payload == NULL) { |
| 2172 | return -1; |
| 2173 | } |
| 2174 | } |
| 2175 | result = PyObject_CallFunctionObjArgs(self->write, payload, NULL); |
| 2176 | Py_XDECREF(mem); |
| 2177 | if (result == NULL) { |
| 2178 | return -1; |
| 2179 | } |
| 2180 | Py_DECREF(result); |
| 2181 | |
| 2182 | /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */ |
| 2183 | if (_Pickler_ClearBuffer(self) < 0) { |
| 2184 | return -1; |
| 2185 | } |
| 2186 | } |
| 2187 | else { |
| 2188 | if (_Pickler_Write(self, data, data_size) < 0) { |
| 2189 | return -1; |
| 2190 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2191 | } |
| 2192 | |
| 2193 | /* Re-enable framing for subsequent calls to _Pickler_Write. */ |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2194 | self->framing = framing; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2195 | |
| 2196 | return 0; |
| 2197 | } |
| 2198 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2199 | static int |
| 2200 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2201 | { |
| 2202 | if (self->proto < 3) { |
| 2203 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2204 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2205 | the __reduce__ method) to permit bytes object unpickling. |
| 2206 | |
| 2207 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2208 | 2 'bytes' is just an alias for 'str' (which has different |
| 2209 | parameters than the actual bytes object), we use codecs.encode |
| 2210 | to create the appropriate 'str' object when unpickled using |
| 2211 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2212 | using Python 3. Again this is a hack and we don't need to do this |
| 2213 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2214 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2215 | int status; |
| 2216 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2217 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2218 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2219 | } |
| 2220 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2221 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2222 | PyObject *unicode_str = |
| 2223 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2224 | PyBytes_GET_SIZE(obj), |
| 2225 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2226 | _Py_IDENTIFIER(latin1); |
| 2227 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2228 | if (unicode_str == NULL) |
| 2229 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2230 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2231 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2232 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2233 | Py_DECREF(unicode_str); |
| 2234 | } |
| 2235 | |
| 2236 | if (reduce_value == NULL) |
| 2237 | return -1; |
| 2238 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2239 | /* save_reduce() will memoize the object automatically. */ |
| 2240 | status = save_reduce(self, reduce_value, obj); |
| 2241 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2242 | return status; |
| 2243 | } |
| 2244 | else { |
| 2245 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2246 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2247 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2248 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2249 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2250 | if (size < 0) |
| 2251 | return -1; |
| 2252 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2253 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2254 | header[0] = SHORT_BINBYTES; |
| 2255 | header[1] = (unsigned char)size; |
| 2256 | len = 2; |
| 2257 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2258 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2259 | header[0] = BINBYTES; |
| 2260 | header[1] = (unsigned char)(size & 0xff); |
| 2261 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2262 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2263 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2264 | len = 5; |
| 2265 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2266 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2267 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2268 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2269 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2270 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2271 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2272 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2273 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2274 | return -1; /* string too large */ |
| 2275 | } |
| 2276 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2277 | if (_Pickler_write_bytes(self, header, len, |
| 2278 | PyBytes_AS_STRING(obj), size, obj) < 0) |
| 2279 | { |
| 2280 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2281 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2282 | |
| 2283 | if (memo_put(self, obj) < 0) |
| 2284 | return -1; |
| 2285 | |
| 2286 | return 0; |
| 2287 | } |
| 2288 | } |
| 2289 | |
| 2290 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2291 | backslash and newline characters to \uXXXX escapes. */ |
| 2292 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2293 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2294 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2295 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2296 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2297 | void *data; |
| 2298 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2299 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2300 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2301 | if (PyUnicode_READY(obj)) |
| 2302 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2303 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2304 | _PyBytesWriter_Init(&writer); |
| 2305 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2306 | size = PyUnicode_GET_LENGTH(obj); |
| 2307 | data = PyUnicode_DATA(obj); |
| 2308 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2309 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2310 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2311 | if (p == NULL) |
| 2312 | goto error; |
| 2313 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2314 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2315 | for (i=0; i < size; i++) { |
| 2316 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2317 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2318 | if (ch >= 0x10000) { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2319 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2320 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2321 | if (p == NULL) |
| 2322 | goto error; |
| 2323 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2324 | *p++ = '\\'; |
| 2325 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2326 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2327 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2328 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2329 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2330 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2331 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2332 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2333 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2334 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2335 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2336 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2337 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2338 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2339 | if (p == NULL) |
| 2340 | goto error; |
| 2341 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2342 | *p++ = '\\'; |
| 2343 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2344 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2345 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2346 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2347 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2348 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2349 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2350 | else |
| 2351 | *p++ = (char) ch; |
| 2352 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2353 | |
| 2354 | return _PyBytesWriter_Finish(&writer, p); |
| 2355 | |
| 2356 | error: |
| 2357 | _PyBytesWriter_Dealloc(&writer); |
| 2358 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2359 | } |
| 2360 | |
| 2361 | static int |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2362 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2363 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2364 | char header[9]; |
| 2365 | Py_ssize_t len; |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2366 | PyObject *encoded = NULL; |
| 2367 | Py_ssize_t size; |
| 2368 | const char *data; |
| 2369 | |
| 2370 | if (PyUnicode_READY(obj)) |
| 2371 | return -1; |
| 2372 | |
| 2373 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2374 | if (data == NULL) { |
| 2375 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2376 | "surrogatepass" error handler. */ |
| 2377 | PyErr_Clear(); |
| 2378 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2379 | if (encoded == NULL) |
| 2380 | return -1; |
| 2381 | |
| 2382 | data = PyBytes_AS_STRING(encoded); |
| 2383 | size = PyBytes_GET_SIZE(encoded); |
| 2384 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2385 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2386 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2387 | if (size <= 0xff && self->proto >= 4) { |
| 2388 | header[0] = SHORT_BINUNICODE; |
| 2389 | header[1] = (unsigned char)(size & 0xff); |
| 2390 | len = 2; |
| 2391 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2392 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2393 | header[0] = BINUNICODE; |
| 2394 | header[1] = (unsigned char)(size & 0xff); |
| 2395 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2396 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2397 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2398 | len = 5; |
| 2399 | } |
| 2400 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2401 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2402 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2403 | len = 9; |
| 2404 | } |
| 2405 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2406 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2407 | "cannot serialize a string larger than 4GiB"); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2408 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2409 | return -1; |
| 2410 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2411 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2412 | if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) { |
| 2413 | Py_XDECREF(encoded); |
| 2414 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2415 | } |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2416 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2417 | return 0; |
| 2418 | } |
| 2419 | |
| 2420 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2421 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2422 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2423 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2424 | if (write_unicode_binary(self, obj) < 0) |
| 2425 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2426 | } |
| 2427 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2428 | PyObject *encoded; |
| 2429 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2430 | const char unicode_op = UNICODE; |
| 2431 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2432 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2433 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2434 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2435 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2436 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2437 | Py_DECREF(encoded); |
| 2438 | return -1; |
| 2439 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2440 | |
| 2441 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2442 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2443 | Py_DECREF(encoded); |
| 2444 | return -1; |
| 2445 | } |
| 2446 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2447 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2448 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2449 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2450 | } |
| 2451 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2452 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2453 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2454 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2455 | } |
| 2456 | |
| 2457 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2458 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2459 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2460 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2461 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2462 | |
| 2463 | assert(PyTuple_Size(t) == len); |
| 2464 | |
| 2465 | for (i = 0; i < len; i++) { |
| 2466 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2467 | |
| 2468 | if (element == NULL) |
| 2469 | return -1; |
| 2470 | if (save(self, element, 0) < 0) |
| 2471 | return -1; |
| 2472 | } |
| 2473 | |
| 2474 | return 0; |
| 2475 | } |
| 2476 | |
| 2477 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2478 | * used across protocols to minimize the space needed to pickle them. |
| 2479 | * Tuples are also the only builtin immutable type that can be recursive |
| 2480 | * (a tuple can be reached from itself), and that requires some subtle |
| 2481 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2482 | */ |
| 2483 | static int |
| 2484 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2485 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2486 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2487 | |
| 2488 | const char mark_op = MARK; |
| 2489 | const char tuple_op = TUPLE; |
| 2490 | const char pop_op = POP; |
| 2491 | const char pop_mark_op = POP_MARK; |
| 2492 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2493 | |
| 2494 | if ((len = PyTuple_Size(obj)) < 0) |
| 2495 | return -1; |
| 2496 | |
| 2497 | if (len == 0) { |
| 2498 | char pdata[2]; |
| 2499 | |
| 2500 | if (self->proto) { |
| 2501 | pdata[0] = EMPTY_TUPLE; |
| 2502 | len = 1; |
| 2503 | } |
| 2504 | else { |
| 2505 | pdata[0] = MARK; |
| 2506 | pdata[1] = TUPLE; |
| 2507 | len = 2; |
| 2508 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2509 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2510 | return -1; |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2514 | /* 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] | 2515 | * saving the tuple elements, the tuple must be recursive, in |
| 2516 | * which case we'll pop everything we put on the stack, and fetch |
| 2517 | * its value from the memo. |
| 2518 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2519 | if (len <= 3 && self->proto >= 2) { |
| 2520 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2521 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2522 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2523 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2524 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2525 | /* pop the len elements */ |
| 2526 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2527 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2528 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2529 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2530 | if (memo_get(self, obj) < 0) |
| 2531 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2532 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2533 | return 0; |
| 2534 | } |
| 2535 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2536 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2537 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2538 | } |
| 2539 | goto memoize; |
| 2540 | } |
| 2541 | |
| 2542 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2543 | * Generate MARK e1 e2 ... TUPLE |
| 2544 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2545 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2546 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2547 | |
| 2548 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2549 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2550 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2551 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2552 | /* pop the stack stuff we pushed */ |
| 2553 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2554 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2555 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2556 | } |
| 2557 | else { |
| 2558 | /* Note that we pop one more than len, to remove |
| 2559 | * the MARK too. |
| 2560 | */ |
| 2561 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2562 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2563 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2564 | } |
| 2565 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2566 | if (memo_get(self, obj) < 0) |
| 2567 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2568 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2569 | return 0; |
| 2570 | } |
| 2571 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2572 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2573 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
| 2576 | memoize: |
| 2577 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2578 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2579 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2580 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2581 | } |
| 2582 | |
| 2583 | /* iter is an iterator giving items, and we batch up chunks of |
| 2584 | * MARK item item ... item APPENDS |
| 2585 | * opcode sequences. Calling code should have arranged to first create an |
| 2586 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2587 | * Returns 0 on success, <0 on error. |
| 2588 | */ |
| 2589 | static int |
| 2590 | batch_list(PicklerObject *self, PyObject *iter) |
| 2591 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2592 | PyObject *obj = NULL; |
| 2593 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2594 | int i, n; |
| 2595 | |
| 2596 | const char mark_op = MARK; |
| 2597 | const char append_op = APPEND; |
| 2598 | const char appends_op = APPENDS; |
| 2599 | |
| 2600 | assert(iter != NULL); |
| 2601 | |
| 2602 | /* XXX: I think this function could be made faster by avoiding the |
| 2603 | iterator interface and fetching objects directly from list using |
| 2604 | PyList_GET_ITEM. |
| 2605 | */ |
| 2606 | |
| 2607 | if (self->proto == 0) { |
| 2608 | /* APPENDS isn't available; do one at a time. */ |
| 2609 | for (;;) { |
| 2610 | obj = PyIter_Next(iter); |
| 2611 | if (obj == NULL) { |
| 2612 | if (PyErr_Occurred()) |
| 2613 | return -1; |
| 2614 | break; |
| 2615 | } |
| 2616 | i = save(self, obj, 0); |
| 2617 | Py_DECREF(obj); |
| 2618 | if (i < 0) |
| 2619 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2620 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2621 | return -1; |
| 2622 | } |
| 2623 | return 0; |
| 2624 | } |
| 2625 | |
| 2626 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2627 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2628 | /* Get first item */ |
| 2629 | firstitem = PyIter_Next(iter); |
| 2630 | if (firstitem == NULL) { |
| 2631 | if (PyErr_Occurred()) |
| 2632 | goto error; |
| 2633 | |
| 2634 | /* nothing more to add */ |
| 2635 | break; |
| 2636 | } |
| 2637 | |
| 2638 | /* Try to get a second item */ |
| 2639 | obj = PyIter_Next(iter); |
| 2640 | if (obj == NULL) { |
| 2641 | if (PyErr_Occurred()) |
| 2642 | goto error; |
| 2643 | |
| 2644 | /* Only one item to write */ |
| 2645 | if (save(self, firstitem, 0) < 0) |
| 2646 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2647 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2648 | goto error; |
| 2649 | Py_CLEAR(firstitem); |
| 2650 | break; |
| 2651 | } |
| 2652 | |
| 2653 | /* More than one item to write */ |
| 2654 | |
| 2655 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2656 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2657 | goto error; |
| 2658 | |
| 2659 | if (save(self, firstitem, 0) < 0) |
| 2660 | goto error; |
| 2661 | Py_CLEAR(firstitem); |
| 2662 | n = 1; |
| 2663 | |
| 2664 | /* Fetch and save up to BATCHSIZE items */ |
| 2665 | while (obj) { |
| 2666 | if (save(self, obj, 0) < 0) |
| 2667 | goto error; |
| 2668 | Py_CLEAR(obj); |
| 2669 | n += 1; |
| 2670 | |
| 2671 | if (n == BATCHSIZE) |
| 2672 | break; |
| 2673 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2674 | obj = PyIter_Next(iter); |
| 2675 | if (obj == NULL) { |
| 2676 | if (PyErr_Occurred()) |
| 2677 | goto error; |
| 2678 | break; |
| 2679 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2682 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2683 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2684 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2685 | } while (n == BATCHSIZE); |
| 2686 | return 0; |
| 2687 | |
| 2688 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2689 | Py_XDECREF(firstitem); |
| 2690 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2691 | return -1; |
| 2692 | } |
| 2693 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2694 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2695 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2696 | * MARK item item ... item APPENDS |
| 2697 | * opcode sequences. Calling code should have arranged to first create an |
| 2698 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2699 | * Returns 0 on success, -1 on error. |
| 2700 | * |
| 2701 | * This version is considerably faster than batch_list(), if less general. |
| 2702 | * |
| 2703 | * Note that this only works for protocols > 0. |
| 2704 | */ |
| 2705 | static int |
| 2706 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2707 | { |
| 2708 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2709 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2710 | |
| 2711 | const char append_op = APPEND; |
| 2712 | const char appends_op = APPENDS; |
| 2713 | const char mark_op = MARK; |
| 2714 | |
| 2715 | assert(obj != NULL); |
| 2716 | assert(self->proto > 0); |
| 2717 | assert(PyList_CheckExact(obj)); |
| 2718 | |
| 2719 | if (PyList_GET_SIZE(obj) == 1) { |
| 2720 | item = PyList_GET_ITEM(obj, 0); |
| 2721 | if (save(self, item, 0) < 0) |
| 2722 | return -1; |
| 2723 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2724 | return -1; |
| 2725 | return 0; |
| 2726 | } |
| 2727 | |
| 2728 | /* Write in batches of BATCHSIZE. */ |
| 2729 | total = 0; |
| 2730 | do { |
| 2731 | this_batch = 0; |
| 2732 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2733 | return -1; |
| 2734 | while (total < PyList_GET_SIZE(obj)) { |
| 2735 | item = PyList_GET_ITEM(obj, total); |
| 2736 | if (save(self, item, 0) < 0) |
| 2737 | return -1; |
| 2738 | total++; |
| 2739 | if (++this_batch == BATCHSIZE) |
| 2740 | break; |
| 2741 | } |
| 2742 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2743 | return -1; |
| 2744 | |
| 2745 | } while (total < PyList_GET_SIZE(obj)); |
| 2746 | |
| 2747 | return 0; |
| 2748 | } |
| 2749 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2750 | static int |
| 2751 | save_list(PicklerObject *self, PyObject *obj) |
| 2752 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2753 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2754 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2755 | int status = 0; |
| 2756 | |
| 2757 | if (self->fast && !fast_save_enter(self, obj)) |
| 2758 | goto error; |
| 2759 | |
| 2760 | /* Create an empty list. */ |
| 2761 | if (self->bin) { |
| 2762 | header[0] = EMPTY_LIST; |
| 2763 | len = 1; |
| 2764 | } |
| 2765 | else { |
| 2766 | header[0] = MARK; |
| 2767 | header[1] = LIST; |
| 2768 | len = 2; |
| 2769 | } |
| 2770 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2771 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2772 | goto error; |
| 2773 | |
| 2774 | /* Get list length, and bow out early if empty. */ |
| 2775 | if ((len = PyList_Size(obj)) < 0) |
| 2776 | goto error; |
| 2777 | |
| 2778 | if (memo_put(self, obj) < 0) |
| 2779 | goto error; |
| 2780 | |
| 2781 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2782 | /* Materialize the list elements. */ |
| 2783 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2784 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2785 | goto error; |
| 2786 | status = batch_list_exact(self, obj); |
| 2787 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2788 | } else { |
| 2789 | PyObject *iter = PyObject_GetIter(obj); |
| 2790 | if (iter == NULL) |
| 2791 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2793 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2794 | Py_DECREF(iter); |
| 2795 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2796 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2797 | status = batch_list(self, iter); |
| 2798 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2799 | Py_DECREF(iter); |
| 2800 | } |
| 2801 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2802 | if (0) { |
| 2803 | error: |
| 2804 | status = -1; |
| 2805 | } |
| 2806 | |
| 2807 | if (self->fast && !fast_save_leave(self, obj)) |
| 2808 | status = -1; |
| 2809 | |
| 2810 | return status; |
| 2811 | } |
| 2812 | |
| 2813 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2814 | * MARK key value ... key value SETITEMS |
| 2815 | * opcode sequences. Calling code should have arranged to first create an |
| 2816 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2817 | * Returns 0 on success, <0 on error. |
| 2818 | * |
| 2819 | * This is very much like batch_list(). The difference between saving |
| 2820 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2821 | * the C level, though, that attempts to combine these routines were too |
| 2822 | * ugly to bear. |
| 2823 | */ |
| 2824 | static int |
| 2825 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2826 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2827 | PyObject *obj = NULL; |
| 2828 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2829 | int i, n; |
| 2830 | |
| 2831 | const char mark_op = MARK; |
| 2832 | const char setitem_op = SETITEM; |
| 2833 | const char setitems_op = SETITEMS; |
| 2834 | |
| 2835 | assert(iter != NULL); |
| 2836 | |
| 2837 | if (self->proto == 0) { |
| 2838 | /* SETITEMS isn't available; do one at a time. */ |
| 2839 | for (;;) { |
| 2840 | obj = PyIter_Next(iter); |
| 2841 | if (obj == NULL) { |
| 2842 | if (PyErr_Occurred()) |
| 2843 | return -1; |
| 2844 | break; |
| 2845 | } |
| 2846 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2847 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2848 | "iterator must return 2-tuples"); |
| 2849 | return -1; |
| 2850 | } |
| 2851 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2852 | if (i >= 0) |
| 2853 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2854 | Py_DECREF(obj); |
| 2855 | if (i < 0) |
| 2856 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2857 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2858 | return -1; |
| 2859 | } |
| 2860 | return 0; |
| 2861 | } |
| 2862 | |
| 2863 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2864 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2865 | /* Get first item */ |
| 2866 | firstitem = PyIter_Next(iter); |
| 2867 | if (firstitem == NULL) { |
| 2868 | if (PyErr_Occurred()) |
| 2869 | goto error; |
| 2870 | |
| 2871 | /* nothing more to add */ |
| 2872 | break; |
| 2873 | } |
| 2874 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2875 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2876 | "iterator must return 2-tuples"); |
| 2877 | goto error; |
| 2878 | } |
| 2879 | |
| 2880 | /* Try to get a second item */ |
| 2881 | obj = PyIter_Next(iter); |
| 2882 | if (obj == NULL) { |
| 2883 | if (PyErr_Occurred()) |
| 2884 | goto error; |
| 2885 | |
| 2886 | /* Only one item to write */ |
| 2887 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2888 | goto error; |
| 2889 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2890 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2891 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2892 | goto error; |
| 2893 | Py_CLEAR(firstitem); |
| 2894 | break; |
| 2895 | } |
| 2896 | |
| 2897 | /* More than one item to write */ |
| 2898 | |
| 2899 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2900 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2901 | goto error; |
| 2902 | |
| 2903 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2904 | goto error; |
| 2905 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2906 | goto error; |
| 2907 | Py_CLEAR(firstitem); |
| 2908 | n = 1; |
| 2909 | |
| 2910 | /* Fetch and save up to BATCHSIZE items */ |
| 2911 | while (obj) { |
| 2912 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2913 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2914 | "iterator must return 2-tuples"); |
| 2915 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2916 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2917 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2918 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2919 | goto error; |
| 2920 | Py_CLEAR(obj); |
| 2921 | n += 1; |
| 2922 | |
| 2923 | if (n == BATCHSIZE) |
| 2924 | break; |
| 2925 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2926 | obj = PyIter_Next(iter); |
| 2927 | if (obj == NULL) { |
| 2928 | if (PyErr_Occurred()) |
| 2929 | goto error; |
| 2930 | break; |
| 2931 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2932 | } |
| 2933 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2934 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2935 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2936 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2937 | } while (n == BATCHSIZE); |
| 2938 | return 0; |
| 2939 | |
| 2940 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2941 | Py_XDECREF(firstitem); |
| 2942 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2943 | return -1; |
| 2944 | } |
| 2945 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2946 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2947 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2948 | * MARK key value ... key value SETITEMS |
| 2949 | * opcode sequences. Calling code should have arranged to first create an |
| 2950 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2951 | * Returns 0 on success, -1 on error. |
| 2952 | * |
| 2953 | * Note that this currently doesn't work for protocol 0. |
| 2954 | */ |
| 2955 | static int |
| 2956 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2957 | { |
| 2958 | PyObject *key = NULL, *value = NULL; |
| 2959 | int i; |
| 2960 | Py_ssize_t dict_size, ppos = 0; |
| 2961 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2962 | const char mark_op = MARK; |
| 2963 | const char setitem_op = SETITEM; |
| 2964 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2965 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2966 | assert(obj != NULL && PyDict_CheckExact(obj)); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2967 | assert(self->proto > 0); |
| 2968 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2969 | dict_size = PyDict_GET_SIZE(obj); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2970 | |
| 2971 | /* Special-case len(d) == 1 to save space. */ |
| 2972 | if (dict_size == 1) { |
| 2973 | PyDict_Next(obj, &ppos, &key, &value); |
| 2974 | if (save(self, key, 0) < 0) |
| 2975 | return -1; |
| 2976 | if (save(self, value, 0) < 0) |
| 2977 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2978 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2979 | return -1; |
| 2980 | return 0; |
| 2981 | } |
| 2982 | |
| 2983 | /* Write in batches of BATCHSIZE. */ |
| 2984 | do { |
| 2985 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2986 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2987 | return -1; |
| 2988 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2989 | if (save(self, key, 0) < 0) |
| 2990 | return -1; |
| 2991 | if (save(self, value, 0) < 0) |
| 2992 | return -1; |
| 2993 | if (++i == BATCHSIZE) |
| 2994 | break; |
| 2995 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2996 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2997 | return -1; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2998 | if (PyDict_GET_SIZE(obj) != dict_size) { |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2999 | PyErr_Format( |
| 3000 | PyExc_RuntimeError, |
| 3001 | "dictionary changed size during iteration"); |
| 3002 | return -1; |
| 3003 | } |
| 3004 | |
| 3005 | } while (i == BATCHSIZE); |
| 3006 | return 0; |
| 3007 | } |
| 3008 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3009 | static int |
| 3010 | save_dict(PicklerObject *self, PyObject *obj) |
| 3011 | { |
| 3012 | PyObject *items, *iter; |
| 3013 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3014 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3015 | int status = 0; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3016 | assert(PyDict_Check(obj)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3017 | |
| 3018 | if (self->fast && !fast_save_enter(self, obj)) |
| 3019 | goto error; |
| 3020 | |
| 3021 | /* Create an empty dict. */ |
| 3022 | if (self->bin) { |
| 3023 | header[0] = EMPTY_DICT; |
| 3024 | len = 1; |
| 3025 | } |
| 3026 | else { |
| 3027 | header[0] = MARK; |
| 3028 | header[1] = DICT; |
| 3029 | len = 2; |
| 3030 | } |
| 3031 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3032 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3033 | goto error; |
| 3034 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3035 | if (memo_put(self, obj) < 0) |
| 3036 | goto error; |
| 3037 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3038 | if (PyDict_GET_SIZE(obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3039 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3040 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 3041 | /* We can take certain shortcuts if we know this is a dict and |
| 3042 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3043 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3044 | goto error; |
| 3045 | status = batch_dict_exact(self, obj); |
| 3046 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3047 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3048 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3049 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3050 | items = _PyObject_CallMethodId(obj, &PyId_items, NULL); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3051 | if (items == NULL) |
| 3052 | goto error; |
| 3053 | iter = PyObject_GetIter(items); |
| 3054 | Py_DECREF(items); |
| 3055 | if (iter == NULL) |
| 3056 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3057 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3058 | Py_DECREF(iter); |
| 3059 | goto error; |
| 3060 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3061 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3062 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3063 | Py_DECREF(iter); |
| 3064 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| 3067 | if (0) { |
| 3068 | error: |
| 3069 | status = -1; |
| 3070 | } |
| 3071 | |
| 3072 | if (self->fast && !fast_save_leave(self, obj)) |
| 3073 | status = -1; |
| 3074 | |
| 3075 | return status; |
| 3076 | } |
| 3077 | |
| 3078 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3079 | save_set(PicklerObject *self, PyObject *obj) |
| 3080 | { |
| 3081 | PyObject *item; |
| 3082 | int i; |
| 3083 | Py_ssize_t set_size, ppos = 0; |
| 3084 | Py_hash_t hash; |
| 3085 | |
| 3086 | const char empty_set_op = EMPTY_SET; |
| 3087 | const char mark_op = MARK; |
| 3088 | const char additems_op = ADDITEMS; |
| 3089 | |
| 3090 | if (self->proto < 4) { |
| 3091 | PyObject *items; |
| 3092 | PyObject *reduce_value; |
| 3093 | int status; |
| 3094 | |
| 3095 | items = PySequence_List(obj); |
| 3096 | if (items == NULL) { |
| 3097 | return -1; |
| 3098 | } |
| 3099 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 3100 | Py_DECREF(items); |
| 3101 | if (reduce_value == NULL) { |
| 3102 | return -1; |
| 3103 | } |
| 3104 | /* save_reduce() will memoize the object automatically. */ |
| 3105 | status = save_reduce(self, reduce_value, obj); |
| 3106 | Py_DECREF(reduce_value); |
| 3107 | return status; |
| 3108 | } |
| 3109 | |
| 3110 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 3111 | return -1; |
| 3112 | |
| 3113 | if (memo_put(self, obj) < 0) |
| 3114 | return -1; |
| 3115 | |
| 3116 | set_size = PySet_GET_SIZE(obj); |
| 3117 | if (set_size == 0) |
| 3118 | return 0; /* nothing to do */ |
| 3119 | |
| 3120 | /* Write in batches of BATCHSIZE. */ |
| 3121 | do { |
| 3122 | i = 0; |
| 3123 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3124 | return -1; |
| 3125 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 3126 | if (save(self, item, 0) < 0) |
| 3127 | return -1; |
| 3128 | if (++i == BATCHSIZE) |
| 3129 | break; |
| 3130 | } |
| 3131 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 3132 | return -1; |
| 3133 | if (PySet_GET_SIZE(obj) != set_size) { |
| 3134 | PyErr_Format( |
| 3135 | PyExc_RuntimeError, |
| 3136 | "set changed size during iteration"); |
| 3137 | return -1; |
| 3138 | } |
| 3139 | } while (i == BATCHSIZE); |
| 3140 | |
| 3141 | return 0; |
| 3142 | } |
| 3143 | |
| 3144 | static int |
| 3145 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 3146 | { |
| 3147 | PyObject *iter; |
| 3148 | |
| 3149 | const char mark_op = MARK; |
| 3150 | const char frozenset_op = FROZENSET; |
| 3151 | |
| 3152 | if (self->fast && !fast_save_enter(self, obj)) |
| 3153 | return -1; |
| 3154 | |
| 3155 | if (self->proto < 4) { |
| 3156 | PyObject *items; |
| 3157 | PyObject *reduce_value; |
| 3158 | int status; |
| 3159 | |
| 3160 | items = PySequence_List(obj); |
| 3161 | if (items == NULL) { |
| 3162 | return -1; |
| 3163 | } |
| 3164 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3165 | items); |
| 3166 | Py_DECREF(items); |
| 3167 | if (reduce_value == NULL) { |
| 3168 | return -1; |
| 3169 | } |
| 3170 | /* save_reduce() will memoize the object automatically. */ |
| 3171 | status = save_reduce(self, reduce_value, obj); |
| 3172 | Py_DECREF(reduce_value); |
| 3173 | return status; |
| 3174 | } |
| 3175 | |
| 3176 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3177 | return -1; |
| 3178 | |
| 3179 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3180 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3181 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3182 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3183 | for (;;) { |
| 3184 | PyObject *item; |
| 3185 | |
| 3186 | item = PyIter_Next(iter); |
| 3187 | if (item == NULL) { |
| 3188 | if (PyErr_Occurred()) { |
| 3189 | Py_DECREF(iter); |
| 3190 | return -1; |
| 3191 | } |
| 3192 | break; |
| 3193 | } |
| 3194 | if (save(self, item, 0) < 0) { |
| 3195 | Py_DECREF(item); |
| 3196 | Py_DECREF(iter); |
| 3197 | return -1; |
| 3198 | } |
| 3199 | Py_DECREF(item); |
| 3200 | } |
| 3201 | Py_DECREF(iter); |
| 3202 | |
| 3203 | /* If the object is already in the memo, this means it is |
| 3204 | recursive. In this case, throw away everything we put on the |
| 3205 | stack, and fetch the object back from the memo. */ |
| 3206 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3207 | const char pop_mark_op = POP_MARK; |
| 3208 | |
| 3209 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3210 | return -1; |
| 3211 | if (memo_get(self, obj) < 0) |
| 3212 | return -1; |
| 3213 | return 0; |
| 3214 | } |
| 3215 | |
| 3216 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3217 | return -1; |
| 3218 | if (memo_put(self, obj) < 0) |
| 3219 | return -1; |
| 3220 | |
| 3221 | return 0; |
| 3222 | } |
| 3223 | |
| 3224 | static int |
| 3225 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3226 | { |
| 3227 | PyObject *key; |
| 3228 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3229 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3230 | |
| 3231 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3232 | if (key == NULL) |
| 3233 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3234 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3235 | Py_DECREF(key); |
| 3236 | if (item) { |
| 3237 | PyObject *fixed_module_name; |
| 3238 | PyObject *fixed_global_name; |
| 3239 | |
| 3240 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3241 | PyErr_Format(PyExc_RuntimeError, |
| 3242 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3243 | "should be 2-tuples, not %.200s", |
| 3244 | Py_TYPE(item)->tp_name); |
| 3245 | return -1; |
| 3246 | } |
| 3247 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3248 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3249 | if (!PyUnicode_Check(fixed_module_name) || |
| 3250 | !PyUnicode_Check(fixed_global_name)) { |
| 3251 | PyErr_Format(PyExc_RuntimeError, |
| 3252 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3253 | "should be pairs of str, not (%.200s, %.200s)", |
| 3254 | Py_TYPE(fixed_module_name)->tp_name, |
| 3255 | Py_TYPE(fixed_global_name)->tp_name); |
| 3256 | return -1; |
| 3257 | } |
| 3258 | |
| 3259 | Py_CLEAR(*module_name); |
| 3260 | Py_CLEAR(*global_name); |
| 3261 | Py_INCREF(fixed_module_name); |
| 3262 | Py_INCREF(fixed_global_name); |
| 3263 | *module_name = fixed_module_name; |
| 3264 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3265 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3266 | } |
| 3267 | else if (PyErr_Occurred()) { |
| 3268 | return -1; |
| 3269 | } |
| 3270 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3271 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3272 | if (item) { |
| 3273 | if (!PyUnicode_Check(item)) { |
| 3274 | PyErr_Format(PyExc_RuntimeError, |
| 3275 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3276 | "should be strings, not %.200s", |
| 3277 | Py_TYPE(item)->tp_name); |
| 3278 | return -1; |
| 3279 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3280 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3281 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3282 | } |
| 3283 | else if (PyErr_Occurred()) { |
| 3284 | return -1; |
| 3285 | } |
| 3286 | |
| 3287 | return 0; |
| 3288 | } |
| 3289 | |
| 3290 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3291 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3292 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3293 | PyObject *global_name = NULL; |
| 3294 | PyObject *module_name = NULL; |
| 3295 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3296 | PyObject *parent = NULL; |
| 3297 | PyObject *dotted_path = NULL; |
| 3298 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3299 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3300 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3301 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3302 | _Py_IDENTIFIER(__name__); |
| 3303 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3304 | |
| 3305 | const char global_op = GLOBAL; |
| 3306 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3307 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3308 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3309 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3310 | } |
| 3311 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3312 | if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0) |
| 3313 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3314 | if (global_name == NULL) { |
| 3315 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3316 | if (global_name == NULL) |
| 3317 | goto error; |
| 3318 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3321 | dotted_path = get_dotted_path(module, global_name); |
| 3322 | if (dotted_path == NULL) |
| 3323 | goto error; |
| 3324 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3325 | if (module_name == NULL) |
| 3326 | goto error; |
| 3327 | |
| 3328 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3329 | relative imports. |
| 3330 | |
| 3331 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3332 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3333 | custom import functions (IMHO, this would be a nice security |
| 3334 | feature). The import C API would need to be extended to support the |
| 3335 | extra parameters of __import__ to fix that. */ |
| 3336 | module = PyImport_Import(module_name); |
| 3337 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3338 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3339 | "Can't pickle %R: import of module %R failed", |
| 3340 | obj, module_name); |
| 3341 | goto error; |
| 3342 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3343 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3344 | Py_INCREF(lastname); |
| 3345 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3346 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3347 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3348 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3349 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3350 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3351 | goto error; |
| 3352 | } |
| 3353 | if (cls != obj) { |
| 3354 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3355 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3356 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3357 | obj, module_name, global_name); |
| 3358 | goto error; |
| 3359 | } |
| 3360 | Py_DECREF(cls); |
| 3361 | |
| 3362 | if (self->proto >= 2) { |
| 3363 | /* See whether this is in the extension registry, and if |
| 3364 | * so generate an EXT opcode. |
| 3365 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3366 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3367 | PyObject *code_obj; /* extension code as Python object */ |
| 3368 | long code; /* extension code as C value */ |
| 3369 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3370 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3371 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3372 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3373 | if (extension_key == NULL) { |
| 3374 | goto error; |
| 3375 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3376 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3377 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3378 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3379 | /* The object is not registered in the extension registry. |
| 3380 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3381 | if (code_obj == NULL) { |
| 3382 | if (PyErr_Occurred()) { |
| 3383 | goto error; |
| 3384 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3385 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3386 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3387 | |
| 3388 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3389 | of the value returned by the extension_registry. It should for |
| 3390 | consistency. */ |
| 3391 | |
| 3392 | /* Verify code_obj has the right type and value. */ |
| 3393 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3394 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3395 | "Can't pickle %R: extension code %R isn't an integer", |
| 3396 | obj, code_obj); |
| 3397 | goto error; |
| 3398 | } |
| 3399 | code = PyLong_AS_LONG(code_obj); |
| 3400 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3401 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3402 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3403 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3404 | goto error; |
| 3405 | } |
| 3406 | |
| 3407 | /* Generate an EXT opcode. */ |
| 3408 | if (code <= 0xff) { |
| 3409 | pdata[0] = EXT1; |
| 3410 | pdata[1] = (unsigned char)code; |
| 3411 | n = 2; |
| 3412 | } |
| 3413 | else if (code <= 0xffff) { |
| 3414 | pdata[0] = EXT2; |
| 3415 | pdata[1] = (unsigned char)(code & 0xff); |
| 3416 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3417 | n = 3; |
| 3418 | } |
| 3419 | else { |
| 3420 | pdata[0] = EXT4; |
| 3421 | pdata[1] = (unsigned char)(code & 0xff); |
| 3422 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3423 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3424 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3425 | n = 5; |
| 3426 | } |
| 3427 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3428 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3429 | goto error; |
| 3430 | } |
| 3431 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3432 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3433 | if (parent == module) { |
| 3434 | Py_INCREF(lastname); |
| 3435 | Py_DECREF(global_name); |
| 3436 | global_name = lastname; |
| 3437 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3438 | if (self->proto >= 4) { |
| 3439 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3440 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3441 | if (save(self, module_name, 0) < 0) |
| 3442 | goto error; |
| 3443 | if (save(self, global_name, 0) < 0) |
| 3444 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3445 | |
| 3446 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3447 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3448 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3449 | else if (parent != module) { |
| 3450 | PickleState *st = _Pickle_GetGlobalState(); |
| 3451 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3452 | st->getattr, parent, lastname); |
| 3453 | status = save_reduce(self, reduce_value, NULL); |
| 3454 | Py_DECREF(reduce_value); |
| 3455 | if (status < 0) |
| 3456 | goto error; |
| 3457 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3458 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3459 | /* Generate a normal global opcode if we are using a pickle |
| 3460 | protocol < 4, or if the object is not registered in the |
| 3461 | extension registry. */ |
| 3462 | PyObject *encoded; |
| 3463 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3464 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3465 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3466 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3467 | |
| 3468 | /* For protocol < 3 and if the user didn't request against doing |
| 3469 | so, we convert module names to the old 2.x module names. */ |
| 3470 | if (self->proto < 3 && self->fix_imports) { |
| 3471 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3472 | goto error; |
| 3473 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3474 | } |
| 3475 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3476 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3477 | both the module name and the global name using UTF-8. We do so |
| 3478 | only when we are using the pickle protocol newer than version |
| 3479 | 3. This is to ensure compatibility with older Unpickler running |
| 3480 | on Python 2.x. */ |
| 3481 | if (self->proto == 3) { |
| 3482 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3483 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3484 | else { |
| 3485 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3486 | } |
| 3487 | encoded = unicode_encoder(module_name); |
| 3488 | if (encoded == NULL) { |
| 3489 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3490 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3491 | "can't pickle module identifier '%S' using " |
| 3492 | "pickle protocol %i", |
| 3493 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3494 | goto error; |
| 3495 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3496 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3497 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3498 | Py_DECREF(encoded); |
| 3499 | goto error; |
| 3500 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3501 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3502 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3503 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3504 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3505 | /* Save the name of the module. */ |
| 3506 | encoded = unicode_encoder(global_name); |
| 3507 | if (encoded == NULL) { |
| 3508 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3509 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3510 | "can't pickle global identifier '%S' using " |
| 3511 | "pickle protocol %i", |
| 3512 | global_name, self->proto); |
| 3513 | goto error; |
| 3514 | } |
| 3515 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3516 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3517 | Py_DECREF(encoded); |
| 3518 | goto error; |
| 3519 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3520 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3521 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3522 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3523 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3524 | /* Memoize the object. */ |
| 3525 | if (memo_put(self, obj) < 0) |
| 3526 | goto error; |
| 3527 | } |
| 3528 | |
| 3529 | if (0) { |
| 3530 | error: |
| 3531 | status = -1; |
| 3532 | } |
| 3533 | Py_XDECREF(module_name); |
| 3534 | Py_XDECREF(global_name); |
| 3535 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3536 | Py_XDECREF(parent); |
| 3537 | Py_XDECREF(dotted_path); |
| 3538 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3539 | |
| 3540 | return status; |
| 3541 | } |
| 3542 | |
| 3543 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3544 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3545 | { |
| 3546 | PyObject *reduce_value; |
| 3547 | int status; |
| 3548 | |
| 3549 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3550 | if (reduce_value == NULL) { |
| 3551 | return -1; |
| 3552 | } |
| 3553 | status = save_reduce(self, reduce_value, obj); |
| 3554 | Py_DECREF(reduce_value); |
| 3555 | return status; |
| 3556 | } |
| 3557 | |
| 3558 | static int |
| 3559 | save_type(PicklerObject *self, PyObject *obj) |
| 3560 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3561 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3562 | return save_singleton_type(self, obj, Py_None); |
| 3563 | } |
| 3564 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3565 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3566 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3567 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3568 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3569 | } |
| 3570 | return save_global(self, obj, NULL); |
| 3571 | } |
| 3572 | |
| 3573 | static int |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3574 | save_pers(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3575 | { |
| 3576 | PyObject *pid = NULL; |
| 3577 | int status = 0; |
| 3578 | |
| 3579 | const char persid_op = PERSID; |
| 3580 | const char binpersid_op = BINPERSID; |
| 3581 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3582 | pid = call_method(self->pers_func, self->pers_func_self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3583 | if (pid == NULL) |
| 3584 | return -1; |
| 3585 | |
| 3586 | if (pid != Py_None) { |
| 3587 | if (self->bin) { |
| 3588 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3589 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3590 | goto error; |
| 3591 | } |
| 3592 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3593 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3594 | |
| 3595 | pid_str = PyObject_Str(pid); |
| 3596 | if (pid_str == NULL) |
| 3597 | goto error; |
| 3598 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3599 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3600 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3601 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3602 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3603 | "persistent IDs in protocol 0 must be " |
| 3604 | "ASCII strings"); |
| 3605 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3606 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3607 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3608 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3609 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3610 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3611 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3612 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3613 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3614 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3615 | } |
| 3616 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3617 | } |
| 3618 | status = 1; |
| 3619 | } |
| 3620 | |
| 3621 | if (0) { |
| 3622 | error: |
| 3623 | status = -1; |
| 3624 | } |
| 3625 | Py_XDECREF(pid); |
| 3626 | |
| 3627 | return status; |
| 3628 | } |
| 3629 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3630 | static PyObject * |
| 3631 | get_class(PyObject *obj) |
| 3632 | { |
| 3633 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3634 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3635 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3636 | if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) { |
| 3637 | cls = (PyObject *) Py_TYPE(obj); |
| 3638 | Py_INCREF(cls); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3639 | } |
| 3640 | return cls; |
| 3641 | } |
| 3642 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3643 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3644 | * appropriate __reduce__ method for obj. |
| 3645 | */ |
| 3646 | static int |
| 3647 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3648 | { |
| 3649 | PyObject *callable; |
| 3650 | PyObject *argtup; |
| 3651 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3652 | PyObject *listitems = Py_None; |
| 3653 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3654 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3655 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3656 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3657 | |
| 3658 | const char reduce_op = REDUCE; |
| 3659 | const char build_op = BUILD; |
| 3660 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3661 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3662 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3663 | size = PyTuple_Size(args); |
| 3664 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3665 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3666 | "__reduce__ must contain 2 through 5 elements"); |
| 3667 | return -1; |
| 3668 | } |
| 3669 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3670 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3671 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3672 | return -1; |
| 3673 | |
| 3674 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3675 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3676 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3677 | return -1; |
| 3678 | } |
| 3679 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3680 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3681 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3682 | return -1; |
| 3683 | } |
| 3684 | |
| 3685 | if (state == Py_None) |
| 3686 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3687 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3688 | if (listitems == Py_None) |
| 3689 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3690 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3691 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3692 | "returned by __reduce__ must be an iterator, not %s", |
| 3693 | Py_TYPE(listitems)->tp_name); |
| 3694 | return -1; |
| 3695 | } |
| 3696 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3697 | if (dictitems == Py_None) |
| 3698 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3699 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3700 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3701 | "returned by __reduce__ must be an iterator, not %s", |
| 3702 | Py_TYPE(dictitems)->tp_name); |
| 3703 | return -1; |
| 3704 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3705 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3706 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3707 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3708 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3709 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3710 | if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) { |
| 3711 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3712 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3713 | if (name != NULL && PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3714 | _Py_IDENTIFIER(__newobj_ex__); |
Serhiy Storchaka | f0f35a6 | 2017-01-09 10:09:43 +0200 | [diff] [blame] | 3715 | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
| 3716 | name, &PyId___newobj_ex__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3717 | if (!use_newobj_ex) { |
| 3718 | _Py_IDENTIFIER(__newobj__); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 3719 | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3720 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3721 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3722 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3723 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3724 | |
| 3725 | if (use_newobj_ex) { |
| 3726 | PyObject *cls; |
| 3727 | PyObject *args; |
| 3728 | PyObject *kwargs; |
| 3729 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3730 | if (PyTuple_GET_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3731 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3732 | "length of the NEWOBJ_EX argument tuple must be " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3733 | "exactly 3, not %zd", PyTuple_GET_SIZE(argtup)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3734 | return -1; |
| 3735 | } |
| 3736 | |
| 3737 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3738 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3739 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3740 | "first item from NEWOBJ_EX argument tuple must " |
| 3741 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3742 | return -1; |
| 3743 | } |
| 3744 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3745 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3746 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3747 | "second item from NEWOBJ_EX argument tuple must " |
| 3748 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3749 | return -1; |
| 3750 | } |
| 3751 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3752 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3753 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3754 | "third item from NEWOBJ_EX argument tuple must " |
| 3755 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3756 | return -1; |
| 3757 | } |
| 3758 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3759 | if (self->proto >= 4) { |
| 3760 | if (save(self, cls, 0) < 0 || |
| 3761 | save(self, args, 0) < 0 || |
| 3762 | save(self, kwargs, 0) < 0 || |
| 3763 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3764 | return -1; |
| 3765 | } |
| 3766 | } |
| 3767 | else { |
| 3768 | PyObject *newargs; |
| 3769 | PyObject *cls_new; |
| 3770 | Py_ssize_t i; |
| 3771 | _Py_IDENTIFIER(__new__); |
| 3772 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3773 | newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3774 | if (newargs == NULL) |
| 3775 | return -1; |
| 3776 | |
| 3777 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 3778 | if (cls_new == NULL) { |
| 3779 | Py_DECREF(newargs); |
| 3780 | return -1; |
| 3781 | } |
| 3782 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 3783 | Py_INCREF(cls); |
| 3784 | PyTuple_SET_ITEM(newargs, 1, cls); |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3785 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3786 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 3787 | Py_INCREF(item); |
| 3788 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 3789 | } |
| 3790 | |
| 3791 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 3792 | Py_DECREF(newargs); |
| 3793 | if (callable == NULL) |
| 3794 | return -1; |
| 3795 | |
| 3796 | newargs = PyTuple_New(0); |
| 3797 | if (newargs == NULL) { |
| 3798 | Py_DECREF(callable); |
| 3799 | return -1; |
| 3800 | } |
| 3801 | |
| 3802 | if (save(self, callable, 0) < 0 || |
| 3803 | save(self, newargs, 0) < 0 || |
| 3804 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 3805 | Py_DECREF(newargs); |
| 3806 | Py_DECREF(callable); |
| 3807 | return -1; |
| 3808 | } |
| 3809 | Py_DECREF(newargs); |
| 3810 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3811 | } |
| 3812 | } |
| 3813 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3814 | PyObject *cls; |
| 3815 | PyObject *newargtup; |
| 3816 | PyObject *obj_class; |
| 3817 | int p; |
| 3818 | |
| 3819 | /* Sanity checks. */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3820 | if (PyTuple_GET_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3821 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3822 | return -1; |
| 3823 | } |
| 3824 | |
| 3825 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3826 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3827 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3828 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3829 | return -1; |
| 3830 | } |
| 3831 | |
| 3832 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3833 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3834 | p = obj_class != cls; /* true iff a problem */ |
| 3835 | Py_DECREF(obj_class); |
| 3836 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3837 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3838 | "__newobj__ args has the wrong class"); |
| 3839 | return -1; |
| 3840 | } |
| 3841 | } |
| 3842 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3843 | what happen if the value returned by the __reduce__() method of |
| 3844 | some extension type contains another object of the same type. Ouch! |
| 3845 | |
| 3846 | Here is a quick example, that I ran into, to illustrate what I |
| 3847 | mean: |
| 3848 | |
| 3849 | >>> import pickle, copyreg |
| 3850 | >>> copyreg.dispatch_table.pop(complex) |
| 3851 | >>> pickle.dumps(1+2j) |
| 3852 | Traceback (most recent call last): |
| 3853 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3854 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3855 | |
| 3856 | Removing the complex class from copyreg.dispatch_table made the |
| 3857 | __reduce_ex__() method emit another complex object: |
| 3858 | |
| 3859 | >>> (1+1j).__reduce_ex__(2) |
| 3860 | (<function __newobj__ at 0xb7b71c3c>, |
| 3861 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3862 | |
| 3863 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3864 | ensued. Of course, the bug was in the complex class which had a |
| 3865 | broken __getnewargs__() that emitted another complex object. But, |
| 3866 | the point, here, is it is quite easy to end up with a broken reduce |
| 3867 | function. */ |
| 3868 | |
| 3869 | /* Save the class and its __new__ arguments. */ |
| 3870 | if (save(self, cls, 0) < 0) |
| 3871 | return -1; |
| 3872 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3873 | newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3874 | if (newargtup == NULL) |
| 3875 | return -1; |
| 3876 | |
| 3877 | p = save(self, newargtup, 0); |
| 3878 | Py_DECREF(newargtup); |
| 3879 | if (p < 0) |
| 3880 | return -1; |
| 3881 | |
| 3882 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3883 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3884 | return -1; |
| 3885 | } |
| 3886 | else { /* Not using NEWOBJ. */ |
| 3887 | if (save(self, callable, 0) < 0 || |
| 3888 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3889 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3890 | return -1; |
| 3891 | } |
| 3892 | |
| 3893 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3894 | the caller do not want to memoize the object. Not particularly useful, |
| 3895 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3896 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3897 | if (obj != NULL) { |
| 3898 | /* If the object is already in the memo, this means it is |
| 3899 | recursive. In this case, throw away everything we put on the |
| 3900 | stack, and fetch the object back from the memo. */ |
| 3901 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3902 | const char pop_op = POP; |
| 3903 | |
| 3904 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3905 | return -1; |
| 3906 | if (memo_get(self, obj) < 0) |
| 3907 | return -1; |
| 3908 | |
| 3909 | return 0; |
| 3910 | } |
| 3911 | else if (memo_put(self, obj) < 0) |
| 3912 | return -1; |
| 3913 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3914 | |
| 3915 | if (listitems && batch_list(self, listitems) < 0) |
| 3916 | return -1; |
| 3917 | |
| 3918 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3919 | return -1; |
| 3920 | |
| 3921 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3922 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3923 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3924 | return -1; |
| 3925 | } |
| 3926 | |
| 3927 | return 0; |
| 3928 | } |
| 3929 | |
| 3930 | static int |
| 3931 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3932 | { |
| 3933 | PyTypeObject *type; |
| 3934 | PyObject *reduce_func = NULL; |
| 3935 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3936 | int status = 0; |
| 3937 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3938 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3939 | return -1; |
| 3940 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3941 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3942 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3943 | |
| 3944 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3945 | on its returned object. */ |
| 3946 | if (!pers_save && self->pers_func) { |
| 3947 | /* save_pers() returns: |
| 3948 | -1 to signal an error; |
| 3949 | 0 if it did nothing successfully; |
| 3950 | 1 if a persistent id was saved. |
| 3951 | */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3952 | if ((status = save_pers(self, obj)) != 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3953 | goto done; |
| 3954 | } |
| 3955 | |
| 3956 | type = Py_TYPE(obj); |
| 3957 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3958 | /* The old cPickle had an optimization that used switch-case statement |
| 3959 | dispatching on the first letter of the type name. This has was removed |
| 3960 | since benchmarks shown that this optimization was actually slowing |
| 3961 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3962 | |
| 3963 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3964 | |
| 3965 | if (obj == Py_None) { |
| 3966 | status = save_none(self, obj); |
| 3967 | goto done; |
| 3968 | } |
| 3969 | else if (obj == Py_False || obj == Py_True) { |
| 3970 | status = save_bool(self, obj); |
| 3971 | goto done; |
| 3972 | } |
| 3973 | else if (type == &PyLong_Type) { |
| 3974 | status = save_long(self, obj); |
| 3975 | goto done; |
| 3976 | } |
| 3977 | else if (type == &PyFloat_Type) { |
| 3978 | status = save_float(self, obj); |
| 3979 | goto done; |
| 3980 | } |
| 3981 | |
| 3982 | /* Check the memo to see if it has the object. If so, generate |
| 3983 | a GET (or BINGET) opcode, instead of pickling the object |
| 3984 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3985 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3986 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3987 | goto error; |
| 3988 | goto done; |
| 3989 | } |
| 3990 | |
| 3991 | if (type == &PyBytes_Type) { |
| 3992 | status = save_bytes(self, obj); |
| 3993 | goto done; |
| 3994 | } |
| 3995 | else if (type == &PyUnicode_Type) { |
| 3996 | status = save_unicode(self, obj); |
| 3997 | goto done; |
| 3998 | } |
| 3999 | else if (type == &PyDict_Type) { |
| 4000 | status = save_dict(self, obj); |
| 4001 | goto done; |
| 4002 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4003 | else if (type == &PySet_Type) { |
| 4004 | status = save_set(self, obj); |
| 4005 | goto done; |
| 4006 | } |
| 4007 | else if (type == &PyFrozenSet_Type) { |
| 4008 | status = save_frozenset(self, obj); |
| 4009 | goto done; |
| 4010 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4011 | else if (type == &PyList_Type) { |
| 4012 | status = save_list(self, obj); |
| 4013 | goto done; |
| 4014 | } |
| 4015 | else if (type == &PyTuple_Type) { |
| 4016 | status = save_tuple(self, obj); |
| 4017 | goto done; |
| 4018 | } |
| 4019 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 4020 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4021 | goto done; |
| 4022 | } |
| 4023 | else if (type == &PyFunction_Type) { |
| 4024 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 4025 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4026 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4027 | |
| 4028 | /* XXX: This part needs some unit tests. */ |
| 4029 | |
| 4030 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4031 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 4032 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4033 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4034 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4035 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4036 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 4037 | (PyObject *)type); |
| 4038 | if (reduce_func == NULL) { |
| 4039 | if (PyErr_Occurred()) { |
| 4040 | goto error; |
| 4041 | } |
| 4042 | } else { |
| 4043 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 4044 | Increase the reference count to be consistent with |
| 4045 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 4046 | Py_INCREF(reduce_func); |
| 4047 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4048 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4049 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 4050 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4051 | if (reduce_func == NULL) { |
| 4052 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4053 | PyErr_Clear(); |
| 4054 | else |
| 4055 | goto error; |
| 4056 | } |
| 4057 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4058 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4059 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4060 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4061 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 4062 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 4063 | status = save_global(self, obj, NULL); |
| 4064 | goto done; |
| 4065 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4066 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4067 | _Py_IDENTIFIER(__reduce__); |
| 4068 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4069 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4070 | |
| 4071 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 4072 | automatically defined as __reduce__. While this is convenient, this |
| 4073 | make it impossible to know which method was actually called. Of |
| 4074 | course, this is not a big deal. But still, it would be nice to let |
| 4075 | the user know which method was called when something go |
| 4076 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 4077 | don't actually have to check for a __reduce__ method. */ |
| 4078 | |
| 4079 | /* Check for a __reduce_ex__ method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4080 | if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) { |
| 4081 | goto error; |
| 4082 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4083 | if (reduce_func != NULL) { |
| 4084 | PyObject *proto; |
| 4085 | proto = PyLong_FromLong(self->proto); |
| 4086 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4087 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4088 | } |
| 4089 | } |
| 4090 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4091 | PickleState *st = _Pickle_GetGlobalState(); |
| 4092 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4093 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4094 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4095 | if (reduce_func != NULL) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4096 | reduce_value = _PyObject_CallNoArg(reduce_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4097 | } |
| 4098 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4099 | PyErr_Format(st->PicklingError, |
| 4100 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4101 | type->tp_name, obj); |
| 4102 | goto error; |
| 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | |
| 4107 | if (reduce_value == NULL) |
| 4108 | goto error; |
| 4109 | |
| 4110 | if (PyUnicode_Check(reduce_value)) { |
| 4111 | status = save_global(self, obj, reduce_value); |
| 4112 | goto done; |
| 4113 | } |
| 4114 | |
| 4115 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4116 | PickleState *st = _Pickle_GetGlobalState(); |
| 4117 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4118 | "__reduce__ must return a string or tuple"); |
| 4119 | goto error; |
| 4120 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4121 | |
| 4122 | status = save_reduce(self, reduce_value, obj); |
| 4123 | |
| 4124 | if (0) { |
| 4125 | error: |
| 4126 | status = -1; |
| 4127 | } |
| 4128 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4129 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 4130 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4131 | Py_XDECREF(reduce_func); |
| 4132 | Py_XDECREF(reduce_value); |
| 4133 | |
| 4134 | return status; |
| 4135 | } |
| 4136 | |
| 4137 | static int |
| 4138 | dump(PicklerObject *self, PyObject *obj) |
| 4139 | { |
| 4140 | const char stop_op = STOP; |
| 4141 | |
| 4142 | if (self->proto >= 2) { |
| 4143 | char header[2]; |
| 4144 | |
| 4145 | header[0] = PROTO; |
| 4146 | assert(self->proto >= 0 && self->proto < 256); |
| 4147 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4148 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4149 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4150 | if (self->proto >= 4) |
| 4151 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4152 | } |
| 4153 | |
| 4154 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4155 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4156 | return -1; |
| 4157 | |
| 4158 | return 0; |
| 4159 | } |
| 4160 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4161 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4162 | |
| 4163 | _pickle.Pickler.clear_memo |
| 4164 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4165 | Clears the pickler's "memo". |
| 4166 | |
| 4167 | The memo is the data structure that remembers which objects the |
| 4168 | pickler has already seen, so that shared or recursive objects are |
| 4169 | pickled by reference and not by value. This method is useful when |
| 4170 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4171 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4172 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4173 | static PyObject * |
| 4174 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4175 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4176 | { |
| 4177 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4178 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4179 | |
| 4180 | Py_RETURN_NONE; |
| 4181 | } |
| 4182 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4183 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4184 | |
| 4185 | _pickle.Pickler.dump |
| 4186 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4187 | obj: object |
| 4188 | / |
| 4189 | |
| 4190 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4191 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4192 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4193 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4194 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4195 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4196 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4197 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4198 | Developers often forget to call __init__() in their subclasses, which |
| 4199 | would trigger a segfault without this check. */ |
| 4200 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4201 | PickleState *st = _Pickle_GetGlobalState(); |
| 4202 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4203 | "Pickler.__init__() was not called by %s.__init__()", |
| 4204 | Py_TYPE(self)->tp_name); |
| 4205 | return NULL; |
| 4206 | } |
| 4207 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4208 | if (_Pickler_ClearBuffer(self) < 0) |
| 4209 | return NULL; |
| 4210 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4211 | if (dump(self, obj) < 0) |
| 4212 | return NULL; |
| 4213 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4214 | if (_Pickler_FlushToFile(self) < 0) |
| 4215 | return NULL; |
| 4216 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4217 | Py_RETURN_NONE; |
| 4218 | } |
| 4219 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4220 | /*[clinic input] |
| 4221 | |
| 4222 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4223 | |
| 4224 | Returns size in memory, in bytes. |
| 4225 | [clinic start generated code]*/ |
| 4226 | |
| 4227 | static Py_ssize_t |
| 4228 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4229 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4230 | { |
| 4231 | Py_ssize_t res, s; |
| 4232 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4233 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4234 | if (self->memo != NULL) { |
| 4235 | res += sizeof(PyMemoTable); |
| 4236 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4237 | } |
| 4238 | if (self->output_buffer != NULL) { |
| 4239 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4240 | if (s == -1) |
| 4241 | return -1; |
| 4242 | res += s; |
| 4243 | } |
| 4244 | return res; |
| 4245 | } |
| 4246 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4247 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4248 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4249 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4250 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4251 | {NULL, NULL} /* sentinel */ |
| 4252 | }; |
| 4253 | |
| 4254 | static void |
| 4255 | Pickler_dealloc(PicklerObject *self) |
| 4256 | { |
| 4257 | PyObject_GC_UnTrack(self); |
| 4258 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4259 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4260 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4261 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4262 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4263 | Py_XDECREF(self->fast_memo); |
| 4264 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4265 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4266 | |
| 4267 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4268 | } |
| 4269 | |
| 4270 | static int |
| 4271 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4272 | { |
| 4273 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4274 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4275 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4276 | Py_VISIT(self->fast_memo); |
| 4277 | return 0; |
| 4278 | } |
| 4279 | |
| 4280 | static int |
| 4281 | Pickler_clear(PicklerObject *self) |
| 4282 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4283 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4284 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4285 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4286 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4287 | Py_CLEAR(self->fast_memo); |
| 4288 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4289 | if (self->memo != NULL) { |
| 4290 | PyMemoTable *memo = self->memo; |
| 4291 | self->memo = NULL; |
| 4292 | PyMemoTable_Del(memo); |
| 4293 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4294 | return 0; |
| 4295 | } |
| 4296 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4297 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4298 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4299 | |
| 4300 | _pickle.Pickler.__init__ |
| 4301 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4302 | file: object |
| 4303 | protocol: object = NULL |
| 4304 | fix_imports: bool = True |
| 4305 | |
| 4306 | This takes a binary file for writing a pickle data stream. |
| 4307 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4308 | The optional *protocol* argument tells the pickler to use the given |
| 4309 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4310 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4311 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4312 | Specifying a negative protocol version selects the highest protocol |
| 4313 | version supported. The higher the protocol used, the more recent the |
| 4314 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4315 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4316 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4317 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4318 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4319 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4320 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4321 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4322 | to map the new Python 3 names to the old module names used in Python |
| 4323 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4324 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4325 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4326 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4327 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4328 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4329 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4330 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4331 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4332 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4333 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4334 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4335 | if (self->write != NULL) |
| 4336 | (void)Pickler_clear(self); |
| 4337 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4338 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4339 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4340 | |
| 4341 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4342 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4343 | |
| 4344 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4345 | if (self->memo == NULL) { |
| 4346 | self->memo = PyMemoTable_New(); |
| 4347 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4348 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4349 | } |
| 4350 | self->output_len = 0; |
| 4351 | if (self->output_buffer == NULL) { |
| 4352 | self->max_output_len = WRITE_BUF_SIZE; |
| 4353 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4354 | self->max_output_len); |
| 4355 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4356 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4357 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4358 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4359 | self->fast = 0; |
| 4360 | self->fast_nesting = 0; |
| 4361 | self->fast_memo = NULL; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4362 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4363 | if (init_method_ref((PyObject *)self, &PyId_persistent_id, |
| 4364 | &self->pers_func, &self->pers_func_self) < 0) |
| 4365 | { |
| 4366 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4367 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4368 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4369 | if (_PyObject_LookupAttrId((PyObject *)self, |
| 4370 | &PyId_dispatch_table, &self->dispatch_table) < 0) { |
| 4371 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4372 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4373 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4374 | return 0; |
| 4375 | } |
| 4376 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4377 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4378 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4379 | * avoid breaking code like: |
| 4380 | * pickler.memo.clear() |
| 4381 | * and |
| 4382 | * pickler.memo = saved_memo |
| 4383 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4384 | * it. Note that we don't implement the entire mapping API here. This is |
| 4385 | * intentional, as these should be treated as black-box implementation details. |
| 4386 | */ |
| 4387 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4388 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4389 | _pickle.PicklerMemoProxy.clear |
| 4390 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4391 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4392 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4393 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4394 | static PyObject * |
| 4395 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4396 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4397 | { |
| 4398 | if (self->pickler->memo) |
| 4399 | PyMemoTable_Clear(self->pickler->memo); |
| 4400 | Py_RETURN_NONE; |
| 4401 | } |
| 4402 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4403 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4404 | _pickle.PicklerMemoProxy.copy |
| 4405 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4406 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4407 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4408 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4409 | static PyObject * |
| 4410 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4411 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4412 | { |
| 4413 | Py_ssize_t i; |
| 4414 | PyMemoTable *memo; |
| 4415 | PyObject *new_memo = PyDict_New(); |
| 4416 | if (new_memo == NULL) |
| 4417 | return NULL; |
| 4418 | |
| 4419 | memo = self->pickler->memo; |
| 4420 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4421 | PyMemoEntry entry = memo->mt_table[i]; |
| 4422 | if (entry.me_key != NULL) { |
| 4423 | int status; |
| 4424 | PyObject *key, *value; |
| 4425 | |
| 4426 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4427 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4428 | |
| 4429 | if (key == NULL || value == NULL) { |
| 4430 | Py_XDECREF(key); |
| 4431 | Py_XDECREF(value); |
| 4432 | goto error; |
| 4433 | } |
| 4434 | status = PyDict_SetItem(new_memo, key, value); |
| 4435 | Py_DECREF(key); |
| 4436 | Py_DECREF(value); |
| 4437 | if (status < 0) |
| 4438 | goto error; |
| 4439 | } |
| 4440 | } |
| 4441 | return new_memo; |
| 4442 | |
| 4443 | error: |
| 4444 | Py_XDECREF(new_memo); |
| 4445 | return NULL; |
| 4446 | } |
| 4447 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4448 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4449 | _pickle.PicklerMemoProxy.__reduce__ |
| 4450 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4451 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4452 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4453 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4454 | static PyObject * |
| 4455 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4456 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4457 | { |
| 4458 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4459 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4460 | if (contents == NULL) |
| 4461 | return NULL; |
| 4462 | |
| 4463 | reduce_value = PyTuple_New(2); |
| 4464 | if (reduce_value == NULL) { |
| 4465 | Py_DECREF(contents); |
| 4466 | return NULL; |
| 4467 | } |
| 4468 | dict_args = PyTuple_New(1); |
| 4469 | if (dict_args == NULL) { |
| 4470 | Py_DECREF(contents); |
| 4471 | Py_DECREF(reduce_value); |
| 4472 | return NULL; |
| 4473 | } |
| 4474 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4475 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4476 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4477 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4478 | return reduce_value; |
| 4479 | } |
| 4480 | |
| 4481 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4482 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4483 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4484 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4485 | {NULL, NULL} /* sentinel */ |
| 4486 | }; |
| 4487 | |
| 4488 | static void |
| 4489 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4490 | { |
| 4491 | PyObject_GC_UnTrack(self); |
| 4492 | Py_XDECREF(self->pickler); |
| 4493 | PyObject_GC_Del((PyObject *)self); |
| 4494 | } |
| 4495 | |
| 4496 | static int |
| 4497 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4498 | visitproc visit, void *arg) |
| 4499 | { |
| 4500 | Py_VISIT(self->pickler); |
| 4501 | return 0; |
| 4502 | } |
| 4503 | |
| 4504 | static int |
| 4505 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4506 | { |
| 4507 | Py_CLEAR(self->pickler); |
| 4508 | return 0; |
| 4509 | } |
| 4510 | |
| 4511 | static PyTypeObject PicklerMemoProxyType = { |
| 4512 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4513 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4514 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4515 | 0, |
| 4516 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4517 | 0, /* tp_print */ |
| 4518 | 0, /* tp_getattr */ |
| 4519 | 0, /* tp_setattr */ |
| 4520 | 0, /* tp_compare */ |
| 4521 | 0, /* tp_repr */ |
| 4522 | 0, /* tp_as_number */ |
| 4523 | 0, /* tp_as_sequence */ |
| 4524 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4525 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4526 | 0, /* tp_call */ |
| 4527 | 0, /* tp_str */ |
| 4528 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4529 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4530 | 0, /* tp_as_buffer */ |
| 4531 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4532 | 0, /* tp_doc */ |
| 4533 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4534 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4535 | 0, /* tp_richcompare */ |
| 4536 | 0, /* tp_weaklistoffset */ |
| 4537 | 0, /* tp_iter */ |
| 4538 | 0, /* tp_iternext */ |
| 4539 | picklerproxy_methods, /* tp_methods */ |
| 4540 | }; |
| 4541 | |
| 4542 | static PyObject * |
| 4543 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4544 | { |
| 4545 | PicklerMemoProxyObject *self; |
| 4546 | |
| 4547 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4548 | if (self == NULL) |
| 4549 | return NULL; |
| 4550 | Py_INCREF(pickler); |
| 4551 | self->pickler = pickler; |
| 4552 | PyObject_GC_Track(self); |
| 4553 | return (PyObject *)self; |
| 4554 | } |
| 4555 | |
| 4556 | /*****************************************************************************/ |
| 4557 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4558 | static PyObject * |
| 4559 | Pickler_get_memo(PicklerObject *self) |
| 4560 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4561 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4562 | } |
| 4563 | |
| 4564 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4565 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4566 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4567 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4568 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4569 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4570 | PyErr_SetString(PyExc_TypeError, |
| 4571 | "attribute deletion is not supported"); |
| 4572 | return -1; |
| 4573 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4574 | |
| 4575 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4576 | PicklerObject *pickler = |
| 4577 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4578 | |
| 4579 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4580 | if (new_memo == NULL) |
| 4581 | return -1; |
| 4582 | } |
| 4583 | else if (PyDict_Check(obj)) { |
| 4584 | Py_ssize_t i = 0; |
| 4585 | PyObject *key, *value; |
| 4586 | |
| 4587 | new_memo = PyMemoTable_New(); |
| 4588 | if (new_memo == NULL) |
| 4589 | return -1; |
| 4590 | |
| 4591 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4592 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4593 | PyObject *memo_obj; |
| 4594 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4595 | if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4596 | PyErr_SetString(PyExc_TypeError, |
| 4597 | "'memo' values must be 2-item tuples"); |
| 4598 | goto error; |
| 4599 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4600 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4601 | if (memo_id == -1 && PyErr_Occurred()) |
| 4602 | goto error; |
| 4603 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4604 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4605 | goto error; |
| 4606 | } |
| 4607 | } |
| 4608 | else { |
| 4609 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b6a9c97 | 2016-04-17 09:39:28 +0300 | [diff] [blame] | 4610 | "'memo' attribute must be a PicklerMemoProxy object" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4611 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4612 | return -1; |
| 4613 | } |
| 4614 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4615 | PyMemoTable_Del(self->memo); |
| 4616 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4617 | |
| 4618 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4619 | |
| 4620 | error: |
| 4621 | if (new_memo) |
| 4622 | PyMemoTable_Del(new_memo); |
| 4623 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | static PyObject * |
| 4627 | Pickler_get_persid(PicklerObject *self) |
| 4628 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4629 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4630 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4631 | return NULL; |
| 4632 | } |
| 4633 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | static int |
| 4637 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4638 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4639 | if (value == NULL) { |
| 4640 | PyErr_SetString(PyExc_TypeError, |
| 4641 | "attribute deletion is not supported"); |
| 4642 | return -1; |
| 4643 | } |
| 4644 | if (!PyCallable_Check(value)) { |
| 4645 | PyErr_SetString(PyExc_TypeError, |
| 4646 | "persistent_id must be a callable taking one argument"); |
| 4647 | return -1; |
| 4648 | } |
| 4649 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4650 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4651 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 4652 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4653 | |
| 4654 | return 0; |
| 4655 | } |
| 4656 | |
| 4657 | static PyMemberDef Pickler_members[] = { |
| 4658 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4659 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4660 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4661 | {NULL} |
| 4662 | }; |
| 4663 | |
| 4664 | static PyGetSetDef Pickler_getsets[] = { |
| 4665 | {"memo", (getter)Pickler_get_memo, |
| 4666 | (setter)Pickler_set_memo}, |
| 4667 | {"persistent_id", (getter)Pickler_get_persid, |
| 4668 | (setter)Pickler_set_persid}, |
| 4669 | {NULL} |
| 4670 | }; |
| 4671 | |
| 4672 | static PyTypeObject Pickler_Type = { |
| 4673 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4674 | "_pickle.Pickler" , /*tp_name*/ |
| 4675 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4676 | 0, /*tp_itemsize*/ |
| 4677 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4678 | 0, /*tp_print*/ |
| 4679 | 0, /*tp_getattr*/ |
| 4680 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4681 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4682 | 0, /*tp_repr*/ |
| 4683 | 0, /*tp_as_number*/ |
| 4684 | 0, /*tp_as_sequence*/ |
| 4685 | 0, /*tp_as_mapping*/ |
| 4686 | 0, /*tp_hash*/ |
| 4687 | 0, /*tp_call*/ |
| 4688 | 0, /*tp_str*/ |
| 4689 | 0, /*tp_getattro*/ |
| 4690 | 0, /*tp_setattro*/ |
| 4691 | 0, /*tp_as_buffer*/ |
| 4692 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4693 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4694 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4695 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4696 | 0, /*tp_richcompare*/ |
| 4697 | 0, /*tp_weaklistoffset*/ |
| 4698 | 0, /*tp_iter*/ |
| 4699 | 0, /*tp_iternext*/ |
| 4700 | Pickler_methods, /*tp_methods*/ |
| 4701 | Pickler_members, /*tp_members*/ |
| 4702 | Pickler_getsets, /*tp_getset*/ |
| 4703 | 0, /*tp_base*/ |
| 4704 | 0, /*tp_dict*/ |
| 4705 | 0, /*tp_descr_get*/ |
| 4706 | 0, /*tp_descr_set*/ |
| 4707 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4708 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4709 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4710 | PyType_GenericNew, /*tp_new*/ |
| 4711 | PyObject_GC_Del, /*tp_free*/ |
| 4712 | 0, /*tp_is_gc*/ |
| 4713 | }; |
| 4714 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4715 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4716 | |
| 4717 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4718 | using directly the C version of find_class(), when find_class() is not |
| 4719 | overridden by a subclass. Although, this could become rather hackish. A |
| 4720 | simpler optimization would be to call the C function when self is not a |
| 4721 | subclass instance. */ |
| 4722 | static PyObject * |
| 4723 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4724 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4725 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4726 | |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 4727 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
| 4728 | module_name, global_name, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4731 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4732 | marker(UnpicklerObject *self) |
| 4733 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4734 | Py_ssize_t mark; |
| 4735 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4736 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4737 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4738 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4739 | return -1; |
| 4740 | } |
| 4741 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4742 | mark = self->marks[--self->num_marks]; |
| 4743 | self->stack->mark_set = self->num_marks != 0; |
| 4744 | self->stack->fence = self->num_marks ? |
| 4745 | self->marks[self->num_marks - 1] : 0; |
| 4746 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | static int |
| 4750 | load_none(UnpicklerObject *self) |
| 4751 | { |
| 4752 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4753 | return 0; |
| 4754 | } |
| 4755 | |
| 4756 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4757 | load_int(UnpicklerObject *self) |
| 4758 | { |
| 4759 | PyObject *value; |
| 4760 | char *endptr, *s; |
| 4761 | Py_ssize_t len; |
| 4762 | long x; |
| 4763 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4764 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4765 | return -1; |
| 4766 | if (len < 2) |
| 4767 | return bad_readline(); |
| 4768 | |
| 4769 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4770 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4771 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4772 | x = strtol(s, &endptr, 0); |
| 4773 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4774 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4775 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4776 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4777 | errno = 0; |
| 4778 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4779 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4780 | if (value == NULL) { |
| 4781 | PyErr_SetString(PyExc_ValueError, |
| 4782 | "could not convert string to int"); |
| 4783 | return -1; |
| 4784 | } |
| 4785 | } |
| 4786 | else { |
| 4787 | if (len == 3 && (x == 0 || x == 1)) { |
| 4788 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4789 | return -1; |
| 4790 | } |
| 4791 | else { |
| 4792 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4793 | return -1; |
| 4794 | } |
| 4795 | } |
| 4796 | |
| 4797 | PDATA_PUSH(self->stack, value, -1); |
| 4798 | return 0; |
| 4799 | } |
| 4800 | |
| 4801 | static int |
| 4802 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4803 | { |
| 4804 | assert(boolean == Py_True || boolean == Py_False); |
| 4805 | PDATA_APPEND(self->stack, boolean, -1); |
| 4806 | return 0; |
| 4807 | } |
| 4808 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4809 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4810 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4811 | */ |
| 4812 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4813 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4814 | { |
| 4815 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4816 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4817 | size_t x = 0; |
| 4818 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4819 | if (nbytes > (int)sizeof(size_t)) { |
| 4820 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4821 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4822 | */ |
| 4823 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4824 | if (s[i]) |
| 4825 | return -1; |
| 4826 | } |
| 4827 | nbytes = (int)sizeof(size_t); |
| 4828 | } |
| 4829 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4830 | x |= (size_t) s[i] << (8 * i); |
| 4831 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4832 | |
| 4833 | if (x > PY_SSIZE_T_MAX) |
| 4834 | return -1; |
| 4835 | else |
| 4836 | return (Py_ssize_t) x; |
| 4837 | } |
| 4838 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4839 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4840 | * 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] | 4841 | * 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] | 4842 | * of x-platform bugs. |
| 4843 | */ |
| 4844 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4845 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4846 | { |
| 4847 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4848 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4849 | long x = 0; |
| 4850 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4851 | for (i = 0; i < nbytes; i++) { |
| 4852 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4853 | } |
| 4854 | |
| 4855 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4856 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4857 | * to extend a BININT's sign bit to the full width. |
| 4858 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4859 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4860 | x |= -(x & (1L << 31)); |
| 4861 | } |
| 4862 | |
| 4863 | return x; |
| 4864 | } |
| 4865 | |
| 4866 | static int |
| 4867 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4868 | { |
| 4869 | PyObject *value; |
| 4870 | long x; |
| 4871 | |
| 4872 | x = calc_binint(s, size); |
| 4873 | |
| 4874 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4875 | return -1; |
| 4876 | |
| 4877 | PDATA_PUSH(self->stack, value, -1); |
| 4878 | return 0; |
| 4879 | } |
| 4880 | |
| 4881 | static int |
| 4882 | load_binint(UnpicklerObject *self) |
| 4883 | { |
| 4884 | char *s; |
| 4885 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4886 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4887 | return -1; |
| 4888 | |
| 4889 | return load_binintx(self, s, 4); |
| 4890 | } |
| 4891 | |
| 4892 | static int |
| 4893 | load_binint1(UnpicklerObject *self) |
| 4894 | { |
| 4895 | char *s; |
| 4896 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4897 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4898 | return -1; |
| 4899 | |
| 4900 | return load_binintx(self, s, 1); |
| 4901 | } |
| 4902 | |
| 4903 | static int |
| 4904 | load_binint2(UnpicklerObject *self) |
| 4905 | { |
| 4906 | char *s; |
| 4907 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4908 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4909 | return -1; |
| 4910 | |
| 4911 | return load_binintx(self, s, 2); |
| 4912 | } |
| 4913 | |
| 4914 | static int |
| 4915 | load_long(UnpicklerObject *self) |
| 4916 | { |
| 4917 | PyObject *value; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 4918 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4919 | Py_ssize_t len; |
| 4920 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4921 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4922 | return -1; |
| 4923 | if (len < 2) |
| 4924 | return bad_readline(); |
| 4925 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4926 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4927 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4928 | compatibility with Python 3.0.0, we don't actually *require* |
| 4929 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4930 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4931 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4932 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4933 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4934 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4935 | return -1; |
| 4936 | |
| 4937 | PDATA_PUSH(self->stack, value, -1); |
| 4938 | return 0; |
| 4939 | } |
| 4940 | |
| 4941 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4942 | * data following. |
| 4943 | */ |
| 4944 | static int |
| 4945 | load_counted_long(UnpicklerObject *self, int size) |
| 4946 | { |
| 4947 | PyObject *value; |
| 4948 | char *nbytes; |
| 4949 | char *pdata; |
| 4950 | |
| 4951 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4952 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4953 | return -1; |
| 4954 | |
| 4955 | size = calc_binint(nbytes, size); |
| 4956 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4957 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4958 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4959 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4960 | "LONG pickle has negative byte count"); |
| 4961 | return -1; |
| 4962 | } |
| 4963 | |
| 4964 | if (size == 0) |
| 4965 | value = PyLong_FromLong(0L); |
| 4966 | else { |
| 4967 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4968 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4969 | return -1; |
| 4970 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4971 | 1 /* little endian */ , 1 /* signed */ ); |
| 4972 | } |
| 4973 | if (value == NULL) |
| 4974 | return -1; |
| 4975 | PDATA_PUSH(self->stack, value, -1); |
| 4976 | return 0; |
| 4977 | } |
| 4978 | |
| 4979 | static int |
| 4980 | load_float(UnpicklerObject *self) |
| 4981 | { |
| 4982 | PyObject *value; |
| 4983 | char *endptr, *s; |
| 4984 | Py_ssize_t len; |
| 4985 | double d; |
| 4986 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4987 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4988 | return -1; |
| 4989 | if (len < 2) |
| 4990 | return bad_readline(); |
| 4991 | |
| 4992 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4993 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4994 | if (d == -1.0 && PyErr_Occurred()) |
| 4995 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4996 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4997 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4998 | return -1; |
| 4999 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5000 | value = PyFloat_FromDouble(d); |
| 5001 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5002 | return -1; |
| 5003 | |
| 5004 | PDATA_PUSH(self->stack, value, -1); |
| 5005 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5006 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5007 | |
| 5008 | static int |
| 5009 | load_binfloat(UnpicklerObject *self) |
| 5010 | { |
| 5011 | PyObject *value; |
| 5012 | double x; |
| 5013 | char *s; |
| 5014 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5015 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5016 | return -1; |
| 5017 | |
| 5018 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 5019 | if (x == -1.0 && PyErr_Occurred()) |
| 5020 | return -1; |
| 5021 | |
| 5022 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 5023 | return -1; |
| 5024 | |
| 5025 | PDATA_PUSH(self->stack, value, -1); |
| 5026 | return 0; |
| 5027 | } |
| 5028 | |
| 5029 | static int |
| 5030 | load_string(UnpicklerObject *self) |
| 5031 | { |
| 5032 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5033 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5034 | Py_ssize_t len; |
| 5035 | char *s, *p; |
| 5036 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5037 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5038 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5039 | /* Strip the newline */ |
| 5040 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5041 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5042 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5043 | p = s + 1; |
| 5044 | len -= 2; |
| 5045 | } |
| 5046 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5047 | PickleState *st = _Pickle_GetGlobalState(); |
| 5048 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5049 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5050 | return -1; |
| 5051 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5052 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5053 | |
| 5054 | /* Use the PyBytes API to decode the string, since that is what is used |
| 5055 | to encode, and then coerce the result to Unicode. */ |
| 5056 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5057 | if (bytes == NULL) |
| 5058 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5059 | |
| 5060 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 5061 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5062 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5063 | obj = bytes; |
| 5064 | } |
| 5065 | else { |
| 5066 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 5067 | Py_DECREF(bytes); |
| 5068 | if (obj == NULL) { |
| 5069 | return -1; |
| 5070 | } |
| 5071 | } |
| 5072 | |
| 5073 | PDATA_PUSH(self->stack, obj, -1); |
| 5074 | return 0; |
| 5075 | } |
| 5076 | |
| 5077 | static int |
| 5078 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 5079 | { |
| 5080 | PyObject *obj; |
| 5081 | Py_ssize_t size; |
| 5082 | char *s; |
| 5083 | |
| 5084 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5085 | return -1; |
| 5086 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5087 | size = calc_binsize(s, nbytes); |
| 5088 | if (size < 0) { |
| 5089 | PickleState *st = _Pickle_GetGlobalState(); |
| 5090 | PyErr_Format(st->UnpicklingError, |
| 5091 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 5092 | PY_SSIZE_T_MAX); |
| 5093 | return -1; |
| 5094 | } |
| 5095 | |
| 5096 | if (_Unpickler_Read(self, &s, size) < 0) |
| 5097 | return -1; |
| 5098 | |
| 5099 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 5100 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5101 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5102 | obj = PyBytes_FromStringAndSize(s, size); |
| 5103 | } |
| 5104 | else { |
| 5105 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 5106 | } |
| 5107 | if (obj == NULL) { |
| 5108 | return -1; |
| 5109 | } |
| 5110 | |
| 5111 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5112 | return 0; |
| 5113 | } |
| 5114 | |
| 5115 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5116 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5117 | { |
| 5118 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5119 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5120 | char *s; |
| 5121 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5122 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5123 | return -1; |
| 5124 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5125 | size = calc_binsize(s, nbytes); |
| 5126 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5127 | PyErr_Format(PyExc_OverflowError, |
| 5128 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5129 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5130 | return -1; |
| 5131 | } |
| 5132 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5133 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5134 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5135 | |
| 5136 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5137 | if (bytes == NULL) |
| 5138 | return -1; |
| 5139 | |
| 5140 | PDATA_PUSH(self->stack, bytes, -1); |
| 5141 | return 0; |
| 5142 | } |
| 5143 | |
| 5144 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5145 | load_unicode(UnpicklerObject *self) |
| 5146 | { |
| 5147 | PyObject *str; |
| 5148 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5149 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5150 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5151 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5152 | return -1; |
| 5153 | if (len < 1) |
| 5154 | return bad_readline(); |
| 5155 | |
| 5156 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5157 | if (str == NULL) |
| 5158 | return -1; |
| 5159 | |
| 5160 | PDATA_PUSH(self->stack, str, -1); |
| 5161 | return 0; |
| 5162 | } |
| 5163 | |
| 5164 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5165 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5166 | { |
| 5167 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5168 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5169 | char *s; |
| 5170 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5171 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5172 | return -1; |
| 5173 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5174 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5175 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5176 | PyErr_Format(PyExc_OverflowError, |
| 5177 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5178 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5179 | return -1; |
| 5180 | } |
| 5181 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5182 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5183 | return -1; |
| 5184 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5185 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5186 | if (str == NULL) |
| 5187 | return -1; |
| 5188 | |
| 5189 | PDATA_PUSH(self->stack, str, -1); |
| 5190 | return 0; |
| 5191 | } |
| 5192 | |
| 5193 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5194 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5195 | { |
| 5196 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5197 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5198 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5199 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5200 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5201 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5202 | if (tuple == NULL) |
| 5203 | return -1; |
| 5204 | PDATA_PUSH(self->stack, tuple, -1); |
| 5205 | return 0; |
| 5206 | } |
| 5207 | |
| 5208 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5209 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5210 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5211 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5212 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5213 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5214 | return -1; |
| 5215 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5216 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5217 | } |
| 5218 | |
| 5219 | static int |
| 5220 | load_empty_list(UnpicklerObject *self) |
| 5221 | { |
| 5222 | PyObject *list; |
| 5223 | |
| 5224 | if ((list = PyList_New(0)) == NULL) |
| 5225 | return -1; |
| 5226 | PDATA_PUSH(self->stack, list, -1); |
| 5227 | return 0; |
| 5228 | } |
| 5229 | |
| 5230 | static int |
| 5231 | load_empty_dict(UnpicklerObject *self) |
| 5232 | { |
| 5233 | PyObject *dict; |
| 5234 | |
| 5235 | if ((dict = PyDict_New()) == NULL) |
| 5236 | return -1; |
| 5237 | PDATA_PUSH(self->stack, dict, -1); |
| 5238 | return 0; |
| 5239 | } |
| 5240 | |
| 5241 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5242 | load_empty_set(UnpicklerObject *self) |
| 5243 | { |
| 5244 | PyObject *set; |
| 5245 | |
| 5246 | if ((set = PySet_New(NULL)) == NULL) |
| 5247 | return -1; |
| 5248 | PDATA_PUSH(self->stack, set, -1); |
| 5249 | return 0; |
| 5250 | } |
| 5251 | |
| 5252 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5253 | load_list(UnpicklerObject *self) |
| 5254 | { |
| 5255 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5256 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5257 | |
| 5258 | if ((i = marker(self)) < 0) |
| 5259 | return -1; |
| 5260 | |
| 5261 | list = Pdata_poplist(self->stack, i); |
| 5262 | if (list == NULL) |
| 5263 | return -1; |
| 5264 | PDATA_PUSH(self->stack, list, -1); |
| 5265 | return 0; |
| 5266 | } |
| 5267 | |
| 5268 | static int |
| 5269 | load_dict(UnpicklerObject *self) |
| 5270 | { |
| 5271 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5272 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5273 | |
| 5274 | if ((i = marker(self)) < 0) |
| 5275 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5276 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5277 | |
| 5278 | if ((dict = PyDict_New()) == NULL) |
| 5279 | return -1; |
| 5280 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5281 | if ((j - i) % 2 != 0) { |
| 5282 | PickleState *st = _Pickle_GetGlobalState(); |
| 5283 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5284 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5285 | return -1; |
| 5286 | } |
| 5287 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5288 | for (k = i + 1; k < j; k += 2) { |
| 5289 | key = self->stack->data[k - 1]; |
| 5290 | value = self->stack->data[k]; |
| 5291 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5292 | Py_DECREF(dict); |
| 5293 | return -1; |
| 5294 | } |
| 5295 | } |
| 5296 | Pdata_clear(self->stack, i); |
| 5297 | PDATA_PUSH(self->stack, dict, -1); |
| 5298 | return 0; |
| 5299 | } |
| 5300 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5301 | static int |
| 5302 | load_frozenset(UnpicklerObject *self) |
| 5303 | { |
| 5304 | PyObject *items; |
| 5305 | PyObject *frozenset; |
| 5306 | Py_ssize_t i; |
| 5307 | |
| 5308 | if ((i = marker(self)) < 0) |
| 5309 | return -1; |
| 5310 | |
| 5311 | items = Pdata_poptuple(self->stack, i); |
| 5312 | if (items == NULL) |
| 5313 | return -1; |
| 5314 | |
| 5315 | frozenset = PyFrozenSet_New(items); |
| 5316 | Py_DECREF(items); |
| 5317 | if (frozenset == NULL) |
| 5318 | return -1; |
| 5319 | |
| 5320 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5321 | return 0; |
| 5322 | } |
| 5323 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5324 | static PyObject * |
| 5325 | instantiate(PyObject *cls, PyObject *args) |
| 5326 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5327 | /* Caller must assure args are a tuple. Normally, args come from |
| 5328 | Pdata_poptuple which packs objects from the top of the stack |
| 5329 | into a newly created tuple. */ |
| 5330 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5331 | if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { |
| 5332 | _Py_IDENTIFIER(__getinitargs__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5333 | _Py_IDENTIFIER(__new__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5334 | PyObject *func; |
| 5335 | if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) { |
| 5336 | return NULL; |
| 5337 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5338 | if (func == NULL) { |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5339 | return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL); |
| 5340 | } |
| 5341 | Py_DECREF(func); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5342 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5343 | return PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
| 5346 | static int |
| 5347 | load_obj(UnpicklerObject *self) |
| 5348 | { |
| 5349 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5350 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5351 | |
| 5352 | if ((i = marker(self)) < 0) |
| 5353 | return -1; |
| 5354 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5355 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5356 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5357 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5358 | args = Pdata_poptuple(self->stack, i + 1); |
| 5359 | if (args == NULL) |
| 5360 | return -1; |
| 5361 | |
| 5362 | PDATA_POP(self->stack, cls); |
| 5363 | if (cls) { |
| 5364 | obj = instantiate(cls, args); |
| 5365 | Py_DECREF(cls); |
| 5366 | } |
| 5367 | Py_DECREF(args); |
| 5368 | if (obj == NULL) |
| 5369 | return -1; |
| 5370 | |
| 5371 | PDATA_PUSH(self->stack, obj, -1); |
| 5372 | return 0; |
| 5373 | } |
| 5374 | |
| 5375 | static int |
| 5376 | load_inst(UnpicklerObject *self) |
| 5377 | { |
| 5378 | PyObject *cls = NULL; |
| 5379 | PyObject *args = NULL; |
| 5380 | PyObject *obj = NULL; |
| 5381 | PyObject *module_name; |
| 5382 | PyObject *class_name; |
| 5383 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5384 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5385 | char *s; |
| 5386 | |
| 5387 | if ((i = marker(self)) < 0) |
| 5388 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5389 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5390 | return -1; |
| 5391 | if (len < 2) |
| 5392 | return bad_readline(); |
| 5393 | |
| 5394 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5395 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5396 | supported by older protocols on Python 2.x. */ |
| 5397 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5398 | if (module_name == NULL) |
| 5399 | return -1; |
| 5400 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5401 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5402 | if (len < 2) { |
| 5403 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5404 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5405 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5406 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5407 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5408 | cls = find_class(self, module_name, class_name); |
| 5409 | Py_DECREF(class_name); |
| 5410 | } |
| 5411 | } |
| 5412 | Py_DECREF(module_name); |
| 5413 | |
| 5414 | if (cls == NULL) |
| 5415 | return -1; |
| 5416 | |
| 5417 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5418 | obj = instantiate(cls, args); |
| 5419 | Py_DECREF(args); |
| 5420 | } |
| 5421 | Py_DECREF(cls); |
| 5422 | |
| 5423 | if (obj == NULL) |
| 5424 | return -1; |
| 5425 | |
| 5426 | PDATA_PUSH(self->stack, obj, -1); |
| 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | static int |
| 5431 | load_newobj(UnpicklerObject *self) |
| 5432 | { |
| 5433 | PyObject *args = NULL; |
| 5434 | PyObject *clsraw = NULL; |
| 5435 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5436 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5437 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5438 | |
| 5439 | /* Stack is ... cls argtuple, and we want to call |
| 5440 | * cls.__new__(cls, *argtuple). |
| 5441 | */ |
| 5442 | PDATA_POP(self->stack, args); |
| 5443 | if (args == NULL) |
| 5444 | goto error; |
| 5445 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5446 | PyErr_SetString(st->UnpicklingError, |
| 5447 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5448 | goto error; |
| 5449 | } |
| 5450 | |
| 5451 | PDATA_POP(self->stack, clsraw); |
| 5452 | cls = (PyTypeObject *)clsraw; |
| 5453 | if (cls == NULL) |
| 5454 | goto error; |
| 5455 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5456 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5457 | "isn't a type object"); |
| 5458 | goto error; |
| 5459 | } |
| 5460 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5461 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5462 | "has NULL tp_new"); |
| 5463 | goto error; |
| 5464 | } |
| 5465 | |
| 5466 | /* Call __new__. */ |
| 5467 | obj = cls->tp_new(cls, args, NULL); |
| 5468 | if (obj == NULL) |
| 5469 | goto error; |
| 5470 | |
| 5471 | Py_DECREF(args); |
| 5472 | Py_DECREF(clsraw); |
| 5473 | PDATA_PUSH(self->stack, obj, -1); |
| 5474 | return 0; |
| 5475 | |
| 5476 | error: |
| 5477 | Py_XDECREF(args); |
| 5478 | Py_XDECREF(clsraw); |
| 5479 | return -1; |
| 5480 | } |
| 5481 | |
| 5482 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5483 | load_newobj_ex(UnpicklerObject *self) |
| 5484 | { |
| 5485 | PyObject *cls, *args, *kwargs; |
| 5486 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5487 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5488 | |
| 5489 | PDATA_POP(self->stack, kwargs); |
| 5490 | if (kwargs == NULL) { |
| 5491 | return -1; |
| 5492 | } |
| 5493 | PDATA_POP(self->stack, args); |
| 5494 | if (args == NULL) { |
| 5495 | Py_DECREF(kwargs); |
| 5496 | return -1; |
| 5497 | } |
| 5498 | PDATA_POP(self->stack, cls); |
| 5499 | if (cls == NULL) { |
| 5500 | Py_DECREF(kwargs); |
| 5501 | Py_DECREF(args); |
| 5502 | return -1; |
| 5503 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5504 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5505 | if (!PyType_Check(cls)) { |
| 5506 | Py_DECREF(kwargs); |
| 5507 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5508 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5509 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5510 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5511 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5512 | return -1; |
| 5513 | } |
| 5514 | |
| 5515 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5516 | Py_DECREF(kwargs); |
| 5517 | Py_DECREF(args); |
| 5518 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5519 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5520 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5521 | return -1; |
| 5522 | } |
| 5523 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5524 | Py_DECREF(kwargs); |
| 5525 | Py_DECREF(args); |
| 5526 | Py_DECREF(cls); |
| 5527 | if (obj == NULL) { |
| 5528 | return -1; |
| 5529 | } |
| 5530 | PDATA_PUSH(self->stack, obj, -1); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
| 5534 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5535 | load_global(UnpicklerObject *self) |
| 5536 | { |
| 5537 | PyObject *global = NULL; |
| 5538 | PyObject *module_name; |
| 5539 | PyObject *global_name; |
| 5540 | Py_ssize_t len; |
| 5541 | char *s; |
| 5542 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5543 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5544 | return -1; |
| 5545 | if (len < 2) |
| 5546 | return bad_readline(); |
| 5547 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5548 | if (!module_name) |
| 5549 | return -1; |
| 5550 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5551 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5552 | if (len < 2) { |
| 5553 | Py_DECREF(module_name); |
| 5554 | return bad_readline(); |
| 5555 | } |
| 5556 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5557 | if (global_name) { |
| 5558 | global = find_class(self, module_name, global_name); |
| 5559 | Py_DECREF(global_name); |
| 5560 | } |
| 5561 | } |
| 5562 | Py_DECREF(module_name); |
| 5563 | |
| 5564 | if (global == NULL) |
| 5565 | return -1; |
| 5566 | PDATA_PUSH(self->stack, global, -1); |
| 5567 | return 0; |
| 5568 | } |
| 5569 | |
| 5570 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5571 | load_stack_global(UnpicklerObject *self) |
| 5572 | { |
| 5573 | PyObject *global; |
| 5574 | PyObject *module_name; |
| 5575 | PyObject *global_name; |
| 5576 | |
| 5577 | PDATA_POP(self->stack, global_name); |
| 5578 | PDATA_POP(self->stack, module_name); |
| 5579 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5580 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5581 | PickleState *st = _Pickle_GetGlobalState(); |
| 5582 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5583 | Py_XDECREF(global_name); |
| 5584 | Py_XDECREF(module_name); |
| 5585 | return -1; |
| 5586 | } |
| 5587 | global = find_class(self, module_name, global_name); |
| 5588 | Py_DECREF(global_name); |
| 5589 | Py_DECREF(module_name); |
| 5590 | if (global == NULL) |
| 5591 | return -1; |
| 5592 | PDATA_PUSH(self->stack, global, -1); |
| 5593 | return 0; |
| 5594 | } |
| 5595 | |
| 5596 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5597 | load_persid(UnpicklerObject *self) |
| 5598 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5599 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5600 | Py_ssize_t len; |
| 5601 | char *s; |
| 5602 | |
| 5603 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5604 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5605 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5606 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5607 | return bad_readline(); |
| 5608 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5609 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5610 | if (pid == NULL) { |
| 5611 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 5612 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 5613 | "persistent IDs in protocol 0 must be " |
| 5614 | "ASCII strings"); |
| 5615 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5616 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5617 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5618 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5619 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5620 | Py_DECREF(pid); |
| 5621 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5622 | return -1; |
| 5623 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5624 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5625 | return 0; |
| 5626 | } |
| 5627 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5628 | PickleState *st = _Pickle_GetGlobalState(); |
| 5629 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5630 | "A load persistent id instruction was encountered,\n" |
| 5631 | "but no persistent_load function was specified."); |
| 5632 | return -1; |
| 5633 | } |
| 5634 | } |
| 5635 | |
| 5636 | static int |
| 5637 | load_binpersid(UnpicklerObject *self) |
| 5638 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5639 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5640 | |
| 5641 | if (self->pers_func) { |
| 5642 | PDATA_POP(self->stack, pid); |
| 5643 | if (pid == NULL) |
| 5644 | return -1; |
| 5645 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5646 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5647 | Py_DECREF(pid); |
| 5648 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5649 | return -1; |
| 5650 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5651 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5652 | return 0; |
| 5653 | } |
| 5654 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5655 | PickleState *st = _Pickle_GetGlobalState(); |
| 5656 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5657 | "A load persistent id instruction was encountered,\n" |
| 5658 | "but no persistent_load function was specified."); |
| 5659 | return -1; |
| 5660 | } |
| 5661 | } |
| 5662 | |
| 5663 | static int |
| 5664 | load_pop(UnpicklerObject *self) |
| 5665 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5666 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5667 | |
| 5668 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5669 | * an object stack and a mark stack. We have to be clever and |
| 5670 | * 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] | 5671 | * mark stack first, and only signalling a stack underflow if |
| 5672 | * the object stack is empty and the mark stack doesn't match |
| 5673 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5674 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5675 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5676 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5677 | self->stack->mark_set = self->num_marks != 0; |
| 5678 | self->stack->fence = self->num_marks ? |
| 5679 | self->marks[self->num_marks - 1] : 0; |
| 5680 | } else if (len <= self->stack->fence) |
| 5681 | return Pdata_stack_underflow(self->stack); |
| 5682 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5683 | len--; |
| 5684 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5685 | Py_SIZE(self->stack) = len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5686 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5687 | return 0; |
| 5688 | } |
| 5689 | |
| 5690 | static int |
| 5691 | load_pop_mark(UnpicklerObject *self) |
| 5692 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5693 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5694 | |
| 5695 | if ((i = marker(self)) < 0) |
| 5696 | return -1; |
| 5697 | |
| 5698 | Pdata_clear(self->stack, i); |
| 5699 | |
| 5700 | return 0; |
| 5701 | } |
| 5702 | |
| 5703 | static int |
| 5704 | load_dup(UnpicklerObject *self) |
| 5705 | { |
| 5706 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5707 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5708 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5709 | if (len <= self->stack->fence) |
| 5710 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5711 | last = self->stack->data[len - 1]; |
| 5712 | PDATA_APPEND(self->stack, last, -1); |
| 5713 | return 0; |
| 5714 | } |
| 5715 | |
| 5716 | static int |
| 5717 | load_get(UnpicklerObject *self) |
| 5718 | { |
| 5719 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5720 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5721 | Py_ssize_t len; |
| 5722 | char *s; |
| 5723 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5724 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5725 | return -1; |
| 5726 | if (len < 2) |
| 5727 | return bad_readline(); |
| 5728 | |
| 5729 | key = PyLong_FromString(s, NULL, 10); |
| 5730 | if (key == NULL) |
| 5731 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5732 | idx = PyLong_AsSsize_t(key); |
| 5733 | if (idx == -1 && PyErr_Occurred()) { |
| 5734 | Py_DECREF(key); |
| 5735 | return -1; |
| 5736 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5737 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5738 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5739 | if (value == NULL) { |
| 5740 | if (!PyErr_Occurred()) |
| 5741 | PyErr_SetObject(PyExc_KeyError, key); |
| 5742 | Py_DECREF(key); |
| 5743 | return -1; |
| 5744 | } |
| 5745 | Py_DECREF(key); |
| 5746 | |
| 5747 | PDATA_APPEND(self->stack, value, -1); |
| 5748 | return 0; |
| 5749 | } |
| 5750 | |
| 5751 | static int |
| 5752 | load_binget(UnpicklerObject *self) |
| 5753 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5754 | PyObject *value; |
| 5755 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5756 | char *s; |
| 5757 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5758 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5759 | return -1; |
| 5760 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5761 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5762 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5763 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5764 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5765 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5766 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5767 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5768 | Py_DECREF(key); |
| 5769 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5770 | return -1; |
| 5771 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5772 | |
| 5773 | PDATA_APPEND(self->stack, value, -1); |
| 5774 | return 0; |
| 5775 | } |
| 5776 | |
| 5777 | static int |
| 5778 | load_long_binget(UnpicklerObject *self) |
| 5779 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5780 | PyObject *value; |
| 5781 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5782 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5783 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5784 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5785 | return -1; |
| 5786 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5787 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5788 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5789 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5790 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5791 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5792 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5794 | Py_DECREF(key); |
| 5795 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5796 | return -1; |
| 5797 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5798 | |
| 5799 | PDATA_APPEND(self->stack, value, -1); |
| 5800 | return 0; |
| 5801 | } |
| 5802 | |
| 5803 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5804 | * the number of bytes following the opcode, holding the index (code) value. |
| 5805 | */ |
| 5806 | static int |
| 5807 | load_extension(UnpicklerObject *self, int nbytes) |
| 5808 | { |
| 5809 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5810 | long code; /* calc_binint returns long */ |
| 5811 | PyObject *py_code; /* code as a Python int */ |
| 5812 | PyObject *obj; /* the object to push */ |
| 5813 | PyObject *pair; /* (module_name, class_name) */ |
| 5814 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5815 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5816 | |
| 5817 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5818 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5819 | return -1; |
| 5820 | code = calc_binint(codebytes, nbytes); |
| 5821 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5822 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5823 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5824 | return -1; |
| 5825 | } |
| 5826 | |
| 5827 | /* Look for the code in the cache. */ |
| 5828 | py_code = PyLong_FromLong(code); |
| 5829 | if (py_code == NULL) |
| 5830 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5831 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5832 | if (obj != NULL) { |
| 5833 | /* Bingo. */ |
| 5834 | Py_DECREF(py_code); |
| 5835 | PDATA_APPEND(self->stack, obj, -1); |
| 5836 | return 0; |
| 5837 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5838 | if (PyErr_Occurred()) { |
| 5839 | Py_DECREF(py_code); |
| 5840 | return -1; |
| 5841 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5842 | |
| 5843 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5844 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5845 | if (pair == NULL) { |
| 5846 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5847 | if (!PyErr_Occurred()) { |
| 5848 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5849 | "code %ld", code); |
| 5850 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5851 | return -1; |
| 5852 | } |
| 5853 | /* Since the extension registry is manipulable via Python code, |
| 5854 | * confirm that pair is really a 2-tuple of strings. |
| 5855 | */ |
| 5856 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5857 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5858 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5859 | Py_DECREF(py_code); |
| 5860 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5861 | "isn't a 2-tuple of strings", code); |
| 5862 | return -1; |
| 5863 | } |
| 5864 | /* Load the object. */ |
| 5865 | obj = find_class(self, module_name, class_name); |
| 5866 | if (obj == NULL) { |
| 5867 | Py_DECREF(py_code); |
| 5868 | return -1; |
| 5869 | } |
| 5870 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5871 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5872 | Py_DECREF(py_code); |
| 5873 | if (code < 0) { |
| 5874 | Py_DECREF(obj); |
| 5875 | return -1; |
| 5876 | } |
| 5877 | PDATA_PUSH(self->stack, obj, -1); |
| 5878 | return 0; |
| 5879 | } |
| 5880 | |
| 5881 | static int |
| 5882 | load_put(UnpicklerObject *self) |
| 5883 | { |
| 5884 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5885 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5886 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5887 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5888 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5889 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5890 | return -1; |
| 5891 | if (len < 2) |
| 5892 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5893 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5894 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5895 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5896 | |
| 5897 | key = PyLong_FromString(s, NULL, 10); |
| 5898 | if (key == NULL) |
| 5899 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5900 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5901 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5902 | if (idx < 0) { |
| 5903 | if (!PyErr_Occurred()) |
| 5904 | PyErr_SetString(PyExc_ValueError, |
| 5905 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5906 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5907 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5908 | |
| 5909 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | static int |
| 5913 | load_binput(UnpicklerObject *self) |
| 5914 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5915 | PyObject *value; |
| 5916 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5917 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5918 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5919 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5920 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5921 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5922 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5923 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5924 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5925 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5926 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5927 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5928 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5929 | } |
| 5930 | |
| 5931 | static int |
| 5932 | load_long_binput(UnpicklerObject *self) |
| 5933 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5934 | PyObject *value; |
| 5935 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5936 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5937 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5938 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5939 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5940 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5941 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5942 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5943 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5944 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5945 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5946 | if (idx < 0) { |
| 5947 | PyErr_SetString(PyExc_ValueError, |
| 5948 | "negative LONG_BINPUT argument"); |
| 5949 | return -1; |
| 5950 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5951 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5952 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5953 | } |
| 5954 | |
| 5955 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5956 | load_memoize(UnpicklerObject *self) |
| 5957 | { |
| 5958 | PyObject *value; |
| 5959 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5960 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5961 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5962 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5963 | |
| 5964 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5965 | } |
| 5966 | |
| 5967 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5968 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5969 | { |
| 5970 | PyObject *value; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5971 | PyObject *slice; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5972 | PyObject *list; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5973 | PyObject *result; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5974 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5975 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5976 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5977 | if (x > len || x <= self->stack->fence) |
| 5978 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5979 | if (len == x) /* nothing to do */ |
| 5980 | return 0; |
| 5981 | |
| 5982 | list = self->stack->data[x - 1]; |
| 5983 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5984 | if (PyList_CheckExact(list)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5985 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5986 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5987 | |
| 5988 | slice = Pdata_poplist(self->stack, x); |
| 5989 | if (!slice) |
| 5990 | return -1; |
| 5991 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5992 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5993 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5994 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5995 | } |
| 5996 | else { |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5997 | PyObject *extend_func; |
| 5998 | _Py_IDENTIFIER(extend); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5999 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6000 | extend_func = _PyObject_GetAttrId(list, &PyId_extend); |
| 6001 | if (extend_func != NULL) { |
| 6002 | slice = Pdata_poplist(self->stack, x); |
| 6003 | if (!slice) { |
| 6004 | Py_DECREF(extend_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6005 | return -1; |
| 6006 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6007 | result = _Pickle_FastCall(extend_func, slice); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6008 | Py_DECREF(extend_func); |
| 6009 | if (result == NULL) |
| 6010 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6011 | Py_DECREF(result); |
| 6012 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6013 | else { |
| 6014 | PyObject *append_func; |
| 6015 | _Py_IDENTIFIER(append); |
| 6016 | |
| 6017 | /* Even if the PEP 307 requires extend() and append() methods, |
| 6018 | fall back on append() if the object has no extend() method |
| 6019 | for backward compatibility. */ |
| 6020 | PyErr_Clear(); |
| 6021 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
| 6022 | if (append_func == NULL) |
| 6023 | return -1; |
| 6024 | for (i = x; i < len; i++) { |
| 6025 | value = self->stack->data[i]; |
| 6026 | result = _Pickle_FastCall(append_func, value); |
| 6027 | if (result == NULL) { |
| 6028 | Pdata_clear(self->stack, i + 1); |
| 6029 | Py_SIZE(self->stack) = x; |
| 6030 | Py_DECREF(append_func); |
| 6031 | return -1; |
| 6032 | } |
| 6033 | Py_DECREF(result); |
| 6034 | } |
| 6035 | Py_SIZE(self->stack) = x; |
| 6036 | Py_DECREF(append_func); |
| 6037 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6038 | } |
| 6039 | |
| 6040 | return 0; |
| 6041 | } |
| 6042 | |
| 6043 | static int |
| 6044 | load_append(UnpicklerObject *self) |
| 6045 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6046 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 6047 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6048 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6049 | } |
| 6050 | |
| 6051 | static int |
| 6052 | load_appends(UnpicklerObject *self) |
| 6053 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6054 | Py_ssize_t i = marker(self); |
| 6055 | if (i < 0) |
| 6056 | return -1; |
| 6057 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6058 | } |
| 6059 | |
| 6060 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6061 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6062 | { |
| 6063 | PyObject *value, *key; |
| 6064 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6065 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6066 | int status = 0; |
| 6067 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6068 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6069 | if (x > len || x <= self->stack->fence) |
| 6070 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6071 | if (len == x) /* nothing to do */ |
| 6072 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6073 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6074 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6075 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6076 | PyErr_SetString(st->UnpicklingError, |
| 6077 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6078 | return -1; |
| 6079 | } |
| 6080 | |
| 6081 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 6082 | that supports the __setitem__ attribute. */ |
| 6083 | dict = self->stack->data[x - 1]; |
| 6084 | |
| 6085 | for (i = x + 1; i < len; i += 2) { |
| 6086 | key = self->stack->data[i - 1]; |
| 6087 | value = self->stack->data[i]; |
| 6088 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 6089 | status = -1; |
| 6090 | break; |
| 6091 | } |
| 6092 | } |
| 6093 | |
| 6094 | Pdata_clear(self->stack, x); |
| 6095 | return status; |
| 6096 | } |
| 6097 | |
| 6098 | static int |
| 6099 | load_setitem(UnpicklerObject *self) |
| 6100 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6101 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6102 | } |
| 6103 | |
| 6104 | static int |
| 6105 | load_setitems(UnpicklerObject *self) |
| 6106 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6107 | Py_ssize_t i = marker(self); |
| 6108 | if (i < 0) |
| 6109 | return -1; |
| 6110 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6111 | } |
| 6112 | |
| 6113 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6114 | load_additems(UnpicklerObject *self) |
| 6115 | { |
| 6116 | PyObject *set; |
| 6117 | Py_ssize_t mark, len, i; |
| 6118 | |
| 6119 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6120 | if (mark < 0) |
| 6121 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6122 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6123 | if (mark > len || mark <= self->stack->fence) |
| 6124 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6125 | if (len == mark) /* nothing to do */ |
| 6126 | return 0; |
| 6127 | |
| 6128 | set = self->stack->data[mark - 1]; |
| 6129 | |
| 6130 | if (PySet_Check(set)) { |
| 6131 | PyObject *items; |
| 6132 | int status; |
| 6133 | |
| 6134 | items = Pdata_poptuple(self->stack, mark); |
| 6135 | if (items == NULL) |
| 6136 | return -1; |
| 6137 | |
| 6138 | status = _PySet_Update(set, items); |
| 6139 | Py_DECREF(items); |
| 6140 | return status; |
| 6141 | } |
| 6142 | else { |
| 6143 | PyObject *add_func; |
| 6144 | _Py_IDENTIFIER(add); |
| 6145 | |
| 6146 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6147 | if (add_func == NULL) |
| 6148 | return -1; |
| 6149 | for (i = mark; i < len; i++) { |
| 6150 | PyObject *result; |
| 6151 | PyObject *item; |
| 6152 | |
| 6153 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6154 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6155 | if (result == NULL) { |
| 6156 | Pdata_clear(self->stack, i + 1); |
| 6157 | Py_SIZE(self->stack) = mark; |
| 6158 | return -1; |
| 6159 | } |
| 6160 | Py_DECREF(result); |
| 6161 | } |
| 6162 | Py_SIZE(self->stack) = mark; |
| 6163 | } |
| 6164 | |
| 6165 | return 0; |
| 6166 | } |
| 6167 | |
| 6168 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6169 | load_build(UnpicklerObject *self) |
| 6170 | { |
| 6171 | PyObject *state, *inst, *slotstate; |
| 6172 | PyObject *setstate; |
| 6173 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6174 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6175 | |
| 6176 | /* Stack is ... instance, state. We want to leave instance at |
| 6177 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6178 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6179 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6180 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6181 | |
| 6182 | PDATA_POP(self->stack, state); |
| 6183 | if (state == NULL) |
| 6184 | return -1; |
| 6185 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6186 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6187 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6188 | if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) { |
| 6189 | Py_DECREF(state); |
| 6190 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6191 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6192 | if (setstate != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6193 | PyObject *result; |
| 6194 | |
| 6195 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6196 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6197 | Py_DECREF(setstate); |
| 6198 | if (result == NULL) |
| 6199 | return -1; |
| 6200 | Py_DECREF(result); |
| 6201 | return 0; |
| 6202 | } |
| 6203 | |
| 6204 | /* A default __setstate__. First see whether state embeds a |
| 6205 | * slot state dict too (a proto 2 addition). |
| 6206 | */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 6207 | if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6208 | PyObject *tmp = state; |
| 6209 | |
| 6210 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6211 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6212 | Py_INCREF(state); |
| 6213 | Py_INCREF(slotstate); |
| 6214 | Py_DECREF(tmp); |
| 6215 | } |
| 6216 | else |
| 6217 | slotstate = NULL; |
| 6218 | |
| 6219 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6220 | if (state != Py_None) { |
| 6221 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6222 | PyObject *d_key, *d_value; |
| 6223 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6224 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6225 | |
| 6226 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6227 | PickleState *st = _Pickle_GetGlobalState(); |
| 6228 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6229 | goto error; |
| 6230 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6231 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6232 | if (dict == NULL) |
| 6233 | goto error; |
| 6234 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6235 | i = 0; |
| 6236 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6237 | /* normally the keys for instance attributes are |
| 6238 | interned. we should try to do that here. */ |
| 6239 | Py_INCREF(d_key); |
| 6240 | if (PyUnicode_CheckExact(d_key)) |
| 6241 | PyUnicode_InternInPlace(&d_key); |
| 6242 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6243 | Py_DECREF(d_key); |
| 6244 | goto error; |
| 6245 | } |
| 6246 | Py_DECREF(d_key); |
| 6247 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6248 | Py_DECREF(dict); |
| 6249 | } |
| 6250 | |
| 6251 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6252 | if (slotstate != NULL) { |
| 6253 | PyObject *d_key, *d_value; |
| 6254 | Py_ssize_t i; |
| 6255 | |
| 6256 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6257 | PickleState *st = _Pickle_GetGlobalState(); |
| 6258 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6259 | "slot state is not a dictionary"); |
| 6260 | goto error; |
| 6261 | } |
| 6262 | i = 0; |
| 6263 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6264 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6265 | goto error; |
| 6266 | } |
| 6267 | } |
| 6268 | |
| 6269 | if (0) { |
| 6270 | error: |
| 6271 | status = -1; |
| 6272 | } |
| 6273 | |
| 6274 | Py_DECREF(state); |
| 6275 | Py_XDECREF(slotstate); |
| 6276 | return status; |
| 6277 | } |
| 6278 | |
| 6279 | static int |
| 6280 | load_mark(UnpicklerObject *self) |
| 6281 | { |
| 6282 | |
| 6283 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6284 | * object stack and a mark stack. Here we push a mark onto the |
| 6285 | * mark stack. |
| 6286 | */ |
| 6287 | |
| 6288 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6289 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6290 | |
| 6291 | /* Use the size_t type to check for overflow. */ |
| 6292 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6293 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6294 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6295 | PyErr_NoMemory(); |
| 6296 | return -1; |
| 6297 | } |
| 6298 | |
| 6299 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6300 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6301 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6302 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6303 | if (self->marks == NULL) { |
| 6304 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6305 | PyErr_NoMemory(); |
| 6306 | return -1; |
| 6307 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6308 | self->marks_size = (Py_ssize_t)alloc; |
| 6309 | } |
| 6310 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6311 | self->stack->mark_set = 1; |
| 6312 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6313 | |
| 6314 | return 0; |
| 6315 | } |
| 6316 | |
| 6317 | static int |
| 6318 | load_reduce(UnpicklerObject *self) |
| 6319 | { |
| 6320 | PyObject *callable = NULL; |
| 6321 | PyObject *argtup = NULL; |
| 6322 | PyObject *obj = NULL; |
| 6323 | |
| 6324 | PDATA_POP(self->stack, argtup); |
| 6325 | if (argtup == NULL) |
| 6326 | return -1; |
| 6327 | PDATA_POP(self->stack, callable); |
| 6328 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6329 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6330 | Py_DECREF(callable); |
| 6331 | } |
| 6332 | Py_DECREF(argtup); |
| 6333 | |
| 6334 | if (obj == NULL) |
| 6335 | return -1; |
| 6336 | |
| 6337 | PDATA_PUSH(self->stack, obj, -1); |
| 6338 | return 0; |
| 6339 | } |
| 6340 | |
| 6341 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6342 | * is the first opcode for protocols >= 2. |
| 6343 | */ |
| 6344 | static int |
| 6345 | load_proto(UnpicklerObject *self) |
| 6346 | { |
| 6347 | char *s; |
| 6348 | int i; |
| 6349 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6350 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6351 | return -1; |
| 6352 | |
| 6353 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6354 | if (i <= HIGHEST_PROTOCOL) { |
| 6355 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6356 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6357 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6358 | |
| 6359 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6360 | return -1; |
| 6361 | } |
| 6362 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6363 | static int |
| 6364 | load_frame(UnpicklerObject *self) |
| 6365 | { |
| 6366 | char *s; |
| 6367 | Py_ssize_t frame_len; |
| 6368 | |
| 6369 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6370 | return -1; |
| 6371 | |
| 6372 | frame_len = calc_binsize(s, 8); |
| 6373 | if (frame_len < 0) { |
| 6374 | PyErr_Format(PyExc_OverflowError, |
| 6375 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6376 | PY_SSIZE_T_MAX); |
| 6377 | return -1; |
| 6378 | } |
| 6379 | |
| 6380 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6381 | return -1; |
| 6382 | |
| 6383 | /* Rewind to start of frame */ |
| 6384 | self->next_read_idx -= frame_len; |
| 6385 | return 0; |
| 6386 | } |
| 6387 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6388 | static PyObject * |
| 6389 | load(UnpicklerObject *self) |
| 6390 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6391 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6392 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6393 | |
| 6394 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6395 | self->stack->mark_set = 0; |
| 6396 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6397 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6398 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6399 | Pdata_clear(self->stack, 0); |
| 6400 | |
| 6401 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6402 | #define OP(opcode, load_func) \ |
| 6403 | case opcode: if (load_func(self) < 0) break; continue; |
| 6404 | |
| 6405 | #define OP_ARG(opcode, load_func, arg) \ |
| 6406 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6407 | |
| 6408 | while (1) { |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6409 | if (_Unpickler_Read(self, &s, 1) < 0) { |
| 6410 | PickleState *st = _Pickle_GetGlobalState(); |
| 6411 | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
| 6412 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 6413 | } |
| 6414 | return NULL; |
| 6415 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6416 | |
| 6417 | switch ((enum opcode)s[0]) { |
| 6418 | OP(NONE, load_none) |
| 6419 | OP(BININT, load_binint) |
| 6420 | OP(BININT1, load_binint1) |
| 6421 | OP(BININT2, load_binint2) |
| 6422 | OP(INT, load_int) |
| 6423 | OP(LONG, load_long) |
| 6424 | OP_ARG(LONG1, load_counted_long, 1) |
| 6425 | OP_ARG(LONG4, load_counted_long, 4) |
| 6426 | OP(FLOAT, load_float) |
| 6427 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6428 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6429 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6430 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6431 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6432 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6433 | OP(STRING, load_string) |
| 6434 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6435 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6436 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6437 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6438 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6439 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6440 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6441 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6442 | OP(TUPLE, load_tuple) |
| 6443 | OP(EMPTY_LIST, load_empty_list) |
| 6444 | OP(LIST, load_list) |
| 6445 | OP(EMPTY_DICT, load_empty_dict) |
| 6446 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6447 | OP(EMPTY_SET, load_empty_set) |
| 6448 | OP(ADDITEMS, load_additems) |
| 6449 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6450 | OP(OBJ, load_obj) |
| 6451 | OP(INST, load_inst) |
| 6452 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6453 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6454 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6455 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6456 | OP(APPEND, load_append) |
| 6457 | OP(APPENDS, load_appends) |
| 6458 | OP(BUILD, load_build) |
| 6459 | OP(DUP, load_dup) |
| 6460 | OP(BINGET, load_binget) |
| 6461 | OP(LONG_BINGET, load_long_binget) |
| 6462 | OP(GET, load_get) |
| 6463 | OP(MARK, load_mark) |
| 6464 | OP(BINPUT, load_binput) |
| 6465 | OP(LONG_BINPUT, load_long_binput) |
| 6466 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6467 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6468 | OP(POP, load_pop) |
| 6469 | OP(POP_MARK, load_pop_mark) |
| 6470 | OP(SETITEM, load_setitem) |
| 6471 | OP(SETITEMS, load_setitems) |
| 6472 | OP(PERSID, load_persid) |
| 6473 | OP(BINPERSID, load_binpersid) |
| 6474 | OP(REDUCE, load_reduce) |
| 6475 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6476 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6477 | OP_ARG(EXT1, load_extension, 1) |
| 6478 | OP_ARG(EXT2, load_extension, 2) |
| 6479 | OP_ARG(EXT4, load_extension, 4) |
| 6480 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6481 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6482 | |
| 6483 | case STOP: |
| 6484 | break; |
| 6485 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6486 | default: |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6487 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6488 | PickleState *st = _Pickle_GetGlobalState(); |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6489 | unsigned char c = (unsigned char) *s; |
| 6490 | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
| 6491 | PyErr_Format(st->UnpicklingError, |
| 6492 | "invalid load key, '%c'.", c); |
| 6493 | } |
| 6494 | else { |
| 6495 | PyErr_Format(st->UnpicklingError, |
| 6496 | "invalid load key, '\\x%02x'.", c); |
| 6497 | } |
| 6498 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6499 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6500 | } |
| 6501 | |
| 6502 | break; /* and we are done! */ |
| 6503 | } |
| 6504 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6505 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6506 | return NULL; |
| 6507 | } |
| 6508 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6509 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6510 | return NULL; |
| 6511 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6512 | PDATA_POP(self->stack, value); |
| 6513 | return value; |
| 6514 | } |
| 6515 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6516 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6517 | |
| 6518 | _pickle.Unpickler.load |
| 6519 | |
| 6520 | Load a pickle. |
| 6521 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6522 | Read a pickled object representation from the open file object given |
| 6523 | in the constructor, and return the reconstituted object hierarchy |
| 6524 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6525 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6526 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6527 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6528 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6529 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6530 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6531 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6532 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6533 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6534 | segfaulting if a subclass overridden __init__ with a function that does |
| 6535 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6536 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6537 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6538 | PickleState *st = _Pickle_GetGlobalState(); |
| 6539 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6540 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6541 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6542 | return NULL; |
| 6543 | } |
| 6544 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6545 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6546 | } |
| 6547 | |
| 6548 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6549 | function is used for loading any global (i.e., functions), not just |
| 6550 | classes. The name is kept only for backward compatibility. */ |
| 6551 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6552 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6553 | |
| 6554 | _pickle.Unpickler.find_class |
| 6555 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6556 | module_name: object |
| 6557 | global_name: object |
| 6558 | / |
| 6559 | |
| 6560 | Return an object from a specified module. |
| 6561 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6562 | If necessary, the module will be imported. Subclasses may override |
| 6563 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6564 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6565 | |
| 6566 | This method is called whenever a class or a function object is |
| 6567 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6568 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6569 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6570 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6571 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6572 | PyObject *module_name, |
| 6573 | PyObject *global_name) |
| 6574 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6575 | { |
| 6576 | PyObject *global; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6577 | PyObject *module; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6578 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6579 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6580 | Python 3.x. We do this only with old pickle protocols and when the |
| 6581 | user has not disabled the feature. */ |
| 6582 | if (self->proto < 3 && self->fix_imports) { |
| 6583 | PyObject *key; |
| 6584 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6585 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6586 | |
| 6587 | /* Check if the global (i.e., a function or a class) was renamed |
| 6588 | or moved to another module. */ |
| 6589 | key = PyTuple_Pack(2, module_name, global_name); |
| 6590 | if (key == NULL) |
| 6591 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6592 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6593 | Py_DECREF(key); |
| 6594 | if (item) { |
| 6595 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6596 | PyErr_Format(PyExc_RuntimeError, |
| 6597 | "_compat_pickle.NAME_MAPPING values should be " |
| 6598 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6599 | return NULL; |
| 6600 | } |
| 6601 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6602 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6603 | if (!PyUnicode_Check(module_name) || |
| 6604 | !PyUnicode_Check(global_name)) { |
| 6605 | PyErr_Format(PyExc_RuntimeError, |
| 6606 | "_compat_pickle.NAME_MAPPING values should be " |
| 6607 | "pairs of str, not (%.200s, %.200s)", |
| 6608 | Py_TYPE(module_name)->tp_name, |
| 6609 | Py_TYPE(global_name)->tp_name); |
| 6610 | return NULL; |
| 6611 | } |
| 6612 | } |
| 6613 | else if (PyErr_Occurred()) { |
| 6614 | return NULL; |
| 6615 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6616 | else { |
| 6617 | /* Check if the module was renamed. */ |
| 6618 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6619 | if (item) { |
| 6620 | if (!PyUnicode_Check(item)) { |
| 6621 | PyErr_Format(PyExc_RuntimeError, |
| 6622 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6623 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6624 | return NULL; |
| 6625 | } |
| 6626 | module_name = item; |
| 6627 | } |
| 6628 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6629 | return NULL; |
| 6630 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6631 | } |
| 6632 | } |
| 6633 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6634 | module = PyImport_GetModule(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6635 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6636 | if (PyErr_Occurred()) |
| 6637 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6638 | module = PyImport_Import(module_name); |
| 6639 | if (module == NULL) |
| 6640 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6641 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6642 | global = getattribute(module, global_name, self->proto >= 4); |
| 6643 | Py_DECREF(module); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6644 | return global; |
| 6645 | } |
| 6646 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6647 | /*[clinic input] |
| 6648 | |
| 6649 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6650 | |
| 6651 | Returns size in memory, in bytes. |
| 6652 | [clinic start generated code]*/ |
| 6653 | |
| 6654 | static Py_ssize_t |
| 6655 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6656 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6657 | { |
| 6658 | Py_ssize_t res; |
| 6659 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 6660 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6661 | if (self->memo != NULL) |
| 6662 | res += self->memo_size * sizeof(PyObject *); |
| 6663 | if (self->marks != NULL) |
| 6664 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6665 | if (self->input_line != NULL) |
| 6666 | res += strlen(self->input_line) + 1; |
| 6667 | if (self->encoding != NULL) |
| 6668 | res += strlen(self->encoding) + 1; |
| 6669 | if (self->errors != NULL) |
| 6670 | res += strlen(self->errors) + 1; |
| 6671 | return res; |
| 6672 | } |
| 6673 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6674 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6675 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6676 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6677 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6678 | {NULL, NULL} /* sentinel */ |
| 6679 | }; |
| 6680 | |
| 6681 | static void |
| 6682 | Unpickler_dealloc(UnpicklerObject *self) |
| 6683 | { |
| 6684 | PyObject_GC_UnTrack((PyObject *)self); |
| 6685 | Py_XDECREF(self->readline); |
| 6686 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6687 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6688 | Py_XDECREF(self->stack); |
| 6689 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6690 | if (self->buffer.buf != NULL) { |
| 6691 | PyBuffer_Release(&self->buffer); |
| 6692 | self->buffer.buf = NULL; |
| 6693 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6695 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6696 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6697 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6698 | PyMem_Free(self->encoding); |
| 6699 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6700 | |
| 6701 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6702 | } |
| 6703 | |
| 6704 | static int |
| 6705 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6706 | { |
| 6707 | Py_VISIT(self->readline); |
| 6708 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6709 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6710 | Py_VISIT(self->stack); |
| 6711 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6712 | return 0; |
| 6713 | } |
| 6714 | |
| 6715 | static int |
| 6716 | Unpickler_clear(UnpicklerObject *self) |
| 6717 | { |
| 6718 | Py_CLEAR(self->readline); |
| 6719 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6720 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6721 | Py_CLEAR(self->stack); |
| 6722 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6723 | if (self->buffer.buf != NULL) { |
| 6724 | PyBuffer_Release(&self->buffer); |
| 6725 | self->buffer.buf = NULL; |
| 6726 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6727 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6728 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6729 | PyMem_Free(self->marks); |
| 6730 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6731 | PyMem_Free(self->input_line); |
| 6732 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6733 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6734 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6735 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6736 | self->errors = NULL; |
| 6737 | |
| 6738 | return 0; |
| 6739 | } |
| 6740 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6741 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6742 | |
| 6743 | _pickle.Unpickler.__init__ |
| 6744 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6745 | file: object |
| 6746 | * |
| 6747 | fix_imports: bool = True |
| 6748 | encoding: str = 'ASCII' |
| 6749 | errors: str = 'strict' |
| 6750 | |
| 6751 | This takes a binary file for reading a pickle data stream. |
| 6752 | |
| 6753 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6754 | protocol argument is needed. Bytes past the pickled object's |
| 6755 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6756 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6757 | The argument *file* must have two methods, a read() method that takes |
| 6758 | an integer argument, and a readline() method that requires no |
| 6759 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6760 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6761 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6762 | |
| 6763 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6764 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6765 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6766 | 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] | 6767 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6768 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6769 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6770 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6771 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6772 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6773 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6774 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6775 | int fix_imports, const char *encoding, |
| 6776 | const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6777 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6778 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6779 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6780 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6781 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6782 | if (self->read != NULL) |
| 6783 | (void)Unpickler_clear(self); |
| 6784 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6785 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6786 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6787 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6788 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6789 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6790 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6791 | self->fix_imports = fix_imports; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6792 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6793 | if (init_method_ref((PyObject *)self, &PyId_persistent_load, |
| 6794 | &self->pers_func, &self->pers_func_self) < 0) |
| 6795 | { |
| 6796 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6797 | } |
| 6798 | |
| 6799 | self->stack = (Pdata *)Pdata_New(); |
| 6800 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6801 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6802 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6803 | self->memo_size = 32; |
| 6804 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6805 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6806 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6807 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6808 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6809 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6810 | return 0; |
| 6811 | } |
| 6812 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6813 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6814 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6815 | * avoid breaking code like: |
| 6816 | * unpickler.memo.clear() |
| 6817 | * and |
| 6818 | * unpickler.memo = saved_memo |
| 6819 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6820 | * it. Note that we don't implement the entire mapping API here. This is |
| 6821 | * intentional, as these should be treated as black-box implementation details. |
| 6822 | * |
| 6823 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6824 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6825 | */ |
| 6826 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6827 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6828 | _pickle.UnpicklerMemoProxy.clear |
| 6829 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6830 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6831 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6832 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6833 | static PyObject * |
| 6834 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6835 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6836 | { |
| 6837 | _Unpickler_MemoCleanup(self->unpickler); |
| 6838 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6839 | if (self->unpickler->memo == NULL) |
| 6840 | return NULL; |
| 6841 | Py_RETURN_NONE; |
| 6842 | } |
| 6843 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6844 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6845 | _pickle.UnpicklerMemoProxy.copy |
| 6846 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6847 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6848 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6849 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6850 | static PyObject * |
| 6851 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6852 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6853 | { |
| 6854 | Py_ssize_t i; |
| 6855 | PyObject *new_memo = PyDict_New(); |
| 6856 | if (new_memo == NULL) |
| 6857 | return NULL; |
| 6858 | |
| 6859 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6860 | int status; |
| 6861 | PyObject *key, *value; |
| 6862 | |
| 6863 | value = self->unpickler->memo[i]; |
| 6864 | if (value == NULL) |
| 6865 | continue; |
| 6866 | |
| 6867 | key = PyLong_FromSsize_t(i); |
| 6868 | if (key == NULL) |
| 6869 | goto error; |
| 6870 | status = PyDict_SetItem(new_memo, key, value); |
| 6871 | Py_DECREF(key); |
| 6872 | if (status < 0) |
| 6873 | goto error; |
| 6874 | } |
| 6875 | return new_memo; |
| 6876 | |
| 6877 | error: |
| 6878 | Py_DECREF(new_memo); |
| 6879 | return NULL; |
| 6880 | } |
| 6881 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6882 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6883 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6884 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6885 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6886 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6887 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6888 | static PyObject * |
| 6889 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6890 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6891 | { |
| 6892 | PyObject *reduce_value; |
| 6893 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6894 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6895 | if (contents == NULL) |
| 6896 | return NULL; |
| 6897 | |
| 6898 | reduce_value = PyTuple_New(2); |
| 6899 | if (reduce_value == NULL) { |
| 6900 | Py_DECREF(contents); |
| 6901 | return NULL; |
| 6902 | } |
| 6903 | constructor_args = PyTuple_New(1); |
| 6904 | if (constructor_args == NULL) { |
| 6905 | Py_DECREF(contents); |
| 6906 | Py_DECREF(reduce_value); |
| 6907 | return NULL; |
| 6908 | } |
| 6909 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6910 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6911 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6912 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6913 | return reduce_value; |
| 6914 | } |
| 6915 | |
| 6916 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6917 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6918 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6919 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6920 | {NULL, NULL} /* sentinel */ |
| 6921 | }; |
| 6922 | |
| 6923 | static void |
| 6924 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6925 | { |
| 6926 | PyObject_GC_UnTrack(self); |
| 6927 | Py_XDECREF(self->unpickler); |
| 6928 | PyObject_GC_Del((PyObject *)self); |
| 6929 | } |
| 6930 | |
| 6931 | static int |
| 6932 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6933 | visitproc visit, void *arg) |
| 6934 | { |
| 6935 | Py_VISIT(self->unpickler); |
| 6936 | return 0; |
| 6937 | } |
| 6938 | |
| 6939 | static int |
| 6940 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6941 | { |
| 6942 | Py_CLEAR(self->unpickler); |
| 6943 | return 0; |
| 6944 | } |
| 6945 | |
| 6946 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6947 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6948 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6949 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6950 | 0, |
| 6951 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6952 | 0, /* tp_print */ |
| 6953 | 0, /* tp_getattr */ |
| 6954 | 0, /* tp_setattr */ |
| 6955 | 0, /* tp_compare */ |
| 6956 | 0, /* tp_repr */ |
| 6957 | 0, /* tp_as_number */ |
| 6958 | 0, /* tp_as_sequence */ |
| 6959 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6960 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6961 | 0, /* tp_call */ |
| 6962 | 0, /* tp_str */ |
| 6963 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6964 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6965 | 0, /* tp_as_buffer */ |
| 6966 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6967 | 0, /* tp_doc */ |
| 6968 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6969 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6970 | 0, /* tp_richcompare */ |
| 6971 | 0, /* tp_weaklistoffset */ |
| 6972 | 0, /* tp_iter */ |
| 6973 | 0, /* tp_iternext */ |
| 6974 | unpicklerproxy_methods, /* tp_methods */ |
| 6975 | }; |
| 6976 | |
| 6977 | static PyObject * |
| 6978 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6979 | { |
| 6980 | UnpicklerMemoProxyObject *self; |
| 6981 | |
| 6982 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6983 | &UnpicklerMemoProxyType); |
| 6984 | if (self == NULL) |
| 6985 | return NULL; |
| 6986 | Py_INCREF(unpickler); |
| 6987 | self->unpickler = unpickler; |
| 6988 | PyObject_GC_Track(self); |
| 6989 | return (PyObject *)self; |
| 6990 | } |
| 6991 | |
| 6992 | /*****************************************************************************/ |
| 6993 | |
| 6994 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6995 | static PyObject * |
| 6996 | Unpickler_get_memo(UnpicklerObject *self) |
| 6997 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6998 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6999 | } |
| 7000 | |
| 7001 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7002 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7003 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7004 | PyObject **new_memo; |
| 7005 | Py_ssize_t new_memo_size = 0; |
| 7006 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7007 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7008 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7009 | PyErr_SetString(PyExc_TypeError, |
| 7010 | "attribute deletion is not supported"); |
| 7011 | return -1; |
| 7012 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7013 | |
| 7014 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 7015 | UnpicklerObject *unpickler = |
| 7016 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 7017 | |
| 7018 | new_memo_size = unpickler->memo_size; |
| 7019 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7020 | if (new_memo == NULL) |
| 7021 | return -1; |
| 7022 | |
| 7023 | for (i = 0; i < new_memo_size; i++) { |
| 7024 | Py_XINCREF(unpickler->memo[i]); |
| 7025 | new_memo[i] = unpickler->memo[i]; |
| 7026 | } |
| 7027 | } |
| 7028 | else if (PyDict_Check(obj)) { |
| 7029 | Py_ssize_t i = 0; |
| 7030 | PyObject *key, *value; |
| 7031 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 7032 | new_memo_size = PyDict_GET_SIZE(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7033 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7034 | if (new_memo == NULL) |
| 7035 | return -1; |
| 7036 | |
| 7037 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 7038 | Py_ssize_t idx; |
| 7039 | if (!PyLong_Check(key)) { |
| 7040 | PyErr_SetString(PyExc_TypeError, |
| 7041 | "memo key must be integers"); |
| 7042 | goto error; |
| 7043 | } |
| 7044 | idx = PyLong_AsSsize_t(key); |
| 7045 | if (idx == -1 && PyErr_Occurred()) |
| 7046 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7047 | if (idx < 0) { |
| 7048 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 7049 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7050 | goto error; |
| 7051 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7052 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 7053 | goto error; |
| 7054 | } |
| 7055 | } |
| 7056 | else { |
| 7057 | PyErr_Format(PyExc_TypeError, |
| 7058 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 7059 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7060 | return -1; |
| 7061 | } |
| 7062 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7063 | _Unpickler_MemoCleanup(self); |
| 7064 | self->memo_size = new_memo_size; |
| 7065 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7066 | |
| 7067 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7068 | |
| 7069 | error: |
| 7070 | if (new_memo_size) { |
| 7071 | i = new_memo_size; |
| 7072 | while (--i >= 0) { |
| 7073 | Py_XDECREF(new_memo[i]); |
| 7074 | } |
| 7075 | PyMem_FREE(new_memo); |
| 7076 | } |
| 7077 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7078 | } |
| 7079 | |
| 7080 | static PyObject * |
| 7081 | Unpickler_get_persload(UnpicklerObject *self) |
| 7082 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7083 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7084 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7085 | return NULL; |
| 7086 | } |
| 7087 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7088 | } |
| 7089 | |
| 7090 | static int |
| 7091 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 7092 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7093 | if (value == NULL) { |
| 7094 | PyErr_SetString(PyExc_TypeError, |
| 7095 | "attribute deletion is not supported"); |
| 7096 | return -1; |
| 7097 | } |
| 7098 | if (!PyCallable_Check(value)) { |
| 7099 | PyErr_SetString(PyExc_TypeError, |
| 7100 | "persistent_load must be a callable taking " |
| 7101 | "one argument"); |
| 7102 | return -1; |
| 7103 | } |
| 7104 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7105 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7106 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 7107 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7108 | |
| 7109 | return 0; |
| 7110 | } |
| 7111 | |
| 7112 | static PyGetSetDef Unpickler_getsets[] = { |
| 7113 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 7114 | {"persistent_load", (getter)Unpickler_get_persload, |
| 7115 | (setter)Unpickler_set_persload}, |
| 7116 | {NULL} |
| 7117 | }; |
| 7118 | |
| 7119 | static PyTypeObject Unpickler_Type = { |
| 7120 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7121 | "_pickle.Unpickler", /*tp_name*/ |
| 7122 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 7123 | 0, /*tp_itemsize*/ |
| 7124 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 7125 | 0, /*tp_print*/ |
| 7126 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7127 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7128 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7129 | 0, /*tp_repr*/ |
| 7130 | 0, /*tp_as_number*/ |
| 7131 | 0, /*tp_as_sequence*/ |
| 7132 | 0, /*tp_as_mapping*/ |
| 7133 | 0, /*tp_hash*/ |
| 7134 | 0, /*tp_call*/ |
| 7135 | 0, /*tp_str*/ |
| 7136 | 0, /*tp_getattro*/ |
| 7137 | 0, /*tp_setattro*/ |
| 7138 | 0, /*tp_as_buffer*/ |
| 7139 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7140 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7141 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7142 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7143 | 0, /*tp_richcompare*/ |
| 7144 | 0, /*tp_weaklistoffset*/ |
| 7145 | 0, /*tp_iter*/ |
| 7146 | 0, /*tp_iternext*/ |
| 7147 | Unpickler_methods, /*tp_methods*/ |
| 7148 | 0, /*tp_members*/ |
| 7149 | Unpickler_getsets, /*tp_getset*/ |
| 7150 | 0, /*tp_base*/ |
| 7151 | 0, /*tp_dict*/ |
| 7152 | 0, /*tp_descr_get*/ |
| 7153 | 0, /*tp_descr_set*/ |
| 7154 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7155 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7156 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7157 | PyType_GenericNew, /*tp_new*/ |
| 7158 | PyObject_GC_Del, /*tp_free*/ |
| 7159 | 0, /*tp_is_gc*/ |
| 7160 | }; |
| 7161 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7162 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7163 | |
| 7164 | _pickle.dump |
| 7165 | |
| 7166 | obj: object |
| 7167 | file: object |
| 7168 | protocol: object = NULL |
| 7169 | * |
| 7170 | fix_imports: bool = True |
| 7171 | |
| 7172 | Write a pickled representation of obj to the open file object file. |
| 7173 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7174 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7175 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7176 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7177 | The optional *protocol* argument tells the pickler to use the given |
| 7178 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 7179 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7180 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7181 | Specifying a negative protocol version selects the highest protocol |
| 7182 | version supported. The higher the protocol used, the more recent the |
| 7183 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7184 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7185 | The *file* argument must have a write() method that accepts a single |
| 7186 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7187 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7188 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7189 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7190 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7191 | to map the new Python 3 names to the old module names used in Python |
| 7192 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7193 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7194 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7195 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7196 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7197 | PyObject *protocol, int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7198 | /*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7199 | { |
| 7200 | PicklerObject *pickler = _Pickler_New(); |
| 7201 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7202 | if (pickler == NULL) |
| 7203 | return NULL; |
| 7204 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7205 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7206 | goto error; |
| 7207 | |
| 7208 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7209 | goto error; |
| 7210 | |
| 7211 | if (dump(pickler, obj) < 0) |
| 7212 | goto error; |
| 7213 | |
| 7214 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7215 | goto error; |
| 7216 | |
| 7217 | Py_DECREF(pickler); |
| 7218 | Py_RETURN_NONE; |
| 7219 | |
| 7220 | error: |
| 7221 | Py_XDECREF(pickler); |
| 7222 | return NULL; |
| 7223 | } |
| 7224 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7225 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7226 | |
| 7227 | _pickle.dumps |
| 7228 | |
| 7229 | obj: object |
| 7230 | protocol: object = NULL |
| 7231 | * |
| 7232 | fix_imports: bool = True |
| 7233 | |
| 7234 | Return the pickled representation of the object as a bytes object. |
| 7235 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7236 | The optional *protocol* argument tells the pickler to use the given |
| 7237 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7238 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7239 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7240 | Specifying a negative protocol version selects the highest protocol |
| 7241 | version supported. The higher the protocol used, the more recent the |
| 7242 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7243 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7244 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7245 | try to map the new Python 3 names to the old module names used in |
| 7246 | 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] | 7247 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7248 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7249 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7250 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7251 | int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7252 | /*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7253 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7254 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7255 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7256 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7257 | if (pickler == NULL) |
| 7258 | return NULL; |
| 7259 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7260 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7261 | goto error; |
| 7262 | |
| 7263 | if (dump(pickler, obj) < 0) |
| 7264 | goto error; |
| 7265 | |
| 7266 | result = _Pickler_GetString(pickler); |
| 7267 | Py_DECREF(pickler); |
| 7268 | return result; |
| 7269 | |
| 7270 | error: |
| 7271 | Py_XDECREF(pickler); |
| 7272 | return NULL; |
| 7273 | } |
| 7274 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7275 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7276 | |
| 7277 | _pickle.load |
| 7278 | |
| 7279 | file: object |
| 7280 | * |
| 7281 | fix_imports: bool = True |
| 7282 | encoding: str = 'ASCII' |
| 7283 | errors: str = 'strict' |
| 7284 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7285 | 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] | 7286 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7287 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7288 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7289 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7290 | The protocol version of the pickle is detected automatically, so no |
| 7291 | protocol argument is needed. Bytes past the pickled object's |
| 7292 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7293 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7294 | The argument *file* must have two methods, a read() method that takes |
| 7295 | an integer argument, and a readline() method that requires no |
| 7296 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7297 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7298 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7299 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7300 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7301 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7302 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7303 | map the old Python 2 names to the new names used in Python 3. The |
| 7304 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7305 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7306 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7307 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7308 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7309 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7310 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7311 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7312 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7313 | /*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7314 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7315 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7316 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7317 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7318 | if (unpickler == NULL) |
| 7319 | return NULL; |
| 7320 | |
| 7321 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7322 | goto error; |
| 7323 | |
| 7324 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7325 | goto error; |
| 7326 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7327 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7328 | |
| 7329 | result = load(unpickler); |
| 7330 | Py_DECREF(unpickler); |
| 7331 | return result; |
| 7332 | |
| 7333 | error: |
| 7334 | Py_XDECREF(unpickler); |
| 7335 | return NULL; |
| 7336 | } |
| 7337 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7338 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7339 | |
| 7340 | _pickle.loads |
| 7341 | |
| 7342 | data: object |
| 7343 | * |
| 7344 | fix_imports: bool = True |
| 7345 | encoding: str = 'ASCII' |
| 7346 | errors: str = 'strict' |
| 7347 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7348 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7349 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7350 | The protocol version of the pickle is detected automatically, so no |
| 7351 | protocol argument is needed. Bytes past the pickled object's |
| 7352 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7353 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7354 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7355 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7356 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7357 | map the old Python 2 names to the new names used in Python 3. The |
| 7358 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7359 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7360 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7361 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7362 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7363 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7364 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7365 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7366 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7367 | /*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7368 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7369 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7370 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7371 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7372 | if (unpickler == NULL) |
| 7373 | return NULL; |
| 7374 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7375 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7376 | goto error; |
| 7377 | |
| 7378 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7379 | goto error; |
| 7380 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7381 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7382 | |
| 7383 | result = load(unpickler); |
| 7384 | Py_DECREF(unpickler); |
| 7385 | return result; |
| 7386 | |
| 7387 | error: |
| 7388 | Py_XDECREF(unpickler); |
| 7389 | return NULL; |
| 7390 | } |
| 7391 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7392 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7393 | _PICKLE_DUMP_METHODDEF |
| 7394 | _PICKLE_DUMPS_METHODDEF |
| 7395 | _PICKLE_LOAD_METHODDEF |
| 7396 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7397 | {NULL, NULL} /* sentinel */ |
| 7398 | }; |
| 7399 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7400 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7401 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7402 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7403 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7404 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7405 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7406 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7407 | static void |
| 7408 | pickle_free(PyObject *m) |
| 7409 | { |
| 7410 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7411 | } |
| 7412 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7413 | static int |
| 7414 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7415 | { |
| 7416 | PickleState *st = _Pickle_GetState(m); |
| 7417 | Py_VISIT(st->PickleError); |
| 7418 | Py_VISIT(st->PicklingError); |
| 7419 | Py_VISIT(st->UnpicklingError); |
| 7420 | Py_VISIT(st->dispatch_table); |
| 7421 | Py_VISIT(st->extension_registry); |
| 7422 | Py_VISIT(st->extension_cache); |
| 7423 | Py_VISIT(st->inverted_registry); |
| 7424 | Py_VISIT(st->name_mapping_2to3); |
| 7425 | Py_VISIT(st->import_mapping_2to3); |
| 7426 | Py_VISIT(st->name_mapping_3to2); |
| 7427 | Py_VISIT(st->import_mapping_3to2); |
| 7428 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7429 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7430 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7431 | } |
| 7432 | |
| 7433 | static struct PyModuleDef _picklemodule = { |
| 7434 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7435 | "_pickle", /* m_name */ |
| 7436 | pickle_module_doc, /* m_doc */ |
| 7437 | sizeof(PickleState), /* m_size */ |
| 7438 | pickle_methods, /* m_methods */ |
| 7439 | NULL, /* m_reload */ |
| 7440 | pickle_traverse, /* m_traverse */ |
| 7441 | pickle_clear, /* m_clear */ |
| 7442 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7443 | }; |
| 7444 | |
| 7445 | PyMODINIT_FUNC |
| 7446 | PyInit__pickle(void) |
| 7447 | { |
| 7448 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7449 | PickleState *st; |
| 7450 | |
| 7451 | m = PyState_FindModule(&_picklemodule); |
| 7452 | if (m) { |
| 7453 | Py_INCREF(m); |
| 7454 | return m; |
| 7455 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7456 | |
| 7457 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7458 | return NULL; |
| 7459 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7460 | return NULL; |
| 7461 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7462 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7463 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7464 | return NULL; |
| 7465 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7466 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7467 | |
| 7468 | /* Create the module and add the functions. */ |
| 7469 | m = PyModule_Create(&_picklemodule); |
| 7470 | if (m == NULL) |
| 7471 | return NULL; |
| 7472 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7473 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7474 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7475 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7476 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7477 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7478 | return NULL; |
| 7479 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7480 | st = _Pickle_GetState(m); |
| 7481 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7482 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7483 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7484 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7485 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7486 | st->PicklingError = \ |
| 7487 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7488 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7489 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7490 | st->UnpicklingError = \ |
| 7491 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7492 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7493 | return NULL; |
| 7494 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7495 | Py_INCREF(st->PickleError); |
| 7496 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7497 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7498 | Py_INCREF(st->PicklingError); |
| 7499 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7500 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7501 | Py_INCREF(st->UnpicklingError); |
| 7502 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7503 | return NULL; |
| 7504 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7505 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7506 | return NULL; |
| 7507 | |
| 7508 | return m; |
| 7509 | } |