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 { |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 603 | size_t mt_mask; |
| 604 | size_t mt_used; |
| 605 | size_t mt_allocated; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 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; |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 651 | size_t memo_size; /* Capacity of the memo array */ |
| 652 | size_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 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 738 | PyMemoTable *new = PyMemoTable_New(); |
| 739 | if (new == NULL) |
| 740 | return NULL; |
| 741 | |
| 742 | new->mt_used = self->mt_used; |
| 743 | new->mt_allocated = self->mt_allocated; |
| 744 | new->mt_mask = self->mt_mask; |
| 745 | /* The table we get from _New() is probably smaller than we wanted. |
| 746 | Free it and allocate one that's the right size. */ |
| 747 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 748 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 749 | if (new->mt_table == NULL) { |
| 750 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 751 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 752 | return NULL; |
| 753 | } |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 754 | for (size_t i = 0; i < self->mt_allocated; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 755 | Py_XINCREF(self->mt_table[i].me_key); |
| 756 | } |
| 757 | memcpy(new->mt_table, self->mt_table, |
| 758 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 759 | |
| 760 | return new; |
| 761 | } |
| 762 | |
| 763 | static Py_ssize_t |
| 764 | PyMemoTable_Size(PyMemoTable *self) |
| 765 | { |
| 766 | return self->mt_used; |
| 767 | } |
| 768 | |
| 769 | static int |
| 770 | PyMemoTable_Clear(PyMemoTable *self) |
| 771 | { |
| 772 | Py_ssize_t i = self->mt_allocated; |
| 773 | |
| 774 | while (--i >= 0) { |
| 775 | Py_XDECREF(self->mt_table[i].me_key); |
| 776 | } |
| 777 | self->mt_used = 0; |
| 778 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 779 | return 0; |
| 780 | } |
| 781 | |
| 782 | static void |
| 783 | PyMemoTable_Del(PyMemoTable *self) |
| 784 | { |
| 785 | if (self == NULL) |
| 786 | return; |
| 787 | PyMemoTable_Clear(self); |
| 788 | |
| 789 | PyMem_FREE(self->mt_table); |
| 790 | PyMem_FREE(self); |
| 791 | } |
| 792 | |
| 793 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 794 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 795 | static PyMemoEntry * |
| 796 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 797 | { |
| 798 | size_t i; |
| 799 | size_t perturb; |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 800 | size_t mask = self->mt_mask; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 801 | PyMemoEntry *table = self->mt_table; |
| 802 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 803 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 804 | |
| 805 | i = hash & mask; |
| 806 | entry = &table[i]; |
| 807 | if (entry->me_key == NULL || entry->me_key == key) |
| 808 | return entry; |
| 809 | |
| 810 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 811 | i = (i << 2) + i + perturb + 1; |
| 812 | entry = &table[i & mask]; |
| 813 | if (entry->me_key == NULL || entry->me_key == key) |
| 814 | return entry; |
| 815 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 816 | Py_UNREACHABLE(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 817 | } |
| 818 | |
| 819 | /* Returns -1 on failure, 0 on success. */ |
| 820 | static int |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 821 | _PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 822 | { |
| 823 | PyMemoEntry *oldtable = NULL; |
| 824 | PyMemoEntry *oldentry, *newentry; |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 825 | size_t new_size = MT_MINSIZE; |
| 826 | size_t to_process; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 827 | |
| 828 | assert(min_size > 0); |
| 829 | |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 830 | if (min_size > PY_SSIZE_T_MAX) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 831 | PyErr_NoMemory(); |
| 832 | return -1; |
| 833 | } |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 834 | |
| 835 | /* Find the smallest valid table size >= min_size. */ |
| 836 | while (new_size < min_size) { |
| 837 | new_size <<= 1; |
| 838 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 839 | /* new_size needs to be a power of two. */ |
| 840 | assert((new_size & (new_size - 1)) == 0); |
| 841 | |
| 842 | /* Allocate new table. */ |
| 843 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 844 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 845 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 846 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 847 | PyErr_NoMemory(); |
| 848 | return -1; |
| 849 | } |
| 850 | self->mt_allocated = new_size; |
| 851 | self->mt_mask = new_size - 1; |
| 852 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 853 | |
| 854 | /* Copy entries from the old table. */ |
| 855 | to_process = self->mt_used; |
| 856 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 857 | if (oldentry->me_key != NULL) { |
| 858 | to_process--; |
| 859 | /* newentry is a pointer to a chunk of the new |
| 860 | mt_table, so we're setting the key:value pair |
| 861 | in-place. */ |
| 862 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 863 | newentry->me_key = oldentry->me_key; |
| 864 | newentry->me_value = oldentry->me_value; |
| 865 | } |
| 866 | } |
| 867 | |
| 868 | /* Deallocate the old table. */ |
| 869 | PyMem_FREE(oldtable); |
| 870 | return 0; |
| 871 | } |
| 872 | |
| 873 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 874 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 875 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 876 | { |
| 877 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 878 | if (entry->me_key == NULL) |
| 879 | return NULL; |
| 880 | return &entry->me_value; |
| 881 | } |
| 882 | |
| 883 | /* Returns -1 on failure, 0 on success. */ |
| 884 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 885 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 886 | { |
| 887 | PyMemoEntry *entry; |
| 888 | |
| 889 | assert(key != NULL); |
| 890 | |
| 891 | entry = _PyMemoTable_Lookup(self, key); |
| 892 | if (entry->me_key != NULL) { |
| 893 | entry->me_value = value; |
| 894 | return 0; |
| 895 | } |
| 896 | Py_INCREF(key); |
| 897 | entry->me_key = key; |
| 898 | entry->me_value = value; |
| 899 | self->mt_used++; |
| 900 | |
| 901 | /* If we added a key, we can safely resize. Otherwise just return! |
| 902 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 903 | * |
| 904 | * Quadrupling the size improves average table sparseness |
| 905 | * (reducing collisions) at the cost of some memory. It also halves |
| 906 | * the number of expensive resize operations in a growing memo table. |
| 907 | * |
| 908 | * Very large memo tables (over 50K items) use doubling instead. |
| 909 | * This may help applications with severe memory constraints. |
| 910 | */ |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 911 | if (SIZE_MAX / 3 >= self->mt_used && self->mt_used * 3 < self->mt_allocated * 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 912 | return 0; |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 913 | } |
| 914 | // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow. |
| 915 | size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used; |
| 916 | return _PyMemoTable_ResizeTable(self, desired_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 917 | } |
| 918 | |
| 919 | #undef MT_MINSIZE |
| 920 | #undef PERTURB_SHIFT |
| 921 | |
| 922 | /*************************************************************************/ |
| 923 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 925 | static int |
| 926 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 927 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 928 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 929 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 930 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 931 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 932 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 933 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | return 0; |
| 935 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 937 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 938 | _write_size64(char *out, size_t value) |
| 939 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 940 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 941 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 942 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 943 | |
| 944 | for (i = 0; i < sizeof(size_t); i++) { |
| 945 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 946 | } |
| 947 | for (i = sizeof(size_t); i < 8; i++) { |
| 948 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 949 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 950 | } |
| 951 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 952 | static int |
| 953 | _Pickler_CommitFrame(PicklerObject *self) |
| 954 | { |
| 955 | size_t frame_len; |
| 956 | char *qdata; |
| 957 | |
| 958 | if (!self->framing || self->frame_start == -1) |
| 959 | return 0; |
| 960 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 961 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 962 | if (frame_len >= FRAME_SIZE_MIN) { |
| 963 | qdata[0] = FRAME; |
| 964 | _write_size64(qdata + 1, frame_len); |
| 965 | } |
| 966 | else { |
| 967 | memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len); |
| 968 | self->output_len -= FRAME_HEADER_SIZE; |
| 969 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 970 | self->frame_start = -1; |
| 971 | return 0; |
| 972 | } |
| 973 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 974 | static PyObject * |
| 975 | _Pickler_GetString(PicklerObject *self) |
| 976 | { |
| 977 | PyObject *output_buffer = self->output_buffer; |
| 978 | |
| 979 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 980 | |
| 981 | if (_Pickler_CommitFrame(self)) |
| 982 | return NULL; |
| 983 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 984 | self->output_buffer = NULL; |
| 985 | /* Resize down to exact size */ |
| 986 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 987 | return NULL; |
| 988 | return output_buffer; |
| 989 | } |
| 990 | |
| 991 | static int |
| 992 | _Pickler_FlushToFile(PicklerObject *self) |
| 993 | { |
| 994 | PyObject *output, *result; |
| 995 | |
| 996 | assert(self->write != NULL); |
| 997 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 998 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 999 | output = _Pickler_GetString(self); |
| 1000 | if (output == NULL) |
| 1001 | return -1; |
| 1002 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1003 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1004 | Py_XDECREF(result); |
| 1005 | return (result == NULL) ? -1 : 0; |
| 1006 | } |
| 1007 | |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1008 | static int |
| 1009 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 1010 | { |
| 1011 | Py_ssize_t frame_len; |
| 1012 | |
| 1013 | if (!self->framing || self->frame_start == -1) { |
| 1014 | return 0; |
| 1015 | } |
| 1016 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 1017 | if (frame_len >= FRAME_SIZE_TARGET) { |
| 1018 | if(_Pickler_CommitFrame(self)) { |
| 1019 | return -1; |
| 1020 | } |
Miss Islington (bot) | e86db34 | 2018-02-03 17:41:43 -0800 | [diff] [blame] | 1021 | /* Flush the content of the committed frame to the underlying |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1022 | * file and reuse the pickler buffer for the next frame so as |
| 1023 | * to limit memory usage when dumping large complex objects to |
| 1024 | * a file. |
| 1025 | * |
| 1026 | * self->write is NULL when called via dumps. |
| 1027 | */ |
| 1028 | if (self->write != NULL) { |
| 1029 | if (_Pickler_FlushToFile(self) < 0) { |
| 1030 | return -1; |
| 1031 | } |
| 1032 | if (_Pickler_ClearBuffer(self) < 0) { |
| 1033 | return -1; |
| 1034 | } |
| 1035 | } |
| 1036 | } |
| 1037 | return 0; |
| 1038 | } |
| 1039 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1040 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1041 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1042 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1043 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1044 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1045 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1046 | |
| 1047 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1048 | need_new_frame = (self->framing && self->frame_start == -1); |
| 1049 | |
| 1050 | if (need_new_frame) |
| 1051 | n = data_len + FRAME_HEADER_SIZE; |
| 1052 | else |
| 1053 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1054 | |
| 1055 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1056 | if (required > self->max_output_len) { |
| 1057 | /* Make place in buffer for the pickle chunk */ |
| 1058 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 1059 | PyErr_NoMemory(); |
| 1060 | return -1; |
| 1061 | } |
| 1062 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 1063 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 1064 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1065 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1066 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1067 | if (need_new_frame) { |
| 1068 | /* Setup new frame */ |
| 1069 | Py_ssize_t frame_start = self->output_len; |
| 1070 | self->frame_start = frame_start; |
| 1071 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 1072 | /* Write an invalid value, for debugging */ |
| 1073 | buffer[frame_start + i] = 0xFE; |
| 1074 | } |
| 1075 | self->output_len += FRAME_HEADER_SIZE; |
| 1076 | } |
| 1077 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1078 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1079 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1080 | buffer[self->output_len + i] = s[i]; |
| 1081 | } |
| 1082 | } |
| 1083 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1084 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1085 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1086 | self->output_len += data_len; |
| 1087 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1090 | static PicklerObject * |
| 1091 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1092 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1093 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1094 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1095 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1096 | if (self == NULL) |
| 1097 | return NULL; |
| 1098 | |
| 1099 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1100 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1101 | self->write = NULL; |
| 1102 | self->proto = 0; |
| 1103 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1104 | self->framing = 0; |
| 1105 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1106 | self->fast = 0; |
| 1107 | self->fast_nesting = 0; |
| 1108 | self->fix_imports = 0; |
| 1109 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1110 | self->max_output_len = WRITE_BUF_SIZE; |
| 1111 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1112 | |
| 1113 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1114 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1115 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1116 | |
| 1117 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1118 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1119 | return NULL; |
| 1120 | } |
| 1121 | return self; |
| 1122 | } |
| 1123 | |
| 1124 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1125 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1126 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1127 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1128 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1129 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1130 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1131 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1132 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1133 | proto = PyLong_AsLong(protocol); |
| 1134 | if (proto < 0) { |
| 1135 | if (proto == -1 && PyErr_Occurred()) |
| 1136 | return -1; |
| 1137 | proto = HIGHEST_PROTOCOL; |
| 1138 | } |
| 1139 | else if (proto > HIGHEST_PROTOCOL) { |
| 1140 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1141 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1142 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1143 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1144 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1145 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1146 | self->bin = proto > 0; |
| 1147 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1148 | return 0; |
| 1149 | } |
| 1150 | |
| 1151 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1152 | be called once on a freshly created Pickler. */ |
| 1153 | static int |
| 1154 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1155 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1156 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1157 | assert(file != NULL); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1158 | if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) { |
| 1159 | return -1; |
| 1160 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1161 | if (self->write == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1162 | PyErr_SetString(PyExc_TypeError, |
| 1163 | "file must have a 'write' attribute"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1164 | return -1; |
| 1165 | } |
| 1166 | |
| 1167 | return 0; |
| 1168 | } |
| 1169 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1170 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1171 | own reference to `input`. */ |
| 1172 | static Py_ssize_t |
| 1173 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1174 | { |
| 1175 | if (self->buffer.buf != NULL) |
| 1176 | PyBuffer_Release(&self->buffer); |
| 1177 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1178 | return -1; |
| 1179 | self->input_buffer = self->buffer.buf; |
| 1180 | self->input_len = self->buffer.len; |
| 1181 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1182 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1183 | return self->input_len; |
| 1184 | } |
| 1185 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1186 | static int |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1187 | bad_readline(void) |
| 1188 | { |
| 1189 | PickleState *st = _Pickle_GetGlobalState(); |
| 1190 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
| 1191 | return -1; |
| 1192 | } |
| 1193 | |
| 1194 | static int |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1195 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1196 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1197 | Py_ssize_t consumed; |
| 1198 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1199 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1200 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1201 | if (consumed <= 0) |
| 1202 | return 0; |
| 1203 | |
| 1204 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1205 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1206 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1207 | if (r == NULL) |
| 1208 | return -1; |
| 1209 | Py_DECREF(r); |
| 1210 | |
| 1211 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1212 | return 0; |
| 1213 | } |
| 1214 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1215 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1216 | |
| 1217 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1218 | may be multiple pickle objects arranged contiguously in the same input |
| 1219 | buffer. |
| 1220 | |
| 1221 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1222 | bytes from the input stream/buffer. |
| 1223 | |
| 1224 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1225 | failure; on success, returns the number of bytes read from the file. |
| 1226 | |
| 1227 | On success, self->input_len will be 0; this is intentional so that when |
| 1228 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1229 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1230 | size to tell you how much data you can process. */ |
| 1231 | static Py_ssize_t |
| 1232 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1233 | { |
| 1234 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1235 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1236 | |
| 1237 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1238 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1239 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1240 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1241 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1242 | if (n == READ_WHOLE_LINE) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 1243 | data = _PyObject_CallNoArg(self->readline); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1244 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1245 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1246 | PyObject *len; |
| 1247 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1248 | if (self->peek && n < PREFETCH) { |
| 1249 | len = PyLong_FromSsize_t(PREFETCH); |
| 1250 | if (len == NULL) |
| 1251 | return -1; |
| 1252 | data = _Pickle_FastCall(self->peek, len); |
| 1253 | if (data == NULL) { |
| 1254 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1255 | return -1; |
| 1256 | /* peek() is probably not supported by the given file object */ |
| 1257 | PyErr_Clear(); |
| 1258 | Py_CLEAR(self->peek); |
| 1259 | } |
| 1260 | else { |
| 1261 | read_size = _Unpickler_SetStringInput(self, data); |
| 1262 | Py_DECREF(data); |
| 1263 | self->prefetched_idx = 0; |
| 1264 | if (n <= read_size) |
| 1265 | return n; |
| 1266 | } |
| 1267 | } |
| 1268 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1269 | if (len == NULL) |
| 1270 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1271 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1272 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1273 | if (data == NULL) |
| 1274 | return -1; |
| 1275 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1276 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1277 | Py_DECREF(data); |
| 1278 | return read_size; |
| 1279 | } |
| 1280 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1281 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1282 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1283 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1284 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1285 | Py_ssize_t num_read; |
| 1286 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1287 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1288 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1289 | PickleState *st = _Pickle_GetGlobalState(); |
| 1290 | PyErr_SetString(st->UnpicklingError, |
| 1291 | "read would overflow (invalid bytecode)"); |
| 1292 | return -1; |
| 1293 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1294 | |
| 1295 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1296 | assert(self->next_read_idx + n > self->input_len); |
| 1297 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1298 | if (!self->read) |
| 1299 | return bad_readline(); |
| 1300 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1301 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1302 | if (num_read < 0) |
| 1303 | return -1; |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1304 | if (num_read < n) |
| 1305 | return bad_readline(); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1306 | *s = self->input_buffer; |
| 1307 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1308 | return n; |
| 1309 | } |
| 1310 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1311 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1312 | |
| 1313 | This should be used for all data reads, rather than accessing the unpickler's |
| 1314 | input buffer directly. This method deals correctly with reading from input |
| 1315 | streams, which the input buffer doesn't deal with. |
| 1316 | |
| 1317 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1318 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1319 | should use this function's return value to know how many bytes you can |
| 1320 | consume. |
| 1321 | |
| 1322 | Returns -1 (with an exception set) on failure. On success, return the |
| 1323 | number of chars read. */ |
| 1324 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1325 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1326 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1327 | (self)->next_read_idx += (n), \ |
| 1328 | (n)) \ |
| 1329 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1330 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1331 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1332 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1333 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1334 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1335 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1336 | if (input_line == NULL) { |
| 1337 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1338 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1339 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1340 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1341 | memcpy(input_line, line, len); |
| 1342 | input_line[len] = '\0'; |
| 1343 | self->input_line = input_line; |
| 1344 | *result = self->input_line; |
| 1345 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1346 | } |
| 1347 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1348 | /* 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] | 1349 | before hitting \n, raise an error. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1350 | |
| 1351 | Returns the number of chars read, or -1 on failure. */ |
| 1352 | static Py_ssize_t |
| 1353 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1354 | { |
| 1355 | Py_ssize_t i, num_read; |
| 1356 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1357 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1358 | if (self->input_buffer[i] == '\n') { |
| 1359 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1360 | num_read = i - self->next_read_idx + 1; |
| 1361 | self->next_read_idx = i + 1; |
| 1362 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1363 | } |
| 1364 | } |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1365 | if (!self->read) |
| 1366 | return bad_readline(); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1367 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1368 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1369 | if (num_read < 0) |
| 1370 | return -1; |
| 1371 | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
| 1372 | return bad_readline(); |
| 1373 | self->next_read_idx = num_read; |
| 1374 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
| 1377 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1378 | will be modified in place. */ |
| 1379 | static int |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 1380 | _Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1381 | { |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 1382 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1383 | |
| 1384 | assert(new_size > self->memo_size); |
| 1385 | |
Miss Islington (bot) | 962051e | 2018-08-16 00:53:00 -0400 | [diff] [blame] | 1386 | PyObject **memo_new = self->memo; |
| 1387 | PyMem_RESIZE(memo_new, PyObject *, new_size); |
| 1388 | if (memo_new == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1389 | PyErr_NoMemory(); |
| 1390 | return -1; |
| 1391 | } |
Miss Islington (bot) | 962051e | 2018-08-16 00:53:00 -0400 | [diff] [blame] | 1392 | self->memo = memo_new; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1393 | for (i = self->memo_size; i < new_size; i++) |
| 1394 | self->memo[i] = NULL; |
| 1395 | self->memo_size = new_size; |
| 1396 | return 0; |
| 1397 | } |
| 1398 | |
| 1399 | /* Returns NULL if idx is out of bounds. */ |
| 1400 | static PyObject * |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 1401 | _Unpickler_MemoGet(UnpicklerObject *self, size_t idx) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1402 | { |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 1403 | if (idx >= self->memo_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1404 | return NULL; |
| 1405 | |
| 1406 | return self->memo[idx]; |
| 1407 | } |
| 1408 | |
| 1409 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1410 | This takes its own reference to `value`. */ |
| 1411 | static int |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 1412 | _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1413 | { |
| 1414 | PyObject *old_item; |
| 1415 | |
| 1416 | if (idx >= self->memo_size) { |
| 1417 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1418 | return -1; |
| 1419 | assert(idx < self->memo_size); |
| 1420 | } |
| 1421 | Py_INCREF(value); |
| 1422 | old_item = self->memo[idx]; |
| 1423 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1424 | if (old_item != NULL) { |
| 1425 | Py_DECREF(old_item); |
| 1426 | } |
| 1427 | else { |
| 1428 | self->memo_len++; |
| 1429 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1430 | return 0; |
| 1431 | } |
| 1432 | |
| 1433 | static PyObject ** |
| 1434 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1435 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1436 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1437 | if (memo == NULL) { |
| 1438 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1439 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1440 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1441 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1442 | return memo; |
| 1443 | } |
| 1444 | |
| 1445 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1446 | static void |
| 1447 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1448 | { |
| 1449 | Py_ssize_t i; |
| 1450 | PyObject **memo = self->memo; |
| 1451 | |
| 1452 | if (self->memo == NULL) |
| 1453 | return; |
| 1454 | self->memo = NULL; |
| 1455 | i = self->memo_size; |
| 1456 | while (--i >= 0) { |
| 1457 | Py_XDECREF(memo[i]); |
| 1458 | } |
| 1459 | PyMem_FREE(memo); |
| 1460 | } |
| 1461 | |
| 1462 | static UnpicklerObject * |
| 1463 | _Unpickler_New(void) |
| 1464 | { |
| 1465 | UnpicklerObject *self; |
| 1466 | |
| 1467 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1468 | if (self == NULL) |
| 1469 | return NULL; |
| 1470 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1471 | self->pers_func = NULL; |
| 1472 | self->input_buffer = NULL; |
| 1473 | self->input_line = NULL; |
| 1474 | self->input_len = 0; |
| 1475 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1476 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1477 | self->read = NULL; |
| 1478 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1479 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1480 | self->encoding = NULL; |
| 1481 | self->errors = NULL; |
| 1482 | self->marks = NULL; |
| 1483 | self->num_marks = 0; |
| 1484 | self->marks_size = 0; |
| 1485 | self->proto = 0; |
| 1486 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1487 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1488 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1489 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1490 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1491 | self->stack = (Pdata *)Pdata_New(); |
| 1492 | |
| 1493 | if (self->memo == NULL || self->stack == NULL) { |
| 1494 | Py_DECREF(self); |
| 1495 | return NULL; |
| 1496 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1497 | |
| 1498 | return self; |
| 1499 | } |
| 1500 | |
| 1501 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1502 | be called once on a freshly created Pickler. */ |
| 1503 | static int |
| 1504 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1505 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1506 | _Py_IDENTIFIER(peek); |
| 1507 | _Py_IDENTIFIER(read); |
| 1508 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1509 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1510 | if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) { |
| 1511 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1512 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1513 | (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read); |
| 1514 | (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1515 | if (self->readline == NULL || self->read == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1516 | if (!PyErr_Occurred()) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1517 | PyErr_SetString(PyExc_TypeError, |
| 1518 | "file must have 'read' and 'readline' attributes"); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1519 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1520 | Py_CLEAR(self->read); |
| 1521 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1522 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1523 | return -1; |
| 1524 | } |
| 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1529 | be called once on a freshly created Pickler. */ |
| 1530 | static int |
| 1531 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1532 | const char *encoding, |
| 1533 | const char *errors) |
| 1534 | { |
| 1535 | if (encoding == NULL) |
| 1536 | encoding = "ASCII"; |
| 1537 | if (errors == NULL) |
| 1538 | errors = "strict"; |
| 1539 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1540 | self->encoding = _PyMem_Strdup(encoding); |
| 1541 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1542 | if (self->encoding == NULL || self->errors == NULL) { |
| 1543 | PyErr_NoMemory(); |
| 1544 | return -1; |
| 1545 | } |
| 1546 | return 0; |
| 1547 | } |
| 1548 | |
| 1549 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1550 | static int |
| 1551 | memo_get(PicklerObject *self, PyObject *key) |
| 1552 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1553 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1554 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1555 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1556 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1557 | value = PyMemoTable_Get(self->memo, key); |
| 1558 | if (value == NULL) { |
| 1559 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1560 | return -1; |
| 1561 | } |
| 1562 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1563 | if (!self->bin) { |
| 1564 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1565 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1566 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1567 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1568 | } |
| 1569 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1570 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1571 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1572 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1573 | len = 2; |
| 1574 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1575 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1576 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1577 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1578 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1579 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1580 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1581 | len = 5; |
| 1582 | } |
| 1583 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1584 | PickleState *st = _Pickle_GetGlobalState(); |
| 1585 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1586 | "memo id too large for LONG_BINGET"); |
| 1587 | return -1; |
| 1588 | } |
| 1589 | } |
| 1590 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1591 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1592 | return -1; |
| 1593 | |
| 1594 | return 0; |
| 1595 | } |
| 1596 | |
| 1597 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1598 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1599 | static int |
| 1600 | memo_put(PicklerObject *self, PyObject *obj) |
| 1601 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1602 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1603 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1604 | Py_ssize_t idx; |
| 1605 | |
| 1606 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1607 | |
| 1608 | if (self->fast) |
| 1609 | return 0; |
| 1610 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1611 | idx = PyMemoTable_Size(self->memo); |
| 1612 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1613 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1614 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1615 | if (self->proto >= 4) { |
| 1616 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1617 | return -1; |
| 1618 | return 0; |
| 1619 | } |
| 1620 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1621 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1622 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1623 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1624 | len = strlen(pdata); |
| 1625 | } |
| 1626 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1627 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1628 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1629 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1630 | len = 2; |
| 1631 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1632 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1633 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1634 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1635 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1636 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1637 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1638 | len = 5; |
| 1639 | } |
| 1640 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1641 | PickleState *st = _Pickle_GetGlobalState(); |
| 1642 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1643 | "memo id too large for LONG_BINPUT"); |
| 1644 | return -1; |
| 1645 | } |
| 1646 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1647 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1648 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1649 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1650 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | static PyObject * |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1654 | get_dotted_path(PyObject *obj, PyObject *name) |
| 1655 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1656 | _Py_static_string(PyId_dot, "."); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1657 | PyObject *dotted_path; |
| 1658 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1659 | |
| 1660 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1661 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1662 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1663 | n = PyList_GET_SIZE(dotted_path); |
| 1664 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1665 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1666 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1667 | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1668 | if (obj == NULL) |
| 1669 | PyErr_Format(PyExc_AttributeError, |
| 1670 | "Can't pickle local object %R", name); |
| 1671 | else |
| 1672 | PyErr_Format(PyExc_AttributeError, |
| 1673 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1674 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1675 | return NULL; |
| 1676 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1677 | } |
| 1678 | return dotted_path; |
| 1679 | } |
| 1680 | |
| 1681 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1682 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1683 | { |
| 1684 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1685 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1686 | |
| 1687 | assert(PyList_CheckExact(names)); |
| 1688 | Py_INCREF(obj); |
| 1689 | n = PyList_GET_SIZE(names); |
| 1690 | for (i = 0; i < n; i++) { |
| 1691 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1692 | Py_XDECREF(parent); |
| 1693 | parent = obj; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1694 | (void)_PyObject_LookupAttr(parent, name, &obj); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1695 | if (obj == NULL) { |
| 1696 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1697 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1698 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1699 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1700 | if (pparent != NULL) |
| 1701 | *pparent = parent; |
| 1702 | else |
| 1703 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1704 | return obj; |
| 1705 | } |
| 1706 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1707 | |
| 1708 | static PyObject * |
| 1709 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1710 | { |
| 1711 | PyObject *dotted_path, *attr; |
| 1712 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1713 | if (allow_qualname) { |
| 1714 | dotted_path = get_dotted_path(obj, name); |
| 1715 | if (dotted_path == NULL) |
| 1716 | return NULL; |
| 1717 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1718 | Py_DECREF(dotted_path); |
| 1719 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1720 | else { |
| 1721 | (void)_PyObject_LookupAttr(obj, name, &attr); |
| 1722 | } |
| 1723 | if (attr == NULL && !PyErr_Occurred()) { |
| 1724 | PyErr_Format(PyExc_AttributeError, |
| 1725 | "Can't get attribute %R on %R", name, obj); |
| 1726 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1727 | return attr; |
| 1728 | } |
| 1729 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1730 | static int |
| 1731 | _checkmodule(PyObject *module_name, PyObject *module, |
| 1732 | PyObject *global, PyObject *dotted_path) |
| 1733 | { |
| 1734 | if (module == Py_None) { |
| 1735 | return -1; |
| 1736 | } |
| 1737 | if (PyUnicode_Check(module_name) && |
| 1738 | _PyUnicode_EqualToASCIIString(module_name, "__main__")) { |
| 1739 | return -1; |
| 1740 | } |
| 1741 | |
| 1742 | PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); |
| 1743 | if (candidate == NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1744 | return -1; |
| 1745 | } |
| 1746 | if (candidate != global) { |
| 1747 | Py_DECREF(candidate); |
| 1748 | return -1; |
| 1749 | } |
| 1750 | Py_DECREF(candidate); |
| 1751 | return 0; |
| 1752 | } |
| 1753 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1754 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1755 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1756 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1757 | PyObject *module_name; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1758 | PyObject *module = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1759 | Py_ssize_t i; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1760 | PyObject *modules; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1761 | _Py_IDENTIFIER(__module__); |
| 1762 | _Py_IDENTIFIER(modules); |
| 1763 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1764 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1765 | if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) { |
| 1766 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1767 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1768 | if (module_name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1769 | /* In some rare cases (e.g., bound methods of extension types), |
| 1770 | __module__ can be None. If it is so, then search sys.modules for |
| 1771 | the module of global. */ |
| 1772 | if (module_name != Py_None) |
| 1773 | return module_name; |
| 1774 | Py_CLEAR(module_name); |
| 1775 | } |
| 1776 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1777 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1778 | /* Fallback on walking sys.modules */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1779 | modules = _PySys_GetObjectId(&PyId_modules); |
| 1780 | if (modules == NULL) { |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1781 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1782 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1783 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1784 | if (PyDict_CheckExact(modules)) { |
| 1785 | i = 0; |
| 1786 | while (PyDict_Next(modules, &i, &module_name, &module)) { |
| 1787 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1788 | Py_INCREF(module_name); |
| 1789 | return module_name; |
| 1790 | } |
| 1791 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1792 | return NULL; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1793 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1794 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1795 | } |
| 1796 | else { |
| 1797 | PyObject *iterator = PyObject_GetIter(modules); |
| 1798 | if (iterator == NULL) { |
| 1799 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1800 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1801 | while ((module_name = PyIter_Next(iterator))) { |
| 1802 | module = PyObject_GetItem(modules, module_name); |
| 1803 | if (module == NULL) { |
| 1804 | Py_DECREF(module_name); |
| 1805 | Py_DECREF(iterator); |
| 1806 | return NULL; |
| 1807 | } |
| 1808 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1809 | Py_DECREF(module); |
| 1810 | Py_DECREF(iterator); |
| 1811 | return module_name; |
| 1812 | } |
| 1813 | Py_DECREF(module); |
| 1814 | Py_DECREF(module_name); |
| 1815 | if (PyErr_Occurred()) { |
| 1816 | Py_DECREF(iterator); |
| 1817 | return NULL; |
| 1818 | } |
| 1819 | } |
| 1820 | Py_DECREF(iterator); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1821 | } |
| 1822 | |
| 1823 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1824 | module_name = _PyUnicode_FromId(&PyId___main__); |
Victor Stinner | af46eb8 | 2017-09-05 23:30:16 +0200 | [diff] [blame] | 1825 | Py_XINCREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1826 | return module_name; |
| 1827 | } |
| 1828 | |
| 1829 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1830 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1831 | memoization disabled). If the nesting of a list or dict object exceed |
| 1832 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1833 | reference to the seen list or dict objects and check whether these objects |
| 1834 | are recursive. These are not strictly necessary, since save() has a |
| 1835 | hard-coded recursion limit, but they give a nicer error message than the |
| 1836 | typical RuntimeError. */ |
| 1837 | static int |
| 1838 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1839 | { |
| 1840 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1841 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1842 | PyObject *key = NULL; |
| 1843 | if (self->fast_memo == NULL) { |
| 1844 | self->fast_memo = PyDict_New(); |
| 1845 | if (self->fast_memo == NULL) { |
| 1846 | self->fast_nesting = -1; |
| 1847 | return 0; |
| 1848 | } |
| 1849 | } |
| 1850 | key = PyLong_FromVoidPtr(obj); |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1851 | if (key == NULL) { |
| 1852 | self->fast_nesting = -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1853 | return 0; |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1854 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1855 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1856 | Py_DECREF(key); |
| 1857 | PyErr_Format(PyExc_ValueError, |
| 1858 | "fast mode: can't pickle cyclic objects " |
| 1859 | "including object type %.200s at %p", |
| 1860 | obj->ob_type->tp_name, obj); |
| 1861 | self->fast_nesting = -1; |
| 1862 | return 0; |
| 1863 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1864 | if (PyErr_Occurred()) { |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1865 | Py_DECREF(key); |
| 1866 | self->fast_nesting = -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1867 | return 0; |
| 1868 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1869 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1870 | Py_DECREF(key); |
| 1871 | self->fast_nesting = -1; |
| 1872 | return 0; |
| 1873 | } |
| 1874 | Py_DECREF(key); |
| 1875 | } |
| 1876 | return 1; |
| 1877 | } |
| 1878 | |
| 1879 | static int |
| 1880 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1881 | { |
| 1882 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1883 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1884 | if (key == NULL) |
| 1885 | return 0; |
| 1886 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1887 | Py_DECREF(key); |
| 1888 | return 0; |
| 1889 | } |
| 1890 | Py_DECREF(key); |
| 1891 | } |
| 1892 | return 1; |
| 1893 | } |
| 1894 | |
| 1895 | static int |
| 1896 | save_none(PicklerObject *self, PyObject *obj) |
| 1897 | { |
| 1898 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1899 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1900 | return -1; |
| 1901 | |
| 1902 | return 0; |
| 1903 | } |
| 1904 | |
| 1905 | static int |
| 1906 | save_bool(PicklerObject *self, PyObject *obj) |
| 1907 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1908 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1909 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1910 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1911 | return -1; |
| 1912 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1913 | else { |
| 1914 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1915 | * so that unpicklers written before bools were introduced unpickle them |
| 1916 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1917 | * Note that protocol 2 added direct ways to pickle bools. |
| 1918 | */ |
| 1919 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1920 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1921 | return -1; |
| 1922 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1923 | return 0; |
| 1924 | } |
| 1925 | |
| 1926 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1927 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1928 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1929 | PyObject *repr = NULL; |
| 1930 | Py_ssize_t size; |
| 1931 | long val; |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1932 | int overflow; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1933 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1934 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1935 | val= PyLong_AsLongAndOverflow(obj, &overflow); |
| 1936 | if (!overflow && (sizeof(long) <= 4 || |
| 1937 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) |
| 1938 | { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1939 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1940 | |
| 1941 | Note: we can't use -0x80000000L in the above condition because some |
| 1942 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1943 | before applying the unary minus when sizeof(long) <= 4. The |
| 1944 | resulting value stays unsigned which is commonly not what we want, |
| 1945 | so MSVC happily warns us about it. However, that result would have |
| 1946 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1947 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1948 | char pdata[32]; |
| 1949 | Py_ssize_t len = 0; |
| 1950 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1951 | if (self->bin) { |
| 1952 | pdata[1] = (unsigned char)(val & 0xff); |
| 1953 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1954 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1955 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1956 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1957 | if ((pdata[4] != 0) || (pdata[3] != 0)) { |
| 1958 | pdata[0] = BININT; |
| 1959 | len = 5; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1960 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1961 | else if (pdata[2] != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1962 | pdata[0] = BININT2; |
| 1963 | len = 3; |
| 1964 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1965 | else { |
| 1966 | pdata[0] = BININT1; |
| 1967 | len = 2; |
| 1968 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1969 | } |
| 1970 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1971 | sprintf(pdata, "%c%ld\n", INT, val); |
| 1972 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1973 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1974 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1975 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1976 | |
| 1977 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1978 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1979 | assert(!PyErr_Occurred()); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1980 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1981 | if (self->proto >= 2) { |
| 1982 | /* Linear-time pickling. */ |
| 1983 | size_t nbits; |
| 1984 | size_t nbytes; |
| 1985 | unsigned char *pdata; |
| 1986 | char header[5]; |
| 1987 | int i; |
| 1988 | int sign = _PyLong_Sign(obj); |
| 1989 | |
| 1990 | if (sign == 0) { |
| 1991 | header[0] = LONG1; |
| 1992 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1993 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1994 | goto error; |
| 1995 | return 0; |
| 1996 | } |
| 1997 | nbits = _PyLong_NumBits(obj); |
| 1998 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1999 | goto error; |
| 2000 | /* How many bytes do we need? There are nbits >> 3 full |
| 2001 | * bytes of data, and nbits & 7 leftover bits. If there |
| 2002 | * are any leftover bits, then we clearly need another |
| 2003 | * byte. Wnat's not so obvious is that we *probably* |
| 2004 | * need another byte even if there aren't any leftovers: |
| 2005 | * the most-significant bit of the most-significant byte |
| 2006 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2007 | * opposite of the one we need. The exception is ints |
| 2008 | * 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] | 2009 | * its own 256's-complement, so has the right sign bit |
| 2010 | * even without the extra byte. That's a pain to check |
| 2011 | * for in advance, though, so we always grab an extra |
| 2012 | * byte at the start, and cut it back later if possible. |
| 2013 | */ |
| 2014 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 2015 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2016 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2017 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2018 | goto error; |
| 2019 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2020 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2021 | if (repr == NULL) |
| 2022 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2023 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2024 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 2025 | pdata, nbytes, |
| 2026 | 1 /* little endian */ , 1 /* signed */ ); |
| 2027 | if (i < 0) |
| 2028 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2029 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2030 | * needed. This is so iff the MSB is all redundant sign |
| 2031 | * bits. |
| 2032 | */ |
| 2033 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2034 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2035 | pdata[nbytes - 1] == 0xff && |
| 2036 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 2037 | nbytes--; |
| 2038 | } |
| 2039 | |
| 2040 | if (nbytes < 256) { |
| 2041 | header[0] = LONG1; |
| 2042 | header[1] = (unsigned char)nbytes; |
| 2043 | size = 2; |
| 2044 | } |
| 2045 | else { |
| 2046 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2047 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2048 | for (i = 1; i < 5; i++) { |
| 2049 | header[i] = (unsigned char)(size & 0xff); |
| 2050 | size >>= 8; |
| 2051 | } |
| 2052 | size = 5; |
| 2053 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2054 | if (_Pickler_Write(self, header, size) < 0 || |
| 2055 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2056 | goto error; |
| 2057 | } |
| 2058 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2059 | const char long_op = LONG; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2060 | const char *string; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2061 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 2062 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 2063 | the number of digits), in both directions. We add a trailing 'L' |
| 2064 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2065 | |
| 2066 | repr = PyObject_Repr(obj); |
| 2067 | if (repr == NULL) |
| 2068 | goto error; |
| 2069 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2070 | string = PyUnicode_AsUTF8AndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2071 | if (string == NULL) |
| 2072 | goto error; |
| 2073 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2074 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 2075 | _Pickler_Write(self, string, size) < 0 || |
| 2076 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2077 | goto error; |
| 2078 | } |
| 2079 | |
| 2080 | if (0) { |
| 2081 | error: |
| 2082 | status = -1; |
| 2083 | } |
| 2084 | Py_XDECREF(repr); |
| 2085 | |
| 2086 | return status; |
| 2087 | } |
| 2088 | |
| 2089 | static int |
| 2090 | save_float(PicklerObject *self, PyObject *obj) |
| 2091 | { |
| 2092 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 2093 | |
| 2094 | if (self->bin) { |
| 2095 | char pdata[9]; |
| 2096 | pdata[0] = BINFLOAT; |
| 2097 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2098 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2099 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2100 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2101 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2102 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2103 | int result = -1; |
| 2104 | char *buf = NULL; |
| 2105 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2106 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2107 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2108 | goto done; |
| 2109 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2110 | 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] | 2111 | if (!buf) { |
| 2112 | PyErr_NoMemory(); |
| 2113 | goto done; |
| 2114 | } |
| 2115 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2116 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2117 | goto done; |
| 2118 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2119 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2120 | goto done; |
| 2121 | |
| 2122 | result = 0; |
| 2123 | done: |
| 2124 | PyMem_Free(buf); |
| 2125 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2126 | } |
| 2127 | |
| 2128 | return 0; |
| 2129 | } |
| 2130 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2131 | /* Perform direct write of the header and payload of the binary object. |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2132 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2133 | The large contiguous data is written directly into the underlying file |
| 2134 | object, bypassing the output_buffer of the Pickler. We intentionally |
| 2135 | do not insert a protocol 4 frame opcode to make it possible to optimize |
| 2136 | file.read calls in the loader. |
| 2137 | */ |
| 2138 | static int |
| 2139 | _Pickler_write_bytes(PicklerObject *self, |
| 2140 | const char *header, Py_ssize_t header_size, |
| 2141 | const char *data, Py_ssize_t data_size, |
| 2142 | PyObject *payload) |
| 2143 | { |
| 2144 | int bypass_buffer = (data_size >= FRAME_SIZE_TARGET); |
| 2145 | int framing = self->framing; |
| 2146 | |
| 2147 | if (bypass_buffer) { |
| 2148 | assert(self->output_buffer != NULL); |
| 2149 | /* Commit the previous frame. */ |
| 2150 | if (_Pickler_CommitFrame(self)) { |
| 2151 | return -1; |
| 2152 | } |
| 2153 | /* Disable framing temporarily */ |
| 2154 | self->framing = 0; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2155 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2156 | |
| 2157 | if (_Pickler_Write(self, header, header_size) < 0) { |
| 2158 | return -1; |
| 2159 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2160 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2161 | if (bypass_buffer && self->write != NULL) { |
| 2162 | /* Bypass the in-memory buffer to directly stream large data |
| 2163 | into the underlying file object. */ |
| 2164 | PyObject *result, *mem = NULL; |
| 2165 | /* Dump the output buffer to the file. */ |
| 2166 | if (_Pickler_FlushToFile(self) < 0) { |
| 2167 | return -1; |
| 2168 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2169 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2170 | /* Stream write the payload into the file without going through the |
| 2171 | output buffer. */ |
| 2172 | if (payload == NULL) { |
Serhiy Storchaka | 5b76bdb | 2018-01-13 00:28:31 +0200 | [diff] [blame] | 2173 | /* TODO: It would be better to use a memoryview with a linked |
| 2174 | original string if this is possible. */ |
| 2175 | payload = mem = PyBytes_FromStringAndSize(data, data_size); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2176 | if (payload == NULL) { |
| 2177 | return -1; |
| 2178 | } |
| 2179 | } |
| 2180 | result = PyObject_CallFunctionObjArgs(self->write, payload, NULL); |
| 2181 | Py_XDECREF(mem); |
| 2182 | if (result == NULL) { |
| 2183 | return -1; |
| 2184 | } |
| 2185 | Py_DECREF(result); |
| 2186 | |
| 2187 | /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */ |
| 2188 | if (_Pickler_ClearBuffer(self) < 0) { |
| 2189 | return -1; |
| 2190 | } |
| 2191 | } |
| 2192 | else { |
| 2193 | if (_Pickler_Write(self, data, data_size) < 0) { |
| 2194 | return -1; |
| 2195 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2196 | } |
| 2197 | |
| 2198 | /* Re-enable framing for subsequent calls to _Pickler_Write. */ |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2199 | self->framing = framing; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2200 | |
| 2201 | return 0; |
| 2202 | } |
| 2203 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2204 | static int |
| 2205 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2206 | { |
| 2207 | if (self->proto < 3) { |
| 2208 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2209 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2210 | the __reduce__ method) to permit bytes object unpickling. |
| 2211 | |
| 2212 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2213 | 2 'bytes' is just an alias for 'str' (which has different |
| 2214 | parameters than the actual bytes object), we use codecs.encode |
| 2215 | to create the appropriate 'str' object when unpickled using |
| 2216 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2217 | using Python 3. Again this is a hack and we don't need to do this |
| 2218 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2219 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2220 | int status; |
| 2221 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2222 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2223 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2224 | } |
| 2225 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2226 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2227 | PyObject *unicode_str = |
| 2228 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2229 | PyBytes_GET_SIZE(obj), |
| 2230 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2231 | _Py_IDENTIFIER(latin1); |
| 2232 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2233 | if (unicode_str == NULL) |
| 2234 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2235 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2236 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2237 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2238 | Py_DECREF(unicode_str); |
| 2239 | } |
| 2240 | |
| 2241 | if (reduce_value == NULL) |
| 2242 | return -1; |
| 2243 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2244 | /* save_reduce() will memoize the object automatically. */ |
| 2245 | status = save_reduce(self, reduce_value, obj); |
| 2246 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2247 | return status; |
| 2248 | } |
| 2249 | else { |
| 2250 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2251 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2252 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2253 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2254 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2255 | if (size < 0) |
| 2256 | return -1; |
| 2257 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2258 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2259 | header[0] = SHORT_BINBYTES; |
| 2260 | header[1] = (unsigned char)size; |
| 2261 | len = 2; |
| 2262 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2263 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2264 | header[0] = BINBYTES; |
| 2265 | header[1] = (unsigned char)(size & 0xff); |
| 2266 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2267 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2268 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2269 | len = 5; |
| 2270 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2271 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2272 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2273 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2274 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2275 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2276 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2277 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2278 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2279 | return -1; /* string too large */ |
| 2280 | } |
| 2281 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2282 | if (_Pickler_write_bytes(self, header, len, |
| 2283 | PyBytes_AS_STRING(obj), size, obj) < 0) |
| 2284 | { |
| 2285 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2286 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2287 | |
| 2288 | if (memo_put(self, obj) < 0) |
| 2289 | return -1; |
| 2290 | |
| 2291 | return 0; |
| 2292 | } |
| 2293 | } |
| 2294 | |
| 2295 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2296 | backslash and newline characters to \uXXXX escapes. */ |
| 2297 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2298 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2299 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2300 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2301 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2302 | void *data; |
| 2303 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2304 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2305 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2306 | if (PyUnicode_READY(obj)) |
| 2307 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2308 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2309 | _PyBytesWriter_Init(&writer); |
| 2310 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2311 | size = PyUnicode_GET_LENGTH(obj); |
| 2312 | data = PyUnicode_DATA(obj); |
| 2313 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2314 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2315 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2316 | if (p == NULL) |
| 2317 | goto error; |
| 2318 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2319 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2320 | for (i=0; i < size; i++) { |
| 2321 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2322 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2323 | if (ch >= 0x10000) { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2324 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2325 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2326 | if (p == NULL) |
| 2327 | goto error; |
| 2328 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2329 | *p++ = '\\'; |
| 2330 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2331 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2332 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2333 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2334 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2335 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2336 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2337 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2338 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2339 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2340 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2341 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2342 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2343 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2344 | if (p == NULL) |
| 2345 | goto error; |
| 2346 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2347 | *p++ = '\\'; |
| 2348 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2349 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2350 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2351 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2352 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2353 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2354 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2355 | else |
| 2356 | *p++ = (char) ch; |
| 2357 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2358 | |
| 2359 | return _PyBytesWriter_Finish(&writer, p); |
| 2360 | |
| 2361 | error: |
| 2362 | _PyBytesWriter_Dealloc(&writer); |
| 2363 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2364 | } |
| 2365 | |
| 2366 | static int |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2367 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2368 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2369 | char header[9]; |
| 2370 | Py_ssize_t len; |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2371 | PyObject *encoded = NULL; |
| 2372 | Py_ssize_t size; |
| 2373 | const char *data; |
| 2374 | |
| 2375 | if (PyUnicode_READY(obj)) |
| 2376 | return -1; |
| 2377 | |
| 2378 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2379 | if (data == NULL) { |
| 2380 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2381 | "surrogatepass" error handler. */ |
| 2382 | PyErr_Clear(); |
| 2383 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2384 | if (encoded == NULL) |
| 2385 | return -1; |
| 2386 | |
| 2387 | data = PyBytes_AS_STRING(encoded); |
| 2388 | size = PyBytes_GET_SIZE(encoded); |
| 2389 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2390 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2391 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2392 | if (size <= 0xff && self->proto >= 4) { |
| 2393 | header[0] = SHORT_BINUNICODE; |
| 2394 | header[1] = (unsigned char)(size & 0xff); |
| 2395 | len = 2; |
| 2396 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2397 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2398 | header[0] = BINUNICODE; |
| 2399 | header[1] = (unsigned char)(size & 0xff); |
| 2400 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2401 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2402 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2403 | len = 5; |
| 2404 | } |
| 2405 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2406 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2407 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2408 | len = 9; |
| 2409 | } |
| 2410 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2411 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2412 | "cannot serialize a string larger than 4GiB"); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2413 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2414 | return -1; |
| 2415 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2416 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2417 | if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) { |
| 2418 | Py_XDECREF(encoded); |
| 2419 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2420 | } |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2421 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2422 | return 0; |
| 2423 | } |
| 2424 | |
| 2425 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2426 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2427 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2428 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2429 | if (write_unicode_binary(self, obj) < 0) |
| 2430 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2431 | } |
| 2432 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2433 | PyObject *encoded; |
| 2434 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2435 | const char unicode_op = UNICODE; |
| 2436 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2437 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2438 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2439 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2440 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2441 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2442 | Py_DECREF(encoded); |
| 2443 | return -1; |
| 2444 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2445 | |
| 2446 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2447 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2448 | Py_DECREF(encoded); |
| 2449 | return -1; |
| 2450 | } |
| 2451 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2452 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2453 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2454 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2455 | } |
| 2456 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2457 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2458 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2459 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2463 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2464 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2465 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2466 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2467 | |
| 2468 | assert(PyTuple_Size(t) == len); |
| 2469 | |
| 2470 | for (i = 0; i < len; i++) { |
| 2471 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2472 | |
| 2473 | if (element == NULL) |
| 2474 | return -1; |
| 2475 | if (save(self, element, 0) < 0) |
| 2476 | return -1; |
| 2477 | } |
| 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2483 | * used across protocols to minimize the space needed to pickle them. |
| 2484 | * Tuples are also the only builtin immutable type that can be recursive |
| 2485 | * (a tuple can be reached from itself), and that requires some subtle |
| 2486 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2487 | */ |
| 2488 | static int |
| 2489 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2490 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2491 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2492 | |
| 2493 | const char mark_op = MARK; |
| 2494 | const char tuple_op = TUPLE; |
| 2495 | const char pop_op = POP; |
| 2496 | const char pop_mark_op = POP_MARK; |
| 2497 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2498 | |
| 2499 | if ((len = PyTuple_Size(obj)) < 0) |
| 2500 | return -1; |
| 2501 | |
| 2502 | if (len == 0) { |
| 2503 | char pdata[2]; |
| 2504 | |
| 2505 | if (self->proto) { |
| 2506 | pdata[0] = EMPTY_TUPLE; |
| 2507 | len = 1; |
| 2508 | } |
| 2509 | else { |
| 2510 | pdata[0] = MARK; |
| 2511 | pdata[1] = TUPLE; |
| 2512 | len = 2; |
| 2513 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2514 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2515 | return -1; |
| 2516 | return 0; |
| 2517 | } |
| 2518 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2519 | /* 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] | 2520 | * saving the tuple elements, the tuple must be recursive, in |
| 2521 | * which case we'll pop everything we put on the stack, and fetch |
| 2522 | * its value from the memo. |
| 2523 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2524 | if (len <= 3 && self->proto >= 2) { |
| 2525 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2526 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2527 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2528 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2529 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2530 | /* pop the len elements */ |
| 2531 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2532 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2533 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2534 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2535 | if (memo_get(self, obj) < 0) |
| 2536 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2537 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2538 | return 0; |
| 2539 | } |
| 2540 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2541 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2542 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2543 | } |
| 2544 | goto memoize; |
| 2545 | } |
| 2546 | |
| 2547 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2548 | * Generate MARK e1 e2 ... TUPLE |
| 2549 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2550 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2551 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2552 | |
| 2553 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2554 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2555 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2556 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2557 | /* pop the stack stuff we pushed */ |
| 2558 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2559 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2560 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2561 | } |
| 2562 | else { |
| 2563 | /* Note that we pop one more than len, to remove |
| 2564 | * the MARK too. |
| 2565 | */ |
| 2566 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2567 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2568 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2569 | } |
| 2570 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2571 | if (memo_get(self, obj) < 0) |
| 2572 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2573 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2574 | return 0; |
| 2575 | } |
| 2576 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2577 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2578 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | memoize: |
| 2582 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2583 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2584 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2585 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2586 | } |
| 2587 | |
| 2588 | /* iter is an iterator giving items, and we batch up chunks of |
| 2589 | * MARK item item ... item APPENDS |
| 2590 | * opcode sequences. Calling code should have arranged to first create an |
| 2591 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2592 | * Returns 0 on success, <0 on error. |
| 2593 | */ |
| 2594 | static int |
| 2595 | batch_list(PicklerObject *self, PyObject *iter) |
| 2596 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2597 | PyObject *obj = NULL; |
| 2598 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2599 | int i, n; |
| 2600 | |
| 2601 | const char mark_op = MARK; |
| 2602 | const char append_op = APPEND; |
| 2603 | const char appends_op = APPENDS; |
| 2604 | |
| 2605 | assert(iter != NULL); |
| 2606 | |
| 2607 | /* XXX: I think this function could be made faster by avoiding the |
| 2608 | iterator interface and fetching objects directly from list using |
| 2609 | PyList_GET_ITEM. |
| 2610 | */ |
| 2611 | |
| 2612 | if (self->proto == 0) { |
| 2613 | /* APPENDS isn't available; do one at a time. */ |
| 2614 | for (;;) { |
| 2615 | obj = PyIter_Next(iter); |
| 2616 | if (obj == NULL) { |
| 2617 | if (PyErr_Occurred()) |
| 2618 | return -1; |
| 2619 | break; |
| 2620 | } |
| 2621 | i = save(self, obj, 0); |
| 2622 | Py_DECREF(obj); |
| 2623 | if (i < 0) |
| 2624 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2625 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2626 | return -1; |
| 2627 | } |
| 2628 | return 0; |
| 2629 | } |
| 2630 | |
| 2631 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2632 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2633 | /* Get first item */ |
| 2634 | firstitem = PyIter_Next(iter); |
| 2635 | if (firstitem == NULL) { |
| 2636 | if (PyErr_Occurred()) |
| 2637 | goto error; |
| 2638 | |
| 2639 | /* nothing more to add */ |
| 2640 | break; |
| 2641 | } |
| 2642 | |
| 2643 | /* Try to get a second item */ |
| 2644 | obj = PyIter_Next(iter); |
| 2645 | if (obj == NULL) { |
| 2646 | if (PyErr_Occurred()) |
| 2647 | goto error; |
| 2648 | |
| 2649 | /* Only one item to write */ |
| 2650 | if (save(self, firstitem, 0) < 0) |
| 2651 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2652 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2653 | goto error; |
| 2654 | Py_CLEAR(firstitem); |
| 2655 | break; |
| 2656 | } |
| 2657 | |
| 2658 | /* More than one item to write */ |
| 2659 | |
| 2660 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2661 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2662 | goto error; |
| 2663 | |
| 2664 | if (save(self, firstitem, 0) < 0) |
| 2665 | goto error; |
| 2666 | Py_CLEAR(firstitem); |
| 2667 | n = 1; |
| 2668 | |
| 2669 | /* Fetch and save up to BATCHSIZE items */ |
| 2670 | while (obj) { |
| 2671 | if (save(self, obj, 0) < 0) |
| 2672 | goto error; |
| 2673 | Py_CLEAR(obj); |
| 2674 | n += 1; |
| 2675 | |
| 2676 | if (n == BATCHSIZE) |
| 2677 | break; |
| 2678 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2679 | obj = PyIter_Next(iter); |
| 2680 | if (obj == NULL) { |
| 2681 | if (PyErr_Occurred()) |
| 2682 | goto error; |
| 2683 | break; |
| 2684 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2685 | } |
| 2686 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2687 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2688 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2689 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2690 | } while (n == BATCHSIZE); |
| 2691 | return 0; |
| 2692 | |
| 2693 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2694 | Py_XDECREF(firstitem); |
| 2695 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2696 | return -1; |
| 2697 | } |
| 2698 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2699 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2700 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2701 | * MARK item item ... item APPENDS |
| 2702 | * opcode sequences. Calling code should have arranged to first create an |
| 2703 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2704 | * Returns 0 on success, -1 on error. |
| 2705 | * |
| 2706 | * This version is considerably faster than batch_list(), if less general. |
| 2707 | * |
| 2708 | * Note that this only works for protocols > 0. |
| 2709 | */ |
| 2710 | static int |
| 2711 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2712 | { |
| 2713 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2714 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2715 | |
| 2716 | const char append_op = APPEND; |
| 2717 | const char appends_op = APPENDS; |
| 2718 | const char mark_op = MARK; |
| 2719 | |
| 2720 | assert(obj != NULL); |
| 2721 | assert(self->proto > 0); |
| 2722 | assert(PyList_CheckExact(obj)); |
| 2723 | |
| 2724 | if (PyList_GET_SIZE(obj) == 1) { |
| 2725 | item = PyList_GET_ITEM(obj, 0); |
| 2726 | if (save(self, item, 0) < 0) |
| 2727 | return -1; |
| 2728 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2729 | return -1; |
| 2730 | return 0; |
| 2731 | } |
| 2732 | |
| 2733 | /* Write in batches of BATCHSIZE. */ |
| 2734 | total = 0; |
| 2735 | do { |
| 2736 | this_batch = 0; |
| 2737 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2738 | return -1; |
| 2739 | while (total < PyList_GET_SIZE(obj)) { |
| 2740 | item = PyList_GET_ITEM(obj, total); |
| 2741 | if (save(self, item, 0) < 0) |
| 2742 | return -1; |
| 2743 | total++; |
| 2744 | if (++this_batch == BATCHSIZE) |
| 2745 | break; |
| 2746 | } |
| 2747 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2748 | return -1; |
| 2749 | |
| 2750 | } while (total < PyList_GET_SIZE(obj)); |
| 2751 | |
| 2752 | return 0; |
| 2753 | } |
| 2754 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2755 | static int |
| 2756 | save_list(PicklerObject *self, PyObject *obj) |
| 2757 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2758 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2759 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2760 | int status = 0; |
| 2761 | |
| 2762 | if (self->fast && !fast_save_enter(self, obj)) |
| 2763 | goto error; |
| 2764 | |
| 2765 | /* Create an empty list. */ |
| 2766 | if (self->bin) { |
| 2767 | header[0] = EMPTY_LIST; |
| 2768 | len = 1; |
| 2769 | } |
| 2770 | else { |
| 2771 | header[0] = MARK; |
| 2772 | header[1] = LIST; |
| 2773 | len = 2; |
| 2774 | } |
| 2775 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2776 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2777 | goto error; |
| 2778 | |
| 2779 | /* Get list length, and bow out early if empty. */ |
| 2780 | if ((len = PyList_Size(obj)) < 0) |
| 2781 | goto error; |
| 2782 | |
| 2783 | if (memo_put(self, obj) < 0) |
| 2784 | goto error; |
| 2785 | |
| 2786 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2787 | /* Materialize the list elements. */ |
| 2788 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2789 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2790 | goto error; |
| 2791 | status = batch_list_exact(self, obj); |
| 2792 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2793 | } else { |
| 2794 | PyObject *iter = PyObject_GetIter(obj); |
| 2795 | if (iter == NULL) |
| 2796 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2797 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2798 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2799 | Py_DECREF(iter); |
| 2800 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2801 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2802 | status = batch_list(self, iter); |
| 2803 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2804 | Py_DECREF(iter); |
| 2805 | } |
| 2806 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2807 | if (0) { |
| 2808 | error: |
| 2809 | status = -1; |
| 2810 | } |
| 2811 | |
| 2812 | if (self->fast && !fast_save_leave(self, obj)) |
| 2813 | status = -1; |
| 2814 | |
| 2815 | return status; |
| 2816 | } |
| 2817 | |
| 2818 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2819 | * MARK key value ... key value SETITEMS |
| 2820 | * opcode sequences. Calling code should have arranged to first create an |
| 2821 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2822 | * Returns 0 on success, <0 on error. |
| 2823 | * |
| 2824 | * This is very much like batch_list(). The difference between saving |
| 2825 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2826 | * the C level, though, that attempts to combine these routines were too |
| 2827 | * ugly to bear. |
| 2828 | */ |
| 2829 | static int |
| 2830 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2831 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2832 | PyObject *obj = NULL; |
| 2833 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2834 | int i, n; |
| 2835 | |
| 2836 | const char mark_op = MARK; |
| 2837 | const char setitem_op = SETITEM; |
| 2838 | const char setitems_op = SETITEMS; |
| 2839 | |
| 2840 | assert(iter != NULL); |
| 2841 | |
| 2842 | if (self->proto == 0) { |
| 2843 | /* SETITEMS isn't available; do one at a time. */ |
| 2844 | for (;;) { |
| 2845 | obj = PyIter_Next(iter); |
| 2846 | if (obj == NULL) { |
| 2847 | if (PyErr_Occurred()) |
| 2848 | return -1; |
| 2849 | break; |
| 2850 | } |
| 2851 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2852 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2853 | "iterator must return 2-tuples"); |
| 2854 | return -1; |
| 2855 | } |
| 2856 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2857 | if (i >= 0) |
| 2858 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2859 | Py_DECREF(obj); |
| 2860 | if (i < 0) |
| 2861 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2862 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2863 | return -1; |
| 2864 | } |
| 2865 | return 0; |
| 2866 | } |
| 2867 | |
| 2868 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2869 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2870 | /* Get first item */ |
| 2871 | firstitem = PyIter_Next(iter); |
| 2872 | if (firstitem == NULL) { |
| 2873 | if (PyErr_Occurred()) |
| 2874 | goto error; |
| 2875 | |
| 2876 | /* nothing more to add */ |
| 2877 | break; |
| 2878 | } |
| 2879 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2880 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2881 | "iterator must return 2-tuples"); |
| 2882 | goto error; |
| 2883 | } |
| 2884 | |
| 2885 | /* Try to get a second item */ |
| 2886 | obj = PyIter_Next(iter); |
| 2887 | if (obj == NULL) { |
| 2888 | if (PyErr_Occurred()) |
| 2889 | goto error; |
| 2890 | |
| 2891 | /* Only one item to write */ |
| 2892 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2893 | goto error; |
| 2894 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2895 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2896 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2897 | goto error; |
| 2898 | Py_CLEAR(firstitem); |
| 2899 | break; |
| 2900 | } |
| 2901 | |
| 2902 | /* More than one item to write */ |
| 2903 | |
| 2904 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2905 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2906 | goto error; |
| 2907 | |
| 2908 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2909 | goto error; |
| 2910 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2911 | goto error; |
| 2912 | Py_CLEAR(firstitem); |
| 2913 | n = 1; |
| 2914 | |
| 2915 | /* Fetch and save up to BATCHSIZE items */ |
| 2916 | while (obj) { |
| 2917 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2918 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2919 | "iterator must return 2-tuples"); |
| 2920 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2921 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2922 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2923 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2924 | goto error; |
| 2925 | Py_CLEAR(obj); |
| 2926 | n += 1; |
| 2927 | |
| 2928 | if (n == BATCHSIZE) |
| 2929 | break; |
| 2930 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2931 | obj = PyIter_Next(iter); |
| 2932 | if (obj == NULL) { |
| 2933 | if (PyErr_Occurred()) |
| 2934 | goto error; |
| 2935 | break; |
| 2936 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2937 | } |
| 2938 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2939 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2940 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2941 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2942 | } while (n == BATCHSIZE); |
| 2943 | return 0; |
| 2944 | |
| 2945 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2946 | Py_XDECREF(firstitem); |
| 2947 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2948 | return -1; |
| 2949 | } |
| 2950 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2951 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2952 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2953 | * MARK key value ... key value SETITEMS |
| 2954 | * opcode sequences. Calling code should have arranged to first create an |
| 2955 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2956 | * Returns 0 on success, -1 on error. |
| 2957 | * |
| 2958 | * Note that this currently doesn't work for protocol 0. |
| 2959 | */ |
| 2960 | static int |
| 2961 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2962 | { |
| 2963 | PyObject *key = NULL, *value = NULL; |
| 2964 | int i; |
| 2965 | Py_ssize_t dict_size, ppos = 0; |
| 2966 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2967 | const char mark_op = MARK; |
| 2968 | const char setitem_op = SETITEM; |
| 2969 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2970 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2971 | assert(obj != NULL && PyDict_CheckExact(obj)); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2972 | assert(self->proto > 0); |
| 2973 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2974 | dict_size = PyDict_GET_SIZE(obj); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2975 | |
| 2976 | /* Special-case len(d) == 1 to save space. */ |
| 2977 | if (dict_size == 1) { |
| 2978 | PyDict_Next(obj, &ppos, &key, &value); |
| 2979 | if (save(self, key, 0) < 0) |
| 2980 | return -1; |
| 2981 | if (save(self, value, 0) < 0) |
| 2982 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2983 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2984 | return -1; |
| 2985 | return 0; |
| 2986 | } |
| 2987 | |
| 2988 | /* Write in batches of BATCHSIZE. */ |
| 2989 | do { |
| 2990 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2991 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2992 | return -1; |
| 2993 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2994 | if (save(self, key, 0) < 0) |
| 2995 | return -1; |
| 2996 | if (save(self, value, 0) < 0) |
| 2997 | return -1; |
| 2998 | if (++i == BATCHSIZE) |
| 2999 | break; |
| 3000 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3001 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3002 | return -1; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3003 | if (PyDict_GET_SIZE(obj) != dict_size) { |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3004 | PyErr_Format( |
| 3005 | PyExc_RuntimeError, |
| 3006 | "dictionary changed size during iteration"); |
| 3007 | return -1; |
| 3008 | } |
| 3009 | |
| 3010 | } while (i == BATCHSIZE); |
| 3011 | return 0; |
| 3012 | } |
| 3013 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3014 | static int |
| 3015 | save_dict(PicklerObject *self, PyObject *obj) |
| 3016 | { |
| 3017 | PyObject *items, *iter; |
| 3018 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3019 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3020 | int status = 0; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3021 | assert(PyDict_Check(obj)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3022 | |
| 3023 | if (self->fast && !fast_save_enter(self, obj)) |
| 3024 | goto error; |
| 3025 | |
| 3026 | /* Create an empty dict. */ |
| 3027 | if (self->bin) { |
| 3028 | header[0] = EMPTY_DICT; |
| 3029 | len = 1; |
| 3030 | } |
| 3031 | else { |
| 3032 | header[0] = MARK; |
| 3033 | header[1] = DICT; |
| 3034 | len = 2; |
| 3035 | } |
| 3036 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3037 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3038 | goto error; |
| 3039 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3040 | if (memo_put(self, obj) < 0) |
| 3041 | goto error; |
| 3042 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3043 | if (PyDict_GET_SIZE(obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3044 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3045 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 3046 | /* We can take certain shortcuts if we know this is a dict and |
| 3047 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3048 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3049 | goto error; |
| 3050 | status = batch_dict_exact(self, obj); |
| 3051 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3052 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3053 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3054 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3055 | items = _PyObject_CallMethodId(obj, &PyId_items, NULL); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3056 | if (items == NULL) |
| 3057 | goto error; |
| 3058 | iter = PyObject_GetIter(items); |
| 3059 | Py_DECREF(items); |
| 3060 | if (iter == NULL) |
| 3061 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3062 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3063 | Py_DECREF(iter); |
| 3064 | goto error; |
| 3065 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3066 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3067 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3068 | Py_DECREF(iter); |
| 3069 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3070 | } |
| 3071 | |
| 3072 | if (0) { |
| 3073 | error: |
| 3074 | status = -1; |
| 3075 | } |
| 3076 | |
| 3077 | if (self->fast && !fast_save_leave(self, obj)) |
| 3078 | status = -1; |
| 3079 | |
| 3080 | return status; |
| 3081 | } |
| 3082 | |
| 3083 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3084 | save_set(PicklerObject *self, PyObject *obj) |
| 3085 | { |
| 3086 | PyObject *item; |
| 3087 | int i; |
| 3088 | Py_ssize_t set_size, ppos = 0; |
| 3089 | Py_hash_t hash; |
| 3090 | |
| 3091 | const char empty_set_op = EMPTY_SET; |
| 3092 | const char mark_op = MARK; |
| 3093 | const char additems_op = ADDITEMS; |
| 3094 | |
| 3095 | if (self->proto < 4) { |
| 3096 | PyObject *items; |
| 3097 | PyObject *reduce_value; |
| 3098 | int status; |
| 3099 | |
| 3100 | items = PySequence_List(obj); |
| 3101 | if (items == NULL) { |
| 3102 | return -1; |
| 3103 | } |
| 3104 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 3105 | Py_DECREF(items); |
| 3106 | if (reduce_value == NULL) { |
| 3107 | return -1; |
| 3108 | } |
| 3109 | /* save_reduce() will memoize the object automatically. */ |
| 3110 | status = save_reduce(self, reduce_value, obj); |
| 3111 | Py_DECREF(reduce_value); |
| 3112 | return status; |
| 3113 | } |
| 3114 | |
| 3115 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 3116 | return -1; |
| 3117 | |
| 3118 | if (memo_put(self, obj) < 0) |
| 3119 | return -1; |
| 3120 | |
| 3121 | set_size = PySet_GET_SIZE(obj); |
| 3122 | if (set_size == 0) |
| 3123 | return 0; /* nothing to do */ |
| 3124 | |
| 3125 | /* Write in batches of BATCHSIZE. */ |
| 3126 | do { |
| 3127 | i = 0; |
| 3128 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3129 | return -1; |
| 3130 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 3131 | if (save(self, item, 0) < 0) |
| 3132 | return -1; |
| 3133 | if (++i == BATCHSIZE) |
| 3134 | break; |
| 3135 | } |
| 3136 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 3137 | return -1; |
| 3138 | if (PySet_GET_SIZE(obj) != set_size) { |
| 3139 | PyErr_Format( |
| 3140 | PyExc_RuntimeError, |
| 3141 | "set changed size during iteration"); |
| 3142 | return -1; |
| 3143 | } |
| 3144 | } while (i == BATCHSIZE); |
| 3145 | |
| 3146 | return 0; |
| 3147 | } |
| 3148 | |
| 3149 | static int |
| 3150 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 3151 | { |
| 3152 | PyObject *iter; |
| 3153 | |
| 3154 | const char mark_op = MARK; |
| 3155 | const char frozenset_op = FROZENSET; |
| 3156 | |
| 3157 | if (self->fast && !fast_save_enter(self, obj)) |
| 3158 | return -1; |
| 3159 | |
| 3160 | if (self->proto < 4) { |
| 3161 | PyObject *items; |
| 3162 | PyObject *reduce_value; |
| 3163 | int status; |
| 3164 | |
| 3165 | items = PySequence_List(obj); |
| 3166 | if (items == NULL) { |
| 3167 | return -1; |
| 3168 | } |
| 3169 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3170 | items); |
| 3171 | Py_DECREF(items); |
| 3172 | if (reduce_value == NULL) { |
| 3173 | return -1; |
| 3174 | } |
| 3175 | /* save_reduce() will memoize the object automatically. */ |
| 3176 | status = save_reduce(self, reduce_value, obj); |
| 3177 | Py_DECREF(reduce_value); |
| 3178 | return status; |
| 3179 | } |
| 3180 | |
| 3181 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3182 | return -1; |
| 3183 | |
| 3184 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3185 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3186 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3187 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3188 | for (;;) { |
| 3189 | PyObject *item; |
| 3190 | |
| 3191 | item = PyIter_Next(iter); |
| 3192 | if (item == NULL) { |
| 3193 | if (PyErr_Occurred()) { |
| 3194 | Py_DECREF(iter); |
| 3195 | return -1; |
| 3196 | } |
| 3197 | break; |
| 3198 | } |
| 3199 | if (save(self, item, 0) < 0) { |
| 3200 | Py_DECREF(item); |
| 3201 | Py_DECREF(iter); |
| 3202 | return -1; |
| 3203 | } |
| 3204 | Py_DECREF(item); |
| 3205 | } |
| 3206 | Py_DECREF(iter); |
| 3207 | |
| 3208 | /* If the object is already in the memo, this means it is |
| 3209 | recursive. In this case, throw away everything we put on the |
| 3210 | stack, and fetch the object back from the memo. */ |
| 3211 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3212 | const char pop_mark_op = POP_MARK; |
| 3213 | |
| 3214 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3215 | return -1; |
| 3216 | if (memo_get(self, obj) < 0) |
| 3217 | return -1; |
| 3218 | return 0; |
| 3219 | } |
| 3220 | |
| 3221 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3222 | return -1; |
| 3223 | if (memo_put(self, obj) < 0) |
| 3224 | return -1; |
| 3225 | |
| 3226 | return 0; |
| 3227 | } |
| 3228 | |
| 3229 | static int |
| 3230 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3231 | { |
| 3232 | PyObject *key; |
| 3233 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3234 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3235 | |
| 3236 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3237 | if (key == NULL) |
| 3238 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3239 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3240 | Py_DECREF(key); |
| 3241 | if (item) { |
| 3242 | PyObject *fixed_module_name; |
| 3243 | PyObject *fixed_global_name; |
| 3244 | |
| 3245 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3246 | PyErr_Format(PyExc_RuntimeError, |
| 3247 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3248 | "should be 2-tuples, not %.200s", |
| 3249 | Py_TYPE(item)->tp_name); |
| 3250 | return -1; |
| 3251 | } |
| 3252 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3253 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3254 | if (!PyUnicode_Check(fixed_module_name) || |
| 3255 | !PyUnicode_Check(fixed_global_name)) { |
| 3256 | PyErr_Format(PyExc_RuntimeError, |
| 3257 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3258 | "should be pairs of str, not (%.200s, %.200s)", |
| 3259 | Py_TYPE(fixed_module_name)->tp_name, |
| 3260 | Py_TYPE(fixed_global_name)->tp_name); |
| 3261 | return -1; |
| 3262 | } |
| 3263 | |
| 3264 | Py_CLEAR(*module_name); |
| 3265 | Py_CLEAR(*global_name); |
| 3266 | Py_INCREF(fixed_module_name); |
| 3267 | Py_INCREF(fixed_global_name); |
| 3268 | *module_name = fixed_module_name; |
| 3269 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3270 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3271 | } |
| 3272 | else if (PyErr_Occurred()) { |
| 3273 | return -1; |
| 3274 | } |
| 3275 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3276 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3277 | if (item) { |
| 3278 | if (!PyUnicode_Check(item)) { |
| 3279 | PyErr_Format(PyExc_RuntimeError, |
| 3280 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3281 | "should be strings, not %.200s", |
| 3282 | Py_TYPE(item)->tp_name); |
| 3283 | return -1; |
| 3284 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3285 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3286 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3287 | } |
| 3288 | else if (PyErr_Occurred()) { |
| 3289 | return -1; |
| 3290 | } |
| 3291 | |
| 3292 | return 0; |
| 3293 | } |
| 3294 | |
| 3295 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3296 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3297 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3298 | PyObject *global_name = NULL; |
| 3299 | PyObject *module_name = NULL; |
| 3300 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3301 | PyObject *parent = NULL; |
| 3302 | PyObject *dotted_path = NULL; |
| 3303 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3304 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3305 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3306 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3307 | _Py_IDENTIFIER(__name__); |
| 3308 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3309 | |
| 3310 | const char global_op = GLOBAL; |
| 3311 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3312 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3313 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3314 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3315 | } |
| 3316 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3317 | if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0) |
| 3318 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3319 | if (global_name == NULL) { |
| 3320 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3321 | if (global_name == NULL) |
| 3322 | goto error; |
| 3323 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3324 | } |
| 3325 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3326 | dotted_path = get_dotted_path(module, global_name); |
| 3327 | if (dotted_path == NULL) |
| 3328 | goto error; |
| 3329 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3330 | if (module_name == NULL) |
| 3331 | goto error; |
| 3332 | |
| 3333 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3334 | relative imports. |
| 3335 | |
| 3336 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3337 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3338 | custom import functions (IMHO, this would be a nice security |
| 3339 | feature). The import C API would need to be extended to support the |
| 3340 | extra parameters of __import__ to fix that. */ |
| 3341 | module = PyImport_Import(module_name); |
| 3342 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3343 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3344 | "Can't pickle %R: import of module %R failed", |
| 3345 | obj, module_name); |
| 3346 | goto error; |
| 3347 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3348 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3349 | Py_INCREF(lastname); |
| 3350 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3351 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3352 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3353 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3354 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3355 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3356 | goto error; |
| 3357 | } |
| 3358 | if (cls != obj) { |
| 3359 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3360 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3361 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3362 | obj, module_name, global_name); |
| 3363 | goto error; |
| 3364 | } |
| 3365 | Py_DECREF(cls); |
| 3366 | |
| 3367 | if (self->proto >= 2) { |
| 3368 | /* See whether this is in the extension registry, and if |
| 3369 | * so generate an EXT opcode. |
| 3370 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3371 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3372 | PyObject *code_obj; /* extension code as Python object */ |
| 3373 | long code; /* extension code as C value */ |
| 3374 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3375 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3376 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3377 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3378 | if (extension_key == NULL) { |
| 3379 | goto error; |
| 3380 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3381 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3382 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3383 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3384 | /* The object is not registered in the extension registry. |
| 3385 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3386 | if (code_obj == NULL) { |
| 3387 | if (PyErr_Occurred()) { |
| 3388 | goto error; |
| 3389 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3390 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3391 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3392 | |
| 3393 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3394 | of the value returned by the extension_registry. It should for |
| 3395 | consistency. */ |
| 3396 | |
| 3397 | /* Verify code_obj has the right type and value. */ |
| 3398 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3399 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3400 | "Can't pickle %R: extension code %R isn't an integer", |
| 3401 | obj, code_obj); |
| 3402 | goto error; |
| 3403 | } |
| 3404 | code = PyLong_AS_LONG(code_obj); |
| 3405 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3406 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3407 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3408 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3409 | goto error; |
| 3410 | } |
| 3411 | |
| 3412 | /* Generate an EXT opcode. */ |
| 3413 | if (code <= 0xff) { |
| 3414 | pdata[0] = EXT1; |
| 3415 | pdata[1] = (unsigned char)code; |
| 3416 | n = 2; |
| 3417 | } |
| 3418 | else if (code <= 0xffff) { |
| 3419 | pdata[0] = EXT2; |
| 3420 | pdata[1] = (unsigned char)(code & 0xff); |
| 3421 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3422 | n = 3; |
| 3423 | } |
| 3424 | else { |
| 3425 | pdata[0] = EXT4; |
| 3426 | pdata[1] = (unsigned char)(code & 0xff); |
| 3427 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3428 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3429 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3430 | n = 5; |
| 3431 | } |
| 3432 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3433 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3434 | goto error; |
| 3435 | } |
| 3436 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3437 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3438 | if (parent == module) { |
| 3439 | Py_INCREF(lastname); |
| 3440 | Py_DECREF(global_name); |
| 3441 | global_name = lastname; |
| 3442 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3443 | if (self->proto >= 4) { |
| 3444 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3445 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3446 | if (save(self, module_name, 0) < 0) |
| 3447 | goto error; |
| 3448 | if (save(self, global_name, 0) < 0) |
| 3449 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3450 | |
| 3451 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3452 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3453 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3454 | else if (parent != module) { |
| 3455 | PickleState *st = _Pickle_GetGlobalState(); |
| 3456 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3457 | st->getattr, parent, lastname); |
Miss Islington (bot) | 3152bc3 | 2018-08-22 01:54:33 -0400 | [diff] [blame] | 3458 | if (reduce_value == NULL) |
| 3459 | goto error; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3460 | status = save_reduce(self, reduce_value, NULL); |
| 3461 | Py_DECREF(reduce_value); |
| 3462 | if (status < 0) |
| 3463 | goto error; |
| 3464 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3465 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3466 | /* Generate a normal global opcode if we are using a pickle |
| 3467 | protocol < 4, or if the object is not registered in the |
| 3468 | extension registry. */ |
| 3469 | PyObject *encoded; |
| 3470 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3471 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3472 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3473 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3474 | |
| 3475 | /* For protocol < 3 and if the user didn't request against doing |
| 3476 | so, we convert module names to the old 2.x module names. */ |
| 3477 | if (self->proto < 3 && self->fix_imports) { |
| 3478 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3479 | goto error; |
| 3480 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3481 | } |
| 3482 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3483 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3484 | both the module name and the global name using UTF-8. We do so |
| 3485 | only when we are using the pickle protocol newer than version |
| 3486 | 3. This is to ensure compatibility with older Unpickler running |
| 3487 | on Python 2.x. */ |
| 3488 | if (self->proto == 3) { |
| 3489 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3490 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3491 | else { |
| 3492 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3493 | } |
| 3494 | encoded = unicode_encoder(module_name); |
| 3495 | if (encoded == NULL) { |
| 3496 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3497 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3498 | "can't pickle module identifier '%S' using " |
| 3499 | "pickle protocol %i", |
| 3500 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3501 | goto error; |
| 3502 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3503 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3504 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3505 | Py_DECREF(encoded); |
| 3506 | goto error; |
| 3507 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3508 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3509 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3510 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3511 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3512 | /* Save the name of the module. */ |
| 3513 | encoded = unicode_encoder(global_name); |
| 3514 | if (encoded == NULL) { |
| 3515 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3516 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3517 | "can't pickle global identifier '%S' using " |
| 3518 | "pickle protocol %i", |
| 3519 | global_name, self->proto); |
| 3520 | goto error; |
| 3521 | } |
| 3522 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3523 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3524 | Py_DECREF(encoded); |
| 3525 | goto error; |
| 3526 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3527 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3528 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3529 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3530 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3531 | /* Memoize the object. */ |
| 3532 | if (memo_put(self, obj) < 0) |
| 3533 | goto error; |
| 3534 | } |
| 3535 | |
| 3536 | if (0) { |
| 3537 | error: |
| 3538 | status = -1; |
| 3539 | } |
| 3540 | Py_XDECREF(module_name); |
| 3541 | Py_XDECREF(global_name); |
| 3542 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3543 | Py_XDECREF(parent); |
| 3544 | Py_XDECREF(dotted_path); |
| 3545 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3546 | |
| 3547 | return status; |
| 3548 | } |
| 3549 | |
| 3550 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3551 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3552 | { |
| 3553 | PyObject *reduce_value; |
| 3554 | int status; |
| 3555 | |
| 3556 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3557 | if (reduce_value == NULL) { |
| 3558 | return -1; |
| 3559 | } |
| 3560 | status = save_reduce(self, reduce_value, obj); |
| 3561 | Py_DECREF(reduce_value); |
| 3562 | return status; |
| 3563 | } |
| 3564 | |
| 3565 | static int |
| 3566 | save_type(PicklerObject *self, PyObject *obj) |
| 3567 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3568 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3569 | return save_singleton_type(self, obj, Py_None); |
| 3570 | } |
| 3571 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3572 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3573 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3574 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3575 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3576 | } |
| 3577 | return save_global(self, obj, NULL); |
| 3578 | } |
| 3579 | |
| 3580 | static int |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3581 | save_pers(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3582 | { |
| 3583 | PyObject *pid = NULL; |
| 3584 | int status = 0; |
| 3585 | |
| 3586 | const char persid_op = PERSID; |
| 3587 | const char binpersid_op = BINPERSID; |
| 3588 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3589 | pid = call_method(self->pers_func, self->pers_func_self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3590 | if (pid == NULL) |
| 3591 | return -1; |
| 3592 | |
| 3593 | if (pid != Py_None) { |
| 3594 | if (self->bin) { |
| 3595 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3596 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3597 | goto error; |
| 3598 | } |
| 3599 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3600 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3601 | |
| 3602 | pid_str = PyObject_Str(pid); |
| 3603 | if (pid_str == NULL) |
| 3604 | goto error; |
| 3605 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3606 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3607 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3608 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3609 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3610 | "persistent IDs in protocol 0 must be " |
| 3611 | "ASCII strings"); |
| 3612 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3613 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3614 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3615 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3616 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3617 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3618 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3619 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3620 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3621 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3622 | } |
| 3623 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3624 | } |
| 3625 | status = 1; |
| 3626 | } |
| 3627 | |
| 3628 | if (0) { |
| 3629 | error: |
| 3630 | status = -1; |
| 3631 | } |
| 3632 | Py_XDECREF(pid); |
| 3633 | |
| 3634 | return status; |
| 3635 | } |
| 3636 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3637 | static PyObject * |
| 3638 | get_class(PyObject *obj) |
| 3639 | { |
| 3640 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3641 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3642 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3643 | if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) { |
| 3644 | cls = (PyObject *) Py_TYPE(obj); |
| 3645 | Py_INCREF(cls); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3646 | } |
| 3647 | return cls; |
| 3648 | } |
| 3649 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3650 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3651 | * appropriate __reduce__ method for obj. |
| 3652 | */ |
| 3653 | static int |
| 3654 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3655 | { |
| 3656 | PyObject *callable; |
| 3657 | PyObject *argtup; |
| 3658 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3659 | PyObject *listitems = Py_None; |
| 3660 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3661 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3662 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3663 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3664 | |
| 3665 | const char reduce_op = REDUCE; |
| 3666 | const char build_op = BUILD; |
| 3667 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3668 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3669 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3670 | size = PyTuple_Size(args); |
| 3671 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3672 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3673 | "__reduce__ must contain 2 through 5 elements"); |
| 3674 | return -1; |
| 3675 | } |
| 3676 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3677 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3678 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3679 | return -1; |
| 3680 | |
| 3681 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3682 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3683 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3684 | return -1; |
| 3685 | } |
| 3686 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3687 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3688 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3689 | return -1; |
| 3690 | } |
| 3691 | |
| 3692 | if (state == Py_None) |
| 3693 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3694 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3695 | if (listitems == Py_None) |
| 3696 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3697 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3698 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3699 | "returned by __reduce__ must be an iterator, not %s", |
| 3700 | Py_TYPE(listitems)->tp_name); |
| 3701 | return -1; |
| 3702 | } |
| 3703 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3704 | if (dictitems == Py_None) |
| 3705 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3706 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3707 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3708 | "returned by __reduce__ must be an iterator, not %s", |
| 3709 | Py_TYPE(dictitems)->tp_name); |
| 3710 | return -1; |
| 3711 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3712 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3713 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3714 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3715 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3716 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3717 | if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) { |
| 3718 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3719 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3720 | if (name != NULL && PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3721 | _Py_IDENTIFIER(__newobj_ex__); |
Serhiy Storchaka | f0f35a6 | 2017-01-09 10:09:43 +0200 | [diff] [blame] | 3722 | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
| 3723 | name, &PyId___newobj_ex__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3724 | if (!use_newobj_ex) { |
| 3725 | _Py_IDENTIFIER(__newobj__); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 3726 | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3727 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3728 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3729 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3730 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3731 | |
| 3732 | if (use_newobj_ex) { |
| 3733 | PyObject *cls; |
| 3734 | PyObject *args; |
| 3735 | PyObject *kwargs; |
| 3736 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3737 | if (PyTuple_GET_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3738 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3739 | "length of the NEWOBJ_EX argument tuple must be " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3740 | "exactly 3, not %zd", PyTuple_GET_SIZE(argtup)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3741 | return -1; |
| 3742 | } |
| 3743 | |
| 3744 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3745 | if (!PyType_Check(cls)) { |
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 | "first item from NEWOBJ_EX argument tuple must " |
| 3748 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3749 | return -1; |
| 3750 | } |
| 3751 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3752 | if (!PyTuple_Check(args)) { |
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 | "second item from NEWOBJ_EX argument tuple must " |
| 3755 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3756 | return -1; |
| 3757 | } |
| 3758 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3759 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3760 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3761 | "third item from NEWOBJ_EX argument tuple must " |
| 3762 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3763 | return -1; |
| 3764 | } |
| 3765 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3766 | if (self->proto >= 4) { |
| 3767 | if (save(self, cls, 0) < 0 || |
| 3768 | save(self, args, 0) < 0 || |
| 3769 | save(self, kwargs, 0) < 0 || |
| 3770 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3771 | return -1; |
| 3772 | } |
| 3773 | } |
| 3774 | else { |
| 3775 | PyObject *newargs; |
| 3776 | PyObject *cls_new; |
| 3777 | Py_ssize_t i; |
| 3778 | _Py_IDENTIFIER(__new__); |
| 3779 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3780 | newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3781 | if (newargs == NULL) |
| 3782 | return -1; |
| 3783 | |
| 3784 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 3785 | if (cls_new == NULL) { |
| 3786 | Py_DECREF(newargs); |
| 3787 | return -1; |
| 3788 | } |
| 3789 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 3790 | Py_INCREF(cls); |
| 3791 | PyTuple_SET_ITEM(newargs, 1, cls); |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3792 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3793 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 3794 | Py_INCREF(item); |
| 3795 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 3796 | } |
| 3797 | |
| 3798 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 3799 | Py_DECREF(newargs); |
| 3800 | if (callable == NULL) |
| 3801 | return -1; |
| 3802 | |
| 3803 | newargs = PyTuple_New(0); |
| 3804 | if (newargs == NULL) { |
| 3805 | Py_DECREF(callable); |
| 3806 | return -1; |
| 3807 | } |
| 3808 | |
| 3809 | if (save(self, callable, 0) < 0 || |
| 3810 | save(self, newargs, 0) < 0 || |
| 3811 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 3812 | Py_DECREF(newargs); |
| 3813 | Py_DECREF(callable); |
| 3814 | return -1; |
| 3815 | } |
| 3816 | Py_DECREF(newargs); |
| 3817 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3818 | } |
| 3819 | } |
| 3820 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3821 | PyObject *cls; |
| 3822 | PyObject *newargtup; |
| 3823 | PyObject *obj_class; |
| 3824 | int p; |
| 3825 | |
| 3826 | /* Sanity checks. */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3827 | if (PyTuple_GET_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3828 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3829 | return -1; |
| 3830 | } |
| 3831 | |
| 3832 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3833 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3834 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3835 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3836 | return -1; |
| 3837 | } |
| 3838 | |
| 3839 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3840 | obj_class = get_class(obj); |
Miss Islington (bot) | e2f376f | 2018-12-05 11:35:41 -0800 | [diff] [blame^] | 3841 | if (obj_class == NULL) { |
| 3842 | return -1; |
| 3843 | } |
| 3844 | p = obj_class != cls; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3845 | Py_DECREF(obj_class); |
| 3846 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3847 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3848 | "__newobj__ args has the wrong class"); |
| 3849 | return -1; |
| 3850 | } |
| 3851 | } |
| 3852 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3853 | what happen if the value returned by the __reduce__() method of |
| 3854 | some extension type contains another object of the same type. Ouch! |
| 3855 | |
| 3856 | Here is a quick example, that I ran into, to illustrate what I |
| 3857 | mean: |
| 3858 | |
| 3859 | >>> import pickle, copyreg |
| 3860 | >>> copyreg.dispatch_table.pop(complex) |
| 3861 | >>> pickle.dumps(1+2j) |
| 3862 | Traceback (most recent call last): |
| 3863 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3864 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3865 | |
| 3866 | Removing the complex class from copyreg.dispatch_table made the |
| 3867 | __reduce_ex__() method emit another complex object: |
| 3868 | |
| 3869 | >>> (1+1j).__reduce_ex__(2) |
| 3870 | (<function __newobj__ at 0xb7b71c3c>, |
| 3871 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3872 | |
| 3873 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3874 | ensued. Of course, the bug was in the complex class which had a |
| 3875 | broken __getnewargs__() that emitted another complex object. But, |
| 3876 | the point, here, is it is quite easy to end up with a broken reduce |
| 3877 | function. */ |
| 3878 | |
| 3879 | /* Save the class and its __new__ arguments. */ |
| 3880 | if (save(self, cls, 0) < 0) |
| 3881 | return -1; |
| 3882 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3883 | newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3884 | if (newargtup == NULL) |
| 3885 | return -1; |
| 3886 | |
| 3887 | p = save(self, newargtup, 0); |
| 3888 | Py_DECREF(newargtup); |
| 3889 | if (p < 0) |
| 3890 | return -1; |
| 3891 | |
| 3892 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3893 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3894 | return -1; |
| 3895 | } |
| 3896 | else { /* Not using NEWOBJ. */ |
| 3897 | if (save(self, callable, 0) < 0 || |
| 3898 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3899 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3900 | return -1; |
| 3901 | } |
| 3902 | |
| 3903 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3904 | the caller do not want to memoize the object. Not particularly useful, |
| 3905 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3906 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3907 | if (obj != NULL) { |
| 3908 | /* If the object is already in the memo, this means it is |
| 3909 | recursive. In this case, throw away everything we put on the |
| 3910 | stack, and fetch the object back from the memo. */ |
| 3911 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3912 | const char pop_op = POP; |
| 3913 | |
| 3914 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3915 | return -1; |
| 3916 | if (memo_get(self, obj) < 0) |
| 3917 | return -1; |
| 3918 | |
| 3919 | return 0; |
| 3920 | } |
| 3921 | else if (memo_put(self, obj) < 0) |
| 3922 | return -1; |
| 3923 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3924 | |
| 3925 | if (listitems && batch_list(self, listitems) < 0) |
| 3926 | return -1; |
| 3927 | |
| 3928 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3929 | return -1; |
| 3930 | |
| 3931 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3932 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3933 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3934 | return -1; |
| 3935 | } |
| 3936 | |
| 3937 | return 0; |
| 3938 | } |
| 3939 | |
| 3940 | static int |
| 3941 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3942 | { |
| 3943 | PyTypeObject *type; |
| 3944 | PyObject *reduce_func = NULL; |
| 3945 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3946 | int status = 0; |
| 3947 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3948 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3949 | return -1; |
| 3950 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3951 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3952 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3953 | |
| 3954 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3955 | on its returned object. */ |
| 3956 | if (!pers_save && self->pers_func) { |
| 3957 | /* save_pers() returns: |
| 3958 | -1 to signal an error; |
| 3959 | 0 if it did nothing successfully; |
| 3960 | 1 if a persistent id was saved. |
| 3961 | */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3962 | if ((status = save_pers(self, obj)) != 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3963 | goto done; |
| 3964 | } |
| 3965 | |
| 3966 | type = Py_TYPE(obj); |
| 3967 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3968 | /* The old cPickle had an optimization that used switch-case statement |
| 3969 | dispatching on the first letter of the type name. This has was removed |
| 3970 | since benchmarks shown that this optimization was actually slowing |
| 3971 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3972 | |
| 3973 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3974 | |
| 3975 | if (obj == Py_None) { |
| 3976 | status = save_none(self, obj); |
| 3977 | goto done; |
| 3978 | } |
| 3979 | else if (obj == Py_False || obj == Py_True) { |
| 3980 | status = save_bool(self, obj); |
| 3981 | goto done; |
| 3982 | } |
| 3983 | else if (type == &PyLong_Type) { |
| 3984 | status = save_long(self, obj); |
| 3985 | goto done; |
| 3986 | } |
| 3987 | else if (type == &PyFloat_Type) { |
| 3988 | status = save_float(self, obj); |
| 3989 | goto done; |
| 3990 | } |
| 3991 | |
| 3992 | /* Check the memo to see if it has the object. If so, generate |
| 3993 | a GET (or BINGET) opcode, instead of pickling the object |
| 3994 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3995 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3996 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3997 | goto error; |
| 3998 | goto done; |
| 3999 | } |
| 4000 | |
| 4001 | if (type == &PyBytes_Type) { |
| 4002 | status = save_bytes(self, obj); |
| 4003 | goto done; |
| 4004 | } |
| 4005 | else if (type == &PyUnicode_Type) { |
| 4006 | status = save_unicode(self, obj); |
| 4007 | goto done; |
| 4008 | } |
| 4009 | else if (type == &PyDict_Type) { |
| 4010 | status = save_dict(self, obj); |
| 4011 | goto done; |
| 4012 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4013 | else if (type == &PySet_Type) { |
| 4014 | status = save_set(self, obj); |
| 4015 | goto done; |
| 4016 | } |
| 4017 | else if (type == &PyFrozenSet_Type) { |
| 4018 | status = save_frozenset(self, obj); |
| 4019 | goto done; |
| 4020 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4021 | else if (type == &PyList_Type) { |
| 4022 | status = save_list(self, obj); |
| 4023 | goto done; |
| 4024 | } |
| 4025 | else if (type == &PyTuple_Type) { |
| 4026 | status = save_tuple(self, obj); |
| 4027 | goto done; |
| 4028 | } |
| 4029 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 4030 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4031 | goto done; |
| 4032 | } |
| 4033 | else if (type == &PyFunction_Type) { |
| 4034 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 4035 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4036 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4037 | |
| 4038 | /* XXX: This part needs some unit tests. */ |
| 4039 | |
| 4040 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4041 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 4042 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4043 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4044 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4045 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4046 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 4047 | (PyObject *)type); |
| 4048 | if (reduce_func == NULL) { |
| 4049 | if (PyErr_Occurred()) { |
| 4050 | goto error; |
| 4051 | } |
| 4052 | } else { |
| 4053 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 4054 | Increase the reference count to be consistent with |
| 4055 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 4056 | Py_INCREF(reduce_func); |
| 4057 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4058 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4059 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 4060 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4061 | if (reduce_func == NULL) { |
| 4062 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4063 | PyErr_Clear(); |
| 4064 | else |
| 4065 | goto error; |
| 4066 | } |
| 4067 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4068 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4069 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4070 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4071 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 4072 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 4073 | status = save_global(self, obj, NULL); |
| 4074 | goto done; |
| 4075 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4076 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4077 | _Py_IDENTIFIER(__reduce__); |
| 4078 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4079 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4080 | |
| 4081 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 4082 | automatically defined as __reduce__. While this is convenient, this |
| 4083 | make it impossible to know which method was actually called. Of |
| 4084 | course, this is not a big deal. But still, it would be nice to let |
| 4085 | the user know which method was called when something go |
| 4086 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 4087 | don't actually have to check for a __reduce__ method. */ |
| 4088 | |
| 4089 | /* Check for a __reduce_ex__ method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4090 | if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) { |
| 4091 | goto error; |
| 4092 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4093 | if (reduce_func != NULL) { |
| 4094 | PyObject *proto; |
| 4095 | proto = PyLong_FromLong(self->proto); |
| 4096 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4097 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4098 | } |
| 4099 | } |
| 4100 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4101 | PickleState *st = _Pickle_GetGlobalState(); |
| 4102 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4103 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4104 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4105 | if (reduce_func != NULL) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4106 | reduce_value = _PyObject_CallNoArg(reduce_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4107 | } |
| 4108 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4109 | PyErr_Format(st->PicklingError, |
| 4110 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4111 | type->tp_name, obj); |
| 4112 | goto error; |
| 4113 | } |
| 4114 | } |
| 4115 | } |
| 4116 | |
| 4117 | if (reduce_value == NULL) |
| 4118 | goto error; |
| 4119 | |
| 4120 | if (PyUnicode_Check(reduce_value)) { |
| 4121 | status = save_global(self, obj, reduce_value); |
| 4122 | goto done; |
| 4123 | } |
| 4124 | |
| 4125 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4126 | PickleState *st = _Pickle_GetGlobalState(); |
| 4127 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4128 | "__reduce__ must return a string or tuple"); |
| 4129 | goto error; |
| 4130 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4131 | |
| 4132 | status = save_reduce(self, reduce_value, obj); |
| 4133 | |
| 4134 | if (0) { |
| 4135 | error: |
| 4136 | status = -1; |
| 4137 | } |
| 4138 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4139 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 4140 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4141 | Py_XDECREF(reduce_func); |
| 4142 | Py_XDECREF(reduce_value); |
| 4143 | |
| 4144 | return status; |
| 4145 | } |
| 4146 | |
| 4147 | static int |
| 4148 | dump(PicklerObject *self, PyObject *obj) |
| 4149 | { |
| 4150 | const char stop_op = STOP; |
| 4151 | |
| 4152 | if (self->proto >= 2) { |
| 4153 | char header[2]; |
| 4154 | |
| 4155 | header[0] = PROTO; |
| 4156 | assert(self->proto >= 0 && self->proto < 256); |
| 4157 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4158 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4159 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4160 | if (self->proto >= 4) |
| 4161 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | if (save(self, obj, 0) < 0 || |
Miss Islington (bot) | 74735e2 | 2018-04-03 14:35:11 -0700 | [diff] [blame] | 4165 | _Pickler_Write(self, &stop_op, 1) < 0 || |
| 4166 | _Pickler_CommitFrame(self) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4167 | return -1; |
Miss Islington (bot) | 74735e2 | 2018-04-03 14:35:11 -0700 | [diff] [blame] | 4168 | self->framing = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4169 | return 0; |
| 4170 | } |
| 4171 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4172 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4173 | |
| 4174 | _pickle.Pickler.clear_memo |
| 4175 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4176 | Clears the pickler's "memo". |
| 4177 | |
| 4178 | The memo is the data structure that remembers which objects the |
| 4179 | pickler has already seen, so that shared or recursive objects are |
| 4180 | pickled by reference and not by value. This method is useful when |
| 4181 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4182 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4183 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4184 | static PyObject * |
| 4185 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4186 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4187 | { |
| 4188 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4189 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4190 | |
| 4191 | Py_RETURN_NONE; |
| 4192 | } |
| 4193 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4194 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4195 | |
| 4196 | _pickle.Pickler.dump |
| 4197 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4198 | obj: object |
| 4199 | / |
| 4200 | |
| 4201 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4202 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4203 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4204 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4205 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4206 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4207 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4208 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4209 | Developers often forget to call __init__() in their subclasses, which |
| 4210 | would trigger a segfault without this check. */ |
| 4211 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4212 | PickleState *st = _Pickle_GetGlobalState(); |
| 4213 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4214 | "Pickler.__init__() was not called by %s.__init__()", |
| 4215 | Py_TYPE(self)->tp_name); |
| 4216 | return NULL; |
| 4217 | } |
| 4218 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4219 | if (_Pickler_ClearBuffer(self) < 0) |
| 4220 | return NULL; |
| 4221 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4222 | if (dump(self, obj) < 0) |
| 4223 | return NULL; |
| 4224 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4225 | if (_Pickler_FlushToFile(self) < 0) |
| 4226 | return NULL; |
| 4227 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4228 | Py_RETURN_NONE; |
| 4229 | } |
| 4230 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4231 | /*[clinic input] |
| 4232 | |
| 4233 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4234 | |
| 4235 | Returns size in memory, in bytes. |
| 4236 | [clinic start generated code]*/ |
| 4237 | |
| 4238 | static Py_ssize_t |
| 4239 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4240 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4241 | { |
| 4242 | Py_ssize_t res, s; |
| 4243 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4244 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4245 | if (self->memo != NULL) { |
| 4246 | res += sizeof(PyMemoTable); |
| 4247 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4248 | } |
| 4249 | if (self->output_buffer != NULL) { |
| 4250 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4251 | if (s == -1) |
| 4252 | return -1; |
| 4253 | res += s; |
| 4254 | } |
| 4255 | return res; |
| 4256 | } |
| 4257 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4258 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4259 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4260 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4261 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4262 | {NULL, NULL} /* sentinel */ |
| 4263 | }; |
| 4264 | |
| 4265 | static void |
| 4266 | Pickler_dealloc(PicklerObject *self) |
| 4267 | { |
| 4268 | PyObject_GC_UnTrack(self); |
| 4269 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4270 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4271 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4272 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4273 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4274 | Py_XDECREF(self->fast_memo); |
| 4275 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4276 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4277 | |
| 4278 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4279 | } |
| 4280 | |
| 4281 | static int |
| 4282 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4283 | { |
| 4284 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4285 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4286 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4287 | Py_VISIT(self->fast_memo); |
| 4288 | return 0; |
| 4289 | } |
| 4290 | |
| 4291 | static int |
| 4292 | Pickler_clear(PicklerObject *self) |
| 4293 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4294 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4295 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4296 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4297 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4298 | Py_CLEAR(self->fast_memo); |
| 4299 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4300 | if (self->memo != NULL) { |
| 4301 | PyMemoTable *memo = self->memo; |
| 4302 | self->memo = NULL; |
| 4303 | PyMemoTable_Del(memo); |
| 4304 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4305 | return 0; |
| 4306 | } |
| 4307 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4308 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4309 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4310 | |
| 4311 | _pickle.Pickler.__init__ |
| 4312 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4313 | file: object |
| 4314 | protocol: object = NULL |
| 4315 | fix_imports: bool = True |
| 4316 | |
| 4317 | This takes a binary file for writing a pickle data stream. |
| 4318 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4319 | The optional *protocol* argument tells the pickler to use the given |
| 4320 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4321 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4322 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4323 | Specifying a negative protocol version selects the highest protocol |
| 4324 | version supported. The higher the protocol used, the more recent the |
| 4325 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4326 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4327 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4328 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4329 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4330 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4331 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4332 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4333 | to map the new Python 3 names to the old module names used in Python |
| 4334 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4335 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4336 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4337 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4338 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4339 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4340 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4341 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4342 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4343 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4344 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4345 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4346 | if (self->write != NULL) |
| 4347 | (void)Pickler_clear(self); |
| 4348 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4349 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4350 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4351 | |
| 4352 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4353 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4354 | |
| 4355 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4356 | if (self->memo == NULL) { |
| 4357 | self->memo = PyMemoTable_New(); |
| 4358 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4359 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4360 | } |
| 4361 | self->output_len = 0; |
| 4362 | if (self->output_buffer == NULL) { |
| 4363 | self->max_output_len = WRITE_BUF_SIZE; |
| 4364 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4365 | self->max_output_len); |
| 4366 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4367 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4368 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4369 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4370 | self->fast = 0; |
| 4371 | self->fast_nesting = 0; |
| 4372 | self->fast_memo = NULL; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4373 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4374 | if (init_method_ref((PyObject *)self, &PyId_persistent_id, |
| 4375 | &self->pers_func, &self->pers_func_self) < 0) |
| 4376 | { |
| 4377 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4378 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4379 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4380 | if (_PyObject_LookupAttrId((PyObject *)self, |
| 4381 | &PyId_dispatch_table, &self->dispatch_table) < 0) { |
| 4382 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4383 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4384 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4385 | return 0; |
| 4386 | } |
| 4387 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4388 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4389 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4390 | * avoid breaking code like: |
| 4391 | * pickler.memo.clear() |
| 4392 | * and |
| 4393 | * pickler.memo = saved_memo |
| 4394 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4395 | * it. Note that we don't implement the entire mapping API here. This is |
| 4396 | * intentional, as these should be treated as black-box implementation details. |
| 4397 | */ |
| 4398 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4399 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4400 | _pickle.PicklerMemoProxy.clear |
| 4401 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4402 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4403 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4404 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4405 | static PyObject * |
| 4406 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4407 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4408 | { |
| 4409 | if (self->pickler->memo) |
| 4410 | PyMemoTable_Clear(self->pickler->memo); |
| 4411 | Py_RETURN_NONE; |
| 4412 | } |
| 4413 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4414 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4415 | _pickle.PicklerMemoProxy.copy |
| 4416 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4417 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4418 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4419 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4420 | static PyObject * |
| 4421 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4422 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4423 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4424 | PyMemoTable *memo; |
| 4425 | PyObject *new_memo = PyDict_New(); |
| 4426 | if (new_memo == NULL) |
| 4427 | return NULL; |
| 4428 | |
| 4429 | memo = self->pickler->memo; |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 4430 | for (size_t i = 0; i < memo->mt_allocated; ++i) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4431 | PyMemoEntry entry = memo->mt_table[i]; |
| 4432 | if (entry.me_key != NULL) { |
| 4433 | int status; |
| 4434 | PyObject *key, *value; |
| 4435 | |
| 4436 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4437 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4438 | |
| 4439 | if (key == NULL || value == NULL) { |
| 4440 | Py_XDECREF(key); |
| 4441 | Py_XDECREF(value); |
| 4442 | goto error; |
| 4443 | } |
| 4444 | status = PyDict_SetItem(new_memo, key, value); |
| 4445 | Py_DECREF(key); |
| 4446 | Py_DECREF(value); |
| 4447 | if (status < 0) |
| 4448 | goto error; |
| 4449 | } |
| 4450 | } |
| 4451 | return new_memo; |
| 4452 | |
| 4453 | error: |
| 4454 | Py_XDECREF(new_memo); |
| 4455 | return NULL; |
| 4456 | } |
| 4457 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4458 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4459 | _pickle.PicklerMemoProxy.__reduce__ |
| 4460 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4461 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4462 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4463 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4464 | static PyObject * |
| 4465 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4466 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4467 | { |
| 4468 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4469 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4470 | if (contents == NULL) |
| 4471 | return NULL; |
| 4472 | |
| 4473 | reduce_value = PyTuple_New(2); |
| 4474 | if (reduce_value == NULL) { |
| 4475 | Py_DECREF(contents); |
| 4476 | return NULL; |
| 4477 | } |
| 4478 | dict_args = PyTuple_New(1); |
| 4479 | if (dict_args == NULL) { |
| 4480 | Py_DECREF(contents); |
| 4481 | Py_DECREF(reduce_value); |
| 4482 | return NULL; |
| 4483 | } |
| 4484 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4485 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4486 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4487 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4488 | return reduce_value; |
| 4489 | } |
| 4490 | |
| 4491 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4492 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4493 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4494 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4495 | {NULL, NULL} /* sentinel */ |
| 4496 | }; |
| 4497 | |
| 4498 | static void |
| 4499 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4500 | { |
| 4501 | PyObject_GC_UnTrack(self); |
| 4502 | Py_XDECREF(self->pickler); |
| 4503 | PyObject_GC_Del((PyObject *)self); |
| 4504 | } |
| 4505 | |
| 4506 | static int |
| 4507 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4508 | visitproc visit, void *arg) |
| 4509 | { |
| 4510 | Py_VISIT(self->pickler); |
| 4511 | return 0; |
| 4512 | } |
| 4513 | |
| 4514 | static int |
| 4515 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4516 | { |
| 4517 | Py_CLEAR(self->pickler); |
| 4518 | return 0; |
| 4519 | } |
| 4520 | |
| 4521 | static PyTypeObject PicklerMemoProxyType = { |
| 4522 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4523 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4524 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4525 | 0, |
| 4526 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4527 | 0, /* tp_print */ |
| 4528 | 0, /* tp_getattr */ |
| 4529 | 0, /* tp_setattr */ |
| 4530 | 0, /* tp_compare */ |
| 4531 | 0, /* tp_repr */ |
| 4532 | 0, /* tp_as_number */ |
| 4533 | 0, /* tp_as_sequence */ |
| 4534 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4535 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4536 | 0, /* tp_call */ |
| 4537 | 0, /* tp_str */ |
| 4538 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4539 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4540 | 0, /* tp_as_buffer */ |
| 4541 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4542 | 0, /* tp_doc */ |
| 4543 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4544 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4545 | 0, /* tp_richcompare */ |
| 4546 | 0, /* tp_weaklistoffset */ |
| 4547 | 0, /* tp_iter */ |
| 4548 | 0, /* tp_iternext */ |
| 4549 | picklerproxy_methods, /* tp_methods */ |
| 4550 | }; |
| 4551 | |
| 4552 | static PyObject * |
| 4553 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4554 | { |
| 4555 | PicklerMemoProxyObject *self; |
| 4556 | |
| 4557 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4558 | if (self == NULL) |
| 4559 | return NULL; |
| 4560 | Py_INCREF(pickler); |
| 4561 | self->pickler = pickler; |
| 4562 | PyObject_GC_Track(self); |
| 4563 | return (PyObject *)self; |
| 4564 | } |
| 4565 | |
| 4566 | /*****************************************************************************/ |
| 4567 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4568 | static PyObject * |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 4569 | Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4570 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4571 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4572 | } |
| 4573 | |
| 4574 | static int |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 4575 | Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4576 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4577 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4578 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4579 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4580 | PyErr_SetString(PyExc_TypeError, |
| 4581 | "attribute deletion is not supported"); |
| 4582 | return -1; |
| 4583 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4584 | |
| 4585 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4586 | PicklerObject *pickler = |
| 4587 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4588 | |
| 4589 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4590 | if (new_memo == NULL) |
| 4591 | return -1; |
| 4592 | } |
| 4593 | else if (PyDict_Check(obj)) { |
| 4594 | Py_ssize_t i = 0; |
| 4595 | PyObject *key, *value; |
| 4596 | |
| 4597 | new_memo = PyMemoTable_New(); |
| 4598 | if (new_memo == NULL) |
| 4599 | return -1; |
| 4600 | |
| 4601 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4602 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4603 | PyObject *memo_obj; |
| 4604 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4605 | if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4606 | PyErr_SetString(PyExc_TypeError, |
| 4607 | "'memo' values must be 2-item tuples"); |
| 4608 | goto error; |
| 4609 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4610 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4611 | if (memo_id == -1 && PyErr_Occurred()) |
| 4612 | goto error; |
| 4613 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4614 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4615 | goto error; |
| 4616 | } |
| 4617 | } |
| 4618 | else { |
| 4619 | PyErr_Format(PyExc_TypeError, |
Miss Islington (bot) | 7beb8c5 | 2018-11-05 06:52:58 -0800 | [diff] [blame] | 4620 | "'memo' attribute must be a PicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4621 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4622 | return -1; |
| 4623 | } |
| 4624 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4625 | PyMemoTable_Del(self->memo); |
| 4626 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4627 | |
| 4628 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4629 | |
| 4630 | error: |
| 4631 | if (new_memo) |
| 4632 | PyMemoTable_Del(new_memo); |
| 4633 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4634 | } |
| 4635 | |
| 4636 | static PyObject * |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 4637 | Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4638 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4639 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4640 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4641 | return NULL; |
| 4642 | } |
| 4643 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | static int |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 4647 | Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4648 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4649 | if (value == NULL) { |
| 4650 | PyErr_SetString(PyExc_TypeError, |
| 4651 | "attribute deletion is not supported"); |
| 4652 | return -1; |
| 4653 | } |
| 4654 | if (!PyCallable_Check(value)) { |
| 4655 | PyErr_SetString(PyExc_TypeError, |
| 4656 | "persistent_id must be a callable taking one argument"); |
| 4657 | return -1; |
| 4658 | } |
| 4659 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4660 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4661 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 4662 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4663 | |
| 4664 | return 0; |
| 4665 | } |
| 4666 | |
| 4667 | static PyMemberDef Pickler_members[] = { |
| 4668 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4669 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4670 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4671 | {NULL} |
| 4672 | }; |
| 4673 | |
| 4674 | static PyGetSetDef Pickler_getsets[] = { |
| 4675 | {"memo", (getter)Pickler_get_memo, |
| 4676 | (setter)Pickler_set_memo}, |
| 4677 | {"persistent_id", (getter)Pickler_get_persid, |
| 4678 | (setter)Pickler_set_persid}, |
| 4679 | {NULL} |
| 4680 | }; |
| 4681 | |
| 4682 | static PyTypeObject Pickler_Type = { |
| 4683 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4684 | "_pickle.Pickler" , /*tp_name*/ |
| 4685 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4686 | 0, /*tp_itemsize*/ |
| 4687 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4688 | 0, /*tp_print*/ |
| 4689 | 0, /*tp_getattr*/ |
| 4690 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4691 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4692 | 0, /*tp_repr*/ |
| 4693 | 0, /*tp_as_number*/ |
| 4694 | 0, /*tp_as_sequence*/ |
| 4695 | 0, /*tp_as_mapping*/ |
| 4696 | 0, /*tp_hash*/ |
| 4697 | 0, /*tp_call*/ |
| 4698 | 0, /*tp_str*/ |
| 4699 | 0, /*tp_getattro*/ |
| 4700 | 0, /*tp_setattro*/ |
| 4701 | 0, /*tp_as_buffer*/ |
| 4702 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4703 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4704 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4705 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4706 | 0, /*tp_richcompare*/ |
| 4707 | 0, /*tp_weaklistoffset*/ |
| 4708 | 0, /*tp_iter*/ |
| 4709 | 0, /*tp_iternext*/ |
| 4710 | Pickler_methods, /*tp_methods*/ |
| 4711 | Pickler_members, /*tp_members*/ |
| 4712 | Pickler_getsets, /*tp_getset*/ |
| 4713 | 0, /*tp_base*/ |
| 4714 | 0, /*tp_dict*/ |
| 4715 | 0, /*tp_descr_get*/ |
| 4716 | 0, /*tp_descr_set*/ |
| 4717 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4718 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4719 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4720 | PyType_GenericNew, /*tp_new*/ |
| 4721 | PyObject_GC_Del, /*tp_free*/ |
| 4722 | 0, /*tp_is_gc*/ |
| 4723 | }; |
| 4724 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4725 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4726 | |
| 4727 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4728 | using directly the C version of find_class(), when find_class() is not |
| 4729 | overridden by a subclass. Although, this could become rather hackish. A |
| 4730 | simpler optimization would be to call the C function when self is not a |
| 4731 | subclass instance. */ |
| 4732 | static PyObject * |
| 4733 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4734 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4735 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4736 | |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 4737 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
| 4738 | module_name, global_name, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4739 | } |
| 4740 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4741 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4742 | marker(UnpicklerObject *self) |
| 4743 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4744 | Py_ssize_t mark; |
| 4745 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4746 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4747 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4748 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4749 | return -1; |
| 4750 | } |
| 4751 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4752 | mark = self->marks[--self->num_marks]; |
| 4753 | self->stack->mark_set = self->num_marks != 0; |
| 4754 | self->stack->fence = self->num_marks ? |
| 4755 | self->marks[self->num_marks - 1] : 0; |
| 4756 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4757 | } |
| 4758 | |
| 4759 | static int |
| 4760 | load_none(UnpicklerObject *self) |
| 4761 | { |
| 4762 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4763 | return 0; |
| 4764 | } |
| 4765 | |
| 4766 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4767 | load_int(UnpicklerObject *self) |
| 4768 | { |
| 4769 | PyObject *value; |
| 4770 | char *endptr, *s; |
| 4771 | Py_ssize_t len; |
| 4772 | long x; |
| 4773 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4774 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4775 | return -1; |
| 4776 | if (len < 2) |
| 4777 | return bad_readline(); |
| 4778 | |
| 4779 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4780 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4781 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4782 | x = strtol(s, &endptr, 0); |
| 4783 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4784 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4785 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4786 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4787 | errno = 0; |
| 4788 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4789 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4790 | if (value == NULL) { |
| 4791 | PyErr_SetString(PyExc_ValueError, |
| 4792 | "could not convert string to int"); |
| 4793 | return -1; |
| 4794 | } |
| 4795 | } |
| 4796 | else { |
| 4797 | if (len == 3 && (x == 0 || x == 1)) { |
| 4798 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4799 | return -1; |
| 4800 | } |
| 4801 | else { |
| 4802 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4803 | return -1; |
| 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | PDATA_PUSH(self->stack, value, -1); |
| 4808 | return 0; |
| 4809 | } |
| 4810 | |
| 4811 | static int |
| 4812 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4813 | { |
| 4814 | assert(boolean == Py_True || boolean == Py_False); |
| 4815 | PDATA_APPEND(self->stack, boolean, -1); |
| 4816 | return 0; |
| 4817 | } |
| 4818 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4819 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4820 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4821 | */ |
| 4822 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4823 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4824 | { |
| 4825 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4826 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4827 | size_t x = 0; |
| 4828 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4829 | if (nbytes > (int)sizeof(size_t)) { |
| 4830 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4831 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4832 | */ |
| 4833 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4834 | if (s[i]) |
| 4835 | return -1; |
| 4836 | } |
| 4837 | nbytes = (int)sizeof(size_t); |
| 4838 | } |
| 4839 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4840 | x |= (size_t) s[i] << (8 * i); |
| 4841 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4842 | |
| 4843 | if (x > PY_SSIZE_T_MAX) |
| 4844 | return -1; |
| 4845 | else |
| 4846 | return (Py_ssize_t) x; |
| 4847 | } |
| 4848 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4849 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4850 | * 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] | 4851 | * 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] | 4852 | * of x-platform bugs. |
| 4853 | */ |
| 4854 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4855 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4856 | { |
| 4857 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4858 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4859 | long x = 0; |
| 4860 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4861 | for (i = 0; i < nbytes; i++) { |
| 4862 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4866 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4867 | * to extend a BININT's sign bit to the full width. |
| 4868 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4869 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4870 | x |= -(x & (1L << 31)); |
| 4871 | } |
| 4872 | |
| 4873 | return x; |
| 4874 | } |
| 4875 | |
| 4876 | static int |
| 4877 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4878 | { |
| 4879 | PyObject *value; |
| 4880 | long x; |
| 4881 | |
| 4882 | x = calc_binint(s, size); |
| 4883 | |
| 4884 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4885 | return -1; |
| 4886 | |
| 4887 | PDATA_PUSH(self->stack, value, -1); |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | static int |
| 4892 | load_binint(UnpicklerObject *self) |
| 4893 | { |
| 4894 | char *s; |
| 4895 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4896 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4897 | return -1; |
| 4898 | |
| 4899 | return load_binintx(self, s, 4); |
| 4900 | } |
| 4901 | |
| 4902 | static int |
| 4903 | load_binint1(UnpicklerObject *self) |
| 4904 | { |
| 4905 | char *s; |
| 4906 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4907 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4908 | return -1; |
| 4909 | |
| 4910 | return load_binintx(self, s, 1); |
| 4911 | } |
| 4912 | |
| 4913 | static int |
| 4914 | load_binint2(UnpicklerObject *self) |
| 4915 | { |
| 4916 | char *s; |
| 4917 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4918 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4919 | return -1; |
| 4920 | |
| 4921 | return load_binintx(self, s, 2); |
| 4922 | } |
| 4923 | |
| 4924 | static int |
| 4925 | load_long(UnpicklerObject *self) |
| 4926 | { |
| 4927 | PyObject *value; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 4928 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4929 | Py_ssize_t len; |
| 4930 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4931 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4932 | return -1; |
| 4933 | if (len < 2) |
| 4934 | return bad_readline(); |
| 4935 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4936 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4937 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4938 | compatibility with Python 3.0.0, we don't actually *require* |
| 4939 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4940 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4941 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4942 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4943 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4944 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4945 | return -1; |
| 4946 | |
| 4947 | PDATA_PUSH(self->stack, value, -1); |
| 4948 | return 0; |
| 4949 | } |
| 4950 | |
| 4951 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4952 | * data following. |
| 4953 | */ |
| 4954 | static int |
| 4955 | load_counted_long(UnpicklerObject *self, int size) |
| 4956 | { |
| 4957 | PyObject *value; |
| 4958 | char *nbytes; |
| 4959 | char *pdata; |
| 4960 | |
| 4961 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4962 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4963 | return -1; |
| 4964 | |
| 4965 | size = calc_binint(nbytes, size); |
| 4966 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4967 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4968 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4969 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4970 | "LONG pickle has negative byte count"); |
| 4971 | return -1; |
| 4972 | } |
| 4973 | |
| 4974 | if (size == 0) |
| 4975 | value = PyLong_FromLong(0L); |
| 4976 | else { |
| 4977 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4978 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4979 | return -1; |
| 4980 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4981 | 1 /* little endian */ , 1 /* signed */ ); |
| 4982 | } |
| 4983 | if (value == NULL) |
| 4984 | return -1; |
| 4985 | PDATA_PUSH(self->stack, value, -1); |
| 4986 | return 0; |
| 4987 | } |
| 4988 | |
| 4989 | static int |
| 4990 | load_float(UnpicklerObject *self) |
| 4991 | { |
| 4992 | PyObject *value; |
| 4993 | char *endptr, *s; |
| 4994 | Py_ssize_t len; |
| 4995 | double d; |
| 4996 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4997 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4998 | return -1; |
| 4999 | if (len < 2) |
| 5000 | return bad_readline(); |
| 5001 | |
| 5002 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5003 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 5004 | if (d == -1.0 && PyErr_Occurred()) |
| 5005 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5006 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5007 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 5008 | return -1; |
| 5009 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5010 | value = PyFloat_FromDouble(d); |
| 5011 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5012 | return -1; |
| 5013 | |
| 5014 | PDATA_PUSH(self->stack, value, -1); |
| 5015 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5016 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5017 | |
| 5018 | static int |
| 5019 | load_binfloat(UnpicklerObject *self) |
| 5020 | { |
| 5021 | PyObject *value; |
| 5022 | double x; |
| 5023 | char *s; |
| 5024 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5025 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5026 | return -1; |
| 5027 | |
| 5028 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 5029 | if (x == -1.0 && PyErr_Occurred()) |
| 5030 | return -1; |
| 5031 | |
| 5032 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 5033 | return -1; |
| 5034 | |
| 5035 | PDATA_PUSH(self->stack, value, -1); |
| 5036 | return 0; |
| 5037 | } |
| 5038 | |
| 5039 | static int |
| 5040 | load_string(UnpicklerObject *self) |
| 5041 | { |
| 5042 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5043 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5044 | Py_ssize_t len; |
| 5045 | char *s, *p; |
| 5046 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5047 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5048 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5049 | /* Strip the newline */ |
| 5050 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5051 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5052 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5053 | p = s + 1; |
| 5054 | len -= 2; |
| 5055 | } |
| 5056 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5057 | PickleState *st = _Pickle_GetGlobalState(); |
| 5058 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5059 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5060 | return -1; |
| 5061 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5062 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5063 | |
| 5064 | /* Use the PyBytes API to decode the string, since that is what is used |
| 5065 | to encode, and then coerce the result to Unicode. */ |
| 5066 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5067 | if (bytes == NULL) |
| 5068 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5069 | |
| 5070 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 5071 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5072 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5073 | obj = bytes; |
| 5074 | } |
| 5075 | else { |
| 5076 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 5077 | Py_DECREF(bytes); |
| 5078 | if (obj == NULL) { |
| 5079 | return -1; |
| 5080 | } |
| 5081 | } |
| 5082 | |
| 5083 | PDATA_PUSH(self->stack, obj, -1); |
| 5084 | return 0; |
| 5085 | } |
| 5086 | |
| 5087 | static int |
| 5088 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 5089 | { |
| 5090 | PyObject *obj; |
| 5091 | Py_ssize_t size; |
| 5092 | char *s; |
| 5093 | |
| 5094 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5095 | return -1; |
| 5096 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5097 | size = calc_binsize(s, nbytes); |
| 5098 | if (size < 0) { |
| 5099 | PickleState *st = _Pickle_GetGlobalState(); |
| 5100 | PyErr_Format(st->UnpicklingError, |
| 5101 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 5102 | PY_SSIZE_T_MAX); |
| 5103 | return -1; |
| 5104 | } |
| 5105 | |
| 5106 | if (_Unpickler_Read(self, &s, size) < 0) |
| 5107 | return -1; |
| 5108 | |
| 5109 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 5110 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5111 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5112 | obj = PyBytes_FromStringAndSize(s, size); |
| 5113 | } |
| 5114 | else { |
| 5115 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 5116 | } |
| 5117 | if (obj == NULL) { |
| 5118 | return -1; |
| 5119 | } |
| 5120 | |
| 5121 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5122 | return 0; |
| 5123 | } |
| 5124 | |
| 5125 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5126 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5127 | { |
| 5128 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5129 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5130 | char *s; |
| 5131 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5132 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5133 | return -1; |
| 5134 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5135 | size = calc_binsize(s, nbytes); |
| 5136 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5137 | PyErr_Format(PyExc_OverflowError, |
| 5138 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5139 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5140 | return -1; |
| 5141 | } |
| 5142 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5143 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5144 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5145 | |
| 5146 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5147 | if (bytes == NULL) |
| 5148 | return -1; |
| 5149 | |
| 5150 | PDATA_PUSH(self->stack, bytes, -1); |
| 5151 | return 0; |
| 5152 | } |
| 5153 | |
| 5154 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5155 | load_unicode(UnpicklerObject *self) |
| 5156 | { |
| 5157 | PyObject *str; |
| 5158 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5159 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5160 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5161 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5162 | return -1; |
| 5163 | if (len < 1) |
| 5164 | return bad_readline(); |
| 5165 | |
| 5166 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5167 | if (str == NULL) |
| 5168 | return -1; |
| 5169 | |
| 5170 | PDATA_PUSH(self->stack, str, -1); |
| 5171 | return 0; |
| 5172 | } |
| 5173 | |
| 5174 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5175 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5176 | { |
| 5177 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5178 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5179 | char *s; |
| 5180 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5181 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5182 | return -1; |
| 5183 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5184 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5185 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5186 | PyErr_Format(PyExc_OverflowError, |
| 5187 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5188 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5189 | return -1; |
| 5190 | } |
| 5191 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5192 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5193 | return -1; |
| 5194 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5195 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5196 | if (str == NULL) |
| 5197 | return -1; |
| 5198 | |
| 5199 | PDATA_PUSH(self->stack, str, -1); |
| 5200 | return 0; |
| 5201 | } |
| 5202 | |
| 5203 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5204 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5205 | { |
| 5206 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5207 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5208 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5209 | return Pdata_stack_underflow(self->stack); |
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 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5212 | if (tuple == NULL) |
| 5213 | return -1; |
| 5214 | PDATA_PUSH(self->stack, tuple, -1); |
| 5215 | return 0; |
| 5216 | } |
| 5217 | |
| 5218 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5219 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5220 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5221 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5222 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5223 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5224 | return -1; |
| 5225 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5226 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5227 | } |
| 5228 | |
| 5229 | static int |
| 5230 | load_empty_list(UnpicklerObject *self) |
| 5231 | { |
| 5232 | PyObject *list; |
| 5233 | |
| 5234 | if ((list = PyList_New(0)) == NULL) |
| 5235 | return -1; |
| 5236 | PDATA_PUSH(self->stack, list, -1); |
| 5237 | return 0; |
| 5238 | } |
| 5239 | |
| 5240 | static int |
| 5241 | load_empty_dict(UnpicklerObject *self) |
| 5242 | { |
| 5243 | PyObject *dict; |
| 5244 | |
| 5245 | if ((dict = PyDict_New()) == NULL) |
| 5246 | return -1; |
| 5247 | PDATA_PUSH(self->stack, dict, -1); |
| 5248 | return 0; |
| 5249 | } |
| 5250 | |
| 5251 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5252 | load_empty_set(UnpicklerObject *self) |
| 5253 | { |
| 5254 | PyObject *set; |
| 5255 | |
| 5256 | if ((set = PySet_New(NULL)) == NULL) |
| 5257 | return -1; |
| 5258 | PDATA_PUSH(self->stack, set, -1); |
| 5259 | return 0; |
| 5260 | } |
| 5261 | |
| 5262 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5263 | load_list(UnpicklerObject *self) |
| 5264 | { |
| 5265 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5266 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5267 | |
| 5268 | if ((i = marker(self)) < 0) |
| 5269 | return -1; |
| 5270 | |
| 5271 | list = Pdata_poplist(self->stack, i); |
| 5272 | if (list == NULL) |
| 5273 | return -1; |
| 5274 | PDATA_PUSH(self->stack, list, -1); |
| 5275 | return 0; |
| 5276 | } |
| 5277 | |
| 5278 | static int |
| 5279 | load_dict(UnpicklerObject *self) |
| 5280 | { |
| 5281 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5282 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5283 | |
| 5284 | if ((i = marker(self)) < 0) |
| 5285 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5286 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5287 | |
| 5288 | if ((dict = PyDict_New()) == NULL) |
| 5289 | return -1; |
| 5290 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5291 | if ((j - i) % 2 != 0) { |
| 5292 | PickleState *st = _Pickle_GetGlobalState(); |
| 5293 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5294 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5295 | return -1; |
| 5296 | } |
| 5297 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5298 | for (k = i + 1; k < j; k += 2) { |
| 5299 | key = self->stack->data[k - 1]; |
| 5300 | value = self->stack->data[k]; |
| 5301 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5302 | Py_DECREF(dict); |
| 5303 | return -1; |
| 5304 | } |
| 5305 | } |
| 5306 | Pdata_clear(self->stack, i); |
| 5307 | PDATA_PUSH(self->stack, dict, -1); |
| 5308 | return 0; |
| 5309 | } |
| 5310 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5311 | static int |
| 5312 | load_frozenset(UnpicklerObject *self) |
| 5313 | { |
| 5314 | PyObject *items; |
| 5315 | PyObject *frozenset; |
| 5316 | Py_ssize_t i; |
| 5317 | |
| 5318 | if ((i = marker(self)) < 0) |
| 5319 | return -1; |
| 5320 | |
| 5321 | items = Pdata_poptuple(self->stack, i); |
| 5322 | if (items == NULL) |
| 5323 | return -1; |
| 5324 | |
| 5325 | frozenset = PyFrozenSet_New(items); |
| 5326 | Py_DECREF(items); |
| 5327 | if (frozenset == NULL) |
| 5328 | return -1; |
| 5329 | |
| 5330 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5331 | return 0; |
| 5332 | } |
| 5333 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5334 | static PyObject * |
| 5335 | instantiate(PyObject *cls, PyObject *args) |
| 5336 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5337 | /* Caller must assure args are a tuple. Normally, args come from |
| 5338 | Pdata_poptuple which packs objects from the top of the stack |
| 5339 | into a newly created tuple. */ |
| 5340 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5341 | if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { |
| 5342 | _Py_IDENTIFIER(__getinitargs__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5343 | _Py_IDENTIFIER(__new__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5344 | PyObject *func; |
| 5345 | if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) { |
| 5346 | return NULL; |
| 5347 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5348 | if (func == NULL) { |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5349 | return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL); |
| 5350 | } |
| 5351 | Py_DECREF(func); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5352 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5353 | return PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5354 | } |
| 5355 | |
| 5356 | static int |
| 5357 | load_obj(UnpicklerObject *self) |
| 5358 | { |
| 5359 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5360 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5361 | |
| 5362 | if ((i = marker(self)) < 0) |
| 5363 | return -1; |
| 5364 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5365 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5366 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5367 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5368 | args = Pdata_poptuple(self->stack, i + 1); |
| 5369 | if (args == NULL) |
| 5370 | return -1; |
| 5371 | |
| 5372 | PDATA_POP(self->stack, cls); |
| 5373 | if (cls) { |
| 5374 | obj = instantiate(cls, args); |
| 5375 | Py_DECREF(cls); |
| 5376 | } |
| 5377 | Py_DECREF(args); |
| 5378 | if (obj == NULL) |
| 5379 | return -1; |
| 5380 | |
| 5381 | PDATA_PUSH(self->stack, obj, -1); |
| 5382 | return 0; |
| 5383 | } |
| 5384 | |
| 5385 | static int |
| 5386 | load_inst(UnpicklerObject *self) |
| 5387 | { |
| 5388 | PyObject *cls = NULL; |
| 5389 | PyObject *args = NULL; |
| 5390 | PyObject *obj = NULL; |
| 5391 | PyObject *module_name; |
| 5392 | PyObject *class_name; |
| 5393 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5394 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5395 | char *s; |
| 5396 | |
| 5397 | if ((i = marker(self)) < 0) |
| 5398 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5399 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5400 | return -1; |
| 5401 | if (len < 2) |
| 5402 | return bad_readline(); |
| 5403 | |
| 5404 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5405 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5406 | supported by older protocols on Python 2.x. */ |
| 5407 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5408 | if (module_name == NULL) |
| 5409 | return -1; |
| 5410 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5411 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5412 | if (len < 2) { |
| 5413 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5414 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5415 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5416 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5417 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5418 | cls = find_class(self, module_name, class_name); |
| 5419 | Py_DECREF(class_name); |
| 5420 | } |
| 5421 | } |
| 5422 | Py_DECREF(module_name); |
| 5423 | |
| 5424 | if (cls == NULL) |
| 5425 | return -1; |
| 5426 | |
| 5427 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5428 | obj = instantiate(cls, args); |
| 5429 | Py_DECREF(args); |
| 5430 | } |
| 5431 | Py_DECREF(cls); |
| 5432 | |
| 5433 | if (obj == NULL) |
| 5434 | return -1; |
| 5435 | |
| 5436 | PDATA_PUSH(self->stack, obj, -1); |
| 5437 | return 0; |
| 5438 | } |
| 5439 | |
| 5440 | static int |
| 5441 | load_newobj(UnpicklerObject *self) |
| 5442 | { |
| 5443 | PyObject *args = NULL; |
| 5444 | PyObject *clsraw = NULL; |
| 5445 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5446 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5447 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5448 | |
| 5449 | /* Stack is ... cls argtuple, and we want to call |
| 5450 | * cls.__new__(cls, *argtuple). |
| 5451 | */ |
| 5452 | PDATA_POP(self->stack, args); |
| 5453 | if (args == NULL) |
| 5454 | goto error; |
| 5455 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5456 | PyErr_SetString(st->UnpicklingError, |
| 5457 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5458 | goto error; |
| 5459 | } |
| 5460 | |
| 5461 | PDATA_POP(self->stack, clsraw); |
| 5462 | cls = (PyTypeObject *)clsraw; |
| 5463 | if (cls == NULL) |
| 5464 | goto error; |
| 5465 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5466 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5467 | "isn't a type object"); |
| 5468 | goto error; |
| 5469 | } |
| 5470 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5471 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5472 | "has NULL tp_new"); |
| 5473 | goto error; |
| 5474 | } |
| 5475 | |
| 5476 | /* Call __new__. */ |
| 5477 | obj = cls->tp_new(cls, args, NULL); |
| 5478 | if (obj == NULL) |
| 5479 | goto error; |
| 5480 | |
| 5481 | Py_DECREF(args); |
| 5482 | Py_DECREF(clsraw); |
| 5483 | PDATA_PUSH(self->stack, obj, -1); |
| 5484 | return 0; |
| 5485 | |
| 5486 | error: |
| 5487 | Py_XDECREF(args); |
| 5488 | Py_XDECREF(clsraw); |
| 5489 | return -1; |
| 5490 | } |
| 5491 | |
| 5492 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5493 | load_newobj_ex(UnpicklerObject *self) |
| 5494 | { |
| 5495 | PyObject *cls, *args, *kwargs; |
| 5496 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5497 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5498 | |
| 5499 | PDATA_POP(self->stack, kwargs); |
| 5500 | if (kwargs == NULL) { |
| 5501 | return -1; |
| 5502 | } |
| 5503 | PDATA_POP(self->stack, args); |
| 5504 | if (args == NULL) { |
| 5505 | Py_DECREF(kwargs); |
| 5506 | return -1; |
| 5507 | } |
| 5508 | PDATA_POP(self->stack, cls); |
| 5509 | if (cls == NULL) { |
| 5510 | Py_DECREF(kwargs); |
| 5511 | Py_DECREF(args); |
| 5512 | return -1; |
| 5513 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5514 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5515 | if (!PyType_Check(cls)) { |
| 5516 | Py_DECREF(kwargs); |
| 5517 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5518 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5519 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5520 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5521 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5522 | return -1; |
| 5523 | } |
| 5524 | |
| 5525 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5526 | Py_DECREF(kwargs); |
| 5527 | Py_DECREF(args); |
| 5528 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5529 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5530 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5531 | return -1; |
| 5532 | } |
| 5533 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5534 | Py_DECREF(kwargs); |
| 5535 | Py_DECREF(args); |
| 5536 | Py_DECREF(cls); |
| 5537 | if (obj == NULL) { |
| 5538 | return -1; |
| 5539 | } |
| 5540 | PDATA_PUSH(self->stack, obj, -1); |
| 5541 | return 0; |
| 5542 | } |
| 5543 | |
| 5544 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5545 | load_global(UnpicklerObject *self) |
| 5546 | { |
| 5547 | PyObject *global = NULL; |
| 5548 | PyObject *module_name; |
| 5549 | PyObject *global_name; |
| 5550 | Py_ssize_t len; |
| 5551 | char *s; |
| 5552 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5553 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5554 | return -1; |
| 5555 | if (len < 2) |
| 5556 | return bad_readline(); |
| 5557 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5558 | if (!module_name) |
| 5559 | return -1; |
| 5560 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5561 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5562 | if (len < 2) { |
| 5563 | Py_DECREF(module_name); |
| 5564 | return bad_readline(); |
| 5565 | } |
| 5566 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5567 | if (global_name) { |
| 5568 | global = find_class(self, module_name, global_name); |
| 5569 | Py_DECREF(global_name); |
| 5570 | } |
| 5571 | } |
| 5572 | Py_DECREF(module_name); |
| 5573 | |
| 5574 | if (global == NULL) |
| 5575 | return -1; |
| 5576 | PDATA_PUSH(self->stack, global, -1); |
| 5577 | return 0; |
| 5578 | } |
| 5579 | |
| 5580 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5581 | load_stack_global(UnpicklerObject *self) |
| 5582 | { |
| 5583 | PyObject *global; |
| 5584 | PyObject *module_name; |
| 5585 | PyObject *global_name; |
| 5586 | |
| 5587 | PDATA_POP(self->stack, global_name); |
| 5588 | PDATA_POP(self->stack, module_name); |
| 5589 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5590 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5591 | PickleState *st = _Pickle_GetGlobalState(); |
| 5592 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5593 | Py_XDECREF(global_name); |
| 5594 | Py_XDECREF(module_name); |
| 5595 | return -1; |
| 5596 | } |
| 5597 | global = find_class(self, module_name, global_name); |
| 5598 | Py_DECREF(global_name); |
| 5599 | Py_DECREF(module_name); |
| 5600 | if (global == NULL) |
| 5601 | return -1; |
| 5602 | PDATA_PUSH(self->stack, global, -1); |
| 5603 | return 0; |
| 5604 | } |
| 5605 | |
| 5606 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5607 | load_persid(UnpicklerObject *self) |
| 5608 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5609 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5610 | Py_ssize_t len; |
| 5611 | char *s; |
| 5612 | |
| 5613 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5614 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5615 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5616 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5617 | return bad_readline(); |
| 5618 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5619 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5620 | if (pid == NULL) { |
| 5621 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 5622 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 5623 | "persistent IDs in protocol 0 must be " |
| 5624 | "ASCII strings"); |
| 5625 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5626 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5627 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5628 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5629 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5630 | Py_DECREF(pid); |
| 5631 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5632 | return -1; |
| 5633 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5634 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5635 | return 0; |
| 5636 | } |
| 5637 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5638 | PickleState *st = _Pickle_GetGlobalState(); |
| 5639 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5640 | "A load persistent id instruction was encountered,\n" |
| 5641 | "but no persistent_load function was specified."); |
| 5642 | return -1; |
| 5643 | } |
| 5644 | } |
| 5645 | |
| 5646 | static int |
| 5647 | load_binpersid(UnpicklerObject *self) |
| 5648 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5649 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5650 | |
| 5651 | if (self->pers_func) { |
| 5652 | PDATA_POP(self->stack, pid); |
| 5653 | if (pid == NULL) |
| 5654 | return -1; |
| 5655 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5656 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5657 | Py_DECREF(pid); |
| 5658 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5659 | return -1; |
| 5660 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5661 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5662 | return 0; |
| 5663 | } |
| 5664 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5665 | PickleState *st = _Pickle_GetGlobalState(); |
| 5666 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5667 | "A load persistent id instruction was encountered,\n" |
| 5668 | "but no persistent_load function was specified."); |
| 5669 | return -1; |
| 5670 | } |
| 5671 | } |
| 5672 | |
| 5673 | static int |
| 5674 | load_pop(UnpicklerObject *self) |
| 5675 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5676 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5677 | |
| 5678 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5679 | * an object stack and a mark stack. We have to be clever and |
| 5680 | * 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] | 5681 | * mark stack first, and only signalling a stack underflow if |
| 5682 | * the object stack is empty and the mark stack doesn't match |
| 5683 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5684 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5685 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5686 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5687 | self->stack->mark_set = self->num_marks != 0; |
| 5688 | self->stack->fence = self->num_marks ? |
| 5689 | self->marks[self->num_marks - 1] : 0; |
| 5690 | } else if (len <= self->stack->fence) |
| 5691 | return Pdata_stack_underflow(self->stack); |
| 5692 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5693 | len--; |
| 5694 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5695 | Py_SIZE(self->stack) = len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5696 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5697 | return 0; |
| 5698 | } |
| 5699 | |
| 5700 | static int |
| 5701 | load_pop_mark(UnpicklerObject *self) |
| 5702 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5703 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5704 | |
| 5705 | if ((i = marker(self)) < 0) |
| 5706 | return -1; |
| 5707 | |
| 5708 | Pdata_clear(self->stack, i); |
| 5709 | |
| 5710 | return 0; |
| 5711 | } |
| 5712 | |
| 5713 | static int |
| 5714 | load_dup(UnpicklerObject *self) |
| 5715 | { |
| 5716 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5717 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5718 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5719 | if (len <= self->stack->fence) |
| 5720 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5721 | last = self->stack->data[len - 1]; |
| 5722 | PDATA_APPEND(self->stack, last, -1); |
| 5723 | return 0; |
| 5724 | } |
| 5725 | |
| 5726 | static int |
| 5727 | load_get(UnpicklerObject *self) |
| 5728 | { |
| 5729 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5730 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5731 | Py_ssize_t len; |
| 5732 | char *s; |
| 5733 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5734 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5735 | return -1; |
| 5736 | if (len < 2) |
| 5737 | return bad_readline(); |
| 5738 | |
| 5739 | key = PyLong_FromString(s, NULL, 10); |
| 5740 | if (key == NULL) |
| 5741 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5742 | idx = PyLong_AsSsize_t(key); |
| 5743 | if (idx == -1 && PyErr_Occurred()) { |
| 5744 | Py_DECREF(key); |
| 5745 | return -1; |
| 5746 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5747 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5748 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5749 | if (value == NULL) { |
| 5750 | if (!PyErr_Occurred()) |
| 5751 | PyErr_SetObject(PyExc_KeyError, key); |
| 5752 | Py_DECREF(key); |
| 5753 | return -1; |
| 5754 | } |
| 5755 | Py_DECREF(key); |
| 5756 | |
| 5757 | PDATA_APPEND(self->stack, value, -1); |
| 5758 | return 0; |
| 5759 | } |
| 5760 | |
| 5761 | static int |
| 5762 | load_binget(UnpicklerObject *self) |
| 5763 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5764 | PyObject *value; |
| 5765 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5766 | char *s; |
| 5767 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5768 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5769 | return -1; |
| 5770 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5771 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5772 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5773 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5774 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5775 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5776 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5777 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5778 | Py_DECREF(key); |
| 5779 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5780 | return -1; |
| 5781 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5782 | |
| 5783 | PDATA_APPEND(self->stack, value, -1); |
| 5784 | return 0; |
| 5785 | } |
| 5786 | |
| 5787 | static int |
| 5788 | load_long_binget(UnpicklerObject *self) |
| 5789 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5790 | PyObject *value; |
| 5791 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5792 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5794 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5795 | return -1; |
| 5796 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5797 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5798 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5799 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5800 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5801 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5802 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5803 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5804 | Py_DECREF(key); |
| 5805 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5806 | return -1; |
| 5807 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5808 | |
| 5809 | PDATA_APPEND(self->stack, value, -1); |
| 5810 | return 0; |
| 5811 | } |
| 5812 | |
| 5813 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5814 | * the number of bytes following the opcode, holding the index (code) value. |
| 5815 | */ |
| 5816 | static int |
| 5817 | load_extension(UnpicklerObject *self, int nbytes) |
| 5818 | { |
| 5819 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5820 | long code; /* calc_binint returns long */ |
| 5821 | PyObject *py_code; /* code as a Python int */ |
| 5822 | PyObject *obj; /* the object to push */ |
| 5823 | PyObject *pair; /* (module_name, class_name) */ |
| 5824 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5825 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5826 | |
| 5827 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5828 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5829 | return -1; |
| 5830 | code = calc_binint(codebytes, nbytes); |
| 5831 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5832 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5833 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5834 | return -1; |
| 5835 | } |
| 5836 | |
| 5837 | /* Look for the code in the cache. */ |
| 5838 | py_code = PyLong_FromLong(code); |
| 5839 | if (py_code == NULL) |
| 5840 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5841 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5842 | if (obj != NULL) { |
| 5843 | /* Bingo. */ |
| 5844 | Py_DECREF(py_code); |
| 5845 | PDATA_APPEND(self->stack, obj, -1); |
| 5846 | return 0; |
| 5847 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5848 | if (PyErr_Occurred()) { |
| 5849 | Py_DECREF(py_code); |
| 5850 | return -1; |
| 5851 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5852 | |
| 5853 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5854 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5855 | if (pair == NULL) { |
| 5856 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5857 | if (!PyErr_Occurred()) { |
| 5858 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5859 | "code %ld", code); |
| 5860 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5861 | return -1; |
| 5862 | } |
| 5863 | /* Since the extension registry is manipulable via Python code, |
| 5864 | * confirm that pair is really a 2-tuple of strings. |
| 5865 | */ |
| 5866 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5867 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5868 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5869 | Py_DECREF(py_code); |
| 5870 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5871 | "isn't a 2-tuple of strings", code); |
| 5872 | return -1; |
| 5873 | } |
| 5874 | /* Load the object. */ |
| 5875 | obj = find_class(self, module_name, class_name); |
| 5876 | if (obj == NULL) { |
| 5877 | Py_DECREF(py_code); |
| 5878 | return -1; |
| 5879 | } |
| 5880 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5881 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5882 | Py_DECREF(py_code); |
| 5883 | if (code < 0) { |
| 5884 | Py_DECREF(obj); |
| 5885 | return -1; |
| 5886 | } |
| 5887 | PDATA_PUSH(self->stack, obj, -1); |
| 5888 | return 0; |
| 5889 | } |
| 5890 | |
| 5891 | static int |
| 5892 | load_put(UnpicklerObject *self) |
| 5893 | { |
| 5894 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5895 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5896 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5897 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5898 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5899 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5900 | return -1; |
| 5901 | if (len < 2) |
| 5902 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5903 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5904 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5905 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5906 | |
| 5907 | key = PyLong_FromString(s, NULL, 10); |
| 5908 | if (key == NULL) |
| 5909 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5910 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5911 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5912 | if (idx < 0) { |
| 5913 | if (!PyErr_Occurred()) |
| 5914 | PyErr_SetString(PyExc_ValueError, |
| 5915 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5916 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5917 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5918 | |
| 5919 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | static int |
| 5923 | load_binput(UnpicklerObject *self) |
| 5924 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5925 | PyObject *value; |
| 5926 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5927 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5928 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5929 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5930 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5931 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5932 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5933 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5934 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5935 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5936 | idx = Py_CHARMASK(s[0]); |
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 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5939 | } |
| 5940 | |
| 5941 | static int |
| 5942 | load_long_binput(UnpicklerObject *self) |
| 5943 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5944 | PyObject *value; |
| 5945 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5946 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5947 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5948 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5949 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5950 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5951 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5952 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5953 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5954 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5955 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5956 | if (idx < 0) { |
| 5957 | PyErr_SetString(PyExc_ValueError, |
| 5958 | "negative LONG_BINPUT argument"); |
| 5959 | return -1; |
| 5960 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5961 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5962 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5963 | } |
| 5964 | |
| 5965 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5966 | load_memoize(UnpicklerObject *self) |
| 5967 | { |
| 5968 | PyObject *value; |
| 5969 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5970 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5971 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5972 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5973 | |
| 5974 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5975 | } |
| 5976 | |
| 5977 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5978 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5979 | { |
| 5980 | PyObject *value; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5981 | PyObject *slice; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5982 | PyObject *list; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5983 | PyObject *result; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5984 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5985 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5986 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5987 | if (x > len || x <= self->stack->fence) |
| 5988 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5989 | if (len == x) /* nothing to do */ |
| 5990 | return 0; |
| 5991 | |
| 5992 | list = self->stack->data[x - 1]; |
| 5993 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5994 | if (PyList_CheckExact(list)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5995 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5996 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5997 | |
| 5998 | slice = Pdata_poplist(self->stack, x); |
| 5999 | if (!slice) |
| 6000 | return -1; |
| 6001 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6002 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6003 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6004 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6005 | } |
| 6006 | else { |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6007 | PyObject *extend_func; |
| 6008 | _Py_IDENTIFIER(extend); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6009 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6010 | extend_func = _PyObject_GetAttrId(list, &PyId_extend); |
| 6011 | if (extend_func != NULL) { |
| 6012 | slice = Pdata_poplist(self->stack, x); |
| 6013 | if (!slice) { |
| 6014 | Py_DECREF(extend_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6015 | return -1; |
| 6016 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6017 | result = _Pickle_FastCall(extend_func, slice); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6018 | Py_DECREF(extend_func); |
| 6019 | if (result == NULL) |
| 6020 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6021 | Py_DECREF(result); |
| 6022 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6023 | else { |
| 6024 | PyObject *append_func; |
| 6025 | _Py_IDENTIFIER(append); |
| 6026 | |
| 6027 | /* Even if the PEP 307 requires extend() and append() methods, |
| 6028 | fall back on append() if the object has no extend() method |
| 6029 | for backward compatibility. */ |
| 6030 | PyErr_Clear(); |
| 6031 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
| 6032 | if (append_func == NULL) |
| 6033 | return -1; |
| 6034 | for (i = x; i < len; i++) { |
| 6035 | value = self->stack->data[i]; |
| 6036 | result = _Pickle_FastCall(append_func, value); |
| 6037 | if (result == NULL) { |
| 6038 | Pdata_clear(self->stack, i + 1); |
| 6039 | Py_SIZE(self->stack) = x; |
| 6040 | Py_DECREF(append_func); |
| 6041 | return -1; |
| 6042 | } |
| 6043 | Py_DECREF(result); |
| 6044 | } |
| 6045 | Py_SIZE(self->stack) = x; |
| 6046 | Py_DECREF(append_func); |
| 6047 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
| 6050 | return 0; |
| 6051 | } |
| 6052 | |
| 6053 | static int |
| 6054 | load_append(UnpicklerObject *self) |
| 6055 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6056 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 6057 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6058 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | static int |
| 6062 | load_appends(UnpicklerObject *self) |
| 6063 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6064 | Py_ssize_t i = marker(self); |
| 6065 | if (i < 0) |
| 6066 | return -1; |
| 6067 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6071 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6072 | { |
| 6073 | PyObject *value, *key; |
| 6074 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6075 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6076 | int status = 0; |
| 6077 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6078 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6079 | if (x > len || x <= self->stack->fence) |
| 6080 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6081 | if (len == x) /* nothing to do */ |
| 6082 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6083 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6084 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6085 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6086 | PyErr_SetString(st->UnpicklingError, |
| 6087 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6088 | return -1; |
| 6089 | } |
| 6090 | |
| 6091 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 6092 | that supports the __setitem__ attribute. */ |
| 6093 | dict = self->stack->data[x - 1]; |
| 6094 | |
| 6095 | for (i = x + 1; i < len; i += 2) { |
| 6096 | key = self->stack->data[i - 1]; |
| 6097 | value = self->stack->data[i]; |
| 6098 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 6099 | status = -1; |
| 6100 | break; |
| 6101 | } |
| 6102 | } |
| 6103 | |
| 6104 | Pdata_clear(self->stack, x); |
| 6105 | return status; |
| 6106 | } |
| 6107 | |
| 6108 | static int |
| 6109 | load_setitem(UnpicklerObject *self) |
| 6110 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6111 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6112 | } |
| 6113 | |
| 6114 | static int |
| 6115 | load_setitems(UnpicklerObject *self) |
| 6116 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6117 | Py_ssize_t i = marker(self); |
| 6118 | if (i < 0) |
| 6119 | return -1; |
| 6120 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6124 | load_additems(UnpicklerObject *self) |
| 6125 | { |
| 6126 | PyObject *set; |
| 6127 | Py_ssize_t mark, len, i; |
| 6128 | |
| 6129 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6130 | if (mark < 0) |
| 6131 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6132 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6133 | if (mark > len || mark <= self->stack->fence) |
| 6134 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6135 | if (len == mark) /* nothing to do */ |
| 6136 | return 0; |
| 6137 | |
| 6138 | set = self->stack->data[mark - 1]; |
| 6139 | |
| 6140 | if (PySet_Check(set)) { |
| 6141 | PyObject *items; |
| 6142 | int status; |
| 6143 | |
| 6144 | items = Pdata_poptuple(self->stack, mark); |
| 6145 | if (items == NULL) |
| 6146 | return -1; |
| 6147 | |
| 6148 | status = _PySet_Update(set, items); |
| 6149 | Py_DECREF(items); |
| 6150 | return status; |
| 6151 | } |
| 6152 | else { |
| 6153 | PyObject *add_func; |
| 6154 | _Py_IDENTIFIER(add); |
| 6155 | |
| 6156 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6157 | if (add_func == NULL) |
| 6158 | return -1; |
| 6159 | for (i = mark; i < len; i++) { |
| 6160 | PyObject *result; |
| 6161 | PyObject *item; |
| 6162 | |
| 6163 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6164 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6165 | if (result == NULL) { |
| 6166 | Pdata_clear(self->stack, i + 1); |
| 6167 | Py_SIZE(self->stack) = mark; |
| 6168 | return -1; |
| 6169 | } |
| 6170 | Py_DECREF(result); |
| 6171 | } |
| 6172 | Py_SIZE(self->stack) = mark; |
| 6173 | } |
| 6174 | |
| 6175 | return 0; |
| 6176 | } |
| 6177 | |
| 6178 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6179 | load_build(UnpicklerObject *self) |
| 6180 | { |
| 6181 | PyObject *state, *inst, *slotstate; |
| 6182 | PyObject *setstate; |
| 6183 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6184 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6185 | |
| 6186 | /* Stack is ... instance, state. We want to leave instance at |
| 6187 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6188 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6189 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6190 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6191 | |
| 6192 | PDATA_POP(self->stack, state); |
| 6193 | if (state == NULL) |
| 6194 | return -1; |
| 6195 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6196 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6197 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6198 | if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) { |
| 6199 | Py_DECREF(state); |
| 6200 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6201 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6202 | if (setstate != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6203 | PyObject *result; |
| 6204 | |
| 6205 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6206 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6207 | Py_DECREF(setstate); |
| 6208 | if (result == NULL) |
| 6209 | return -1; |
| 6210 | Py_DECREF(result); |
| 6211 | return 0; |
| 6212 | } |
| 6213 | |
| 6214 | /* A default __setstate__. First see whether state embeds a |
| 6215 | * slot state dict too (a proto 2 addition). |
| 6216 | */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 6217 | if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6218 | PyObject *tmp = state; |
| 6219 | |
| 6220 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6221 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6222 | Py_INCREF(state); |
| 6223 | Py_INCREF(slotstate); |
| 6224 | Py_DECREF(tmp); |
| 6225 | } |
| 6226 | else |
| 6227 | slotstate = NULL; |
| 6228 | |
| 6229 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6230 | if (state != Py_None) { |
| 6231 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6232 | PyObject *d_key, *d_value; |
| 6233 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6234 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6235 | |
| 6236 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6237 | PickleState *st = _Pickle_GetGlobalState(); |
| 6238 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6239 | goto error; |
| 6240 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6241 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6242 | if (dict == NULL) |
| 6243 | goto error; |
| 6244 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6245 | i = 0; |
| 6246 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6247 | /* normally the keys for instance attributes are |
| 6248 | interned. we should try to do that here. */ |
| 6249 | Py_INCREF(d_key); |
| 6250 | if (PyUnicode_CheckExact(d_key)) |
| 6251 | PyUnicode_InternInPlace(&d_key); |
| 6252 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6253 | Py_DECREF(d_key); |
| 6254 | goto error; |
| 6255 | } |
| 6256 | Py_DECREF(d_key); |
| 6257 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6258 | Py_DECREF(dict); |
| 6259 | } |
| 6260 | |
| 6261 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6262 | if (slotstate != NULL) { |
| 6263 | PyObject *d_key, *d_value; |
| 6264 | Py_ssize_t i; |
| 6265 | |
| 6266 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6267 | PickleState *st = _Pickle_GetGlobalState(); |
| 6268 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6269 | "slot state is not a dictionary"); |
| 6270 | goto error; |
| 6271 | } |
| 6272 | i = 0; |
| 6273 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6274 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6275 | goto error; |
| 6276 | } |
| 6277 | } |
| 6278 | |
| 6279 | if (0) { |
| 6280 | error: |
| 6281 | status = -1; |
| 6282 | } |
| 6283 | |
| 6284 | Py_DECREF(state); |
| 6285 | Py_XDECREF(slotstate); |
| 6286 | return status; |
| 6287 | } |
| 6288 | |
| 6289 | static int |
| 6290 | load_mark(UnpicklerObject *self) |
| 6291 | { |
| 6292 | |
| 6293 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6294 | * object stack and a mark stack. Here we push a mark onto the |
| 6295 | * mark stack. |
| 6296 | */ |
| 6297 | |
| 6298 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6299 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6300 | |
| 6301 | /* Use the size_t type to check for overflow. */ |
| 6302 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6303 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6304 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6305 | PyErr_NoMemory(); |
| 6306 | return -1; |
| 6307 | } |
| 6308 | |
Miss Islington (bot) | 962051e | 2018-08-16 00:53:00 -0400 | [diff] [blame] | 6309 | Py_ssize_t *marks_old = self->marks; |
| 6310 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6311 | if (self->marks == NULL) { |
Miss Islington (bot) | 962051e | 2018-08-16 00:53:00 -0400 | [diff] [blame] | 6312 | PyMem_FREE(marks_old); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6313 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6314 | PyErr_NoMemory(); |
| 6315 | return -1; |
| 6316 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6317 | self->marks_size = (Py_ssize_t)alloc; |
| 6318 | } |
| 6319 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6320 | self->stack->mark_set = 1; |
| 6321 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6322 | |
| 6323 | return 0; |
| 6324 | } |
| 6325 | |
| 6326 | static int |
| 6327 | load_reduce(UnpicklerObject *self) |
| 6328 | { |
| 6329 | PyObject *callable = NULL; |
| 6330 | PyObject *argtup = NULL; |
| 6331 | PyObject *obj = NULL; |
| 6332 | |
| 6333 | PDATA_POP(self->stack, argtup); |
| 6334 | if (argtup == NULL) |
| 6335 | return -1; |
| 6336 | PDATA_POP(self->stack, callable); |
| 6337 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6338 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6339 | Py_DECREF(callable); |
| 6340 | } |
| 6341 | Py_DECREF(argtup); |
| 6342 | |
| 6343 | if (obj == NULL) |
| 6344 | return -1; |
| 6345 | |
| 6346 | PDATA_PUSH(self->stack, obj, -1); |
| 6347 | return 0; |
| 6348 | } |
| 6349 | |
| 6350 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6351 | * is the first opcode for protocols >= 2. |
| 6352 | */ |
| 6353 | static int |
| 6354 | load_proto(UnpicklerObject *self) |
| 6355 | { |
| 6356 | char *s; |
| 6357 | int i; |
| 6358 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6359 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6360 | return -1; |
| 6361 | |
| 6362 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6363 | if (i <= HIGHEST_PROTOCOL) { |
| 6364 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6365 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6366 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6367 | |
| 6368 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6369 | return -1; |
| 6370 | } |
| 6371 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6372 | static int |
| 6373 | load_frame(UnpicklerObject *self) |
| 6374 | { |
| 6375 | char *s; |
| 6376 | Py_ssize_t frame_len; |
| 6377 | |
| 6378 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6379 | return -1; |
| 6380 | |
| 6381 | frame_len = calc_binsize(s, 8); |
| 6382 | if (frame_len < 0) { |
| 6383 | PyErr_Format(PyExc_OverflowError, |
| 6384 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6385 | PY_SSIZE_T_MAX); |
| 6386 | return -1; |
| 6387 | } |
| 6388 | |
| 6389 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6390 | return -1; |
| 6391 | |
| 6392 | /* Rewind to start of frame */ |
| 6393 | self->next_read_idx -= frame_len; |
| 6394 | return 0; |
| 6395 | } |
| 6396 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6397 | static PyObject * |
| 6398 | load(UnpicklerObject *self) |
| 6399 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6400 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6401 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6402 | |
| 6403 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6404 | self->stack->mark_set = 0; |
| 6405 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6406 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6407 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6408 | Pdata_clear(self->stack, 0); |
| 6409 | |
| 6410 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6411 | #define OP(opcode, load_func) \ |
| 6412 | case opcode: if (load_func(self) < 0) break; continue; |
| 6413 | |
| 6414 | #define OP_ARG(opcode, load_func, arg) \ |
| 6415 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6416 | |
| 6417 | while (1) { |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6418 | if (_Unpickler_Read(self, &s, 1) < 0) { |
| 6419 | PickleState *st = _Pickle_GetGlobalState(); |
| 6420 | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
| 6421 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 6422 | } |
| 6423 | return NULL; |
| 6424 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6425 | |
| 6426 | switch ((enum opcode)s[0]) { |
| 6427 | OP(NONE, load_none) |
| 6428 | OP(BININT, load_binint) |
| 6429 | OP(BININT1, load_binint1) |
| 6430 | OP(BININT2, load_binint2) |
| 6431 | OP(INT, load_int) |
| 6432 | OP(LONG, load_long) |
| 6433 | OP_ARG(LONG1, load_counted_long, 1) |
| 6434 | OP_ARG(LONG4, load_counted_long, 4) |
| 6435 | OP(FLOAT, load_float) |
| 6436 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6437 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6438 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6439 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6440 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6441 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6442 | OP(STRING, load_string) |
| 6443 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6444 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6445 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6446 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6447 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6448 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6449 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6450 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6451 | OP(TUPLE, load_tuple) |
| 6452 | OP(EMPTY_LIST, load_empty_list) |
| 6453 | OP(LIST, load_list) |
| 6454 | OP(EMPTY_DICT, load_empty_dict) |
| 6455 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6456 | OP(EMPTY_SET, load_empty_set) |
| 6457 | OP(ADDITEMS, load_additems) |
| 6458 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6459 | OP(OBJ, load_obj) |
| 6460 | OP(INST, load_inst) |
| 6461 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6462 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6463 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6464 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6465 | OP(APPEND, load_append) |
| 6466 | OP(APPENDS, load_appends) |
| 6467 | OP(BUILD, load_build) |
| 6468 | OP(DUP, load_dup) |
| 6469 | OP(BINGET, load_binget) |
| 6470 | OP(LONG_BINGET, load_long_binget) |
| 6471 | OP(GET, load_get) |
| 6472 | OP(MARK, load_mark) |
| 6473 | OP(BINPUT, load_binput) |
| 6474 | OP(LONG_BINPUT, load_long_binput) |
| 6475 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6476 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6477 | OP(POP, load_pop) |
| 6478 | OP(POP_MARK, load_pop_mark) |
| 6479 | OP(SETITEM, load_setitem) |
| 6480 | OP(SETITEMS, load_setitems) |
| 6481 | OP(PERSID, load_persid) |
| 6482 | OP(BINPERSID, load_binpersid) |
| 6483 | OP(REDUCE, load_reduce) |
| 6484 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6485 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6486 | OP_ARG(EXT1, load_extension, 1) |
| 6487 | OP_ARG(EXT2, load_extension, 2) |
| 6488 | OP_ARG(EXT4, load_extension, 4) |
| 6489 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6490 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6491 | |
| 6492 | case STOP: |
| 6493 | break; |
| 6494 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6495 | default: |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6496 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6497 | PickleState *st = _Pickle_GetGlobalState(); |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6498 | unsigned char c = (unsigned char) *s; |
| 6499 | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
| 6500 | PyErr_Format(st->UnpicklingError, |
| 6501 | "invalid load key, '%c'.", c); |
| 6502 | } |
| 6503 | else { |
| 6504 | PyErr_Format(st->UnpicklingError, |
| 6505 | "invalid load key, '\\x%02x'.", c); |
| 6506 | } |
| 6507 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6508 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6509 | } |
| 6510 | |
| 6511 | break; /* and we are done! */ |
| 6512 | } |
| 6513 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6514 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6515 | return NULL; |
| 6516 | } |
| 6517 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6518 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6519 | return NULL; |
| 6520 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6521 | PDATA_POP(self->stack, value); |
| 6522 | return value; |
| 6523 | } |
| 6524 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6525 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6526 | |
| 6527 | _pickle.Unpickler.load |
| 6528 | |
| 6529 | Load a pickle. |
| 6530 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6531 | Read a pickled object representation from the open file object given |
| 6532 | in the constructor, and return the reconstituted object hierarchy |
| 6533 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6534 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6535 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6536 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6537 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6538 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6539 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6540 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6541 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6542 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6543 | segfaulting if a subclass overridden __init__ with a function that does |
| 6544 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6545 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6546 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6547 | PickleState *st = _Pickle_GetGlobalState(); |
| 6548 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6549 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6550 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6551 | return NULL; |
| 6552 | } |
| 6553 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6554 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6558 | function is used for loading any global (i.e., functions), not just |
| 6559 | classes. The name is kept only for backward compatibility. */ |
| 6560 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6561 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6562 | |
| 6563 | _pickle.Unpickler.find_class |
| 6564 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6565 | module_name: object |
| 6566 | global_name: object |
| 6567 | / |
| 6568 | |
| 6569 | Return an object from a specified module. |
| 6570 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6571 | If necessary, the module will be imported. Subclasses may override |
| 6572 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6573 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6574 | |
| 6575 | This method is called whenever a class or a function object is |
| 6576 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6577 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6578 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6579 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6580 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6581 | PyObject *module_name, |
| 6582 | PyObject *global_name) |
| 6583 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6584 | { |
| 6585 | PyObject *global; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6586 | PyObject *module; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6587 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6588 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6589 | Python 3.x. We do this only with old pickle protocols and when the |
| 6590 | user has not disabled the feature. */ |
| 6591 | if (self->proto < 3 && self->fix_imports) { |
| 6592 | PyObject *key; |
| 6593 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6594 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6595 | |
| 6596 | /* Check if the global (i.e., a function or a class) was renamed |
| 6597 | or moved to another module. */ |
| 6598 | key = PyTuple_Pack(2, module_name, global_name); |
| 6599 | if (key == NULL) |
| 6600 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6601 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6602 | Py_DECREF(key); |
| 6603 | if (item) { |
| 6604 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6605 | PyErr_Format(PyExc_RuntimeError, |
| 6606 | "_compat_pickle.NAME_MAPPING values should be " |
| 6607 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6608 | return NULL; |
| 6609 | } |
| 6610 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6611 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6612 | if (!PyUnicode_Check(module_name) || |
| 6613 | !PyUnicode_Check(global_name)) { |
| 6614 | PyErr_Format(PyExc_RuntimeError, |
| 6615 | "_compat_pickle.NAME_MAPPING values should be " |
| 6616 | "pairs of str, not (%.200s, %.200s)", |
| 6617 | Py_TYPE(module_name)->tp_name, |
| 6618 | Py_TYPE(global_name)->tp_name); |
| 6619 | return NULL; |
| 6620 | } |
| 6621 | } |
| 6622 | else if (PyErr_Occurred()) { |
| 6623 | return NULL; |
| 6624 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6625 | else { |
| 6626 | /* Check if the module was renamed. */ |
| 6627 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6628 | if (item) { |
| 6629 | if (!PyUnicode_Check(item)) { |
| 6630 | PyErr_Format(PyExc_RuntimeError, |
| 6631 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6632 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6633 | return NULL; |
| 6634 | } |
| 6635 | module_name = item; |
| 6636 | } |
| 6637 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6638 | return NULL; |
| 6639 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6640 | } |
| 6641 | } |
| 6642 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6643 | module = PyImport_GetModule(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6644 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6645 | if (PyErr_Occurred()) |
| 6646 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6647 | module = PyImport_Import(module_name); |
| 6648 | if (module == NULL) |
| 6649 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6650 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6651 | global = getattribute(module, global_name, self->proto >= 4); |
| 6652 | Py_DECREF(module); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6653 | return global; |
| 6654 | } |
| 6655 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6656 | /*[clinic input] |
| 6657 | |
| 6658 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6659 | |
| 6660 | Returns size in memory, in bytes. |
| 6661 | [clinic start generated code]*/ |
| 6662 | |
| 6663 | static Py_ssize_t |
| 6664 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6665 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6666 | { |
| 6667 | Py_ssize_t res; |
| 6668 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 6669 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6670 | if (self->memo != NULL) |
| 6671 | res += self->memo_size * sizeof(PyObject *); |
| 6672 | if (self->marks != NULL) |
| 6673 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6674 | if (self->input_line != NULL) |
| 6675 | res += strlen(self->input_line) + 1; |
| 6676 | if (self->encoding != NULL) |
| 6677 | res += strlen(self->encoding) + 1; |
| 6678 | if (self->errors != NULL) |
| 6679 | res += strlen(self->errors) + 1; |
| 6680 | return res; |
| 6681 | } |
| 6682 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6683 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6684 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6685 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6686 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6687 | {NULL, NULL} /* sentinel */ |
| 6688 | }; |
| 6689 | |
| 6690 | static void |
| 6691 | Unpickler_dealloc(UnpicklerObject *self) |
| 6692 | { |
| 6693 | PyObject_GC_UnTrack((PyObject *)self); |
| 6694 | Py_XDECREF(self->readline); |
| 6695 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6696 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6697 | Py_XDECREF(self->stack); |
| 6698 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6699 | if (self->buffer.buf != NULL) { |
| 6700 | PyBuffer_Release(&self->buffer); |
| 6701 | self->buffer.buf = NULL; |
| 6702 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6703 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6704 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6705 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6706 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6707 | PyMem_Free(self->encoding); |
| 6708 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6709 | |
| 6710 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6711 | } |
| 6712 | |
| 6713 | static int |
| 6714 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6715 | { |
| 6716 | Py_VISIT(self->readline); |
| 6717 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6718 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6719 | Py_VISIT(self->stack); |
| 6720 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6721 | return 0; |
| 6722 | } |
| 6723 | |
| 6724 | static int |
| 6725 | Unpickler_clear(UnpicklerObject *self) |
| 6726 | { |
| 6727 | Py_CLEAR(self->readline); |
| 6728 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6729 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6730 | Py_CLEAR(self->stack); |
| 6731 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6732 | if (self->buffer.buf != NULL) { |
| 6733 | PyBuffer_Release(&self->buffer); |
| 6734 | self->buffer.buf = NULL; |
| 6735 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6736 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6737 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6738 | PyMem_Free(self->marks); |
| 6739 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6740 | PyMem_Free(self->input_line); |
| 6741 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6742 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6743 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6744 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6745 | self->errors = NULL; |
| 6746 | |
| 6747 | return 0; |
| 6748 | } |
| 6749 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6750 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6751 | |
| 6752 | _pickle.Unpickler.__init__ |
| 6753 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6754 | file: object |
| 6755 | * |
| 6756 | fix_imports: bool = True |
| 6757 | encoding: str = 'ASCII' |
| 6758 | errors: str = 'strict' |
| 6759 | |
| 6760 | This takes a binary file for reading a pickle data stream. |
| 6761 | |
| 6762 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6763 | protocol argument is needed. Bytes past the pickled object's |
| 6764 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6765 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6766 | The argument *file* must have two methods, a read() method that takes |
| 6767 | an integer argument, and a readline() method that requires no |
| 6768 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6769 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6770 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6771 | |
| 6772 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6773 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6774 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6775 | 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] | 6776 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6777 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6778 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6779 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6780 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6781 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6782 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6783 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6784 | int fix_imports, const char *encoding, |
| 6785 | const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6786 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6787 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6788 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6789 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6790 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6791 | if (self->read != NULL) |
| 6792 | (void)Unpickler_clear(self); |
| 6793 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6794 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6795 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6796 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6797 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6798 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6799 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6800 | self->fix_imports = fix_imports; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6801 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6802 | if (init_method_ref((PyObject *)self, &PyId_persistent_load, |
| 6803 | &self->pers_func, &self->pers_func_self) < 0) |
| 6804 | { |
| 6805 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6806 | } |
| 6807 | |
| 6808 | self->stack = (Pdata *)Pdata_New(); |
| 6809 | if (self->stack == NULL) |
Miss Islington (bot) | 758ad54 | 2018-09-28 23:01:48 -0700 | [diff] [blame] | 6810 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6811 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6812 | self->memo_size = 32; |
| 6813 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6814 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6815 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6816 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6817 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6818 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6819 | return 0; |
| 6820 | } |
| 6821 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6822 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6823 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6824 | * avoid breaking code like: |
| 6825 | * unpickler.memo.clear() |
| 6826 | * and |
| 6827 | * unpickler.memo = saved_memo |
| 6828 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6829 | * it. Note that we don't implement the entire mapping API here. This is |
| 6830 | * intentional, as these should be treated as black-box implementation details. |
| 6831 | * |
| 6832 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6833 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6834 | */ |
| 6835 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6836 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6837 | _pickle.UnpicklerMemoProxy.clear |
| 6838 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6839 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6840 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6841 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6842 | static PyObject * |
| 6843 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6844 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6845 | { |
| 6846 | _Unpickler_MemoCleanup(self->unpickler); |
| 6847 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6848 | if (self->unpickler->memo == NULL) |
| 6849 | return NULL; |
| 6850 | Py_RETURN_NONE; |
| 6851 | } |
| 6852 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6853 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6854 | _pickle.UnpicklerMemoProxy.copy |
| 6855 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6856 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6857 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6858 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6859 | static PyObject * |
| 6860 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6861 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6862 | { |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 6863 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6864 | PyObject *new_memo = PyDict_New(); |
| 6865 | if (new_memo == NULL) |
| 6866 | return NULL; |
| 6867 | |
| 6868 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6869 | int status; |
| 6870 | PyObject *key, *value; |
| 6871 | |
| 6872 | value = self->unpickler->memo[i]; |
| 6873 | if (value == NULL) |
| 6874 | continue; |
| 6875 | |
| 6876 | key = PyLong_FromSsize_t(i); |
| 6877 | if (key == NULL) |
| 6878 | goto error; |
| 6879 | status = PyDict_SetItem(new_memo, key, value); |
| 6880 | Py_DECREF(key); |
| 6881 | if (status < 0) |
| 6882 | goto error; |
| 6883 | } |
| 6884 | return new_memo; |
| 6885 | |
| 6886 | error: |
| 6887 | Py_DECREF(new_memo); |
| 6888 | return NULL; |
| 6889 | } |
| 6890 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6891 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6892 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6893 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6894 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6895 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6896 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6897 | static PyObject * |
| 6898 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6899 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6900 | { |
| 6901 | PyObject *reduce_value; |
| 6902 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6903 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6904 | if (contents == NULL) |
| 6905 | return NULL; |
| 6906 | |
| 6907 | reduce_value = PyTuple_New(2); |
| 6908 | if (reduce_value == NULL) { |
| 6909 | Py_DECREF(contents); |
| 6910 | return NULL; |
| 6911 | } |
| 6912 | constructor_args = PyTuple_New(1); |
| 6913 | if (constructor_args == NULL) { |
| 6914 | Py_DECREF(contents); |
| 6915 | Py_DECREF(reduce_value); |
| 6916 | return NULL; |
| 6917 | } |
| 6918 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6919 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6920 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6921 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6922 | return reduce_value; |
| 6923 | } |
| 6924 | |
| 6925 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6926 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6927 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6928 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6929 | {NULL, NULL} /* sentinel */ |
| 6930 | }; |
| 6931 | |
| 6932 | static void |
| 6933 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6934 | { |
| 6935 | PyObject_GC_UnTrack(self); |
| 6936 | Py_XDECREF(self->unpickler); |
| 6937 | PyObject_GC_Del((PyObject *)self); |
| 6938 | } |
| 6939 | |
| 6940 | static int |
| 6941 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6942 | visitproc visit, void *arg) |
| 6943 | { |
| 6944 | Py_VISIT(self->unpickler); |
| 6945 | return 0; |
| 6946 | } |
| 6947 | |
| 6948 | static int |
| 6949 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6950 | { |
| 6951 | Py_CLEAR(self->unpickler); |
| 6952 | return 0; |
| 6953 | } |
| 6954 | |
| 6955 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6956 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6957 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6958 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6959 | 0, |
| 6960 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6961 | 0, /* tp_print */ |
| 6962 | 0, /* tp_getattr */ |
| 6963 | 0, /* tp_setattr */ |
| 6964 | 0, /* tp_compare */ |
| 6965 | 0, /* tp_repr */ |
| 6966 | 0, /* tp_as_number */ |
| 6967 | 0, /* tp_as_sequence */ |
| 6968 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6969 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6970 | 0, /* tp_call */ |
| 6971 | 0, /* tp_str */ |
| 6972 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6973 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6974 | 0, /* tp_as_buffer */ |
| 6975 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6976 | 0, /* tp_doc */ |
| 6977 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6978 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6979 | 0, /* tp_richcompare */ |
| 6980 | 0, /* tp_weaklistoffset */ |
| 6981 | 0, /* tp_iter */ |
| 6982 | 0, /* tp_iternext */ |
| 6983 | unpicklerproxy_methods, /* tp_methods */ |
| 6984 | }; |
| 6985 | |
| 6986 | static PyObject * |
| 6987 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6988 | { |
| 6989 | UnpicklerMemoProxyObject *self; |
| 6990 | |
| 6991 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6992 | &UnpicklerMemoProxyType); |
| 6993 | if (self == NULL) |
| 6994 | return NULL; |
| 6995 | Py_INCREF(unpickler); |
| 6996 | self->unpickler = unpickler; |
| 6997 | PyObject_GC_Track(self); |
| 6998 | return (PyObject *)self; |
| 6999 | } |
| 7000 | |
| 7001 | /*****************************************************************************/ |
| 7002 | |
| 7003 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7004 | static PyObject * |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 7005 | Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7006 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7007 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7008 | } |
| 7009 | |
| 7010 | static int |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 7011 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7012 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7013 | PyObject **new_memo; |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 7014 | size_t new_memo_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7015 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7016 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7017 | PyErr_SetString(PyExc_TypeError, |
| 7018 | "attribute deletion is not supported"); |
| 7019 | return -1; |
| 7020 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7021 | |
| 7022 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 7023 | UnpicklerObject *unpickler = |
| 7024 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 7025 | |
| 7026 | new_memo_size = unpickler->memo_size; |
| 7027 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7028 | if (new_memo == NULL) |
| 7029 | return -1; |
| 7030 | |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 7031 | for (size_t i = 0; i < new_memo_size; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7032 | Py_XINCREF(unpickler->memo[i]); |
| 7033 | new_memo[i] = unpickler->memo[i]; |
| 7034 | } |
| 7035 | } |
| 7036 | else if (PyDict_Check(obj)) { |
| 7037 | Py_ssize_t i = 0; |
| 7038 | PyObject *key, *value; |
| 7039 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 7040 | new_memo_size = PyDict_GET_SIZE(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7041 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7042 | if (new_memo == NULL) |
| 7043 | return -1; |
| 7044 | |
| 7045 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 7046 | Py_ssize_t idx; |
| 7047 | if (!PyLong_Check(key)) { |
| 7048 | PyErr_SetString(PyExc_TypeError, |
| 7049 | "memo key must be integers"); |
| 7050 | goto error; |
| 7051 | } |
| 7052 | idx = PyLong_AsSsize_t(key); |
| 7053 | if (idx == -1 && PyErr_Occurred()) |
| 7054 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7055 | if (idx < 0) { |
| 7056 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 7057 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7058 | goto error; |
| 7059 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7060 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 7061 | goto error; |
| 7062 | } |
| 7063 | } |
| 7064 | else { |
| 7065 | PyErr_Format(PyExc_TypeError, |
Miss Islington (bot) | 7beb8c5 | 2018-11-05 06:52:58 -0800 | [diff] [blame] | 7066 | "'memo' attribute must be an UnpicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7067 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7068 | return -1; |
| 7069 | } |
| 7070 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7071 | _Unpickler_MemoCleanup(self); |
| 7072 | self->memo_size = new_memo_size; |
| 7073 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7074 | |
| 7075 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7076 | |
| 7077 | error: |
| 7078 | if (new_memo_size) { |
Miss Islington (bot) | ef4306b | 2018-09-20 18:52:36 -0700 | [diff] [blame] | 7079 | for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7080 | Py_XDECREF(new_memo[i]); |
| 7081 | } |
| 7082 | PyMem_FREE(new_memo); |
| 7083 | } |
| 7084 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7085 | } |
| 7086 | |
| 7087 | static PyObject * |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 7088 | Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7089 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7090 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7091 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7092 | return NULL; |
| 7093 | } |
| 7094 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7095 | } |
| 7096 | |
| 7097 | static int |
Miss Islington (bot) | 5ceb701 | 2018-11-27 09:58:07 -0800 | [diff] [blame] | 7098 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7099 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7100 | if (value == NULL) { |
| 7101 | PyErr_SetString(PyExc_TypeError, |
| 7102 | "attribute deletion is not supported"); |
| 7103 | return -1; |
| 7104 | } |
| 7105 | if (!PyCallable_Check(value)) { |
| 7106 | PyErr_SetString(PyExc_TypeError, |
| 7107 | "persistent_load must be a callable taking " |
| 7108 | "one argument"); |
| 7109 | return -1; |
| 7110 | } |
| 7111 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7112 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7113 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 7114 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7115 | |
| 7116 | return 0; |
| 7117 | } |
| 7118 | |
| 7119 | static PyGetSetDef Unpickler_getsets[] = { |
| 7120 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 7121 | {"persistent_load", (getter)Unpickler_get_persload, |
| 7122 | (setter)Unpickler_set_persload}, |
| 7123 | {NULL} |
| 7124 | }; |
| 7125 | |
| 7126 | static PyTypeObject Unpickler_Type = { |
| 7127 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7128 | "_pickle.Unpickler", /*tp_name*/ |
| 7129 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 7130 | 0, /*tp_itemsize*/ |
| 7131 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 7132 | 0, /*tp_print*/ |
| 7133 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7134 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7135 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7136 | 0, /*tp_repr*/ |
| 7137 | 0, /*tp_as_number*/ |
| 7138 | 0, /*tp_as_sequence*/ |
| 7139 | 0, /*tp_as_mapping*/ |
| 7140 | 0, /*tp_hash*/ |
| 7141 | 0, /*tp_call*/ |
| 7142 | 0, /*tp_str*/ |
| 7143 | 0, /*tp_getattro*/ |
| 7144 | 0, /*tp_setattro*/ |
| 7145 | 0, /*tp_as_buffer*/ |
| 7146 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7147 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7148 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7149 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7150 | 0, /*tp_richcompare*/ |
| 7151 | 0, /*tp_weaklistoffset*/ |
| 7152 | 0, /*tp_iter*/ |
| 7153 | 0, /*tp_iternext*/ |
| 7154 | Unpickler_methods, /*tp_methods*/ |
| 7155 | 0, /*tp_members*/ |
| 7156 | Unpickler_getsets, /*tp_getset*/ |
| 7157 | 0, /*tp_base*/ |
| 7158 | 0, /*tp_dict*/ |
| 7159 | 0, /*tp_descr_get*/ |
| 7160 | 0, /*tp_descr_set*/ |
| 7161 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7162 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7163 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7164 | PyType_GenericNew, /*tp_new*/ |
| 7165 | PyObject_GC_Del, /*tp_free*/ |
| 7166 | 0, /*tp_is_gc*/ |
| 7167 | }; |
| 7168 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7169 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7170 | |
| 7171 | _pickle.dump |
| 7172 | |
| 7173 | obj: object |
| 7174 | file: object |
| 7175 | protocol: object = NULL |
| 7176 | * |
| 7177 | fix_imports: bool = True |
| 7178 | |
| 7179 | Write a pickled representation of obj to the open file object file. |
| 7180 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7181 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7182 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7183 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7184 | The optional *protocol* argument tells the pickler to use the given |
| 7185 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 7186 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7187 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7188 | Specifying a negative protocol version selects the highest protocol |
| 7189 | version supported. The higher the protocol used, the more recent the |
| 7190 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7191 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7192 | The *file* argument must have a write() method that accepts a single |
| 7193 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7194 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7195 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7196 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7197 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7198 | to map the new Python 3 names to the old module names used in Python |
| 7199 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7200 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7201 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7202 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7203 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7204 | PyObject *protocol, int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7205 | /*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7206 | { |
| 7207 | PicklerObject *pickler = _Pickler_New(); |
| 7208 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7209 | if (pickler == NULL) |
| 7210 | return NULL; |
| 7211 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7212 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7213 | goto error; |
| 7214 | |
| 7215 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7216 | goto error; |
| 7217 | |
| 7218 | if (dump(pickler, obj) < 0) |
| 7219 | goto error; |
| 7220 | |
| 7221 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7222 | goto error; |
| 7223 | |
| 7224 | Py_DECREF(pickler); |
| 7225 | Py_RETURN_NONE; |
| 7226 | |
| 7227 | error: |
| 7228 | Py_XDECREF(pickler); |
| 7229 | return NULL; |
| 7230 | } |
| 7231 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7232 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7233 | |
| 7234 | _pickle.dumps |
| 7235 | |
| 7236 | obj: object |
| 7237 | protocol: object = NULL |
| 7238 | * |
| 7239 | fix_imports: bool = True |
| 7240 | |
| 7241 | Return the pickled representation of the object as a bytes object. |
| 7242 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7243 | The optional *protocol* argument tells the pickler to use the given |
| 7244 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7245 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7246 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7247 | Specifying a negative protocol version selects the highest protocol |
| 7248 | version supported. The higher the protocol used, the more recent the |
| 7249 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7250 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7251 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7252 | try to map the new Python 3 names to the old module names used in |
| 7253 | 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] | 7254 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7255 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7256 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7257 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7258 | int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7259 | /*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7260 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7261 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7262 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7263 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7264 | if (pickler == NULL) |
| 7265 | return NULL; |
| 7266 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7267 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7268 | goto error; |
| 7269 | |
| 7270 | if (dump(pickler, obj) < 0) |
| 7271 | goto error; |
| 7272 | |
| 7273 | result = _Pickler_GetString(pickler); |
| 7274 | Py_DECREF(pickler); |
| 7275 | return result; |
| 7276 | |
| 7277 | error: |
| 7278 | Py_XDECREF(pickler); |
| 7279 | return NULL; |
| 7280 | } |
| 7281 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7282 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7283 | |
| 7284 | _pickle.load |
| 7285 | |
| 7286 | file: object |
| 7287 | * |
| 7288 | fix_imports: bool = True |
| 7289 | encoding: str = 'ASCII' |
| 7290 | errors: str = 'strict' |
| 7291 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7292 | 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] | 7293 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7294 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7295 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7296 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7297 | The protocol version of the pickle is detected automatically, so no |
| 7298 | protocol argument is needed. Bytes past the pickled object's |
| 7299 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7300 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7301 | The argument *file* must have two methods, a read() method that takes |
| 7302 | an integer argument, and a readline() method that requires no |
| 7303 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7304 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7305 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7306 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7307 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7308 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7309 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7310 | map the old Python 2 names to the new names used in Python 3. The |
| 7311 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7312 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7313 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7314 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7315 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7316 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7317 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7318 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7319 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7320 | /*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7321 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7322 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7323 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7324 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7325 | if (unpickler == NULL) |
| 7326 | return NULL; |
| 7327 | |
| 7328 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7329 | goto error; |
| 7330 | |
| 7331 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7332 | goto error; |
| 7333 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7334 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7335 | |
| 7336 | result = load(unpickler); |
| 7337 | Py_DECREF(unpickler); |
| 7338 | return result; |
| 7339 | |
| 7340 | error: |
| 7341 | Py_XDECREF(unpickler); |
| 7342 | return NULL; |
| 7343 | } |
| 7344 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7345 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7346 | |
| 7347 | _pickle.loads |
| 7348 | |
| 7349 | data: object |
| 7350 | * |
| 7351 | fix_imports: bool = True |
| 7352 | encoding: str = 'ASCII' |
| 7353 | errors: str = 'strict' |
| 7354 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7355 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7356 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7357 | The protocol version of the pickle is detected automatically, so no |
| 7358 | protocol argument is needed. Bytes past the pickled object's |
| 7359 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7360 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7361 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7362 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7363 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7364 | map the old Python 2 names to the new names used in Python 3. The |
| 7365 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7366 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7367 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7368 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7369 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7370 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7371 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7372 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7373 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7374 | /*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7375 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7376 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7377 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7378 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7379 | if (unpickler == NULL) |
| 7380 | return NULL; |
| 7381 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7382 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7383 | goto error; |
| 7384 | |
| 7385 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7386 | goto error; |
| 7387 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7388 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7389 | |
| 7390 | result = load(unpickler); |
| 7391 | Py_DECREF(unpickler); |
| 7392 | return result; |
| 7393 | |
| 7394 | error: |
| 7395 | Py_XDECREF(unpickler); |
| 7396 | return NULL; |
| 7397 | } |
| 7398 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7399 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7400 | _PICKLE_DUMP_METHODDEF |
| 7401 | _PICKLE_DUMPS_METHODDEF |
| 7402 | _PICKLE_LOAD_METHODDEF |
| 7403 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7404 | {NULL, NULL} /* sentinel */ |
| 7405 | }; |
| 7406 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7407 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7408 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7409 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7410 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7411 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7412 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7413 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7414 | static void |
| 7415 | pickle_free(PyObject *m) |
| 7416 | { |
| 7417 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7418 | } |
| 7419 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7420 | static int |
| 7421 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7422 | { |
| 7423 | PickleState *st = _Pickle_GetState(m); |
| 7424 | Py_VISIT(st->PickleError); |
| 7425 | Py_VISIT(st->PicklingError); |
| 7426 | Py_VISIT(st->UnpicklingError); |
| 7427 | Py_VISIT(st->dispatch_table); |
| 7428 | Py_VISIT(st->extension_registry); |
| 7429 | Py_VISIT(st->extension_cache); |
| 7430 | Py_VISIT(st->inverted_registry); |
| 7431 | Py_VISIT(st->name_mapping_2to3); |
| 7432 | Py_VISIT(st->import_mapping_2to3); |
| 7433 | Py_VISIT(st->name_mapping_3to2); |
| 7434 | Py_VISIT(st->import_mapping_3to2); |
| 7435 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7436 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7437 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7438 | } |
| 7439 | |
| 7440 | static struct PyModuleDef _picklemodule = { |
| 7441 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7442 | "_pickle", /* m_name */ |
| 7443 | pickle_module_doc, /* m_doc */ |
| 7444 | sizeof(PickleState), /* m_size */ |
| 7445 | pickle_methods, /* m_methods */ |
| 7446 | NULL, /* m_reload */ |
| 7447 | pickle_traverse, /* m_traverse */ |
| 7448 | pickle_clear, /* m_clear */ |
| 7449 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7450 | }; |
| 7451 | |
| 7452 | PyMODINIT_FUNC |
| 7453 | PyInit__pickle(void) |
| 7454 | { |
| 7455 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7456 | PickleState *st; |
| 7457 | |
| 7458 | m = PyState_FindModule(&_picklemodule); |
| 7459 | if (m) { |
| 7460 | Py_INCREF(m); |
| 7461 | return m; |
| 7462 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7463 | |
| 7464 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7465 | return NULL; |
| 7466 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7467 | return NULL; |
| 7468 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7469 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7470 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7471 | return NULL; |
| 7472 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7473 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7474 | |
| 7475 | /* Create the module and add the functions. */ |
| 7476 | m = PyModule_Create(&_picklemodule); |
| 7477 | if (m == NULL) |
| 7478 | return NULL; |
| 7479 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7480 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7481 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7482 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7483 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7484 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7485 | return NULL; |
| 7486 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7487 | st = _Pickle_GetState(m); |
| 7488 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7489 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7490 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7491 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7492 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7493 | st->PicklingError = \ |
| 7494 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7495 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7496 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7497 | st->UnpicklingError = \ |
| 7498 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7499 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7500 | return NULL; |
| 7501 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7502 | Py_INCREF(st->PickleError); |
| 7503 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7504 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7505 | Py_INCREF(st->PicklingError); |
| 7506 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7507 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7508 | Py_INCREF(st->UnpicklingError); |
| 7509 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7510 | return NULL; |
| 7511 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7512 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7513 | return NULL; |
| 7514 | |
| 7515 | return m; |
| 7516 | } |