Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 1 | /* pickle accelerator C extensor: _pickle module. |
| 2 | * |
| 3 | * It is built as a built-in module (Py_BUILD_CORE_BUILTIN define) on Windows |
| 4 | * and as an extension module (Py_BUILD_CORE_MODULE define) on other |
| 5 | * platforms. */ |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 6 | |
Victor Stinner | 5c75f37 | 2019-04-17 23:02:26 +0200 | [diff] [blame] | 7 | #if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE) |
| 8 | # error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined" |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 9 | #endif |
| 10 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 11 | #include "Python.h" |
| 12 | #include "structmember.h" |
| 13 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 14 | PyDoc_STRVAR(pickle_module_doc, |
| 15 | "Optimized C implementation for the Python pickle module."); |
| 16 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 17 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 18 | module _pickle |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 19 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 20 | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
| 21 | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
| 22 | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 23 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 24 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 25 | |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 26 | /* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol. |
| 27 | Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python |
| 28 | already includes it. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 29 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 30 | HIGHEST_PROTOCOL = 4, |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 31 | DEFAULT_PROTOCOL = 4 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 32 | }; |
| 33 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 34 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 35 | Extensive docs are in pickletools.py. */ |
| 36 | enum opcode { |
| 37 | MARK = '(', |
| 38 | STOP = '.', |
| 39 | POP = '0', |
| 40 | POP_MARK = '1', |
| 41 | DUP = '2', |
| 42 | FLOAT = 'F', |
| 43 | INT = 'I', |
| 44 | BININT = 'J', |
| 45 | BININT1 = 'K', |
| 46 | LONG = 'L', |
| 47 | BININT2 = 'M', |
| 48 | NONE = 'N', |
| 49 | PERSID = 'P', |
| 50 | BINPERSID = 'Q', |
| 51 | REDUCE = 'R', |
| 52 | STRING = 'S', |
| 53 | BINSTRING = 'T', |
| 54 | SHORT_BINSTRING = 'U', |
| 55 | UNICODE = 'V', |
| 56 | BINUNICODE = 'X', |
| 57 | APPEND = 'a', |
| 58 | BUILD = 'b', |
| 59 | GLOBAL = 'c', |
| 60 | DICT = 'd', |
| 61 | EMPTY_DICT = '}', |
| 62 | APPENDS = 'e', |
| 63 | GET = 'g', |
| 64 | BINGET = 'h', |
| 65 | INST = 'i', |
| 66 | LONG_BINGET = 'j', |
| 67 | LIST = 'l', |
| 68 | EMPTY_LIST = ']', |
| 69 | OBJ = 'o', |
| 70 | PUT = 'p', |
| 71 | BINPUT = 'q', |
| 72 | LONG_BINPUT = 'r', |
| 73 | SETITEM = 's', |
| 74 | TUPLE = 't', |
| 75 | EMPTY_TUPLE = ')', |
| 76 | SETITEMS = 'u', |
| 77 | BINFLOAT = 'G', |
| 78 | |
| 79 | /* Protocol 2. */ |
| 80 | PROTO = '\x80', |
| 81 | NEWOBJ = '\x81', |
| 82 | EXT1 = '\x82', |
| 83 | EXT2 = '\x83', |
| 84 | EXT4 = '\x84', |
| 85 | TUPLE1 = '\x85', |
| 86 | TUPLE2 = '\x86', |
| 87 | TUPLE3 = '\x87', |
| 88 | NEWTRUE = '\x88', |
| 89 | NEWFALSE = '\x89', |
| 90 | LONG1 = '\x8a', |
| 91 | LONG4 = '\x8b', |
| 92 | |
| 93 | /* Protocol 3 (Python 3.x) */ |
| 94 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 95 | SHORT_BINBYTES = 'C', |
| 96 | |
| 97 | /* Protocol 4 */ |
| 98 | SHORT_BINUNICODE = '\x8c', |
| 99 | BINUNICODE8 = '\x8d', |
| 100 | BINBYTES8 = '\x8e', |
| 101 | EMPTY_SET = '\x8f', |
| 102 | ADDITEMS = '\x90', |
| 103 | FROZENSET = '\x91', |
| 104 | NEWOBJ_EX = '\x92', |
| 105 | STACK_GLOBAL = '\x93', |
| 106 | MEMOIZE = '\x94', |
| 107 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 108 | }; |
| 109 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 110 | enum { |
| 111 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 112 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 113 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 114 | help anything either. */ |
| 115 | BATCHSIZE = 1000, |
| 116 | |
| 117 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 118 | checking for self-referential data-structures. */ |
| 119 | FAST_NESTING_LIMIT = 50, |
| 120 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 121 | /* Initial size of the write buffer of Pickler. */ |
| 122 | WRITE_BUF_SIZE = 4096, |
| 123 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 124 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 125 | PREFETCH = 8192 * 16, |
| 126 | |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 127 | FRAME_SIZE_MIN = 4, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 128 | FRAME_SIZE_TARGET = 64 * 1024, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 129 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 132 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 133 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 134 | /* State of the pickle module, per PEP 3121. */ |
| 135 | typedef struct { |
| 136 | /* Exception classes for pickle. */ |
| 137 | PyObject *PickleError; |
| 138 | PyObject *PicklingError; |
| 139 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 140 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 141 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 142 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 143 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 144 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 145 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 146 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 147 | PyObject *extension_registry; |
| 148 | /* copyreg._extension_cache, {code: object} */ |
| 149 | PyObject *extension_cache; |
| 150 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 151 | PyObject *inverted_registry; |
| 152 | |
| 153 | /* Import mappings for compatibility with Python 2.x */ |
| 154 | |
| 155 | /* _compat_pickle.NAME_MAPPING, |
| 156 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 157 | PyObject *name_mapping_2to3; |
| 158 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 159 | PyObject *import_mapping_2to3; |
| 160 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 161 | PyObject *name_mapping_3to2; |
| 162 | PyObject *import_mapping_3to2; |
| 163 | |
| 164 | /* codecs.encode, used for saving bytes in older protocols */ |
| 165 | PyObject *codecs_encode; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 166 | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
| 167 | PyObject *getattr; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 168 | /* functools.partial, used for implementing __newobj_ex__ with protocols |
| 169 | 2 and 3 */ |
| 170 | PyObject *partial; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 171 | } PickleState; |
| 172 | |
| 173 | /* Forward declaration of the _pickle module definition. */ |
| 174 | static struct PyModuleDef _picklemodule; |
| 175 | |
| 176 | /* Given a module object, get its per-module state. */ |
| 177 | static PickleState * |
| 178 | _Pickle_GetState(PyObject *module) |
| 179 | { |
| 180 | return (PickleState *)PyModule_GetState(module); |
| 181 | } |
| 182 | |
| 183 | /* Find the module instance imported in the currently running sub-interpreter |
| 184 | and get its state. */ |
| 185 | static PickleState * |
| 186 | _Pickle_GetGlobalState(void) |
| 187 | { |
| 188 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 189 | } |
| 190 | |
| 191 | /* Clear the given pickle module state. */ |
| 192 | static void |
| 193 | _Pickle_ClearState(PickleState *st) |
| 194 | { |
| 195 | Py_CLEAR(st->PickleError); |
| 196 | Py_CLEAR(st->PicklingError); |
| 197 | Py_CLEAR(st->UnpicklingError); |
| 198 | Py_CLEAR(st->dispatch_table); |
| 199 | Py_CLEAR(st->extension_registry); |
| 200 | Py_CLEAR(st->extension_cache); |
| 201 | Py_CLEAR(st->inverted_registry); |
| 202 | Py_CLEAR(st->name_mapping_2to3); |
| 203 | Py_CLEAR(st->import_mapping_2to3); |
| 204 | Py_CLEAR(st->name_mapping_3to2); |
| 205 | Py_CLEAR(st->import_mapping_3to2); |
| 206 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 207 | Py_CLEAR(st->getattr); |
Victor Stinner | 9ba97df | 2015-11-17 12:15:07 +0100 | [diff] [blame] | 208 | Py_CLEAR(st->partial); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | /* Initialize the given pickle module state. */ |
| 212 | static int |
| 213 | _Pickle_InitState(PickleState *st) |
| 214 | { |
| 215 | PyObject *copyreg = NULL; |
| 216 | PyObject *compat_pickle = NULL; |
| 217 | PyObject *codecs = NULL; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 218 | PyObject *functools = NULL; |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 219 | _Py_IDENTIFIER(getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 220 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 221 | st->getattr = _PyEval_GetBuiltinId(&PyId_getattr); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 222 | if (st->getattr == NULL) |
| 223 | goto error; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 224 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 225 | copyreg = PyImport_ImportModule("copyreg"); |
| 226 | if (!copyreg) |
| 227 | goto error; |
| 228 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 229 | if (!st->dispatch_table) |
| 230 | goto error; |
| 231 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 232 | PyErr_Format(PyExc_RuntimeError, |
| 233 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 234 | Py_TYPE(st->dispatch_table)->tp_name); |
| 235 | goto error; |
| 236 | } |
| 237 | st->extension_registry = \ |
| 238 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 239 | if (!st->extension_registry) |
| 240 | goto error; |
| 241 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 242 | PyErr_Format(PyExc_RuntimeError, |
| 243 | "copyreg._extension_registry should be a dict, " |
| 244 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 245 | goto error; |
| 246 | } |
| 247 | st->inverted_registry = \ |
| 248 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 249 | if (!st->inverted_registry) |
| 250 | goto error; |
| 251 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 252 | PyErr_Format(PyExc_RuntimeError, |
| 253 | "copyreg._inverted_registry should be a dict, " |
| 254 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 255 | goto error; |
| 256 | } |
| 257 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 258 | if (!st->extension_cache) |
| 259 | goto error; |
| 260 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 261 | PyErr_Format(PyExc_RuntimeError, |
| 262 | "copyreg._extension_cache should be a dict, " |
| 263 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 264 | goto error; |
| 265 | } |
| 266 | Py_CLEAR(copyreg); |
| 267 | |
| 268 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 269 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 270 | if (!compat_pickle) |
| 271 | goto error; |
| 272 | st->name_mapping_2to3 = \ |
| 273 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 274 | if (!st->name_mapping_2to3) |
| 275 | goto error; |
| 276 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 277 | PyErr_Format(PyExc_RuntimeError, |
| 278 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 279 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 280 | goto error; |
| 281 | } |
| 282 | st->import_mapping_2to3 = \ |
| 283 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 284 | if (!st->import_mapping_2to3) |
| 285 | goto error; |
| 286 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 287 | PyErr_Format(PyExc_RuntimeError, |
| 288 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 289 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 290 | goto error; |
| 291 | } |
| 292 | /* ... and the 3.x -> 2.x mapping tables */ |
| 293 | st->name_mapping_3to2 = \ |
| 294 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 295 | if (!st->name_mapping_3to2) |
| 296 | goto error; |
| 297 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 298 | PyErr_Format(PyExc_RuntimeError, |
| 299 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 300 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 301 | goto error; |
| 302 | } |
| 303 | st->import_mapping_3to2 = \ |
| 304 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 305 | if (!st->import_mapping_3to2) |
| 306 | goto error; |
| 307 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 308 | PyErr_Format(PyExc_RuntimeError, |
| 309 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 310 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 311 | goto error; |
| 312 | } |
| 313 | Py_CLEAR(compat_pickle); |
| 314 | |
| 315 | codecs = PyImport_ImportModule("codecs"); |
| 316 | if (codecs == NULL) |
| 317 | goto error; |
| 318 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 319 | if (st->codecs_encode == NULL) { |
| 320 | goto error; |
| 321 | } |
| 322 | if (!PyCallable_Check(st->codecs_encode)) { |
| 323 | PyErr_Format(PyExc_RuntimeError, |
| 324 | "codecs.encode should be a callable, not %.200s", |
| 325 | Py_TYPE(st->codecs_encode)->tp_name); |
| 326 | goto error; |
| 327 | } |
| 328 | Py_CLEAR(codecs); |
| 329 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 330 | functools = PyImport_ImportModule("functools"); |
| 331 | if (!functools) |
| 332 | goto error; |
| 333 | st->partial = PyObject_GetAttrString(functools, "partial"); |
| 334 | if (!st->partial) |
| 335 | goto error; |
| 336 | Py_CLEAR(functools); |
| 337 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 338 | return 0; |
| 339 | |
| 340 | error: |
| 341 | Py_CLEAR(copyreg); |
| 342 | Py_CLEAR(compat_pickle); |
| 343 | Py_CLEAR(codecs); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 344 | Py_CLEAR(functools); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 345 | _Pickle_ClearState(st); |
| 346 | return -1; |
| 347 | } |
| 348 | |
| 349 | /* Helper for calling a function with a single argument quickly. |
| 350 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 351 | This function steals the reference of the given argument. */ |
| 352 | static PyObject * |
| 353 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 354 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 355 | PyObject *result; |
| 356 | |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 357 | result = PyObject_CallFunctionObjArgs(func, obj, NULL); |
Victor Stinner | 7521069 | 2016-08-19 18:59:15 +0200 | [diff] [blame] | 358 | Py_DECREF(obj); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 359 | return result; |
| 360 | } |
| 361 | |
| 362 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 363 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 364 | /* Retrieve and deconstruct a method for avoiding a reference cycle |
| 365 | (pickler -> bound method of pickler -> pickler) */ |
| 366 | static int |
| 367 | init_method_ref(PyObject *self, _Py_Identifier *name, |
| 368 | PyObject **method_func, PyObject **method_self) |
| 369 | { |
| 370 | PyObject *func, *func2; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 371 | int ret; |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 372 | |
| 373 | /* *method_func and *method_self should be consistent. All refcount decrements |
| 374 | should be occurred after setting *method_self and *method_func. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 375 | ret = _PyObject_LookupAttrId(self, name, &func); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 376 | if (func == NULL) { |
| 377 | *method_self = NULL; |
| 378 | Py_CLEAR(*method_func); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 379 | return ret; |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) { |
| 383 | /* Deconstruct a bound Python method */ |
| 384 | func2 = PyMethod_GET_FUNCTION(func); |
| 385 | Py_INCREF(func2); |
| 386 | *method_self = self; /* borrowed */ |
| 387 | Py_XSETREF(*method_func, func2); |
| 388 | Py_DECREF(func); |
| 389 | return 0; |
| 390 | } |
| 391 | else { |
| 392 | *method_self = NULL; |
| 393 | Py_XSETREF(*method_func, func); |
| 394 | return 0; |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | /* Bind a method if it was deconstructed */ |
| 399 | static PyObject * |
| 400 | reconstruct_method(PyObject *func, PyObject *self) |
| 401 | { |
| 402 | if (self) { |
| 403 | return PyMethod_New(func, self); |
| 404 | } |
| 405 | else { |
| 406 | Py_INCREF(func); |
| 407 | return func; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | static PyObject * |
| 412 | call_method(PyObject *func, PyObject *self, PyObject *obj) |
| 413 | { |
| 414 | if (self) { |
| 415 | return PyObject_CallFunctionObjArgs(func, self, obj, NULL); |
| 416 | } |
| 417 | else { |
| 418 | return PyObject_CallFunctionObjArgs(func, obj, NULL); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | /*************************************************************************/ |
| 423 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 424 | /* Internal data type used as the unpickling stack. */ |
| 425 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 426 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 427 | PyObject **data; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 428 | int mark_set; /* is MARK set? */ |
| 429 | Py_ssize_t fence; /* position of top MARK or 0 */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 430 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 431 | } Pdata; |
| 432 | |
| 433 | static void |
| 434 | Pdata_dealloc(Pdata *self) |
| 435 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 436 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 437 | while (--i >= 0) { |
| 438 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 439 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 440 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 441 | PyObject_Del(self); |
| 442 | } |
| 443 | |
| 444 | static PyTypeObject Pdata_Type = { |
| 445 | PyVarObject_HEAD_INIT(NULL, 0) |
| 446 | "_pickle.Pdata", /*tp_name*/ |
| 447 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 448 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 449 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 450 | }; |
| 451 | |
| 452 | static PyObject * |
| 453 | Pdata_New(void) |
| 454 | { |
| 455 | Pdata *self; |
| 456 | |
| 457 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 458 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 459 | Py_SIZE(self) = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 460 | self->mark_set = 0; |
| 461 | self->fence = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 462 | self->allocated = 8; |
| 463 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 464 | if (self->data) |
| 465 | return (PyObject *)self; |
| 466 | Py_DECREF(self); |
| 467 | return PyErr_NoMemory(); |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /* Retain only the initial clearto items. If clearto >= the current |
| 472 | * number of items, this is a (non-erroneous) NOP. |
| 473 | */ |
| 474 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 475 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 476 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 477 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 478 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 479 | assert(clearto >= self->fence); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 480 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 481 | return 0; |
| 482 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 483 | while (--i >= clearto) { |
| 484 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 485 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 486 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 487 | return 0; |
| 488 | } |
| 489 | |
| 490 | static int |
| 491 | Pdata_grow(Pdata *self) |
| 492 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 493 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 494 | size_t allocated = (size_t)self->allocated; |
| 495 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 496 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 497 | new_allocated = (allocated >> 3) + 6; |
| 498 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 499 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 500 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 501 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 502 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 503 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 504 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 505 | |
| 506 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 507 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 508 | return 0; |
| 509 | |
| 510 | nomemory: |
| 511 | PyErr_NoMemory(); |
| 512 | return -1; |
| 513 | } |
| 514 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 515 | static int |
| 516 | Pdata_stack_underflow(Pdata *self) |
| 517 | { |
| 518 | PickleState *st = _Pickle_GetGlobalState(); |
| 519 | PyErr_SetString(st->UnpicklingError, |
| 520 | self->mark_set ? |
| 521 | "unexpected MARK found" : |
| 522 | "unpickling stack underflow"); |
| 523 | return -1; |
| 524 | } |
| 525 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 526 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 527 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 528 | * is raised and V is set to NULL. |
| 529 | */ |
| 530 | static PyObject * |
| 531 | Pdata_pop(Pdata *self) |
| 532 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 533 | if (Py_SIZE(self) <= self->fence) { |
| 534 | Pdata_stack_underflow(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 535 | return NULL; |
| 536 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 537 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 538 | } |
| 539 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 540 | |
| 541 | static int |
| 542 | Pdata_push(Pdata *self, PyObject *obj) |
| 543 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 544 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 545 | return -1; |
| 546 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 547 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 548 | return 0; |
| 549 | } |
| 550 | |
| 551 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 552 | #define PDATA_PUSH(D, O, ER) do { \ |
| 553 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 554 | |
| 555 | /* Push an object on stack, adding a new reference to the object. */ |
| 556 | #define PDATA_APPEND(D, O, ER) do { \ |
| 557 | Py_INCREF((O)); \ |
| 558 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 559 | |
| 560 | static PyObject * |
| 561 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 562 | { |
| 563 | PyObject *tuple; |
| 564 | Py_ssize_t len, i, j; |
| 565 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 566 | if (start < self->fence) { |
| 567 | Pdata_stack_underflow(self); |
| 568 | return NULL; |
| 569 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 570 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 571 | tuple = PyTuple_New(len); |
| 572 | if (tuple == NULL) |
| 573 | return NULL; |
| 574 | for (i = start, j = 0; j < len; i++, j++) |
| 575 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 576 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 577 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 578 | return tuple; |
| 579 | } |
| 580 | |
| 581 | static PyObject * |
| 582 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 583 | { |
| 584 | PyObject *list; |
| 585 | Py_ssize_t len, i, j; |
| 586 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 587 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 588 | list = PyList_New(len); |
| 589 | if (list == NULL) |
| 590 | return NULL; |
| 591 | for (i = start, j = 0; j < len; i++, j++) |
| 592 | PyList_SET_ITEM(list, j, self->data[i]); |
| 593 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 594 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 595 | return list; |
| 596 | } |
| 597 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 598 | typedef struct { |
| 599 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 600 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 601 | } PyMemoEntry; |
| 602 | |
| 603 | typedef struct { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 604 | size_t mt_mask; |
| 605 | size_t mt_used; |
| 606 | size_t mt_allocated; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 607 | PyMemoEntry *mt_table; |
| 608 | } PyMemoTable; |
| 609 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 610 | typedef struct PicklerObject { |
| 611 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 612 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 613 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 614 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 615 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 616 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 617 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 618 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 619 | PyObject *reducer_override; /* hook for invoking user-defined callbacks |
| 620 | instead of save_global when pickling |
| 621 | functions and classes*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 622 | |
| 623 | PyObject *write; /* write() method of the output stream. */ |
| 624 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 625 | flushing to the stream. */ |
| 626 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 627 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 628 | int proto; /* Pickle protocol number, >= 0 */ |
| 629 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 630 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 631 | Py_ssize_t frame_start; /* Position in output_buffer where the |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 632 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 633 | is no frame currently open. */ |
| 634 | |
| 635 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 636 | int fast; /* Enable fast mode if set to a true value. |
| 637 | The fast mode disable the usage of memo, |
| 638 | therefore speeding the pickling process by |
| 639 | not generating superfluous PUT opcodes. It |
| 640 | should not be used if with self-referential |
| 641 | objects. */ |
| 642 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 643 | int fix_imports; /* Indicate whether Pickler should fix |
| 644 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 645 | PyObject *fast_memo; |
| 646 | } PicklerObject; |
| 647 | |
| 648 | typedef struct UnpicklerObject { |
| 649 | PyObject_HEAD |
| 650 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 651 | |
| 652 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 653 | is unnecessary, since the keys are contiguous ints. */ |
| 654 | PyObject **memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 655 | size_t memo_size; /* Capacity of the memo array */ |
| 656 | size_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 657 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 658 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 659 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 660 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 661 | |
| 662 | Py_buffer buffer; |
| 663 | char *input_buffer; |
| 664 | char *input_line; |
| 665 | Py_ssize_t input_len; |
| 666 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 667 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 668 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 669 | PyObject *read; /* read() method of the input stream. */ |
| 670 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 671 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 672 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 673 | char *encoding; /* Name of the encoding to be used for |
| 674 | decoding strings pickled using Python |
| 675 | 2.x. The default value is "ASCII" */ |
| 676 | char *errors; /* Name of errors handling scheme to used when |
| 677 | decoding strings. The default value is |
| 678 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 679 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 680 | objects. */ |
| 681 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 682 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 683 | int proto; /* Protocol of the pickle loaded. */ |
| 684 | int fix_imports; /* Indicate whether Unpickler should fix |
| 685 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 686 | } UnpicklerObject; |
| 687 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 688 | typedef struct { |
| 689 | PyObject_HEAD |
| 690 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 691 | } PicklerMemoProxyObject; |
| 692 | |
| 693 | typedef struct { |
| 694 | PyObject_HEAD |
| 695 | UnpicklerObject *unpickler; |
| 696 | } UnpicklerMemoProxyObject; |
| 697 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 698 | /* Forward declarations */ |
| 699 | static int save(PicklerObject *, PyObject *, int); |
| 700 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 701 | static PyTypeObject Pickler_Type; |
| 702 | static PyTypeObject Unpickler_Type; |
| 703 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 704 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 705 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 706 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 707 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 708 | 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] | 709 | a bunch of unnecessary object creation. This makes a huge performance |
| 710 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 711 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 712 | #define MT_MINSIZE 8 |
| 713 | #define PERTURB_SHIFT 5 |
| 714 | |
| 715 | |
| 716 | static PyMemoTable * |
| 717 | PyMemoTable_New(void) |
| 718 | { |
| 719 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 720 | if (memo == NULL) { |
| 721 | PyErr_NoMemory(); |
| 722 | return NULL; |
| 723 | } |
| 724 | |
| 725 | memo->mt_used = 0; |
| 726 | memo->mt_allocated = MT_MINSIZE; |
| 727 | memo->mt_mask = MT_MINSIZE - 1; |
| 728 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 729 | if (memo->mt_table == NULL) { |
| 730 | PyMem_FREE(memo); |
| 731 | PyErr_NoMemory(); |
| 732 | return NULL; |
| 733 | } |
| 734 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 735 | |
| 736 | return memo; |
| 737 | } |
| 738 | |
| 739 | static PyMemoTable * |
| 740 | PyMemoTable_Copy(PyMemoTable *self) |
| 741 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 742 | PyMemoTable *new = PyMemoTable_New(); |
| 743 | if (new == NULL) |
| 744 | return NULL; |
| 745 | |
| 746 | new->mt_used = self->mt_used; |
| 747 | new->mt_allocated = self->mt_allocated; |
| 748 | new->mt_mask = self->mt_mask; |
| 749 | /* The table we get from _New() is probably smaller than we wanted. |
| 750 | Free it and allocate one that's the right size. */ |
| 751 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 752 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 753 | if (new->mt_table == NULL) { |
| 754 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 755 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 756 | return NULL; |
| 757 | } |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 758 | for (size_t i = 0; i < self->mt_allocated; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 759 | Py_XINCREF(self->mt_table[i].me_key); |
| 760 | } |
| 761 | memcpy(new->mt_table, self->mt_table, |
| 762 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 763 | |
| 764 | return new; |
| 765 | } |
| 766 | |
| 767 | static Py_ssize_t |
| 768 | PyMemoTable_Size(PyMemoTable *self) |
| 769 | { |
| 770 | return self->mt_used; |
| 771 | } |
| 772 | |
| 773 | static int |
| 774 | PyMemoTable_Clear(PyMemoTable *self) |
| 775 | { |
| 776 | Py_ssize_t i = self->mt_allocated; |
| 777 | |
| 778 | while (--i >= 0) { |
| 779 | Py_XDECREF(self->mt_table[i].me_key); |
| 780 | } |
| 781 | self->mt_used = 0; |
| 782 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static void |
| 787 | PyMemoTable_Del(PyMemoTable *self) |
| 788 | { |
| 789 | if (self == NULL) |
| 790 | return; |
| 791 | PyMemoTable_Clear(self); |
| 792 | |
| 793 | PyMem_FREE(self->mt_table); |
| 794 | PyMem_FREE(self); |
| 795 | } |
| 796 | |
| 797 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 798 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 799 | static PyMemoEntry * |
| 800 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 801 | { |
| 802 | size_t i; |
| 803 | size_t perturb; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 804 | size_t mask = self->mt_mask; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 805 | PyMemoEntry *table = self->mt_table; |
| 806 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 807 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 808 | |
| 809 | i = hash & mask; |
| 810 | entry = &table[i]; |
| 811 | if (entry->me_key == NULL || entry->me_key == key) |
| 812 | return entry; |
| 813 | |
| 814 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 815 | i = (i << 2) + i + perturb + 1; |
| 816 | entry = &table[i & mask]; |
| 817 | if (entry->me_key == NULL || entry->me_key == key) |
| 818 | return entry; |
| 819 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 820 | Py_UNREACHABLE(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* Returns -1 on failure, 0 on success. */ |
| 824 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 825 | _PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 826 | { |
| 827 | PyMemoEntry *oldtable = NULL; |
| 828 | PyMemoEntry *oldentry, *newentry; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 829 | size_t new_size = MT_MINSIZE; |
| 830 | size_t to_process; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 831 | |
| 832 | assert(min_size > 0); |
| 833 | |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 834 | if (min_size > PY_SSIZE_T_MAX) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 835 | PyErr_NoMemory(); |
| 836 | return -1; |
| 837 | } |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 838 | |
| 839 | /* Find the smallest valid table size >= min_size. */ |
| 840 | while (new_size < min_size) { |
| 841 | new_size <<= 1; |
| 842 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 843 | /* new_size needs to be a power of two. */ |
| 844 | assert((new_size & (new_size - 1)) == 0); |
| 845 | |
| 846 | /* Allocate new table. */ |
| 847 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 848 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 849 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 850 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 851 | PyErr_NoMemory(); |
| 852 | return -1; |
| 853 | } |
| 854 | self->mt_allocated = new_size; |
| 855 | self->mt_mask = new_size - 1; |
| 856 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 857 | |
| 858 | /* Copy entries from the old table. */ |
| 859 | to_process = self->mt_used; |
| 860 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 861 | if (oldentry->me_key != NULL) { |
| 862 | to_process--; |
| 863 | /* newentry is a pointer to a chunk of the new |
| 864 | mt_table, so we're setting the key:value pair |
| 865 | in-place. */ |
| 866 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 867 | newentry->me_key = oldentry->me_key; |
| 868 | newentry->me_value = oldentry->me_value; |
| 869 | } |
| 870 | } |
| 871 | |
| 872 | /* Deallocate the old table. */ |
| 873 | PyMem_FREE(oldtable); |
| 874 | return 0; |
| 875 | } |
| 876 | |
| 877 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 878 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 879 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 880 | { |
| 881 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 882 | if (entry->me_key == NULL) |
| 883 | return NULL; |
| 884 | return &entry->me_value; |
| 885 | } |
| 886 | |
| 887 | /* Returns -1 on failure, 0 on success. */ |
| 888 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 889 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 890 | { |
| 891 | PyMemoEntry *entry; |
| 892 | |
| 893 | assert(key != NULL); |
| 894 | |
| 895 | entry = _PyMemoTable_Lookup(self, key); |
| 896 | if (entry->me_key != NULL) { |
| 897 | entry->me_value = value; |
| 898 | return 0; |
| 899 | } |
| 900 | Py_INCREF(key); |
| 901 | entry->me_key = key; |
| 902 | entry->me_value = value; |
| 903 | self->mt_used++; |
| 904 | |
| 905 | /* If we added a key, we can safely resize. Otherwise just return! |
| 906 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 907 | * |
| 908 | * Quadrupling the size improves average table sparseness |
| 909 | * (reducing collisions) at the cost of some memory. It also halves |
| 910 | * the number of expensive resize operations in a growing memo table. |
| 911 | * |
| 912 | * Very large memo tables (over 50K items) use doubling instead. |
| 913 | * This may help applications with severe memory constraints. |
| 914 | */ |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 915 | 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] | 916 | return 0; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 917 | } |
| 918 | // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow. |
| 919 | size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used; |
| 920 | return _PyMemoTable_ResizeTable(self, desired_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 921 | } |
| 922 | |
| 923 | #undef MT_MINSIZE |
| 924 | #undef PERTURB_SHIFT |
| 925 | |
| 926 | /*************************************************************************/ |
| 927 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 928 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 929 | static int |
| 930 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 931 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 932 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 933 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 935 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 936 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 937 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 938 | return 0; |
| 939 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 941 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 942 | _write_size64(char *out, size_t value) |
| 943 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 944 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 945 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 946 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 947 | |
| 948 | for (i = 0; i < sizeof(size_t); i++) { |
| 949 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 950 | } |
| 951 | for (i = sizeof(size_t); i < 8; i++) { |
| 952 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 953 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 954 | } |
| 955 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 956 | static int |
| 957 | _Pickler_CommitFrame(PicklerObject *self) |
| 958 | { |
| 959 | size_t frame_len; |
| 960 | char *qdata; |
| 961 | |
| 962 | if (!self->framing || self->frame_start == -1) |
| 963 | return 0; |
| 964 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 965 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 966 | if (frame_len >= FRAME_SIZE_MIN) { |
| 967 | qdata[0] = FRAME; |
| 968 | _write_size64(qdata + 1, frame_len); |
| 969 | } |
| 970 | else { |
| 971 | memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len); |
| 972 | self->output_len -= FRAME_HEADER_SIZE; |
| 973 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 974 | self->frame_start = -1; |
| 975 | return 0; |
| 976 | } |
| 977 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 978 | static PyObject * |
| 979 | _Pickler_GetString(PicklerObject *self) |
| 980 | { |
| 981 | PyObject *output_buffer = self->output_buffer; |
| 982 | |
| 983 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 984 | |
| 985 | if (_Pickler_CommitFrame(self)) |
| 986 | return NULL; |
| 987 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 988 | self->output_buffer = NULL; |
| 989 | /* Resize down to exact size */ |
| 990 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 991 | return NULL; |
| 992 | return output_buffer; |
| 993 | } |
| 994 | |
| 995 | static int |
| 996 | _Pickler_FlushToFile(PicklerObject *self) |
| 997 | { |
| 998 | PyObject *output, *result; |
| 999 | |
| 1000 | assert(self->write != NULL); |
| 1001 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1002 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1003 | output = _Pickler_GetString(self); |
| 1004 | if (output == NULL) |
| 1005 | return -1; |
| 1006 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1007 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1008 | Py_XDECREF(result); |
| 1009 | return (result == NULL) ? -1 : 0; |
| 1010 | } |
| 1011 | |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1012 | static int |
| 1013 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 1014 | { |
| 1015 | Py_ssize_t frame_len; |
| 1016 | |
| 1017 | if (!self->framing || self->frame_start == -1) { |
| 1018 | return 0; |
| 1019 | } |
| 1020 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 1021 | if (frame_len >= FRAME_SIZE_TARGET) { |
| 1022 | if(_Pickler_CommitFrame(self)) { |
| 1023 | return -1; |
| 1024 | } |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 1025 | /* Flush the content of the committed frame to the underlying |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1026 | * file and reuse the pickler buffer for the next frame so as |
| 1027 | * to limit memory usage when dumping large complex objects to |
| 1028 | * a file. |
| 1029 | * |
| 1030 | * self->write is NULL when called via dumps. |
| 1031 | */ |
| 1032 | if (self->write != NULL) { |
| 1033 | if (_Pickler_FlushToFile(self) < 0) { |
| 1034 | return -1; |
| 1035 | } |
| 1036 | if (_Pickler_ClearBuffer(self) < 0) { |
| 1037 | return -1; |
| 1038 | } |
| 1039 | } |
| 1040 | } |
| 1041 | return 0; |
| 1042 | } |
| 1043 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1044 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1045 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1046 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1047 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1048 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1049 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1050 | |
| 1051 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1052 | need_new_frame = (self->framing && self->frame_start == -1); |
| 1053 | |
| 1054 | if (need_new_frame) |
| 1055 | n = data_len + FRAME_HEADER_SIZE; |
| 1056 | else |
| 1057 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1058 | |
| 1059 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1060 | if (required > self->max_output_len) { |
| 1061 | /* Make place in buffer for the pickle chunk */ |
| 1062 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 1063 | PyErr_NoMemory(); |
| 1064 | return -1; |
| 1065 | } |
| 1066 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 1067 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 1068 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1069 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1070 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1071 | if (need_new_frame) { |
| 1072 | /* Setup new frame */ |
| 1073 | Py_ssize_t frame_start = self->output_len; |
| 1074 | self->frame_start = frame_start; |
| 1075 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 1076 | /* Write an invalid value, for debugging */ |
| 1077 | buffer[frame_start + i] = 0xFE; |
| 1078 | } |
| 1079 | self->output_len += FRAME_HEADER_SIZE; |
| 1080 | } |
| 1081 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1082 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1083 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1084 | buffer[self->output_len + i] = s[i]; |
| 1085 | } |
| 1086 | } |
| 1087 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1088 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1089 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1090 | self->output_len += data_len; |
| 1091 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1092 | } |
| 1093 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1094 | static PicklerObject * |
| 1095 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1096 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1097 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1098 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1099 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1100 | if (self == NULL) |
| 1101 | return NULL; |
| 1102 | |
| 1103 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1104 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1105 | self->write = NULL; |
| 1106 | self->proto = 0; |
| 1107 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1108 | self->framing = 0; |
| 1109 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1110 | self->fast = 0; |
| 1111 | self->fast_nesting = 0; |
| 1112 | self->fix_imports = 0; |
| 1113 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1114 | self->max_output_len = WRITE_BUF_SIZE; |
| 1115 | self->output_len = 0; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 1116 | self->reducer_override = NULL; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1117 | |
| 1118 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1119 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1120 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1121 | |
| 1122 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1123 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1124 | return NULL; |
| 1125 | } |
Zackery Spytz | 359bd4f | 2019-04-23 05:56:08 -0600 | [diff] [blame] | 1126 | |
| 1127 | PyObject_GC_Track(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1128 | return self; |
| 1129 | } |
| 1130 | |
| 1131 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1132 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1133 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1134 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1135 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1136 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1137 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1138 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1139 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1140 | proto = PyLong_AsLong(protocol); |
| 1141 | if (proto < 0) { |
| 1142 | if (proto == -1 && PyErr_Occurred()) |
| 1143 | return -1; |
| 1144 | proto = HIGHEST_PROTOCOL; |
| 1145 | } |
| 1146 | else if (proto > HIGHEST_PROTOCOL) { |
| 1147 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1148 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1149 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1150 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1151 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1152 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1153 | self->bin = proto > 0; |
| 1154 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1155 | return 0; |
| 1156 | } |
| 1157 | |
| 1158 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1159 | be called once on a freshly created Pickler. */ |
| 1160 | static int |
| 1161 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1162 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1163 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1164 | assert(file != NULL); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1165 | if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) { |
| 1166 | return -1; |
| 1167 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1168 | if (self->write == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1169 | PyErr_SetString(PyExc_TypeError, |
| 1170 | "file must have a 'write' attribute"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1171 | return -1; |
| 1172 | } |
| 1173 | |
| 1174 | return 0; |
| 1175 | } |
| 1176 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1177 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1178 | own reference to `input`. */ |
| 1179 | static Py_ssize_t |
| 1180 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1181 | { |
| 1182 | if (self->buffer.buf != NULL) |
| 1183 | PyBuffer_Release(&self->buffer); |
| 1184 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1185 | return -1; |
| 1186 | self->input_buffer = self->buffer.buf; |
| 1187 | self->input_len = self->buffer.len; |
| 1188 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1189 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1190 | return self->input_len; |
| 1191 | } |
| 1192 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1193 | static int |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1194 | bad_readline(void) |
| 1195 | { |
| 1196 | PickleState *st = _Pickle_GetGlobalState(); |
| 1197 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
| 1198 | return -1; |
| 1199 | } |
| 1200 | |
| 1201 | static int |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1202 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1203 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1204 | Py_ssize_t consumed; |
| 1205 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1206 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1207 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1208 | if (consumed <= 0) |
| 1209 | return 0; |
| 1210 | |
| 1211 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1212 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1213 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1214 | if (r == NULL) |
| 1215 | return -1; |
| 1216 | Py_DECREF(r); |
| 1217 | |
| 1218 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1219 | return 0; |
| 1220 | } |
| 1221 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1222 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1223 | |
| 1224 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1225 | may be multiple pickle objects arranged contiguously in the same input |
| 1226 | buffer. |
| 1227 | |
| 1228 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1229 | bytes from the input stream/buffer. |
| 1230 | |
| 1231 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1232 | failure; on success, returns the number of bytes read from the file. |
| 1233 | |
| 1234 | On success, self->input_len will be 0; this is intentional so that when |
| 1235 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1236 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1237 | size to tell you how much data you can process. */ |
| 1238 | static Py_ssize_t |
| 1239 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1240 | { |
| 1241 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1242 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1243 | |
| 1244 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1245 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1246 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1247 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1248 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1249 | if (n == READ_WHOLE_LINE) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 1250 | data = _PyObject_CallNoArg(self->readline); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1251 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1252 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1253 | PyObject *len; |
| 1254 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1255 | if (self->peek && n < PREFETCH) { |
| 1256 | len = PyLong_FromSsize_t(PREFETCH); |
| 1257 | if (len == NULL) |
| 1258 | return -1; |
| 1259 | data = _Pickle_FastCall(self->peek, len); |
| 1260 | if (data == NULL) { |
| 1261 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1262 | return -1; |
| 1263 | /* peek() is probably not supported by the given file object */ |
| 1264 | PyErr_Clear(); |
| 1265 | Py_CLEAR(self->peek); |
| 1266 | } |
| 1267 | else { |
| 1268 | read_size = _Unpickler_SetStringInput(self, data); |
| 1269 | Py_DECREF(data); |
| 1270 | self->prefetched_idx = 0; |
| 1271 | if (n <= read_size) |
| 1272 | return n; |
| 1273 | } |
| 1274 | } |
| 1275 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1276 | if (len == NULL) |
| 1277 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1278 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1279 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1280 | if (data == NULL) |
| 1281 | return -1; |
| 1282 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1283 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1284 | Py_DECREF(data); |
| 1285 | return read_size; |
| 1286 | } |
| 1287 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1288 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1289 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1290 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1291 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1292 | Py_ssize_t num_read; |
| 1293 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1294 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1295 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1296 | PickleState *st = _Pickle_GetGlobalState(); |
| 1297 | PyErr_SetString(st->UnpicklingError, |
| 1298 | "read would overflow (invalid bytecode)"); |
| 1299 | return -1; |
| 1300 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1301 | |
| 1302 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1303 | assert(self->next_read_idx + n > self->input_len); |
| 1304 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1305 | if (!self->read) |
| 1306 | return bad_readline(); |
| 1307 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1308 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1309 | if (num_read < 0) |
| 1310 | return -1; |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1311 | if (num_read < n) |
| 1312 | return bad_readline(); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1313 | *s = self->input_buffer; |
| 1314 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1315 | return n; |
| 1316 | } |
| 1317 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1318 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1319 | |
| 1320 | This should be used for all data reads, rather than accessing the unpickler's |
| 1321 | input buffer directly. This method deals correctly with reading from input |
| 1322 | streams, which the input buffer doesn't deal with. |
| 1323 | |
| 1324 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1325 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1326 | should use this function's return value to know how many bytes you can |
| 1327 | consume. |
| 1328 | |
| 1329 | Returns -1 (with an exception set) on failure. On success, return the |
| 1330 | number of chars read. */ |
| 1331 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1332 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1333 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1334 | (self)->next_read_idx += (n), \ |
| 1335 | (n)) \ |
| 1336 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1337 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1338 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1339 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1340 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1341 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1342 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1343 | if (input_line == NULL) { |
| 1344 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1345 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1346 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1347 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1348 | memcpy(input_line, line, len); |
| 1349 | input_line[len] = '\0'; |
| 1350 | self->input_line = input_line; |
| 1351 | *result = self->input_line; |
| 1352 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1353 | } |
| 1354 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1355 | /* 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] | 1356 | before hitting \n, raise an error. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1357 | |
| 1358 | Returns the number of chars read, or -1 on failure. */ |
| 1359 | static Py_ssize_t |
| 1360 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1361 | { |
| 1362 | Py_ssize_t i, num_read; |
| 1363 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1364 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1365 | if (self->input_buffer[i] == '\n') { |
| 1366 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1367 | num_read = i - self->next_read_idx + 1; |
| 1368 | self->next_read_idx = i + 1; |
| 1369 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1370 | } |
| 1371 | } |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1372 | if (!self->read) |
| 1373 | return bad_readline(); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1374 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1375 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1376 | if (num_read < 0) |
| 1377 | return -1; |
| 1378 | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
| 1379 | return bad_readline(); |
| 1380 | self->next_read_idx = num_read; |
| 1381 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1382 | } |
| 1383 | |
| 1384 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1385 | will be modified in place. */ |
| 1386 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1387 | _Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1388 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1389 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1390 | |
| 1391 | assert(new_size > self->memo_size); |
| 1392 | |
Sergey Fedoseev | 67b9cc8 | 2018-08-16 09:27:50 +0500 | [diff] [blame] | 1393 | PyObject **memo_new = self->memo; |
| 1394 | PyMem_RESIZE(memo_new, PyObject *, new_size); |
| 1395 | if (memo_new == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1396 | PyErr_NoMemory(); |
| 1397 | return -1; |
| 1398 | } |
Sergey Fedoseev | 67b9cc8 | 2018-08-16 09:27:50 +0500 | [diff] [blame] | 1399 | self->memo = memo_new; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1400 | for (i = self->memo_size; i < new_size; i++) |
| 1401 | self->memo[i] = NULL; |
| 1402 | self->memo_size = new_size; |
| 1403 | return 0; |
| 1404 | } |
| 1405 | |
| 1406 | /* Returns NULL if idx is out of bounds. */ |
| 1407 | static PyObject * |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1408 | _Unpickler_MemoGet(UnpicklerObject *self, size_t idx) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1409 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1410 | if (idx >= self->memo_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1411 | return NULL; |
| 1412 | |
| 1413 | return self->memo[idx]; |
| 1414 | } |
| 1415 | |
| 1416 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1417 | This takes its own reference to `value`. */ |
| 1418 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1419 | _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1420 | { |
| 1421 | PyObject *old_item; |
| 1422 | |
| 1423 | if (idx >= self->memo_size) { |
| 1424 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1425 | return -1; |
| 1426 | assert(idx < self->memo_size); |
| 1427 | } |
| 1428 | Py_INCREF(value); |
| 1429 | old_item = self->memo[idx]; |
| 1430 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1431 | if (old_item != NULL) { |
| 1432 | Py_DECREF(old_item); |
| 1433 | } |
| 1434 | else { |
| 1435 | self->memo_len++; |
| 1436 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static PyObject ** |
| 1441 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1442 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1443 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1444 | if (memo == NULL) { |
| 1445 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1446 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1447 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1448 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1449 | return memo; |
| 1450 | } |
| 1451 | |
| 1452 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1453 | static void |
| 1454 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1455 | { |
| 1456 | Py_ssize_t i; |
| 1457 | PyObject **memo = self->memo; |
| 1458 | |
| 1459 | if (self->memo == NULL) |
| 1460 | return; |
| 1461 | self->memo = NULL; |
| 1462 | i = self->memo_size; |
| 1463 | while (--i >= 0) { |
| 1464 | Py_XDECREF(memo[i]); |
| 1465 | } |
| 1466 | PyMem_FREE(memo); |
| 1467 | } |
| 1468 | |
| 1469 | static UnpicklerObject * |
| 1470 | _Unpickler_New(void) |
| 1471 | { |
| 1472 | UnpicklerObject *self; |
| 1473 | |
| 1474 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1475 | if (self == NULL) |
| 1476 | return NULL; |
| 1477 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1478 | self->pers_func = NULL; |
| 1479 | self->input_buffer = NULL; |
| 1480 | self->input_line = NULL; |
| 1481 | self->input_len = 0; |
| 1482 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1483 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1484 | self->read = NULL; |
| 1485 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1486 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1487 | self->encoding = NULL; |
| 1488 | self->errors = NULL; |
| 1489 | self->marks = NULL; |
| 1490 | self->num_marks = 0; |
| 1491 | self->marks_size = 0; |
| 1492 | self->proto = 0; |
| 1493 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1494 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1495 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1496 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1497 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1498 | self->stack = (Pdata *)Pdata_New(); |
| 1499 | |
| 1500 | if (self->memo == NULL || self->stack == NULL) { |
| 1501 | Py_DECREF(self); |
| 1502 | return NULL; |
| 1503 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1504 | |
Zackery Spytz | 359bd4f | 2019-04-23 05:56:08 -0600 | [diff] [blame] | 1505 | PyObject_GC_Track(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1506 | return self; |
| 1507 | } |
| 1508 | |
| 1509 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1510 | be called once on a freshly created Pickler. */ |
| 1511 | static int |
| 1512 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1513 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1514 | _Py_IDENTIFIER(peek); |
| 1515 | _Py_IDENTIFIER(read); |
| 1516 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1517 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1518 | if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) { |
| 1519 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1520 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1521 | (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read); |
| 1522 | (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1523 | if (self->readline == NULL || self->read == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1524 | if (!PyErr_Occurred()) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1525 | PyErr_SetString(PyExc_TypeError, |
| 1526 | "file must have 'read' and 'readline' attributes"); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1527 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1528 | Py_CLEAR(self->read); |
| 1529 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1530 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1531 | return -1; |
| 1532 | } |
| 1533 | return 0; |
| 1534 | } |
| 1535 | |
| 1536 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1537 | be called once on a freshly created Pickler. */ |
| 1538 | static int |
| 1539 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1540 | const char *encoding, |
| 1541 | const char *errors) |
| 1542 | { |
| 1543 | if (encoding == NULL) |
| 1544 | encoding = "ASCII"; |
| 1545 | if (errors == NULL) |
| 1546 | errors = "strict"; |
| 1547 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1548 | self->encoding = _PyMem_Strdup(encoding); |
| 1549 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1550 | if (self->encoding == NULL || self->errors == NULL) { |
| 1551 | PyErr_NoMemory(); |
| 1552 | return -1; |
| 1553 | } |
| 1554 | return 0; |
| 1555 | } |
| 1556 | |
| 1557 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1558 | static int |
| 1559 | memo_get(PicklerObject *self, PyObject *key) |
| 1560 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1561 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1562 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1563 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1564 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1565 | value = PyMemoTable_Get(self->memo, key); |
| 1566 | if (value == NULL) { |
| 1567 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1568 | return -1; |
| 1569 | } |
| 1570 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1571 | if (!self->bin) { |
| 1572 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1573 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1574 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1575 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1576 | } |
| 1577 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1578 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1579 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1580 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1581 | len = 2; |
| 1582 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1583 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1584 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1585 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1586 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1587 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1588 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1589 | len = 5; |
| 1590 | } |
| 1591 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1592 | PickleState *st = _Pickle_GetGlobalState(); |
| 1593 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1594 | "memo id too large for LONG_BINGET"); |
| 1595 | return -1; |
| 1596 | } |
| 1597 | } |
| 1598 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1599 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1600 | return -1; |
| 1601 | |
| 1602 | return 0; |
| 1603 | } |
| 1604 | |
| 1605 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1606 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1607 | static int |
| 1608 | memo_put(PicklerObject *self, PyObject *obj) |
| 1609 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1610 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1611 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1612 | Py_ssize_t idx; |
| 1613 | |
| 1614 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1615 | |
| 1616 | if (self->fast) |
| 1617 | return 0; |
| 1618 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1619 | idx = PyMemoTable_Size(self->memo); |
| 1620 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1621 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1622 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1623 | if (self->proto >= 4) { |
| 1624 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1625 | return -1; |
| 1626 | return 0; |
| 1627 | } |
| 1628 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1629 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1630 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1631 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1632 | len = strlen(pdata); |
| 1633 | } |
| 1634 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1635 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1636 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1637 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1638 | len = 2; |
| 1639 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1640 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1641 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1642 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1643 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1644 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1645 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1646 | len = 5; |
| 1647 | } |
| 1648 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1649 | PickleState *st = _Pickle_GetGlobalState(); |
| 1650 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1651 | "memo id too large for LONG_BINPUT"); |
| 1652 | return -1; |
| 1653 | } |
| 1654 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1655 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1656 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1657 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1658 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | static PyObject * |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1662 | get_dotted_path(PyObject *obj, PyObject *name) |
| 1663 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1664 | _Py_static_string(PyId_dot, "."); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1665 | PyObject *dotted_path; |
| 1666 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1667 | |
| 1668 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1669 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1670 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1671 | n = PyList_GET_SIZE(dotted_path); |
| 1672 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1673 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1674 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1675 | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1676 | if (obj == NULL) |
| 1677 | PyErr_Format(PyExc_AttributeError, |
| 1678 | "Can't pickle local object %R", name); |
| 1679 | else |
| 1680 | PyErr_Format(PyExc_AttributeError, |
| 1681 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1682 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1683 | return NULL; |
| 1684 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1685 | } |
| 1686 | return dotted_path; |
| 1687 | } |
| 1688 | |
| 1689 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1690 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1691 | { |
| 1692 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1693 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1694 | |
| 1695 | assert(PyList_CheckExact(names)); |
| 1696 | Py_INCREF(obj); |
| 1697 | n = PyList_GET_SIZE(names); |
| 1698 | for (i = 0; i < n; i++) { |
| 1699 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1700 | Py_XDECREF(parent); |
| 1701 | parent = obj; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1702 | (void)_PyObject_LookupAttr(parent, name, &obj); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1703 | if (obj == NULL) { |
| 1704 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1705 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1706 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1707 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1708 | if (pparent != NULL) |
| 1709 | *pparent = parent; |
| 1710 | else |
| 1711 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1712 | return obj; |
| 1713 | } |
| 1714 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1715 | |
| 1716 | static PyObject * |
| 1717 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1718 | { |
| 1719 | PyObject *dotted_path, *attr; |
| 1720 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1721 | if (allow_qualname) { |
| 1722 | dotted_path = get_dotted_path(obj, name); |
| 1723 | if (dotted_path == NULL) |
| 1724 | return NULL; |
| 1725 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1726 | Py_DECREF(dotted_path); |
| 1727 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1728 | else { |
| 1729 | (void)_PyObject_LookupAttr(obj, name, &attr); |
| 1730 | } |
| 1731 | if (attr == NULL && !PyErr_Occurred()) { |
| 1732 | PyErr_Format(PyExc_AttributeError, |
| 1733 | "Can't get attribute %R on %R", name, obj); |
| 1734 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1735 | return attr; |
| 1736 | } |
| 1737 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1738 | static int |
| 1739 | _checkmodule(PyObject *module_name, PyObject *module, |
| 1740 | PyObject *global, PyObject *dotted_path) |
| 1741 | { |
| 1742 | if (module == Py_None) { |
| 1743 | return -1; |
| 1744 | } |
| 1745 | if (PyUnicode_Check(module_name) && |
| 1746 | _PyUnicode_EqualToASCIIString(module_name, "__main__")) { |
| 1747 | return -1; |
| 1748 | } |
| 1749 | |
| 1750 | PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); |
| 1751 | if (candidate == NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1752 | return -1; |
| 1753 | } |
| 1754 | if (candidate != global) { |
| 1755 | Py_DECREF(candidate); |
| 1756 | return -1; |
| 1757 | } |
| 1758 | Py_DECREF(candidate); |
| 1759 | return 0; |
| 1760 | } |
| 1761 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1762 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1763 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1764 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1765 | PyObject *module_name; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1766 | PyObject *module = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1767 | Py_ssize_t i; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1768 | PyObject *modules; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1769 | _Py_IDENTIFIER(__module__); |
| 1770 | _Py_IDENTIFIER(modules); |
| 1771 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1772 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1773 | if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) { |
| 1774 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1775 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1776 | if (module_name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1777 | /* In some rare cases (e.g., bound methods of extension types), |
| 1778 | __module__ can be None. If it is so, then search sys.modules for |
| 1779 | the module of global. */ |
| 1780 | if (module_name != Py_None) |
| 1781 | return module_name; |
| 1782 | Py_CLEAR(module_name); |
| 1783 | } |
| 1784 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1785 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1786 | /* Fallback on walking sys.modules */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1787 | modules = _PySys_GetObjectId(&PyId_modules); |
| 1788 | if (modules == NULL) { |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1789 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1790 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1791 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1792 | if (PyDict_CheckExact(modules)) { |
| 1793 | i = 0; |
| 1794 | while (PyDict_Next(modules, &i, &module_name, &module)) { |
| 1795 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1796 | Py_INCREF(module_name); |
| 1797 | return module_name; |
| 1798 | } |
| 1799 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1800 | return NULL; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1801 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1802 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1803 | } |
| 1804 | else { |
| 1805 | PyObject *iterator = PyObject_GetIter(modules); |
| 1806 | if (iterator == NULL) { |
| 1807 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1808 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1809 | while ((module_name = PyIter_Next(iterator))) { |
| 1810 | module = PyObject_GetItem(modules, module_name); |
| 1811 | if (module == NULL) { |
| 1812 | Py_DECREF(module_name); |
| 1813 | Py_DECREF(iterator); |
| 1814 | return NULL; |
| 1815 | } |
| 1816 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1817 | Py_DECREF(module); |
| 1818 | Py_DECREF(iterator); |
| 1819 | return module_name; |
| 1820 | } |
| 1821 | Py_DECREF(module); |
| 1822 | Py_DECREF(module_name); |
| 1823 | if (PyErr_Occurred()) { |
| 1824 | Py_DECREF(iterator); |
| 1825 | return NULL; |
| 1826 | } |
| 1827 | } |
| 1828 | Py_DECREF(iterator); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
| 1831 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1832 | module_name = _PyUnicode_FromId(&PyId___main__); |
Victor Stinner | af46eb8 | 2017-09-05 23:30:16 +0200 | [diff] [blame] | 1833 | Py_XINCREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1834 | return module_name; |
| 1835 | } |
| 1836 | |
| 1837 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1838 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1839 | memoization disabled). If the nesting of a list or dict object exceed |
| 1840 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1841 | reference to the seen list or dict objects and check whether these objects |
| 1842 | are recursive. These are not strictly necessary, since save() has a |
| 1843 | hard-coded recursion limit, but they give a nicer error message than the |
| 1844 | typical RuntimeError. */ |
| 1845 | static int |
| 1846 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1847 | { |
| 1848 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1849 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1850 | PyObject *key = NULL; |
| 1851 | if (self->fast_memo == NULL) { |
| 1852 | self->fast_memo = PyDict_New(); |
| 1853 | if (self->fast_memo == NULL) { |
| 1854 | self->fast_nesting = -1; |
| 1855 | return 0; |
| 1856 | } |
| 1857 | } |
| 1858 | key = PyLong_FromVoidPtr(obj); |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1859 | if (key == NULL) { |
| 1860 | self->fast_nesting = -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1861 | return 0; |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1862 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1863 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1864 | Py_DECREF(key); |
| 1865 | PyErr_Format(PyExc_ValueError, |
| 1866 | "fast mode: can't pickle cyclic objects " |
| 1867 | "including object type %.200s at %p", |
| 1868 | obj->ob_type->tp_name, obj); |
| 1869 | self->fast_nesting = -1; |
| 1870 | return 0; |
| 1871 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1872 | if (PyErr_Occurred()) { |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1873 | Py_DECREF(key); |
| 1874 | self->fast_nesting = -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1875 | return 0; |
| 1876 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1877 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1878 | Py_DECREF(key); |
| 1879 | self->fast_nesting = -1; |
| 1880 | return 0; |
| 1881 | } |
| 1882 | Py_DECREF(key); |
| 1883 | } |
| 1884 | return 1; |
| 1885 | } |
| 1886 | |
| 1887 | static int |
| 1888 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1889 | { |
| 1890 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1891 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1892 | if (key == NULL) |
| 1893 | return 0; |
| 1894 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1895 | Py_DECREF(key); |
| 1896 | return 0; |
| 1897 | } |
| 1898 | Py_DECREF(key); |
| 1899 | } |
| 1900 | return 1; |
| 1901 | } |
| 1902 | |
| 1903 | static int |
| 1904 | save_none(PicklerObject *self, PyObject *obj) |
| 1905 | { |
| 1906 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1907 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1908 | return -1; |
| 1909 | |
| 1910 | return 0; |
| 1911 | } |
| 1912 | |
| 1913 | static int |
| 1914 | save_bool(PicklerObject *self, PyObject *obj) |
| 1915 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1916 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1917 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1918 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1919 | return -1; |
| 1920 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1921 | else { |
| 1922 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1923 | * so that unpicklers written before bools were introduced unpickle them |
| 1924 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1925 | * Note that protocol 2 added direct ways to pickle bools. |
| 1926 | */ |
| 1927 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1928 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1929 | return -1; |
| 1930 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
| 1933 | |
| 1934 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1935 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1936 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1937 | PyObject *repr = NULL; |
| 1938 | Py_ssize_t size; |
| 1939 | long val; |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1940 | int overflow; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1941 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1942 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1943 | val= PyLong_AsLongAndOverflow(obj, &overflow); |
| 1944 | if (!overflow && (sizeof(long) <= 4 || |
| 1945 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) |
| 1946 | { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1947 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1948 | |
| 1949 | Note: we can't use -0x80000000L in the above condition because some |
| 1950 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1951 | before applying the unary minus when sizeof(long) <= 4. The |
| 1952 | resulting value stays unsigned which is commonly not what we want, |
| 1953 | so MSVC happily warns us about it. However, that result would have |
| 1954 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1955 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1956 | char pdata[32]; |
| 1957 | Py_ssize_t len = 0; |
| 1958 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1959 | if (self->bin) { |
| 1960 | pdata[1] = (unsigned char)(val & 0xff); |
| 1961 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1962 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1963 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1964 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1965 | if ((pdata[4] != 0) || (pdata[3] != 0)) { |
| 1966 | pdata[0] = BININT; |
| 1967 | len = 5; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1968 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1969 | else if (pdata[2] != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1970 | pdata[0] = BININT2; |
| 1971 | len = 3; |
| 1972 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1973 | else { |
| 1974 | pdata[0] = BININT1; |
| 1975 | len = 2; |
| 1976 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1977 | } |
| 1978 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1979 | sprintf(pdata, "%c%ld\n", INT, val); |
| 1980 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1981 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1982 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1983 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1984 | |
| 1985 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1986 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1987 | assert(!PyErr_Occurred()); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1988 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1989 | if (self->proto >= 2) { |
| 1990 | /* Linear-time pickling. */ |
| 1991 | size_t nbits; |
| 1992 | size_t nbytes; |
| 1993 | unsigned char *pdata; |
| 1994 | char header[5]; |
| 1995 | int i; |
| 1996 | int sign = _PyLong_Sign(obj); |
| 1997 | |
| 1998 | if (sign == 0) { |
| 1999 | header[0] = LONG1; |
| 2000 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2001 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2002 | goto error; |
| 2003 | return 0; |
| 2004 | } |
| 2005 | nbits = _PyLong_NumBits(obj); |
| 2006 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 2007 | goto error; |
| 2008 | /* How many bytes do we need? There are nbits >> 3 full |
| 2009 | * bytes of data, and nbits & 7 leftover bits. If there |
| 2010 | * are any leftover bits, then we clearly need another |
| 2011 | * byte. Wnat's not so obvious is that we *probably* |
| 2012 | * need another byte even if there aren't any leftovers: |
| 2013 | * the most-significant bit of the most-significant byte |
| 2014 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2015 | * opposite of the one we need. The exception is ints |
| 2016 | * 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] | 2017 | * its own 256's-complement, so has the right sign bit |
| 2018 | * even without the extra byte. That's a pain to check |
| 2019 | * for in advance, though, so we always grab an extra |
| 2020 | * byte at the start, and cut it back later if possible. |
| 2021 | */ |
| 2022 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 2023 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2024 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2025 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2026 | goto error; |
| 2027 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2028 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2029 | if (repr == NULL) |
| 2030 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2031 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2032 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 2033 | pdata, nbytes, |
| 2034 | 1 /* little endian */ , 1 /* signed */ ); |
| 2035 | if (i < 0) |
| 2036 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2037 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2038 | * needed. This is so iff the MSB is all redundant sign |
| 2039 | * bits. |
| 2040 | */ |
| 2041 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2042 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2043 | pdata[nbytes - 1] == 0xff && |
| 2044 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 2045 | nbytes--; |
| 2046 | } |
| 2047 | |
| 2048 | if (nbytes < 256) { |
| 2049 | header[0] = LONG1; |
| 2050 | header[1] = (unsigned char)nbytes; |
| 2051 | size = 2; |
| 2052 | } |
| 2053 | else { |
| 2054 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2055 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2056 | for (i = 1; i < 5; i++) { |
| 2057 | header[i] = (unsigned char)(size & 0xff); |
| 2058 | size >>= 8; |
| 2059 | } |
| 2060 | size = 5; |
| 2061 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2062 | if (_Pickler_Write(self, header, size) < 0 || |
| 2063 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2064 | goto error; |
| 2065 | } |
| 2066 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2067 | const char long_op = LONG; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2068 | const char *string; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2069 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 2070 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 2071 | the number of digits), in both directions. We add a trailing 'L' |
| 2072 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2073 | |
| 2074 | repr = PyObject_Repr(obj); |
| 2075 | if (repr == NULL) |
| 2076 | goto error; |
| 2077 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2078 | string = PyUnicode_AsUTF8AndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2079 | if (string == NULL) |
| 2080 | goto error; |
| 2081 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2082 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 2083 | _Pickler_Write(self, string, size) < 0 || |
| 2084 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2085 | goto error; |
| 2086 | } |
| 2087 | |
| 2088 | if (0) { |
| 2089 | error: |
| 2090 | status = -1; |
| 2091 | } |
| 2092 | Py_XDECREF(repr); |
| 2093 | |
| 2094 | return status; |
| 2095 | } |
| 2096 | |
| 2097 | static int |
| 2098 | save_float(PicklerObject *self, PyObject *obj) |
| 2099 | { |
| 2100 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 2101 | |
| 2102 | if (self->bin) { |
| 2103 | char pdata[9]; |
| 2104 | pdata[0] = BINFLOAT; |
| 2105 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2106 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2107 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2108 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2109 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2110 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2111 | int result = -1; |
| 2112 | char *buf = NULL; |
| 2113 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2114 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2115 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2116 | goto done; |
| 2117 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2118 | 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] | 2119 | if (!buf) { |
| 2120 | PyErr_NoMemory(); |
| 2121 | goto done; |
| 2122 | } |
| 2123 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2124 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2125 | goto done; |
| 2126 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2127 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2128 | goto done; |
| 2129 | |
| 2130 | result = 0; |
| 2131 | done: |
| 2132 | PyMem_Free(buf); |
| 2133 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2134 | } |
| 2135 | |
| 2136 | return 0; |
| 2137 | } |
| 2138 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2139 | /* Perform direct write of the header and payload of the binary object. |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2140 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2141 | The large contiguous data is written directly into the underlying file |
| 2142 | object, bypassing the output_buffer of the Pickler. We intentionally |
| 2143 | do not insert a protocol 4 frame opcode to make it possible to optimize |
| 2144 | file.read calls in the loader. |
| 2145 | */ |
| 2146 | static int |
| 2147 | _Pickler_write_bytes(PicklerObject *self, |
| 2148 | const char *header, Py_ssize_t header_size, |
| 2149 | const char *data, Py_ssize_t data_size, |
| 2150 | PyObject *payload) |
| 2151 | { |
| 2152 | int bypass_buffer = (data_size >= FRAME_SIZE_TARGET); |
| 2153 | int framing = self->framing; |
| 2154 | |
| 2155 | if (bypass_buffer) { |
| 2156 | assert(self->output_buffer != NULL); |
| 2157 | /* Commit the previous frame. */ |
| 2158 | if (_Pickler_CommitFrame(self)) { |
| 2159 | return -1; |
| 2160 | } |
| 2161 | /* Disable framing temporarily */ |
| 2162 | self->framing = 0; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2163 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2164 | |
| 2165 | if (_Pickler_Write(self, header, header_size) < 0) { |
| 2166 | return -1; |
| 2167 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2168 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2169 | if (bypass_buffer && self->write != NULL) { |
| 2170 | /* Bypass the in-memory buffer to directly stream large data |
| 2171 | into the underlying file object. */ |
| 2172 | PyObject *result, *mem = NULL; |
| 2173 | /* Dump the output buffer to the file. */ |
| 2174 | if (_Pickler_FlushToFile(self) < 0) { |
| 2175 | return -1; |
| 2176 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2177 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2178 | /* Stream write the payload into the file without going through the |
| 2179 | output buffer. */ |
| 2180 | if (payload == NULL) { |
Serhiy Storchaka | 5b76bdb | 2018-01-13 00:28:31 +0200 | [diff] [blame] | 2181 | /* TODO: It would be better to use a memoryview with a linked |
| 2182 | original string if this is possible. */ |
| 2183 | payload = mem = PyBytes_FromStringAndSize(data, data_size); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2184 | if (payload == NULL) { |
| 2185 | return -1; |
| 2186 | } |
| 2187 | } |
| 2188 | result = PyObject_CallFunctionObjArgs(self->write, payload, NULL); |
| 2189 | Py_XDECREF(mem); |
| 2190 | if (result == NULL) { |
| 2191 | return -1; |
| 2192 | } |
| 2193 | Py_DECREF(result); |
| 2194 | |
| 2195 | /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */ |
| 2196 | if (_Pickler_ClearBuffer(self) < 0) { |
| 2197 | return -1; |
| 2198 | } |
| 2199 | } |
| 2200 | else { |
| 2201 | if (_Pickler_Write(self, data, data_size) < 0) { |
| 2202 | return -1; |
| 2203 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2204 | } |
| 2205 | |
| 2206 | /* Re-enable framing for subsequent calls to _Pickler_Write. */ |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2207 | self->framing = framing; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2208 | |
| 2209 | return 0; |
| 2210 | } |
| 2211 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2212 | static int |
| 2213 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2214 | { |
| 2215 | if (self->proto < 3) { |
| 2216 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2217 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2218 | the __reduce__ method) to permit bytes object unpickling. |
| 2219 | |
| 2220 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2221 | 2 'bytes' is just an alias for 'str' (which has different |
| 2222 | parameters than the actual bytes object), we use codecs.encode |
| 2223 | to create the appropriate 'str' object when unpickled using |
| 2224 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2225 | using Python 3. Again this is a hack and we don't need to do this |
| 2226 | with newer protocols. */ |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 2227 | PyObject *reduce_value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2228 | int status; |
| 2229 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2230 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2231 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2232 | } |
| 2233 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2234 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2235 | PyObject *unicode_str = |
| 2236 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2237 | PyBytes_GET_SIZE(obj), |
| 2238 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2239 | _Py_IDENTIFIER(latin1); |
| 2240 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2241 | if (unicode_str == NULL) |
| 2242 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2243 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2244 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2245 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2246 | Py_DECREF(unicode_str); |
| 2247 | } |
| 2248 | |
| 2249 | if (reduce_value == NULL) |
| 2250 | return -1; |
| 2251 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2252 | /* save_reduce() will memoize the object automatically. */ |
| 2253 | status = save_reduce(self, reduce_value, obj); |
| 2254 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2255 | return status; |
| 2256 | } |
| 2257 | else { |
| 2258 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2259 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2260 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2261 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2262 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2263 | if (size < 0) |
| 2264 | return -1; |
| 2265 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2266 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2267 | header[0] = SHORT_BINBYTES; |
| 2268 | header[1] = (unsigned char)size; |
| 2269 | len = 2; |
| 2270 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2271 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2272 | header[0] = BINBYTES; |
| 2273 | header[1] = (unsigned char)(size & 0xff); |
| 2274 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2275 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2276 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2277 | len = 5; |
| 2278 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2279 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2280 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2281 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2282 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2283 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2284 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2285 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2286 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2287 | return -1; /* string too large */ |
| 2288 | } |
| 2289 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2290 | if (_Pickler_write_bytes(self, header, len, |
| 2291 | PyBytes_AS_STRING(obj), size, obj) < 0) |
| 2292 | { |
| 2293 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2294 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2295 | |
| 2296 | if (memo_put(self, obj) < 0) |
| 2297 | return -1; |
| 2298 | |
| 2299 | return 0; |
| 2300 | } |
| 2301 | } |
| 2302 | |
| 2303 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2304 | backslash and newline characters to \uXXXX escapes. */ |
| 2305 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2306 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2307 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2308 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2309 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2310 | void *data; |
| 2311 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2312 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2313 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2314 | if (PyUnicode_READY(obj)) |
| 2315 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2316 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2317 | _PyBytesWriter_Init(&writer); |
| 2318 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2319 | size = PyUnicode_GET_LENGTH(obj); |
| 2320 | data = PyUnicode_DATA(obj); |
| 2321 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2322 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2323 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2324 | if (p == NULL) |
| 2325 | goto error; |
| 2326 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2327 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2328 | for (i=0; i < size; i++) { |
| 2329 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2330 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2331 | if (ch >= 0x10000) { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2332 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2333 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2334 | if (p == NULL) |
| 2335 | goto error; |
| 2336 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2337 | *p++ = '\\'; |
| 2338 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2339 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2340 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2341 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2342 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2343 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2344 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2345 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2346 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2347 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2348 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2349 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2350 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2351 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2352 | if (p == NULL) |
| 2353 | goto error; |
| 2354 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2355 | *p++ = '\\'; |
| 2356 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2357 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2358 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2359 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2360 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2361 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2362 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2363 | else |
| 2364 | *p++ = (char) ch; |
| 2365 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2366 | |
| 2367 | return _PyBytesWriter_Finish(&writer, p); |
| 2368 | |
| 2369 | error: |
| 2370 | _PyBytesWriter_Dealloc(&writer); |
| 2371 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2372 | } |
| 2373 | |
| 2374 | static int |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2375 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2376 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2377 | char header[9]; |
| 2378 | Py_ssize_t len; |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2379 | PyObject *encoded = NULL; |
| 2380 | Py_ssize_t size; |
| 2381 | const char *data; |
| 2382 | |
| 2383 | if (PyUnicode_READY(obj)) |
| 2384 | return -1; |
| 2385 | |
| 2386 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2387 | if (data == NULL) { |
| 2388 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2389 | "surrogatepass" error handler. */ |
| 2390 | PyErr_Clear(); |
| 2391 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2392 | if (encoded == NULL) |
| 2393 | return -1; |
| 2394 | |
| 2395 | data = PyBytes_AS_STRING(encoded); |
| 2396 | size = PyBytes_GET_SIZE(encoded); |
| 2397 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2398 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2399 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2400 | if (size <= 0xff && self->proto >= 4) { |
| 2401 | header[0] = SHORT_BINUNICODE; |
| 2402 | header[1] = (unsigned char)(size & 0xff); |
| 2403 | len = 2; |
| 2404 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2405 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2406 | header[0] = BINUNICODE; |
| 2407 | header[1] = (unsigned char)(size & 0xff); |
| 2408 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2409 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2410 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2411 | len = 5; |
| 2412 | } |
| 2413 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2414 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2415 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2416 | len = 9; |
| 2417 | } |
| 2418 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2419 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2420 | "cannot serialize a string larger than 4GiB"); |
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 -1; |
| 2423 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2424 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2425 | if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) { |
| 2426 | Py_XDECREF(encoded); |
| 2427 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2428 | } |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2429 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2430 | return 0; |
| 2431 | } |
| 2432 | |
| 2433 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2434 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2435 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2436 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2437 | if (write_unicode_binary(self, obj) < 0) |
| 2438 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2439 | } |
| 2440 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2441 | PyObject *encoded; |
| 2442 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2443 | const char unicode_op = UNICODE; |
| 2444 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2445 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2446 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2447 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2448 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2449 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2450 | Py_DECREF(encoded); |
| 2451 | return -1; |
| 2452 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2453 | |
| 2454 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2455 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2456 | Py_DECREF(encoded); |
| 2457 | return -1; |
| 2458 | } |
| 2459 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2461 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2462 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2463 | } |
| 2464 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2465 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2466 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2467 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2468 | } |
| 2469 | |
| 2470 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2471 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2472 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2473 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2474 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2475 | |
| 2476 | assert(PyTuple_Size(t) == len); |
| 2477 | |
| 2478 | for (i = 0; i < len; i++) { |
| 2479 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2480 | |
| 2481 | if (element == NULL) |
| 2482 | return -1; |
| 2483 | if (save(self, element, 0) < 0) |
| 2484 | return -1; |
| 2485 | } |
| 2486 | |
| 2487 | return 0; |
| 2488 | } |
| 2489 | |
| 2490 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2491 | * used across protocols to minimize the space needed to pickle them. |
| 2492 | * Tuples are also the only builtin immutable type that can be recursive |
| 2493 | * (a tuple can be reached from itself), and that requires some subtle |
| 2494 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2495 | */ |
| 2496 | static int |
| 2497 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2498 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2499 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2500 | |
| 2501 | const char mark_op = MARK; |
| 2502 | const char tuple_op = TUPLE; |
| 2503 | const char pop_op = POP; |
| 2504 | const char pop_mark_op = POP_MARK; |
| 2505 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2506 | |
| 2507 | if ((len = PyTuple_Size(obj)) < 0) |
| 2508 | return -1; |
| 2509 | |
| 2510 | if (len == 0) { |
| 2511 | char pdata[2]; |
| 2512 | |
| 2513 | if (self->proto) { |
| 2514 | pdata[0] = EMPTY_TUPLE; |
| 2515 | len = 1; |
| 2516 | } |
| 2517 | else { |
| 2518 | pdata[0] = MARK; |
| 2519 | pdata[1] = TUPLE; |
| 2520 | len = 2; |
| 2521 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2522 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2523 | return -1; |
| 2524 | return 0; |
| 2525 | } |
| 2526 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2527 | /* 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] | 2528 | * saving the tuple elements, the tuple must be recursive, in |
| 2529 | * which case we'll pop everything we put on the stack, and fetch |
| 2530 | * its value from the memo. |
| 2531 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2532 | if (len <= 3 && self->proto >= 2) { |
| 2533 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2534 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2535 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2536 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2537 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2538 | /* pop the len elements */ |
| 2539 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2540 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2541 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2542 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2543 | if (memo_get(self, obj) < 0) |
| 2544 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2545 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2546 | return 0; |
| 2547 | } |
| 2548 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2549 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2550 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2551 | } |
| 2552 | goto memoize; |
| 2553 | } |
| 2554 | |
| 2555 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2556 | * Generate MARK e1 e2 ... TUPLE |
| 2557 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2558 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2559 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2560 | |
| 2561 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2562 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2563 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2564 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2565 | /* pop the stack stuff we pushed */ |
| 2566 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2567 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2568 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2569 | } |
| 2570 | else { |
| 2571 | /* Note that we pop one more than len, to remove |
| 2572 | * the MARK too. |
| 2573 | */ |
| 2574 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2575 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2576 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2577 | } |
| 2578 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2579 | if (memo_get(self, obj) < 0) |
| 2580 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2581 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2582 | return 0; |
| 2583 | } |
| 2584 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2585 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2586 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | memoize: |
| 2590 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2591 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2592 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2593 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2594 | } |
| 2595 | |
| 2596 | /* iter is an iterator giving items, and we batch up chunks of |
| 2597 | * MARK item item ... item APPENDS |
| 2598 | * opcode sequences. Calling code should have arranged to first create an |
| 2599 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2600 | * Returns 0 on success, <0 on error. |
| 2601 | */ |
| 2602 | static int |
| 2603 | batch_list(PicklerObject *self, PyObject *iter) |
| 2604 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2605 | PyObject *obj = NULL; |
| 2606 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2607 | int i, n; |
| 2608 | |
| 2609 | const char mark_op = MARK; |
| 2610 | const char append_op = APPEND; |
| 2611 | const char appends_op = APPENDS; |
| 2612 | |
| 2613 | assert(iter != NULL); |
| 2614 | |
| 2615 | /* XXX: I think this function could be made faster by avoiding the |
| 2616 | iterator interface and fetching objects directly from list using |
| 2617 | PyList_GET_ITEM. |
| 2618 | */ |
| 2619 | |
| 2620 | if (self->proto == 0) { |
| 2621 | /* APPENDS isn't available; do one at a time. */ |
| 2622 | for (;;) { |
| 2623 | obj = PyIter_Next(iter); |
| 2624 | if (obj == NULL) { |
| 2625 | if (PyErr_Occurred()) |
| 2626 | return -1; |
| 2627 | break; |
| 2628 | } |
| 2629 | i = save(self, obj, 0); |
| 2630 | Py_DECREF(obj); |
| 2631 | if (i < 0) |
| 2632 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2633 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2634 | return -1; |
| 2635 | } |
| 2636 | return 0; |
| 2637 | } |
| 2638 | |
| 2639 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2640 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2641 | /* Get first item */ |
| 2642 | firstitem = PyIter_Next(iter); |
| 2643 | if (firstitem == NULL) { |
| 2644 | if (PyErr_Occurred()) |
| 2645 | goto error; |
| 2646 | |
| 2647 | /* nothing more to add */ |
| 2648 | break; |
| 2649 | } |
| 2650 | |
| 2651 | /* Try to get a second item */ |
| 2652 | obj = PyIter_Next(iter); |
| 2653 | if (obj == NULL) { |
| 2654 | if (PyErr_Occurred()) |
| 2655 | goto error; |
| 2656 | |
| 2657 | /* Only one item to write */ |
| 2658 | if (save(self, firstitem, 0) < 0) |
| 2659 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2660 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2661 | goto error; |
| 2662 | Py_CLEAR(firstitem); |
| 2663 | break; |
| 2664 | } |
| 2665 | |
| 2666 | /* More than one item to write */ |
| 2667 | |
| 2668 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2669 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2670 | goto error; |
| 2671 | |
| 2672 | if (save(self, firstitem, 0) < 0) |
| 2673 | goto error; |
| 2674 | Py_CLEAR(firstitem); |
| 2675 | n = 1; |
| 2676 | |
| 2677 | /* Fetch and save up to BATCHSIZE items */ |
| 2678 | while (obj) { |
| 2679 | if (save(self, obj, 0) < 0) |
| 2680 | goto error; |
| 2681 | Py_CLEAR(obj); |
| 2682 | n += 1; |
| 2683 | |
| 2684 | if (n == BATCHSIZE) |
| 2685 | break; |
| 2686 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2687 | obj = PyIter_Next(iter); |
| 2688 | if (obj == NULL) { |
| 2689 | if (PyErr_Occurred()) |
| 2690 | goto error; |
| 2691 | break; |
| 2692 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2695 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2696 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2697 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2698 | } while (n == BATCHSIZE); |
| 2699 | return 0; |
| 2700 | |
| 2701 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2702 | Py_XDECREF(firstitem); |
| 2703 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2704 | return -1; |
| 2705 | } |
| 2706 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2707 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2708 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2709 | * MARK item item ... item APPENDS |
| 2710 | * opcode sequences. Calling code should have arranged to first create an |
| 2711 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2712 | * Returns 0 on success, -1 on error. |
| 2713 | * |
| 2714 | * This version is considerably faster than batch_list(), if less general. |
| 2715 | * |
| 2716 | * Note that this only works for protocols > 0. |
| 2717 | */ |
| 2718 | static int |
| 2719 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2720 | { |
| 2721 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2722 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2723 | |
| 2724 | const char append_op = APPEND; |
| 2725 | const char appends_op = APPENDS; |
| 2726 | const char mark_op = MARK; |
| 2727 | |
| 2728 | assert(obj != NULL); |
| 2729 | assert(self->proto > 0); |
| 2730 | assert(PyList_CheckExact(obj)); |
| 2731 | |
| 2732 | if (PyList_GET_SIZE(obj) == 1) { |
| 2733 | item = PyList_GET_ITEM(obj, 0); |
| 2734 | if (save(self, item, 0) < 0) |
| 2735 | return -1; |
| 2736 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2737 | return -1; |
| 2738 | return 0; |
| 2739 | } |
| 2740 | |
| 2741 | /* Write in batches of BATCHSIZE. */ |
| 2742 | total = 0; |
| 2743 | do { |
| 2744 | this_batch = 0; |
| 2745 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2746 | return -1; |
| 2747 | while (total < PyList_GET_SIZE(obj)) { |
| 2748 | item = PyList_GET_ITEM(obj, total); |
| 2749 | if (save(self, item, 0) < 0) |
| 2750 | return -1; |
| 2751 | total++; |
| 2752 | if (++this_batch == BATCHSIZE) |
| 2753 | break; |
| 2754 | } |
| 2755 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2756 | return -1; |
| 2757 | |
| 2758 | } while (total < PyList_GET_SIZE(obj)); |
| 2759 | |
| 2760 | return 0; |
| 2761 | } |
| 2762 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2763 | static int |
| 2764 | save_list(PicklerObject *self, PyObject *obj) |
| 2765 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2766 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2767 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2768 | int status = 0; |
| 2769 | |
| 2770 | if (self->fast && !fast_save_enter(self, obj)) |
| 2771 | goto error; |
| 2772 | |
| 2773 | /* Create an empty list. */ |
| 2774 | if (self->bin) { |
| 2775 | header[0] = EMPTY_LIST; |
| 2776 | len = 1; |
| 2777 | } |
| 2778 | else { |
| 2779 | header[0] = MARK; |
| 2780 | header[1] = LIST; |
| 2781 | len = 2; |
| 2782 | } |
| 2783 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2784 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2785 | goto error; |
| 2786 | |
| 2787 | /* Get list length, and bow out early if empty. */ |
| 2788 | if ((len = PyList_Size(obj)) < 0) |
| 2789 | goto error; |
| 2790 | |
| 2791 | if (memo_put(self, obj) < 0) |
| 2792 | goto error; |
| 2793 | |
| 2794 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2795 | /* Materialize the list elements. */ |
| 2796 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2797 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2798 | goto error; |
| 2799 | status = batch_list_exact(self, obj); |
| 2800 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2801 | } else { |
| 2802 | PyObject *iter = PyObject_GetIter(obj); |
| 2803 | if (iter == NULL) |
| 2804 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2805 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2806 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2807 | Py_DECREF(iter); |
| 2808 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2809 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2810 | status = batch_list(self, iter); |
| 2811 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2812 | Py_DECREF(iter); |
| 2813 | } |
| 2814 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2815 | if (0) { |
| 2816 | error: |
| 2817 | status = -1; |
| 2818 | } |
| 2819 | |
| 2820 | if (self->fast && !fast_save_leave(self, obj)) |
| 2821 | status = -1; |
| 2822 | |
| 2823 | return status; |
| 2824 | } |
| 2825 | |
| 2826 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2827 | * MARK key value ... key value SETITEMS |
| 2828 | * opcode sequences. Calling code should have arranged to first create an |
| 2829 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2830 | * Returns 0 on success, <0 on error. |
| 2831 | * |
| 2832 | * This is very much like batch_list(). The difference between saving |
| 2833 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2834 | * the C level, though, that attempts to combine these routines were too |
| 2835 | * ugly to bear. |
| 2836 | */ |
| 2837 | static int |
| 2838 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2839 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2840 | PyObject *obj = NULL; |
| 2841 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2842 | int i, n; |
| 2843 | |
| 2844 | const char mark_op = MARK; |
| 2845 | const char setitem_op = SETITEM; |
| 2846 | const char setitems_op = SETITEMS; |
| 2847 | |
| 2848 | assert(iter != NULL); |
| 2849 | |
| 2850 | if (self->proto == 0) { |
| 2851 | /* SETITEMS isn't available; do one at a time. */ |
| 2852 | for (;;) { |
| 2853 | obj = PyIter_Next(iter); |
| 2854 | if (obj == NULL) { |
| 2855 | if (PyErr_Occurred()) |
| 2856 | return -1; |
| 2857 | break; |
| 2858 | } |
| 2859 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2860 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2861 | "iterator must return 2-tuples"); |
| 2862 | return -1; |
| 2863 | } |
| 2864 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2865 | if (i >= 0) |
| 2866 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2867 | Py_DECREF(obj); |
| 2868 | if (i < 0) |
| 2869 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2870 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2871 | return -1; |
| 2872 | } |
| 2873 | return 0; |
| 2874 | } |
| 2875 | |
| 2876 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2877 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2878 | /* Get first item */ |
| 2879 | firstitem = PyIter_Next(iter); |
| 2880 | if (firstitem == NULL) { |
| 2881 | if (PyErr_Occurred()) |
| 2882 | goto error; |
| 2883 | |
| 2884 | /* nothing more to add */ |
| 2885 | break; |
| 2886 | } |
| 2887 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2888 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2889 | "iterator must return 2-tuples"); |
| 2890 | goto error; |
| 2891 | } |
| 2892 | |
| 2893 | /* Try to get a second item */ |
| 2894 | obj = PyIter_Next(iter); |
| 2895 | if (obj == NULL) { |
| 2896 | if (PyErr_Occurred()) |
| 2897 | goto error; |
| 2898 | |
| 2899 | /* Only one item to write */ |
| 2900 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2901 | goto error; |
| 2902 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2903 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2904 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2905 | goto error; |
| 2906 | Py_CLEAR(firstitem); |
| 2907 | break; |
| 2908 | } |
| 2909 | |
| 2910 | /* More than one item to write */ |
| 2911 | |
| 2912 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2913 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2914 | goto error; |
| 2915 | |
| 2916 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2917 | goto error; |
| 2918 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2919 | goto error; |
| 2920 | Py_CLEAR(firstitem); |
| 2921 | n = 1; |
| 2922 | |
| 2923 | /* Fetch and save up to BATCHSIZE items */ |
| 2924 | while (obj) { |
| 2925 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2926 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2927 | "iterator must return 2-tuples"); |
| 2928 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2929 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2930 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2931 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2932 | goto error; |
| 2933 | Py_CLEAR(obj); |
| 2934 | n += 1; |
| 2935 | |
| 2936 | if (n == BATCHSIZE) |
| 2937 | break; |
| 2938 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2939 | obj = PyIter_Next(iter); |
| 2940 | if (obj == NULL) { |
| 2941 | if (PyErr_Occurred()) |
| 2942 | goto error; |
| 2943 | break; |
| 2944 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2945 | } |
| 2946 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2947 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2948 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2949 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2950 | } while (n == BATCHSIZE); |
| 2951 | return 0; |
| 2952 | |
| 2953 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2954 | Py_XDECREF(firstitem); |
| 2955 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2956 | return -1; |
| 2957 | } |
| 2958 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2959 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2960 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2961 | * MARK key value ... key value SETITEMS |
| 2962 | * opcode sequences. Calling code should have arranged to first create an |
| 2963 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2964 | * Returns 0 on success, -1 on error. |
| 2965 | * |
| 2966 | * Note that this currently doesn't work for protocol 0. |
| 2967 | */ |
| 2968 | static int |
| 2969 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2970 | { |
| 2971 | PyObject *key = NULL, *value = NULL; |
| 2972 | int i; |
| 2973 | Py_ssize_t dict_size, ppos = 0; |
| 2974 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2975 | const char mark_op = MARK; |
| 2976 | const char setitem_op = SETITEM; |
| 2977 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2978 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2979 | assert(obj != NULL && PyDict_CheckExact(obj)); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2980 | assert(self->proto > 0); |
| 2981 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2982 | dict_size = PyDict_GET_SIZE(obj); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2983 | |
| 2984 | /* Special-case len(d) == 1 to save space. */ |
| 2985 | if (dict_size == 1) { |
| 2986 | PyDict_Next(obj, &ppos, &key, &value); |
| 2987 | if (save(self, key, 0) < 0) |
| 2988 | return -1; |
| 2989 | if (save(self, value, 0) < 0) |
| 2990 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2991 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2992 | return -1; |
| 2993 | return 0; |
| 2994 | } |
| 2995 | |
| 2996 | /* Write in batches of BATCHSIZE. */ |
| 2997 | do { |
| 2998 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2999 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3000 | return -1; |
| 3001 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 3002 | if (save(self, key, 0) < 0) |
| 3003 | return -1; |
| 3004 | if (save(self, value, 0) < 0) |
| 3005 | return -1; |
| 3006 | if (++i == BATCHSIZE) |
| 3007 | break; |
| 3008 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3009 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3010 | return -1; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3011 | if (PyDict_GET_SIZE(obj) != dict_size) { |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3012 | PyErr_Format( |
| 3013 | PyExc_RuntimeError, |
| 3014 | "dictionary changed size during iteration"); |
| 3015 | return -1; |
| 3016 | } |
| 3017 | |
| 3018 | } while (i == BATCHSIZE); |
| 3019 | return 0; |
| 3020 | } |
| 3021 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3022 | static int |
| 3023 | save_dict(PicklerObject *self, PyObject *obj) |
| 3024 | { |
| 3025 | PyObject *items, *iter; |
| 3026 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3027 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3028 | int status = 0; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3029 | assert(PyDict_Check(obj)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3030 | |
| 3031 | if (self->fast && !fast_save_enter(self, obj)) |
| 3032 | goto error; |
| 3033 | |
| 3034 | /* Create an empty dict. */ |
| 3035 | if (self->bin) { |
| 3036 | header[0] = EMPTY_DICT; |
| 3037 | len = 1; |
| 3038 | } |
| 3039 | else { |
| 3040 | header[0] = MARK; |
| 3041 | header[1] = DICT; |
| 3042 | len = 2; |
| 3043 | } |
| 3044 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3045 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3046 | goto error; |
| 3047 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3048 | if (memo_put(self, obj) < 0) |
| 3049 | goto error; |
| 3050 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3051 | if (PyDict_GET_SIZE(obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3052 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3053 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 3054 | /* We can take certain shortcuts if we know this is a dict and |
| 3055 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3056 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3057 | goto error; |
| 3058 | status = batch_dict_exact(self, obj); |
| 3059 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3060 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3061 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3062 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 3063 | items = _PyObject_CallMethodId(obj, &PyId_items, NULL); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3064 | if (items == NULL) |
| 3065 | goto error; |
| 3066 | iter = PyObject_GetIter(items); |
| 3067 | Py_DECREF(items); |
| 3068 | if (iter == NULL) |
| 3069 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3070 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3071 | Py_DECREF(iter); |
| 3072 | goto error; |
| 3073 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3074 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3075 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3076 | Py_DECREF(iter); |
| 3077 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | if (0) { |
| 3081 | error: |
| 3082 | status = -1; |
| 3083 | } |
| 3084 | |
| 3085 | if (self->fast && !fast_save_leave(self, obj)) |
| 3086 | status = -1; |
| 3087 | |
| 3088 | return status; |
| 3089 | } |
| 3090 | |
| 3091 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3092 | save_set(PicklerObject *self, PyObject *obj) |
| 3093 | { |
| 3094 | PyObject *item; |
| 3095 | int i; |
| 3096 | Py_ssize_t set_size, ppos = 0; |
| 3097 | Py_hash_t hash; |
| 3098 | |
| 3099 | const char empty_set_op = EMPTY_SET; |
| 3100 | const char mark_op = MARK; |
| 3101 | const char additems_op = ADDITEMS; |
| 3102 | |
| 3103 | if (self->proto < 4) { |
| 3104 | PyObject *items; |
| 3105 | PyObject *reduce_value; |
| 3106 | int status; |
| 3107 | |
| 3108 | items = PySequence_List(obj); |
| 3109 | if (items == NULL) { |
| 3110 | return -1; |
| 3111 | } |
| 3112 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 3113 | Py_DECREF(items); |
| 3114 | if (reduce_value == NULL) { |
| 3115 | return -1; |
| 3116 | } |
| 3117 | /* save_reduce() will memoize the object automatically. */ |
| 3118 | status = save_reduce(self, reduce_value, obj); |
| 3119 | Py_DECREF(reduce_value); |
| 3120 | return status; |
| 3121 | } |
| 3122 | |
| 3123 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 3124 | return -1; |
| 3125 | |
| 3126 | if (memo_put(self, obj) < 0) |
| 3127 | return -1; |
| 3128 | |
| 3129 | set_size = PySet_GET_SIZE(obj); |
| 3130 | if (set_size == 0) |
| 3131 | return 0; /* nothing to do */ |
| 3132 | |
| 3133 | /* Write in batches of BATCHSIZE. */ |
| 3134 | do { |
| 3135 | i = 0; |
| 3136 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3137 | return -1; |
| 3138 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 3139 | if (save(self, item, 0) < 0) |
| 3140 | return -1; |
| 3141 | if (++i == BATCHSIZE) |
| 3142 | break; |
| 3143 | } |
| 3144 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 3145 | return -1; |
| 3146 | if (PySet_GET_SIZE(obj) != set_size) { |
| 3147 | PyErr_Format( |
| 3148 | PyExc_RuntimeError, |
| 3149 | "set changed size during iteration"); |
| 3150 | return -1; |
| 3151 | } |
| 3152 | } while (i == BATCHSIZE); |
| 3153 | |
| 3154 | return 0; |
| 3155 | } |
| 3156 | |
| 3157 | static int |
| 3158 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 3159 | { |
| 3160 | PyObject *iter; |
| 3161 | |
| 3162 | const char mark_op = MARK; |
| 3163 | const char frozenset_op = FROZENSET; |
| 3164 | |
| 3165 | if (self->fast && !fast_save_enter(self, obj)) |
| 3166 | return -1; |
| 3167 | |
| 3168 | if (self->proto < 4) { |
| 3169 | PyObject *items; |
| 3170 | PyObject *reduce_value; |
| 3171 | int status; |
| 3172 | |
| 3173 | items = PySequence_List(obj); |
| 3174 | if (items == NULL) { |
| 3175 | return -1; |
| 3176 | } |
| 3177 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3178 | items); |
| 3179 | Py_DECREF(items); |
| 3180 | if (reduce_value == NULL) { |
| 3181 | return -1; |
| 3182 | } |
| 3183 | /* save_reduce() will memoize the object automatically. */ |
| 3184 | status = save_reduce(self, reduce_value, obj); |
| 3185 | Py_DECREF(reduce_value); |
| 3186 | return status; |
| 3187 | } |
| 3188 | |
| 3189 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3190 | return -1; |
| 3191 | |
| 3192 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3193 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3194 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3195 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3196 | for (;;) { |
| 3197 | PyObject *item; |
| 3198 | |
| 3199 | item = PyIter_Next(iter); |
| 3200 | if (item == NULL) { |
| 3201 | if (PyErr_Occurred()) { |
| 3202 | Py_DECREF(iter); |
| 3203 | return -1; |
| 3204 | } |
| 3205 | break; |
| 3206 | } |
| 3207 | if (save(self, item, 0) < 0) { |
| 3208 | Py_DECREF(item); |
| 3209 | Py_DECREF(iter); |
| 3210 | return -1; |
| 3211 | } |
| 3212 | Py_DECREF(item); |
| 3213 | } |
| 3214 | Py_DECREF(iter); |
| 3215 | |
| 3216 | /* If the object is already in the memo, this means it is |
| 3217 | recursive. In this case, throw away everything we put on the |
| 3218 | stack, and fetch the object back from the memo. */ |
| 3219 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3220 | const char pop_mark_op = POP_MARK; |
| 3221 | |
| 3222 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3223 | return -1; |
| 3224 | if (memo_get(self, obj) < 0) |
| 3225 | return -1; |
| 3226 | return 0; |
| 3227 | } |
| 3228 | |
| 3229 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3230 | return -1; |
| 3231 | if (memo_put(self, obj) < 0) |
| 3232 | return -1; |
| 3233 | |
| 3234 | return 0; |
| 3235 | } |
| 3236 | |
| 3237 | static int |
| 3238 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3239 | { |
| 3240 | PyObject *key; |
| 3241 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3242 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3243 | |
| 3244 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3245 | if (key == NULL) |
| 3246 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3247 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3248 | Py_DECREF(key); |
| 3249 | if (item) { |
| 3250 | PyObject *fixed_module_name; |
| 3251 | PyObject *fixed_global_name; |
| 3252 | |
| 3253 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3254 | PyErr_Format(PyExc_RuntimeError, |
| 3255 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3256 | "should be 2-tuples, not %.200s", |
| 3257 | Py_TYPE(item)->tp_name); |
| 3258 | return -1; |
| 3259 | } |
| 3260 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3261 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3262 | if (!PyUnicode_Check(fixed_module_name) || |
| 3263 | !PyUnicode_Check(fixed_global_name)) { |
| 3264 | PyErr_Format(PyExc_RuntimeError, |
| 3265 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3266 | "should be pairs of str, not (%.200s, %.200s)", |
| 3267 | Py_TYPE(fixed_module_name)->tp_name, |
| 3268 | Py_TYPE(fixed_global_name)->tp_name); |
| 3269 | return -1; |
| 3270 | } |
| 3271 | |
| 3272 | Py_CLEAR(*module_name); |
| 3273 | Py_CLEAR(*global_name); |
| 3274 | Py_INCREF(fixed_module_name); |
| 3275 | Py_INCREF(fixed_global_name); |
| 3276 | *module_name = fixed_module_name; |
| 3277 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3278 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3279 | } |
| 3280 | else if (PyErr_Occurred()) { |
| 3281 | return -1; |
| 3282 | } |
| 3283 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3284 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3285 | if (item) { |
| 3286 | if (!PyUnicode_Check(item)) { |
| 3287 | PyErr_Format(PyExc_RuntimeError, |
| 3288 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3289 | "should be strings, not %.200s", |
| 3290 | Py_TYPE(item)->tp_name); |
| 3291 | return -1; |
| 3292 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3293 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3294 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3295 | } |
| 3296 | else if (PyErr_Occurred()) { |
| 3297 | return -1; |
| 3298 | } |
| 3299 | |
| 3300 | return 0; |
| 3301 | } |
| 3302 | |
| 3303 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3304 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3305 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3306 | PyObject *global_name = NULL; |
| 3307 | PyObject *module_name = NULL; |
| 3308 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3309 | PyObject *parent = NULL; |
| 3310 | PyObject *dotted_path = NULL; |
| 3311 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3312 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3313 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3314 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3315 | _Py_IDENTIFIER(__name__); |
| 3316 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3317 | |
| 3318 | const char global_op = GLOBAL; |
| 3319 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3320 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3321 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3322 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3323 | } |
| 3324 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3325 | if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0) |
| 3326 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3327 | if (global_name == NULL) { |
| 3328 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3329 | if (global_name == NULL) |
| 3330 | goto error; |
| 3331 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3332 | } |
| 3333 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3334 | dotted_path = get_dotted_path(module, global_name); |
| 3335 | if (dotted_path == NULL) |
| 3336 | goto error; |
| 3337 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3338 | if (module_name == NULL) |
| 3339 | goto error; |
| 3340 | |
| 3341 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3342 | relative imports. |
| 3343 | |
| 3344 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3345 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3346 | custom import functions (IMHO, this would be a nice security |
| 3347 | feature). The import C API would need to be extended to support the |
| 3348 | extra parameters of __import__ to fix that. */ |
| 3349 | module = PyImport_Import(module_name); |
| 3350 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3351 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3352 | "Can't pickle %R: import of module %R failed", |
| 3353 | obj, module_name); |
| 3354 | goto error; |
| 3355 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3356 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3357 | Py_INCREF(lastname); |
| 3358 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3359 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3360 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3361 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3362 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3363 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3364 | goto error; |
| 3365 | } |
| 3366 | if (cls != obj) { |
| 3367 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3368 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3369 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3370 | obj, module_name, global_name); |
| 3371 | goto error; |
| 3372 | } |
| 3373 | Py_DECREF(cls); |
| 3374 | |
| 3375 | if (self->proto >= 2) { |
| 3376 | /* See whether this is in the extension registry, and if |
| 3377 | * so generate an EXT opcode. |
| 3378 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3379 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3380 | PyObject *code_obj; /* extension code as Python object */ |
| 3381 | long code; /* extension code as C value */ |
| 3382 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3383 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3384 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3385 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3386 | if (extension_key == NULL) { |
| 3387 | goto error; |
| 3388 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3389 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3390 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3391 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3392 | /* The object is not registered in the extension registry. |
| 3393 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3394 | if (code_obj == NULL) { |
| 3395 | if (PyErr_Occurred()) { |
| 3396 | goto error; |
| 3397 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3398 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3399 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3400 | |
| 3401 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3402 | of the value returned by the extension_registry. It should for |
| 3403 | consistency. */ |
| 3404 | |
| 3405 | /* Verify code_obj has the right type and value. */ |
| 3406 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3407 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3408 | "Can't pickle %R: extension code %R isn't an integer", |
| 3409 | obj, code_obj); |
| 3410 | goto error; |
| 3411 | } |
| 3412 | code = PyLong_AS_LONG(code_obj); |
| 3413 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3414 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3415 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3416 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3417 | goto error; |
| 3418 | } |
| 3419 | |
| 3420 | /* Generate an EXT opcode. */ |
| 3421 | if (code <= 0xff) { |
| 3422 | pdata[0] = EXT1; |
| 3423 | pdata[1] = (unsigned char)code; |
| 3424 | n = 2; |
| 3425 | } |
| 3426 | else if (code <= 0xffff) { |
| 3427 | pdata[0] = EXT2; |
| 3428 | pdata[1] = (unsigned char)(code & 0xff); |
| 3429 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3430 | n = 3; |
| 3431 | } |
| 3432 | else { |
| 3433 | pdata[0] = EXT4; |
| 3434 | pdata[1] = (unsigned char)(code & 0xff); |
| 3435 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3436 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3437 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3438 | n = 5; |
| 3439 | } |
| 3440 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3441 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3442 | goto error; |
| 3443 | } |
| 3444 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3445 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3446 | if (parent == module) { |
| 3447 | Py_INCREF(lastname); |
| 3448 | Py_DECREF(global_name); |
| 3449 | global_name = lastname; |
| 3450 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3451 | if (self->proto >= 4) { |
| 3452 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3453 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3454 | if (save(self, module_name, 0) < 0) |
| 3455 | goto error; |
| 3456 | if (save(self, global_name, 0) < 0) |
| 3457 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3458 | |
| 3459 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3460 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3461 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3462 | else if (parent != module) { |
| 3463 | PickleState *st = _Pickle_GetGlobalState(); |
| 3464 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3465 | st->getattr, parent, lastname); |
Alexey Izbyshev | f8c06b0 | 2018-08-22 07:51:25 +0300 | [diff] [blame] | 3466 | if (reduce_value == NULL) |
| 3467 | goto error; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3468 | status = save_reduce(self, reduce_value, NULL); |
| 3469 | Py_DECREF(reduce_value); |
| 3470 | if (status < 0) |
| 3471 | goto error; |
| 3472 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3473 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3474 | /* Generate a normal global opcode if we are using a pickle |
| 3475 | protocol < 4, or if the object is not registered in the |
| 3476 | extension registry. */ |
| 3477 | PyObject *encoded; |
| 3478 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3479 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3480 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3481 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3482 | |
| 3483 | /* For protocol < 3 and if the user didn't request against doing |
| 3484 | so, we convert module names to the old 2.x module names. */ |
| 3485 | if (self->proto < 3 && self->fix_imports) { |
| 3486 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3487 | goto error; |
| 3488 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3491 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3492 | both the module name and the global name using UTF-8. We do so |
| 3493 | only when we are using the pickle protocol newer than version |
| 3494 | 3. This is to ensure compatibility with older Unpickler running |
| 3495 | on Python 2.x. */ |
| 3496 | if (self->proto == 3) { |
| 3497 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3498 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3499 | else { |
| 3500 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3501 | } |
| 3502 | encoded = unicode_encoder(module_name); |
| 3503 | if (encoded == NULL) { |
| 3504 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3505 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3506 | "can't pickle module identifier '%S' using " |
| 3507 | "pickle protocol %i", |
| 3508 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3509 | goto error; |
| 3510 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3511 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3512 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3513 | Py_DECREF(encoded); |
| 3514 | goto error; |
| 3515 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3516 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3517 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3518 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3519 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3520 | /* Save the name of the module. */ |
| 3521 | encoded = unicode_encoder(global_name); |
| 3522 | if (encoded == NULL) { |
| 3523 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3524 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3525 | "can't pickle global identifier '%S' using " |
| 3526 | "pickle protocol %i", |
| 3527 | global_name, self->proto); |
| 3528 | goto error; |
| 3529 | } |
| 3530 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3531 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3532 | Py_DECREF(encoded); |
| 3533 | goto error; |
| 3534 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3535 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3536 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3537 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3538 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3539 | /* Memoize the object. */ |
| 3540 | if (memo_put(self, obj) < 0) |
| 3541 | goto error; |
| 3542 | } |
| 3543 | |
| 3544 | if (0) { |
| 3545 | error: |
| 3546 | status = -1; |
| 3547 | } |
| 3548 | Py_XDECREF(module_name); |
| 3549 | Py_XDECREF(global_name); |
| 3550 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3551 | Py_XDECREF(parent); |
| 3552 | Py_XDECREF(dotted_path); |
| 3553 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3554 | |
| 3555 | return status; |
| 3556 | } |
| 3557 | |
| 3558 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3559 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3560 | { |
| 3561 | PyObject *reduce_value; |
| 3562 | int status; |
| 3563 | |
| 3564 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3565 | if (reduce_value == NULL) { |
| 3566 | return -1; |
| 3567 | } |
| 3568 | status = save_reduce(self, reduce_value, obj); |
| 3569 | Py_DECREF(reduce_value); |
| 3570 | return status; |
| 3571 | } |
| 3572 | |
| 3573 | static int |
| 3574 | save_type(PicklerObject *self, PyObject *obj) |
| 3575 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3576 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3577 | return save_singleton_type(self, obj, Py_None); |
| 3578 | } |
| 3579 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3580 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3581 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3582 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3583 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3584 | } |
| 3585 | return save_global(self, obj, NULL); |
| 3586 | } |
| 3587 | |
| 3588 | static int |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3589 | save_pers(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3590 | { |
| 3591 | PyObject *pid = NULL; |
| 3592 | int status = 0; |
| 3593 | |
| 3594 | const char persid_op = PERSID; |
| 3595 | const char binpersid_op = BINPERSID; |
| 3596 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3597 | pid = call_method(self->pers_func, self->pers_func_self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3598 | if (pid == NULL) |
| 3599 | return -1; |
| 3600 | |
| 3601 | if (pid != Py_None) { |
| 3602 | if (self->bin) { |
| 3603 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3604 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3605 | goto error; |
| 3606 | } |
| 3607 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3608 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3609 | |
| 3610 | pid_str = PyObject_Str(pid); |
| 3611 | if (pid_str == NULL) |
| 3612 | goto error; |
| 3613 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3614 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3615 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3616 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3617 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3618 | "persistent IDs in protocol 0 must be " |
| 3619 | "ASCII strings"); |
| 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 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3623 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3624 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3625 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3626 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3627 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3628 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3629 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3630 | } |
| 3631 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3632 | } |
| 3633 | status = 1; |
| 3634 | } |
| 3635 | |
| 3636 | if (0) { |
| 3637 | error: |
| 3638 | status = -1; |
| 3639 | } |
| 3640 | Py_XDECREF(pid); |
| 3641 | |
| 3642 | return status; |
| 3643 | } |
| 3644 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3645 | static PyObject * |
| 3646 | get_class(PyObject *obj) |
| 3647 | { |
| 3648 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3649 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3650 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3651 | if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) { |
| 3652 | cls = (PyObject *) Py_TYPE(obj); |
| 3653 | Py_INCREF(cls); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3654 | } |
| 3655 | return cls; |
| 3656 | } |
| 3657 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3658 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3659 | * appropriate __reduce__ method for obj. |
| 3660 | */ |
| 3661 | static int |
| 3662 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3663 | { |
| 3664 | PyObject *callable; |
| 3665 | PyObject *argtup; |
| 3666 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3667 | PyObject *listitems = Py_None; |
| 3668 | PyObject *dictitems = Py_None; |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3669 | PyObject *state_setter = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3670 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3671 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3672 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3673 | |
| 3674 | const char reduce_op = REDUCE; |
| 3675 | const char build_op = BUILD; |
| 3676 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3677 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3678 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3679 | size = PyTuple_Size(args); |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3680 | if (size < 2 || size > 6) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3681 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3682 | "__reduce__ must contain 2 through 6 elements"); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3683 | return -1; |
| 3684 | } |
| 3685 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3686 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6, |
| 3687 | &callable, &argtup, &state, &listitems, &dictitems, |
| 3688 | &state_setter)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3689 | return -1; |
| 3690 | |
| 3691 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3692 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3693 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3694 | return -1; |
| 3695 | } |
| 3696 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3697 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3698 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3699 | return -1; |
| 3700 | } |
| 3701 | |
| 3702 | if (state == Py_None) |
| 3703 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3704 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3705 | if (listitems == Py_None) |
| 3706 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3707 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3708 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3709 | "returned by __reduce__ must be an iterator, not %s", |
| 3710 | Py_TYPE(listitems)->tp_name); |
| 3711 | return -1; |
| 3712 | } |
| 3713 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3714 | if (dictitems == Py_None) |
| 3715 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3716 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3717 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3718 | "returned by __reduce__ must be an iterator, not %s", |
| 3719 | Py_TYPE(dictitems)->tp_name); |
| 3720 | return -1; |
| 3721 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3722 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3723 | if (state_setter == Py_None) |
| 3724 | state_setter = NULL; |
| 3725 | else if (!PyCallable_Check(state_setter)) { |
| 3726 | PyErr_Format(st->PicklingError, "sixth element of the tuple " |
| 3727 | "returned by __reduce__ must be a function, not %s", |
| 3728 | Py_TYPE(state_setter)->tp_name); |
| 3729 | return -1; |
| 3730 | } |
| 3731 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3732 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3733 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3734 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3735 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3736 | if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) { |
| 3737 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3738 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3739 | if (name != NULL && PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3740 | _Py_IDENTIFIER(__newobj_ex__); |
Serhiy Storchaka | f0f35a6 | 2017-01-09 10:09:43 +0200 | [diff] [blame] | 3741 | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
| 3742 | name, &PyId___newobj_ex__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3743 | if (!use_newobj_ex) { |
| 3744 | _Py_IDENTIFIER(__newobj__); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 3745 | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3746 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3747 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3748 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3749 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3750 | |
| 3751 | if (use_newobj_ex) { |
| 3752 | PyObject *cls; |
| 3753 | PyObject *args; |
| 3754 | PyObject *kwargs; |
| 3755 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3756 | if (PyTuple_GET_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3757 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3758 | "length of the NEWOBJ_EX argument tuple must be " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3759 | "exactly 3, not %zd", PyTuple_GET_SIZE(argtup)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3760 | return -1; |
| 3761 | } |
| 3762 | |
| 3763 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3764 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3765 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3766 | "first item from NEWOBJ_EX argument tuple must " |
| 3767 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3768 | return -1; |
| 3769 | } |
| 3770 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3771 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3772 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3773 | "second item from NEWOBJ_EX argument tuple must " |
| 3774 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3775 | return -1; |
| 3776 | } |
| 3777 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3778 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3779 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3780 | "third item from NEWOBJ_EX argument tuple must " |
| 3781 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3782 | return -1; |
| 3783 | } |
| 3784 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3785 | if (self->proto >= 4) { |
| 3786 | if (save(self, cls, 0) < 0 || |
| 3787 | save(self, args, 0) < 0 || |
| 3788 | save(self, kwargs, 0) < 0 || |
| 3789 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3790 | return -1; |
| 3791 | } |
| 3792 | } |
| 3793 | else { |
| 3794 | PyObject *newargs; |
| 3795 | PyObject *cls_new; |
| 3796 | Py_ssize_t i; |
| 3797 | _Py_IDENTIFIER(__new__); |
| 3798 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3799 | newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3800 | if (newargs == NULL) |
| 3801 | return -1; |
| 3802 | |
| 3803 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 3804 | if (cls_new == NULL) { |
| 3805 | Py_DECREF(newargs); |
| 3806 | return -1; |
| 3807 | } |
| 3808 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 3809 | Py_INCREF(cls); |
| 3810 | PyTuple_SET_ITEM(newargs, 1, cls); |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3811 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3812 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 3813 | Py_INCREF(item); |
| 3814 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 3815 | } |
| 3816 | |
| 3817 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 3818 | Py_DECREF(newargs); |
| 3819 | if (callable == NULL) |
| 3820 | return -1; |
| 3821 | |
| 3822 | newargs = PyTuple_New(0); |
| 3823 | if (newargs == NULL) { |
| 3824 | Py_DECREF(callable); |
| 3825 | return -1; |
| 3826 | } |
| 3827 | |
| 3828 | if (save(self, callable, 0) < 0 || |
| 3829 | save(self, newargs, 0) < 0 || |
| 3830 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 3831 | Py_DECREF(newargs); |
| 3832 | Py_DECREF(callable); |
| 3833 | return -1; |
| 3834 | } |
| 3835 | Py_DECREF(newargs); |
| 3836 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3837 | } |
| 3838 | } |
| 3839 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3840 | PyObject *cls; |
| 3841 | PyObject *newargtup; |
| 3842 | PyObject *obj_class; |
| 3843 | int p; |
| 3844 | |
| 3845 | /* Sanity checks. */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3846 | if (PyTuple_GET_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3847 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3848 | return -1; |
| 3849 | } |
| 3850 | |
| 3851 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3852 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3853 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3854 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3855 | return -1; |
| 3856 | } |
| 3857 | |
| 3858 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3859 | obj_class = get_class(obj); |
Zackery Spytz | 25d3897 | 2018-12-05 11:29:20 -0700 | [diff] [blame] | 3860 | if (obj_class == NULL) { |
| 3861 | return -1; |
| 3862 | } |
| 3863 | p = obj_class != cls; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3864 | Py_DECREF(obj_class); |
| 3865 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3866 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3867 | "__newobj__ args has the wrong class"); |
| 3868 | return -1; |
| 3869 | } |
| 3870 | } |
| 3871 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3872 | what happen if the value returned by the __reduce__() method of |
| 3873 | some extension type contains another object of the same type. Ouch! |
| 3874 | |
| 3875 | Here is a quick example, that I ran into, to illustrate what I |
| 3876 | mean: |
| 3877 | |
| 3878 | >>> import pickle, copyreg |
| 3879 | >>> copyreg.dispatch_table.pop(complex) |
| 3880 | >>> pickle.dumps(1+2j) |
| 3881 | Traceback (most recent call last): |
| 3882 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3883 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3884 | |
| 3885 | Removing the complex class from copyreg.dispatch_table made the |
| 3886 | __reduce_ex__() method emit another complex object: |
| 3887 | |
| 3888 | >>> (1+1j).__reduce_ex__(2) |
| 3889 | (<function __newobj__ at 0xb7b71c3c>, |
| 3890 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3891 | |
| 3892 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3893 | ensued. Of course, the bug was in the complex class which had a |
| 3894 | broken __getnewargs__() that emitted another complex object. But, |
| 3895 | the point, here, is it is quite easy to end up with a broken reduce |
| 3896 | function. */ |
| 3897 | |
| 3898 | /* Save the class and its __new__ arguments. */ |
| 3899 | if (save(self, cls, 0) < 0) |
| 3900 | return -1; |
| 3901 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3902 | newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3903 | if (newargtup == NULL) |
| 3904 | return -1; |
| 3905 | |
| 3906 | p = save(self, newargtup, 0); |
| 3907 | Py_DECREF(newargtup); |
| 3908 | if (p < 0) |
| 3909 | return -1; |
| 3910 | |
| 3911 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3912 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3913 | return -1; |
| 3914 | } |
| 3915 | else { /* Not using NEWOBJ. */ |
| 3916 | if (save(self, callable, 0) < 0 || |
| 3917 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3918 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3919 | return -1; |
| 3920 | } |
| 3921 | |
| 3922 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3923 | the caller do not want to memoize the object. Not particularly useful, |
| 3924 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3925 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3926 | if (obj != NULL) { |
| 3927 | /* If the object is already in the memo, this means it is |
| 3928 | recursive. In this case, throw away everything we put on the |
| 3929 | stack, and fetch the object back from the memo. */ |
| 3930 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3931 | const char pop_op = POP; |
| 3932 | |
| 3933 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3934 | return -1; |
| 3935 | if (memo_get(self, obj) < 0) |
| 3936 | return -1; |
| 3937 | |
| 3938 | return 0; |
| 3939 | } |
| 3940 | else if (memo_put(self, obj) < 0) |
| 3941 | return -1; |
| 3942 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3943 | |
| 3944 | if (listitems && batch_list(self, listitems) < 0) |
| 3945 | return -1; |
| 3946 | |
| 3947 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3948 | return -1; |
| 3949 | |
| 3950 | if (state) { |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3951 | if (state_setter == NULL) { |
| 3952 | if (save(self, state, 0) < 0 || |
| 3953 | _Pickler_Write(self, &build_op, 1) < 0) |
| 3954 | return -1; |
| 3955 | } |
| 3956 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3957 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3958 | /* If a state_setter is specified, call it instead of load_build to |
| 3959 | * update obj's with its previous state. |
| 3960 | * The first 4 save/write instructions push state_setter and its |
| 3961 | * tuple of expected arguments (obj, state) onto the stack. The |
| 3962 | * REDUCE opcode triggers the state_setter(obj, state) function |
| 3963 | * call. Finally, because state-updating routines only do in-place |
| 3964 | * modification, the whole operation has to be stack-transparent. |
| 3965 | * Thus, we finally pop the call's output from the stack.*/ |
| 3966 | |
| 3967 | const char tupletwo_op = TUPLE2; |
| 3968 | const char pop_op = POP; |
| 3969 | if (save(self, state_setter, 0) < 0 || |
| 3970 | save(self, obj, 0) < 0 || save(self, state, 0) < 0 || |
| 3971 | _Pickler_Write(self, &tupletwo_op, 1) < 0 || |
| 3972 | _Pickler_Write(self, &reduce_op, 1) < 0 || |
| 3973 | _Pickler_Write(self, &pop_op, 1) < 0) |
| 3974 | return -1; |
| 3975 | } |
| 3976 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3977 | return 0; |
| 3978 | } |
| 3979 | |
| 3980 | static int |
| 3981 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3982 | { |
| 3983 | PyTypeObject *type; |
| 3984 | PyObject *reduce_func = NULL; |
| 3985 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3986 | int status = 0; |
| 3987 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3988 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3989 | return -1; |
| 3990 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3991 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3992 | on its returned object. */ |
| 3993 | if (!pers_save && self->pers_func) { |
| 3994 | /* save_pers() returns: |
| 3995 | -1 to signal an error; |
| 3996 | 0 if it did nothing successfully; |
| 3997 | 1 if a persistent id was saved. |
| 3998 | */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3999 | if ((status = save_pers(self, obj)) != 0) |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4000 | return status; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4001 | } |
| 4002 | |
| 4003 | type = Py_TYPE(obj); |
| 4004 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4005 | /* The old cPickle had an optimization that used switch-case statement |
| 4006 | dispatching on the first letter of the type name. This has was removed |
| 4007 | since benchmarks shown that this optimization was actually slowing |
| 4008 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4009 | |
| 4010 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 4011 | |
| 4012 | if (obj == Py_None) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4013 | return save_none(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4014 | } |
| 4015 | else if (obj == Py_False || obj == Py_True) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4016 | return save_bool(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4017 | } |
| 4018 | else if (type == &PyLong_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4019 | return save_long(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4020 | } |
| 4021 | else if (type == &PyFloat_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4022 | return save_float(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
| 4025 | /* Check the memo to see if it has the object. If so, generate |
| 4026 | a GET (or BINGET) opcode, instead of pickling the object |
| 4027 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4028 | if (PyMemoTable_Get(self->memo, obj)) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4029 | return memo_get(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4030 | } |
| 4031 | |
| 4032 | if (type == &PyBytes_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4033 | return save_bytes(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4034 | } |
| 4035 | else if (type == &PyUnicode_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4036 | return save_unicode(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4037 | } |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4038 | |
| 4039 | /* We're only calling Py_EnterRecursiveCall here so that atomic |
| 4040 | types above are pickled faster. */ |
| 4041 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 4042 | return -1; |
| 4043 | } |
| 4044 | |
| 4045 | if (type == &PyDict_Type) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4046 | status = save_dict(self, obj); |
| 4047 | goto done; |
| 4048 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4049 | else if (type == &PySet_Type) { |
| 4050 | status = save_set(self, obj); |
| 4051 | goto done; |
| 4052 | } |
| 4053 | else if (type == &PyFrozenSet_Type) { |
| 4054 | status = save_frozenset(self, obj); |
| 4055 | goto done; |
| 4056 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4057 | else if (type == &PyList_Type) { |
| 4058 | status = save_list(self, obj); |
| 4059 | goto done; |
| 4060 | } |
| 4061 | else if (type == &PyTuple_Type) { |
| 4062 | status = save_tuple(self, obj); |
| 4063 | goto done; |
| 4064 | } |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4065 | |
| 4066 | /* Now, check reducer_override. If it returns NotImplemented, |
| 4067 | * fallback to save_type or save_global, and then perhaps to the |
| 4068 | * regular reduction mechanism. |
| 4069 | */ |
| 4070 | if (self->reducer_override != NULL) { |
| 4071 | reduce_value = PyObject_CallFunctionObjArgs(self->reducer_override, |
| 4072 | obj, NULL); |
| 4073 | if (reduce_value == NULL) { |
| 4074 | goto error; |
| 4075 | } |
| 4076 | if (reduce_value != Py_NotImplemented) { |
| 4077 | goto reduce; |
| 4078 | } |
| 4079 | Py_DECREF(reduce_value); |
| 4080 | reduce_value = NULL; |
| 4081 | } |
| 4082 | |
| 4083 | if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 4084 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4085 | goto done; |
| 4086 | } |
| 4087 | else if (type == &PyFunction_Type) { |
| 4088 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 4089 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4090 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4091 | |
| 4092 | /* XXX: This part needs some unit tests. */ |
| 4093 | |
| 4094 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4095 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 4096 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4097 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4098 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4099 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4100 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 4101 | (PyObject *)type); |
| 4102 | if (reduce_func == NULL) { |
| 4103 | if (PyErr_Occurred()) { |
| 4104 | goto error; |
| 4105 | } |
| 4106 | } else { |
| 4107 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 4108 | Increase the reference count to be consistent with |
| 4109 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 4110 | Py_INCREF(reduce_func); |
| 4111 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4112 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4113 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 4114 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4115 | if (reduce_func == NULL) { |
| 4116 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4117 | PyErr_Clear(); |
| 4118 | else |
| 4119 | goto error; |
| 4120 | } |
| 4121 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4122 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4123 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4124 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4125 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 4126 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 4127 | status = save_global(self, obj, NULL); |
| 4128 | goto done; |
| 4129 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4130 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4131 | _Py_IDENTIFIER(__reduce__); |
| 4132 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4133 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4134 | |
| 4135 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 4136 | automatically defined as __reduce__. While this is convenient, this |
| 4137 | make it impossible to know which method was actually called. Of |
| 4138 | course, this is not a big deal. But still, it would be nice to let |
| 4139 | the user know which method was called when something go |
| 4140 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 4141 | don't actually have to check for a __reduce__ method. */ |
| 4142 | |
| 4143 | /* Check for a __reduce_ex__ method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4144 | if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) { |
| 4145 | goto error; |
| 4146 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4147 | if (reduce_func != NULL) { |
| 4148 | PyObject *proto; |
| 4149 | proto = PyLong_FromLong(self->proto); |
| 4150 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4151 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4152 | } |
| 4153 | } |
| 4154 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4155 | PickleState *st = _Pickle_GetGlobalState(); |
| 4156 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4157 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4158 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4159 | if (reduce_func != NULL) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4160 | reduce_value = _PyObject_CallNoArg(reduce_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4161 | } |
| 4162 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4163 | PyErr_Format(st->PicklingError, |
| 4164 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4165 | type->tp_name, obj); |
| 4166 | goto error; |
| 4167 | } |
| 4168 | } |
| 4169 | } |
| 4170 | |
| 4171 | if (reduce_value == NULL) |
| 4172 | goto error; |
| 4173 | |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4174 | reduce: |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4175 | if (PyUnicode_Check(reduce_value)) { |
| 4176 | status = save_global(self, obj, reduce_value); |
| 4177 | goto done; |
| 4178 | } |
| 4179 | |
| 4180 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4181 | PickleState *st = _Pickle_GetGlobalState(); |
| 4182 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4183 | "__reduce__ must return a string or tuple"); |
| 4184 | goto error; |
| 4185 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4186 | |
| 4187 | status = save_reduce(self, reduce_value, obj); |
| 4188 | |
| 4189 | if (0) { |
| 4190 | error: |
| 4191 | status = -1; |
| 4192 | } |
| 4193 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4194 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 4195 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4196 | Py_XDECREF(reduce_func); |
| 4197 | Py_XDECREF(reduce_value); |
| 4198 | |
| 4199 | return status; |
| 4200 | } |
| 4201 | |
| 4202 | static int |
| 4203 | dump(PicklerObject *self, PyObject *obj) |
| 4204 | { |
| 4205 | const char stop_op = STOP; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4206 | PyObject *tmp; |
| 4207 | _Py_IDENTIFIER(reducer_override); |
| 4208 | |
| 4209 | if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override, |
| 4210 | &tmp) < 0) { |
| 4211 | return -1; |
| 4212 | } |
| 4213 | /* Cache the reducer_override method, if it exists. */ |
| 4214 | if (tmp != NULL) { |
| 4215 | Py_XSETREF(self->reducer_override, tmp); |
| 4216 | } |
| 4217 | else { |
| 4218 | Py_CLEAR(self->reducer_override); |
| 4219 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4220 | |
| 4221 | if (self->proto >= 2) { |
| 4222 | char header[2]; |
| 4223 | |
| 4224 | header[0] = PROTO; |
| 4225 | assert(self->proto >= 0 && self->proto < 256); |
| 4226 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4227 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4228 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4229 | if (self->proto >= 4) |
| 4230 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
| 4233 | if (save(self, obj, 0) < 0 || |
Serhiy Storchaka | c869529 | 2018-04-04 00:11:27 +0300 | [diff] [blame] | 4234 | _Pickler_Write(self, &stop_op, 1) < 0 || |
| 4235 | _Pickler_CommitFrame(self) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4236 | return -1; |
Serhiy Storchaka | c869529 | 2018-04-04 00:11:27 +0300 | [diff] [blame] | 4237 | self->framing = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4238 | return 0; |
| 4239 | } |
| 4240 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4241 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4242 | |
| 4243 | _pickle.Pickler.clear_memo |
| 4244 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4245 | Clears the pickler's "memo". |
| 4246 | |
| 4247 | The memo is the data structure that remembers which objects the |
| 4248 | pickler has already seen, so that shared or recursive objects are |
| 4249 | pickled by reference and not by value. This method is useful when |
| 4250 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4251 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4252 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4253 | static PyObject * |
| 4254 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4255 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4256 | { |
| 4257 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4258 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4259 | |
| 4260 | Py_RETURN_NONE; |
| 4261 | } |
| 4262 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4263 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4264 | |
| 4265 | _pickle.Pickler.dump |
| 4266 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4267 | obj: object |
| 4268 | / |
| 4269 | |
| 4270 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4271 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4272 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4273 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4274 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4275 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4276 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4277 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4278 | Developers often forget to call __init__() in their subclasses, which |
| 4279 | would trigger a segfault without this check. */ |
| 4280 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4281 | PickleState *st = _Pickle_GetGlobalState(); |
| 4282 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4283 | "Pickler.__init__() was not called by %s.__init__()", |
| 4284 | Py_TYPE(self)->tp_name); |
| 4285 | return NULL; |
| 4286 | } |
| 4287 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4288 | if (_Pickler_ClearBuffer(self) < 0) |
| 4289 | return NULL; |
| 4290 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4291 | if (dump(self, obj) < 0) |
| 4292 | return NULL; |
| 4293 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4294 | if (_Pickler_FlushToFile(self) < 0) |
| 4295 | return NULL; |
| 4296 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4297 | Py_RETURN_NONE; |
| 4298 | } |
| 4299 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4300 | /*[clinic input] |
| 4301 | |
| 4302 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4303 | |
| 4304 | Returns size in memory, in bytes. |
| 4305 | [clinic start generated code]*/ |
| 4306 | |
| 4307 | static Py_ssize_t |
| 4308 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4309 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4310 | { |
| 4311 | Py_ssize_t res, s; |
| 4312 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4313 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4314 | if (self->memo != NULL) { |
| 4315 | res += sizeof(PyMemoTable); |
| 4316 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4317 | } |
| 4318 | if (self->output_buffer != NULL) { |
| 4319 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4320 | if (s == -1) |
| 4321 | return -1; |
| 4322 | res += s; |
| 4323 | } |
| 4324 | return res; |
| 4325 | } |
| 4326 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4327 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4328 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4329 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4330 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4331 | {NULL, NULL} /* sentinel */ |
| 4332 | }; |
| 4333 | |
| 4334 | static void |
| 4335 | Pickler_dealloc(PicklerObject *self) |
| 4336 | { |
| 4337 | PyObject_GC_UnTrack(self); |
| 4338 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4339 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4340 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4341 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4342 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4343 | Py_XDECREF(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4344 | Py_XDECREF(self->reducer_override); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4345 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4346 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4347 | |
| 4348 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4349 | } |
| 4350 | |
| 4351 | static int |
| 4352 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4353 | { |
| 4354 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4355 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4356 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4357 | Py_VISIT(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4358 | Py_VISIT(self->reducer_override); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4359 | return 0; |
| 4360 | } |
| 4361 | |
| 4362 | static int |
| 4363 | Pickler_clear(PicklerObject *self) |
| 4364 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4365 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4366 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4367 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4368 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4369 | Py_CLEAR(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4370 | Py_CLEAR(self->reducer_override); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4371 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4372 | if (self->memo != NULL) { |
| 4373 | PyMemoTable *memo = self->memo; |
| 4374 | self->memo = NULL; |
| 4375 | PyMemoTable_Del(memo); |
| 4376 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4377 | return 0; |
| 4378 | } |
| 4379 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4380 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4381 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4382 | |
| 4383 | _pickle.Pickler.__init__ |
| 4384 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4385 | file: object |
| 4386 | protocol: object = NULL |
| 4387 | fix_imports: bool = True |
| 4388 | |
| 4389 | This takes a binary file for writing a pickle data stream. |
| 4390 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4391 | The optional *protocol* argument tells the pickler to use the given |
| 4392 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4393 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4394 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4395 | Specifying a negative protocol version selects the highest protocol |
| 4396 | version supported. The higher the protocol used, the more recent the |
| 4397 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4398 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4399 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4400 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4401 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4402 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4403 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4404 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4405 | to map the new Python 3 names to the old module names used in Python |
| 4406 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4407 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4408 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4409 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4410 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4411 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4412 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4413 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4414 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4415 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4416 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4417 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4418 | if (self->write != NULL) |
| 4419 | (void)Pickler_clear(self); |
| 4420 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4421 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4422 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4423 | |
| 4424 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4425 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4426 | |
| 4427 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4428 | if (self->memo == NULL) { |
| 4429 | self->memo = PyMemoTable_New(); |
| 4430 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4431 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4432 | } |
| 4433 | self->output_len = 0; |
| 4434 | if (self->output_buffer == NULL) { |
| 4435 | self->max_output_len = WRITE_BUF_SIZE; |
| 4436 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4437 | self->max_output_len); |
| 4438 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4439 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4440 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4441 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4442 | self->fast = 0; |
| 4443 | self->fast_nesting = 0; |
| 4444 | self->fast_memo = NULL; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4445 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4446 | if (init_method_ref((PyObject *)self, &PyId_persistent_id, |
| 4447 | &self->pers_func, &self->pers_func_self) < 0) |
| 4448 | { |
| 4449 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4450 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4451 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4452 | if (_PyObject_LookupAttrId((PyObject *)self, |
| 4453 | &PyId_dispatch_table, &self->dispatch_table) < 0) { |
| 4454 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4455 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4456 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4457 | return 0; |
| 4458 | } |
| 4459 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4460 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4461 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4462 | * avoid breaking code like: |
| 4463 | * pickler.memo.clear() |
| 4464 | * and |
| 4465 | * pickler.memo = saved_memo |
| 4466 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4467 | * it. Note that we don't implement the entire mapping API here. This is |
| 4468 | * intentional, as these should be treated as black-box implementation details. |
| 4469 | */ |
| 4470 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4471 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4472 | _pickle.PicklerMemoProxy.clear |
| 4473 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4474 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4475 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4476 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4477 | static PyObject * |
| 4478 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4479 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4480 | { |
| 4481 | if (self->pickler->memo) |
| 4482 | PyMemoTable_Clear(self->pickler->memo); |
| 4483 | Py_RETURN_NONE; |
| 4484 | } |
| 4485 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4486 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4487 | _pickle.PicklerMemoProxy.copy |
| 4488 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4489 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4490 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4491 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4492 | static PyObject * |
| 4493 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4494 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4495 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4496 | PyMemoTable *memo; |
| 4497 | PyObject *new_memo = PyDict_New(); |
| 4498 | if (new_memo == NULL) |
| 4499 | return NULL; |
| 4500 | |
| 4501 | memo = self->pickler->memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 4502 | for (size_t i = 0; i < memo->mt_allocated; ++i) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4503 | PyMemoEntry entry = memo->mt_table[i]; |
| 4504 | if (entry.me_key != NULL) { |
| 4505 | int status; |
| 4506 | PyObject *key, *value; |
| 4507 | |
| 4508 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4509 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4510 | |
| 4511 | if (key == NULL || value == NULL) { |
| 4512 | Py_XDECREF(key); |
| 4513 | Py_XDECREF(value); |
| 4514 | goto error; |
| 4515 | } |
| 4516 | status = PyDict_SetItem(new_memo, key, value); |
| 4517 | Py_DECREF(key); |
| 4518 | Py_DECREF(value); |
| 4519 | if (status < 0) |
| 4520 | goto error; |
| 4521 | } |
| 4522 | } |
| 4523 | return new_memo; |
| 4524 | |
| 4525 | error: |
| 4526 | Py_XDECREF(new_memo); |
| 4527 | return NULL; |
| 4528 | } |
| 4529 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4530 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4531 | _pickle.PicklerMemoProxy.__reduce__ |
| 4532 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4533 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4534 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4535 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4536 | static PyObject * |
| 4537 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4538 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4539 | { |
| 4540 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4541 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4542 | if (contents == NULL) |
| 4543 | return NULL; |
| 4544 | |
| 4545 | reduce_value = PyTuple_New(2); |
| 4546 | if (reduce_value == NULL) { |
| 4547 | Py_DECREF(contents); |
| 4548 | return NULL; |
| 4549 | } |
| 4550 | dict_args = PyTuple_New(1); |
| 4551 | if (dict_args == NULL) { |
| 4552 | Py_DECREF(contents); |
| 4553 | Py_DECREF(reduce_value); |
| 4554 | return NULL; |
| 4555 | } |
| 4556 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4557 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4558 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4559 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4560 | return reduce_value; |
| 4561 | } |
| 4562 | |
| 4563 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4564 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4565 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4566 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4567 | {NULL, NULL} /* sentinel */ |
| 4568 | }; |
| 4569 | |
| 4570 | static void |
| 4571 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4572 | { |
| 4573 | PyObject_GC_UnTrack(self); |
| 4574 | Py_XDECREF(self->pickler); |
| 4575 | PyObject_GC_Del((PyObject *)self); |
| 4576 | } |
| 4577 | |
| 4578 | static int |
| 4579 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4580 | visitproc visit, void *arg) |
| 4581 | { |
| 4582 | Py_VISIT(self->pickler); |
| 4583 | return 0; |
| 4584 | } |
| 4585 | |
| 4586 | static int |
| 4587 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4588 | { |
| 4589 | Py_CLEAR(self->pickler); |
| 4590 | return 0; |
| 4591 | } |
| 4592 | |
| 4593 | static PyTypeObject PicklerMemoProxyType = { |
| 4594 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4595 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4596 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4597 | 0, |
| 4598 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4599 | 0, /* tp_print */ |
| 4600 | 0, /* tp_getattr */ |
| 4601 | 0, /* tp_setattr */ |
| 4602 | 0, /* tp_compare */ |
| 4603 | 0, /* tp_repr */ |
| 4604 | 0, /* tp_as_number */ |
| 4605 | 0, /* tp_as_sequence */ |
| 4606 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4607 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4608 | 0, /* tp_call */ |
| 4609 | 0, /* tp_str */ |
| 4610 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4611 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4612 | 0, /* tp_as_buffer */ |
| 4613 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4614 | 0, /* tp_doc */ |
| 4615 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4616 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4617 | 0, /* tp_richcompare */ |
| 4618 | 0, /* tp_weaklistoffset */ |
| 4619 | 0, /* tp_iter */ |
| 4620 | 0, /* tp_iternext */ |
| 4621 | picklerproxy_methods, /* tp_methods */ |
| 4622 | }; |
| 4623 | |
| 4624 | static PyObject * |
| 4625 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4626 | { |
| 4627 | PicklerMemoProxyObject *self; |
| 4628 | |
| 4629 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4630 | if (self == NULL) |
| 4631 | return NULL; |
| 4632 | Py_INCREF(pickler); |
| 4633 | self->pickler = pickler; |
| 4634 | PyObject_GC_Track(self); |
| 4635 | return (PyObject *)self; |
| 4636 | } |
| 4637 | |
| 4638 | /*****************************************************************************/ |
| 4639 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4640 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4641 | Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4642 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4643 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4644 | } |
| 4645 | |
| 4646 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4647 | Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4648 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4649 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4650 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4651 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4652 | PyErr_SetString(PyExc_TypeError, |
| 4653 | "attribute deletion is not supported"); |
| 4654 | return -1; |
| 4655 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4656 | |
| 4657 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4658 | PicklerObject *pickler = |
| 4659 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4660 | |
| 4661 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4662 | if (new_memo == NULL) |
| 4663 | return -1; |
| 4664 | } |
| 4665 | else if (PyDict_Check(obj)) { |
| 4666 | Py_ssize_t i = 0; |
| 4667 | PyObject *key, *value; |
| 4668 | |
| 4669 | new_memo = PyMemoTable_New(); |
| 4670 | if (new_memo == NULL) |
| 4671 | return -1; |
| 4672 | |
| 4673 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4674 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4675 | PyObject *memo_obj; |
| 4676 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4677 | if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4678 | PyErr_SetString(PyExc_TypeError, |
| 4679 | "'memo' values must be 2-item tuples"); |
| 4680 | goto error; |
| 4681 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4682 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4683 | if (memo_id == -1 && PyErr_Occurred()) |
| 4684 | goto error; |
| 4685 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4686 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4687 | goto error; |
| 4688 | } |
| 4689 | } |
| 4690 | else { |
| 4691 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 4692 | "'memo' attribute must be a PicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4693 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4694 | return -1; |
| 4695 | } |
| 4696 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4697 | PyMemoTable_Del(self->memo); |
| 4698 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4699 | |
| 4700 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4701 | |
| 4702 | error: |
| 4703 | if (new_memo) |
| 4704 | PyMemoTable_Del(new_memo); |
| 4705 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4706 | } |
| 4707 | |
| 4708 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4709 | Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4710 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4711 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4712 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4713 | return NULL; |
| 4714 | } |
| 4715 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4716 | } |
| 4717 | |
| 4718 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4719 | Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4720 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4721 | if (value == NULL) { |
| 4722 | PyErr_SetString(PyExc_TypeError, |
| 4723 | "attribute deletion is not supported"); |
| 4724 | return -1; |
| 4725 | } |
| 4726 | if (!PyCallable_Check(value)) { |
| 4727 | PyErr_SetString(PyExc_TypeError, |
| 4728 | "persistent_id must be a callable taking one argument"); |
| 4729 | return -1; |
| 4730 | } |
| 4731 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4732 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4733 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 4734 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4735 | |
| 4736 | return 0; |
| 4737 | } |
| 4738 | |
| 4739 | static PyMemberDef Pickler_members[] = { |
| 4740 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4741 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4742 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4743 | {NULL} |
| 4744 | }; |
| 4745 | |
| 4746 | static PyGetSetDef Pickler_getsets[] = { |
| 4747 | {"memo", (getter)Pickler_get_memo, |
| 4748 | (setter)Pickler_set_memo}, |
| 4749 | {"persistent_id", (getter)Pickler_get_persid, |
| 4750 | (setter)Pickler_set_persid}, |
| 4751 | {NULL} |
| 4752 | }; |
| 4753 | |
| 4754 | static PyTypeObject Pickler_Type = { |
| 4755 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4756 | "_pickle.Pickler" , /*tp_name*/ |
| 4757 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4758 | 0, /*tp_itemsize*/ |
| 4759 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4760 | 0, /*tp_print*/ |
| 4761 | 0, /*tp_getattr*/ |
| 4762 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4763 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4764 | 0, /*tp_repr*/ |
| 4765 | 0, /*tp_as_number*/ |
| 4766 | 0, /*tp_as_sequence*/ |
| 4767 | 0, /*tp_as_mapping*/ |
| 4768 | 0, /*tp_hash*/ |
| 4769 | 0, /*tp_call*/ |
| 4770 | 0, /*tp_str*/ |
| 4771 | 0, /*tp_getattro*/ |
| 4772 | 0, /*tp_setattro*/ |
| 4773 | 0, /*tp_as_buffer*/ |
| 4774 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4775 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4776 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4777 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4778 | 0, /*tp_richcompare*/ |
| 4779 | 0, /*tp_weaklistoffset*/ |
| 4780 | 0, /*tp_iter*/ |
| 4781 | 0, /*tp_iternext*/ |
| 4782 | Pickler_methods, /*tp_methods*/ |
| 4783 | Pickler_members, /*tp_members*/ |
| 4784 | Pickler_getsets, /*tp_getset*/ |
| 4785 | 0, /*tp_base*/ |
| 4786 | 0, /*tp_dict*/ |
| 4787 | 0, /*tp_descr_get*/ |
| 4788 | 0, /*tp_descr_set*/ |
| 4789 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4790 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4791 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4792 | PyType_GenericNew, /*tp_new*/ |
| 4793 | PyObject_GC_Del, /*tp_free*/ |
| 4794 | 0, /*tp_is_gc*/ |
| 4795 | }; |
| 4796 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4797 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4798 | |
| 4799 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4800 | using directly the C version of find_class(), when find_class() is not |
| 4801 | overridden by a subclass. Although, this could become rather hackish. A |
| 4802 | simpler optimization would be to call the C function when self is not a |
| 4803 | subclass instance. */ |
| 4804 | static PyObject * |
| 4805 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4806 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4807 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4808 | |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 4809 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
| 4810 | module_name, global_name, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4811 | } |
| 4812 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4813 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4814 | marker(UnpicklerObject *self) |
| 4815 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4816 | Py_ssize_t mark; |
| 4817 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4818 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4819 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4820 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4821 | return -1; |
| 4822 | } |
| 4823 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4824 | mark = self->marks[--self->num_marks]; |
| 4825 | self->stack->mark_set = self->num_marks != 0; |
| 4826 | self->stack->fence = self->num_marks ? |
| 4827 | self->marks[self->num_marks - 1] : 0; |
| 4828 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | static int |
| 4832 | load_none(UnpicklerObject *self) |
| 4833 | { |
| 4834 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4835 | return 0; |
| 4836 | } |
| 4837 | |
| 4838 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4839 | load_int(UnpicklerObject *self) |
| 4840 | { |
| 4841 | PyObject *value; |
| 4842 | char *endptr, *s; |
| 4843 | Py_ssize_t len; |
| 4844 | long x; |
| 4845 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4846 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4847 | return -1; |
| 4848 | if (len < 2) |
| 4849 | return bad_readline(); |
| 4850 | |
| 4851 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4852 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4853 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4854 | x = strtol(s, &endptr, 0); |
| 4855 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4856 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4857 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4858 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4859 | errno = 0; |
| 4860 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4861 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4862 | if (value == NULL) { |
| 4863 | PyErr_SetString(PyExc_ValueError, |
| 4864 | "could not convert string to int"); |
| 4865 | return -1; |
| 4866 | } |
| 4867 | } |
| 4868 | else { |
| 4869 | if (len == 3 && (x == 0 || x == 1)) { |
| 4870 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4871 | return -1; |
| 4872 | } |
| 4873 | else { |
| 4874 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4875 | return -1; |
| 4876 | } |
| 4877 | } |
| 4878 | |
| 4879 | PDATA_PUSH(self->stack, value, -1); |
| 4880 | return 0; |
| 4881 | } |
| 4882 | |
| 4883 | static int |
| 4884 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4885 | { |
| 4886 | assert(boolean == Py_True || boolean == Py_False); |
| 4887 | PDATA_APPEND(self->stack, boolean, -1); |
| 4888 | return 0; |
| 4889 | } |
| 4890 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4891 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4892 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4893 | */ |
| 4894 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4895 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4896 | { |
| 4897 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4898 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4899 | size_t x = 0; |
| 4900 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4901 | if (nbytes > (int)sizeof(size_t)) { |
| 4902 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4903 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4904 | */ |
| 4905 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4906 | if (s[i]) |
| 4907 | return -1; |
| 4908 | } |
| 4909 | nbytes = (int)sizeof(size_t); |
| 4910 | } |
| 4911 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4912 | x |= (size_t) s[i] << (8 * i); |
| 4913 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4914 | |
| 4915 | if (x > PY_SSIZE_T_MAX) |
| 4916 | return -1; |
| 4917 | else |
| 4918 | return (Py_ssize_t) x; |
| 4919 | } |
| 4920 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4921 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4922 | * 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] | 4923 | * 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] | 4924 | * of x-platform bugs. |
| 4925 | */ |
| 4926 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4927 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4928 | { |
| 4929 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4930 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4931 | long x = 0; |
| 4932 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4933 | for (i = 0; i < nbytes; i++) { |
| 4934 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4935 | } |
| 4936 | |
| 4937 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4938 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4939 | * to extend a BININT's sign bit to the full width. |
| 4940 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4941 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4942 | x |= -(x & (1L << 31)); |
| 4943 | } |
| 4944 | |
| 4945 | return x; |
| 4946 | } |
| 4947 | |
| 4948 | static int |
| 4949 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4950 | { |
| 4951 | PyObject *value; |
| 4952 | long x; |
| 4953 | |
| 4954 | x = calc_binint(s, size); |
| 4955 | |
| 4956 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4957 | return -1; |
| 4958 | |
| 4959 | PDATA_PUSH(self->stack, value, -1); |
| 4960 | return 0; |
| 4961 | } |
| 4962 | |
| 4963 | static int |
| 4964 | load_binint(UnpicklerObject *self) |
| 4965 | { |
| 4966 | char *s; |
| 4967 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4968 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4969 | return -1; |
| 4970 | |
| 4971 | return load_binintx(self, s, 4); |
| 4972 | } |
| 4973 | |
| 4974 | static int |
| 4975 | load_binint1(UnpicklerObject *self) |
| 4976 | { |
| 4977 | char *s; |
| 4978 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4979 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4980 | return -1; |
| 4981 | |
| 4982 | return load_binintx(self, s, 1); |
| 4983 | } |
| 4984 | |
| 4985 | static int |
| 4986 | load_binint2(UnpicklerObject *self) |
| 4987 | { |
| 4988 | char *s; |
| 4989 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4990 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4991 | return -1; |
| 4992 | |
| 4993 | return load_binintx(self, s, 2); |
| 4994 | } |
| 4995 | |
| 4996 | static int |
| 4997 | load_long(UnpicklerObject *self) |
| 4998 | { |
| 4999 | PyObject *value; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5000 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5001 | Py_ssize_t len; |
| 5002 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5003 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5004 | return -1; |
| 5005 | if (len < 2) |
| 5006 | return bad_readline(); |
| 5007 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 5008 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 5009 | the 'L' before calling PyLong_FromString. In order to maintain |
| 5010 | compatibility with Python 3.0.0, we don't actually *require* |
| 5011 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5012 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 5013 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 5014 | /* XXX: Should the base argument explicitly set to 10? */ |
| 5015 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 5016 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5017 | return -1; |
| 5018 | |
| 5019 | PDATA_PUSH(self->stack, value, -1); |
| 5020 | return 0; |
| 5021 | } |
| 5022 | |
| 5023 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 5024 | * data following. |
| 5025 | */ |
| 5026 | static int |
| 5027 | load_counted_long(UnpicklerObject *self, int size) |
| 5028 | { |
| 5029 | PyObject *value; |
| 5030 | char *nbytes; |
| 5031 | char *pdata; |
| 5032 | |
| 5033 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5034 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5035 | return -1; |
| 5036 | |
| 5037 | size = calc_binint(nbytes, size); |
| 5038 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5039 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5040 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5041 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5042 | "LONG pickle has negative byte count"); |
| 5043 | return -1; |
| 5044 | } |
| 5045 | |
| 5046 | if (size == 0) |
| 5047 | value = PyLong_FromLong(0L); |
| 5048 | else { |
| 5049 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5050 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5051 | return -1; |
| 5052 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 5053 | 1 /* little endian */ , 1 /* signed */ ); |
| 5054 | } |
| 5055 | if (value == NULL) |
| 5056 | return -1; |
| 5057 | PDATA_PUSH(self->stack, value, -1); |
| 5058 | return 0; |
| 5059 | } |
| 5060 | |
| 5061 | static int |
| 5062 | load_float(UnpicklerObject *self) |
| 5063 | { |
| 5064 | PyObject *value; |
| 5065 | char *endptr, *s; |
| 5066 | Py_ssize_t len; |
| 5067 | double d; |
| 5068 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5069 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5070 | return -1; |
| 5071 | if (len < 2) |
| 5072 | return bad_readline(); |
| 5073 | |
| 5074 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5075 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 5076 | if (d == -1.0 && PyErr_Occurred()) |
| 5077 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5078 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5079 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 5080 | return -1; |
| 5081 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5082 | value = PyFloat_FromDouble(d); |
| 5083 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5084 | return -1; |
| 5085 | |
| 5086 | PDATA_PUSH(self->stack, value, -1); |
| 5087 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5088 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5089 | |
| 5090 | static int |
| 5091 | load_binfloat(UnpicklerObject *self) |
| 5092 | { |
| 5093 | PyObject *value; |
| 5094 | double x; |
| 5095 | char *s; |
| 5096 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5097 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5098 | return -1; |
| 5099 | |
| 5100 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 5101 | if (x == -1.0 && PyErr_Occurred()) |
| 5102 | return -1; |
| 5103 | |
| 5104 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 5105 | return -1; |
| 5106 | |
| 5107 | PDATA_PUSH(self->stack, value, -1); |
| 5108 | return 0; |
| 5109 | } |
| 5110 | |
| 5111 | static int |
| 5112 | load_string(UnpicklerObject *self) |
| 5113 | { |
| 5114 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5115 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5116 | Py_ssize_t len; |
| 5117 | char *s, *p; |
| 5118 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5119 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5120 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5121 | /* Strip the newline */ |
| 5122 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5123 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5124 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5125 | p = s + 1; |
| 5126 | len -= 2; |
| 5127 | } |
| 5128 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5129 | PickleState *st = _Pickle_GetGlobalState(); |
| 5130 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5131 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5132 | return -1; |
| 5133 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5134 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5135 | |
| 5136 | /* Use the PyBytes API to decode the string, since that is what is used |
| 5137 | to encode, and then coerce the result to Unicode. */ |
| 5138 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5139 | if (bytes == NULL) |
| 5140 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5141 | |
| 5142 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 5143 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5144 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5145 | obj = bytes; |
| 5146 | } |
| 5147 | else { |
| 5148 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 5149 | Py_DECREF(bytes); |
| 5150 | if (obj == NULL) { |
| 5151 | return -1; |
| 5152 | } |
| 5153 | } |
| 5154 | |
| 5155 | PDATA_PUSH(self->stack, obj, -1); |
| 5156 | return 0; |
| 5157 | } |
| 5158 | |
| 5159 | static int |
| 5160 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 5161 | { |
| 5162 | PyObject *obj; |
| 5163 | Py_ssize_t size; |
| 5164 | char *s; |
| 5165 | |
| 5166 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5167 | return -1; |
| 5168 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5169 | size = calc_binsize(s, nbytes); |
| 5170 | if (size < 0) { |
| 5171 | PickleState *st = _Pickle_GetGlobalState(); |
| 5172 | PyErr_Format(st->UnpicklingError, |
| 5173 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 5174 | PY_SSIZE_T_MAX); |
| 5175 | return -1; |
| 5176 | } |
| 5177 | |
| 5178 | if (_Unpickler_Read(self, &s, size) < 0) |
| 5179 | return -1; |
| 5180 | |
| 5181 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 5182 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5183 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5184 | obj = PyBytes_FromStringAndSize(s, size); |
| 5185 | } |
| 5186 | else { |
| 5187 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 5188 | } |
| 5189 | if (obj == NULL) { |
| 5190 | return -1; |
| 5191 | } |
| 5192 | |
| 5193 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5194 | return 0; |
| 5195 | } |
| 5196 | |
| 5197 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5198 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5199 | { |
| 5200 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5201 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5202 | char *s; |
| 5203 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5204 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5205 | return -1; |
| 5206 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5207 | size = calc_binsize(s, nbytes); |
| 5208 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5209 | PyErr_Format(PyExc_OverflowError, |
| 5210 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5211 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5212 | return -1; |
| 5213 | } |
| 5214 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5215 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5216 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5217 | |
| 5218 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5219 | if (bytes == NULL) |
| 5220 | return -1; |
| 5221 | |
| 5222 | PDATA_PUSH(self->stack, bytes, -1); |
| 5223 | return 0; |
| 5224 | } |
| 5225 | |
| 5226 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5227 | load_unicode(UnpicklerObject *self) |
| 5228 | { |
| 5229 | PyObject *str; |
| 5230 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5231 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5232 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5233 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5234 | return -1; |
| 5235 | if (len < 1) |
| 5236 | return bad_readline(); |
| 5237 | |
| 5238 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5239 | if (str == NULL) |
| 5240 | return -1; |
| 5241 | |
| 5242 | PDATA_PUSH(self->stack, str, -1); |
| 5243 | return 0; |
| 5244 | } |
| 5245 | |
| 5246 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5247 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5248 | { |
| 5249 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5250 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5251 | char *s; |
| 5252 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5253 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5254 | return -1; |
| 5255 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5256 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5257 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5258 | PyErr_Format(PyExc_OverflowError, |
| 5259 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5260 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5261 | return -1; |
| 5262 | } |
| 5263 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5264 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5265 | return -1; |
| 5266 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5267 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5268 | if (str == NULL) |
| 5269 | return -1; |
| 5270 | |
| 5271 | PDATA_PUSH(self->stack, str, -1); |
| 5272 | return 0; |
| 5273 | } |
| 5274 | |
| 5275 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5276 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5277 | { |
| 5278 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5279 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5280 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5281 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5282 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5283 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5284 | if (tuple == NULL) |
| 5285 | return -1; |
| 5286 | PDATA_PUSH(self->stack, tuple, -1); |
| 5287 | return 0; |
| 5288 | } |
| 5289 | |
| 5290 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5291 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5292 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5293 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5294 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5295 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5296 | return -1; |
| 5297 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5298 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | static int |
| 5302 | load_empty_list(UnpicklerObject *self) |
| 5303 | { |
| 5304 | PyObject *list; |
| 5305 | |
| 5306 | if ((list = PyList_New(0)) == NULL) |
| 5307 | return -1; |
| 5308 | PDATA_PUSH(self->stack, list, -1); |
| 5309 | return 0; |
| 5310 | } |
| 5311 | |
| 5312 | static int |
| 5313 | load_empty_dict(UnpicklerObject *self) |
| 5314 | { |
| 5315 | PyObject *dict; |
| 5316 | |
| 5317 | if ((dict = PyDict_New()) == NULL) |
| 5318 | return -1; |
| 5319 | PDATA_PUSH(self->stack, dict, -1); |
| 5320 | return 0; |
| 5321 | } |
| 5322 | |
| 5323 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5324 | load_empty_set(UnpicklerObject *self) |
| 5325 | { |
| 5326 | PyObject *set; |
| 5327 | |
| 5328 | if ((set = PySet_New(NULL)) == NULL) |
| 5329 | return -1; |
| 5330 | PDATA_PUSH(self->stack, set, -1); |
| 5331 | return 0; |
| 5332 | } |
| 5333 | |
| 5334 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5335 | load_list(UnpicklerObject *self) |
| 5336 | { |
| 5337 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5338 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5339 | |
| 5340 | if ((i = marker(self)) < 0) |
| 5341 | return -1; |
| 5342 | |
| 5343 | list = Pdata_poplist(self->stack, i); |
| 5344 | if (list == NULL) |
| 5345 | return -1; |
| 5346 | PDATA_PUSH(self->stack, list, -1); |
| 5347 | return 0; |
| 5348 | } |
| 5349 | |
| 5350 | static int |
| 5351 | load_dict(UnpicklerObject *self) |
| 5352 | { |
| 5353 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5354 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5355 | |
| 5356 | if ((i = marker(self)) < 0) |
| 5357 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5358 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5359 | |
| 5360 | if ((dict = PyDict_New()) == NULL) |
| 5361 | return -1; |
| 5362 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5363 | if ((j - i) % 2 != 0) { |
| 5364 | PickleState *st = _Pickle_GetGlobalState(); |
| 5365 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5366 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5367 | return -1; |
| 5368 | } |
| 5369 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5370 | for (k = i + 1; k < j; k += 2) { |
| 5371 | key = self->stack->data[k - 1]; |
| 5372 | value = self->stack->data[k]; |
| 5373 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5374 | Py_DECREF(dict); |
| 5375 | return -1; |
| 5376 | } |
| 5377 | } |
| 5378 | Pdata_clear(self->stack, i); |
| 5379 | PDATA_PUSH(self->stack, dict, -1); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5383 | static int |
| 5384 | load_frozenset(UnpicklerObject *self) |
| 5385 | { |
| 5386 | PyObject *items; |
| 5387 | PyObject *frozenset; |
| 5388 | Py_ssize_t i; |
| 5389 | |
| 5390 | if ((i = marker(self)) < 0) |
| 5391 | return -1; |
| 5392 | |
| 5393 | items = Pdata_poptuple(self->stack, i); |
| 5394 | if (items == NULL) |
| 5395 | return -1; |
| 5396 | |
| 5397 | frozenset = PyFrozenSet_New(items); |
| 5398 | Py_DECREF(items); |
| 5399 | if (frozenset == NULL) |
| 5400 | return -1; |
| 5401 | |
| 5402 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5403 | return 0; |
| 5404 | } |
| 5405 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5406 | static PyObject * |
| 5407 | instantiate(PyObject *cls, PyObject *args) |
| 5408 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5409 | /* Caller must assure args are a tuple. Normally, args come from |
| 5410 | Pdata_poptuple which packs objects from the top of the stack |
| 5411 | into a newly created tuple. */ |
| 5412 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5413 | if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { |
| 5414 | _Py_IDENTIFIER(__getinitargs__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5415 | _Py_IDENTIFIER(__new__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5416 | PyObject *func; |
| 5417 | if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) { |
| 5418 | return NULL; |
| 5419 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5420 | if (func == NULL) { |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5421 | return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL); |
| 5422 | } |
| 5423 | Py_DECREF(func); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5424 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5425 | return PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5426 | } |
| 5427 | |
| 5428 | static int |
| 5429 | load_obj(UnpicklerObject *self) |
| 5430 | { |
| 5431 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5432 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5433 | |
| 5434 | if ((i = marker(self)) < 0) |
| 5435 | return -1; |
| 5436 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5437 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5438 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5439 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5440 | args = Pdata_poptuple(self->stack, i + 1); |
| 5441 | if (args == NULL) |
| 5442 | return -1; |
| 5443 | |
| 5444 | PDATA_POP(self->stack, cls); |
| 5445 | if (cls) { |
| 5446 | obj = instantiate(cls, args); |
| 5447 | Py_DECREF(cls); |
| 5448 | } |
| 5449 | Py_DECREF(args); |
| 5450 | if (obj == NULL) |
| 5451 | return -1; |
| 5452 | |
| 5453 | PDATA_PUSH(self->stack, obj, -1); |
| 5454 | return 0; |
| 5455 | } |
| 5456 | |
| 5457 | static int |
| 5458 | load_inst(UnpicklerObject *self) |
| 5459 | { |
| 5460 | PyObject *cls = NULL; |
| 5461 | PyObject *args = NULL; |
| 5462 | PyObject *obj = NULL; |
| 5463 | PyObject *module_name; |
| 5464 | PyObject *class_name; |
| 5465 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5466 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5467 | char *s; |
| 5468 | |
| 5469 | if ((i = marker(self)) < 0) |
| 5470 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5471 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5472 | return -1; |
| 5473 | if (len < 2) |
| 5474 | return bad_readline(); |
| 5475 | |
| 5476 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5477 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5478 | supported by older protocols on Python 2.x. */ |
| 5479 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5480 | if (module_name == NULL) |
| 5481 | return -1; |
| 5482 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5483 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5484 | if (len < 2) { |
| 5485 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5486 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5487 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5488 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5489 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5490 | cls = find_class(self, module_name, class_name); |
| 5491 | Py_DECREF(class_name); |
| 5492 | } |
| 5493 | } |
| 5494 | Py_DECREF(module_name); |
| 5495 | |
| 5496 | if (cls == NULL) |
| 5497 | return -1; |
| 5498 | |
| 5499 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5500 | obj = instantiate(cls, args); |
| 5501 | Py_DECREF(args); |
| 5502 | } |
| 5503 | Py_DECREF(cls); |
| 5504 | |
| 5505 | if (obj == NULL) |
| 5506 | return -1; |
| 5507 | |
| 5508 | PDATA_PUSH(self->stack, obj, -1); |
| 5509 | return 0; |
| 5510 | } |
| 5511 | |
| 5512 | static int |
| 5513 | load_newobj(UnpicklerObject *self) |
| 5514 | { |
| 5515 | PyObject *args = NULL; |
| 5516 | PyObject *clsraw = NULL; |
| 5517 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5518 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5519 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5520 | |
| 5521 | /* Stack is ... cls argtuple, and we want to call |
| 5522 | * cls.__new__(cls, *argtuple). |
| 5523 | */ |
| 5524 | PDATA_POP(self->stack, args); |
| 5525 | if (args == NULL) |
| 5526 | goto error; |
| 5527 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5528 | PyErr_SetString(st->UnpicklingError, |
| 5529 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5530 | goto error; |
| 5531 | } |
| 5532 | |
| 5533 | PDATA_POP(self->stack, clsraw); |
| 5534 | cls = (PyTypeObject *)clsraw; |
| 5535 | if (cls == NULL) |
| 5536 | goto error; |
| 5537 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5538 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5539 | "isn't a type object"); |
| 5540 | goto error; |
| 5541 | } |
| 5542 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5543 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5544 | "has NULL tp_new"); |
| 5545 | goto error; |
| 5546 | } |
| 5547 | |
| 5548 | /* Call __new__. */ |
| 5549 | obj = cls->tp_new(cls, args, NULL); |
| 5550 | if (obj == NULL) |
| 5551 | goto error; |
| 5552 | |
| 5553 | Py_DECREF(args); |
| 5554 | Py_DECREF(clsraw); |
| 5555 | PDATA_PUSH(self->stack, obj, -1); |
| 5556 | return 0; |
| 5557 | |
| 5558 | error: |
| 5559 | Py_XDECREF(args); |
| 5560 | Py_XDECREF(clsraw); |
| 5561 | return -1; |
| 5562 | } |
| 5563 | |
| 5564 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5565 | load_newobj_ex(UnpicklerObject *self) |
| 5566 | { |
| 5567 | PyObject *cls, *args, *kwargs; |
| 5568 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5569 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5570 | |
| 5571 | PDATA_POP(self->stack, kwargs); |
| 5572 | if (kwargs == NULL) { |
| 5573 | return -1; |
| 5574 | } |
| 5575 | PDATA_POP(self->stack, args); |
| 5576 | if (args == NULL) { |
| 5577 | Py_DECREF(kwargs); |
| 5578 | return -1; |
| 5579 | } |
| 5580 | PDATA_POP(self->stack, cls); |
| 5581 | if (cls == NULL) { |
| 5582 | Py_DECREF(kwargs); |
| 5583 | Py_DECREF(args); |
| 5584 | return -1; |
| 5585 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5586 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5587 | if (!PyType_Check(cls)) { |
| 5588 | Py_DECREF(kwargs); |
| 5589 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5590 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5591 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5592 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5593 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5594 | return -1; |
| 5595 | } |
| 5596 | |
| 5597 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5598 | Py_DECREF(kwargs); |
| 5599 | Py_DECREF(args); |
| 5600 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5601 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5602 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5603 | return -1; |
| 5604 | } |
| 5605 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5606 | Py_DECREF(kwargs); |
| 5607 | Py_DECREF(args); |
| 5608 | Py_DECREF(cls); |
| 5609 | if (obj == NULL) { |
| 5610 | return -1; |
| 5611 | } |
| 5612 | PDATA_PUSH(self->stack, obj, -1); |
| 5613 | return 0; |
| 5614 | } |
| 5615 | |
| 5616 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5617 | load_global(UnpicklerObject *self) |
| 5618 | { |
| 5619 | PyObject *global = NULL; |
| 5620 | PyObject *module_name; |
| 5621 | PyObject *global_name; |
| 5622 | Py_ssize_t len; |
| 5623 | char *s; |
| 5624 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5625 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5626 | return -1; |
| 5627 | if (len < 2) |
| 5628 | return bad_readline(); |
| 5629 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5630 | if (!module_name) |
| 5631 | return -1; |
| 5632 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5633 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5634 | if (len < 2) { |
| 5635 | Py_DECREF(module_name); |
| 5636 | return bad_readline(); |
| 5637 | } |
| 5638 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5639 | if (global_name) { |
| 5640 | global = find_class(self, module_name, global_name); |
| 5641 | Py_DECREF(global_name); |
| 5642 | } |
| 5643 | } |
| 5644 | Py_DECREF(module_name); |
| 5645 | |
| 5646 | if (global == NULL) |
| 5647 | return -1; |
| 5648 | PDATA_PUSH(self->stack, global, -1); |
| 5649 | return 0; |
| 5650 | } |
| 5651 | |
| 5652 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5653 | load_stack_global(UnpicklerObject *self) |
| 5654 | { |
| 5655 | PyObject *global; |
| 5656 | PyObject *module_name; |
| 5657 | PyObject *global_name; |
| 5658 | |
| 5659 | PDATA_POP(self->stack, global_name); |
| 5660 | PDATA_POP(self->stack, module_name); |
| 5661 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5662 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5663 | PickleState *st = _Pickle_GetGlobalState(); |
| 5664 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5665 | Py_XDECREF(global_name); |
| 5666 | Py_XDECREF(module_name); |
| 5667 | return -1; |
| 5668 | } |
| 5669 | global = find_class(self, module_name, global_name); |
| 5670 | Py_DECREF(global_name); |
| 5671 | Py_DECREF(module_name); |
| 5672 | if (global == NULL) |
| 5673 | return -1; |
| 5674 | PDATA_PUSH(self->stack, global, -1); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
| 5678 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5679 | load_persid(UnpicklerObject *self) |
| 5680 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5681 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5682 | Py_ssize_t len; |
| 5683 | char *s; |
| 5684 | |
| 5685 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5686 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5687 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5688 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5689 | return bad_readline(); |
| 5690 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5691 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5692 | if (pid == NULL) { |
| 5693 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 5694 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 5695 | "persistent IDs in protocol 0 must be " |
| 5696 | "ASCII strings"); |
| 5697 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5698 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5699 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5700 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5701 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5702 | Py_DECREF(pid); |
| 5703 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5704 | return -1; |
| 5705 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5706 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5707 | return 0; |
| 5708 | } |
| 5709 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5710 | PickleState *st = _Pickle_GetGlobalState(); |
| 5711 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5712 | "A load persistent id instruction was encountered,\n" |
| 5713 | "but no persistent_load function was specified."); |
| 5714 | return -1; |
| 5715 | } |
| 5716 | } |
| 5717 | |
| 5718 | static int |
| 5719 | load_binpersid(UnpicklerObject *self) |
| 5720 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5721 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5722 | |
| 5723 | if (self->pers_func) { |
| 5724 | PDATA_POP(self->stack, pid); |
| 5725 | if (pid == NULL) |
| 5726 | return -1; |
| 5727 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5728 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5729 | Py_DECREF(pid); |
| 5730 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5731 | return -1; |
| 5732 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5733 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5734 | return 0; |
| 5735 | } |
| 5736 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5737 | PickleState *st = _Pickle_GetGlobalState(); |
| 5738 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5739 | "A load persistent id instruction was encountered,\n" |
| 5740 | "but no persistent_load function was specified."); |
| 5741 | return -1; |
| 5742 | } |
| 5743 | } |
| 5744 | |
| 5745 | static int |
| 5746 | load_pop(UnpicklerObject *self) |
| 5747 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5748 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5749 | |
| 5750 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5751 | * an object stack and a mark stack. We have to be clever and |
| 5752 | * 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] | 5753 | * mark stack first, and only signalling a stack underflow if |
| 5754 | * the object stack is empty and the mark stack doesn't match |
| 5755 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5756 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5757 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5758 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5759 | self->stack->mark_set = self->num_marks != 0; |
| 5760 | self->stack->fence = self->num_marks ? |
| 5761 | self->marks[self->num_marks - 1] : 0; |
| 5762 | } else if (len <= self->stack->fence) |
| 5763 | return Pdata_stack_underflow(self->stack); |
| 5764 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5765 | len--; |
| 5766 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5767 | Py_SIZE(self->stack) = len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5768 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5769 | return 0; |
| 5770 | } |
| 5771 | |
| 5772 | static int |
| 5773 | load_pop_mark(UnpicklerObject *self) |
| 5774 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5775 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5776 | |
| 5777 | if ((i = marker(self)) < 0) |
| 5778 | return -1; |
| 5779 | |
| 5780 | Pdata_clear(self->stack, i); |
| 5781 | |
| 5782 | return 0; |
| 5783 | } |
| 5784 | |
| 5785 | static int |
| 5786 | load_dup(UnpicklerObject *self) |
| 5787 | { |
| 5788 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5789 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5790 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5791 | if (len <= self->stack->fence) |
| 5792 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | last = self->stack->data[len - 1]; |
| 5794 | PDATA_APPEND(self->stack, last, -1); |
| 5795 | return 0; |
| 5796 | } |
| 5797 | |
| 5798 | static int |
| 5799 | load_get(UnpicklerObject *self) |
| 5800 | { |
| 5801 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5802 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5803 | Py_ssize_t len; |
| 5804 | char *s; |
| 5805 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5806 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5807 | return -1; |
| 5808 | if (len < 2) |
| 5809 | return bad_readline(); |
| 5810 | |
| 5811 | key = PyLong_FromString(s, NULL, 10); |
| 5812 | if (key == NULL) |
| 5813 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5814 | idx = PyLong_AsSsize_t(key); |
| 5815 | if (idx == -1 && PyErr_Occurred()) { |
| 5816 | Py_DECREF(key); |
| 5817 | return -1; |
| 5818 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5819 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5820 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5821 | if (value == NULL) { |
| 5822 | if (!PyErr_Occurred()) |
| 5823 | PyErr_SetObject(PyExc_KeyError, key); |
| 5824 | Py_DECREF(key); |
| 5825 | return -1; |
| 5826 | } |
| 5827 | Py_DECREF(key); |
| 5828 | |
| 5829 | PDATA_APPEND(self->stack, value, -1); |
| 5830 | return 0; |
| 5831 | } |
| 5832 | |
| 5833 | static int |
| 5834 | load_binget(UnpicklerObject *self) |
| 5835 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5836 | PyObject *value; |
| 5837 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5838 | char *s; |
| 5839 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5840 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5841 | return -1; |
| 5842 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5843 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5844 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5845 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5846 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5847 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5848 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5849 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5850 | Py_DECREF(key); |
| 5851 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5852 | return -1; |
| 5853 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5854 | |
| 5855 | PDATA_APPEND(self->stack, value, -1); |
| 5856 | return 0; |
| 5857 | } |
| 5858 | |
| 5859 | static int |
| 5860 | load_long_binget(UnpicklerObject *self) |
| 5861 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5862 | PyObject *value; |
| 5863 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5864 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5865 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5866 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5867 | return -1; |
| 5868 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5869 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5870 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5871 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5872 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5873 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5874 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5875 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5876 | Py_DECREF(key); |
| 5877 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5878 | return -1; |
| 5879 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5880 | |
| 5881 | PDATA_APPEND(self->stack, value, -1); |
| 5882 | return 0; |
| 5883 | } |
| 5884 | |
| 5885 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5886 | * the number of bytes following the opcode, holding the index (code) value. |
| 5887 | */ |
| 5888 | static int |
| 5889 | load_extension(UnpicklerObject *self, int nbytes) |
| 5890 | { |
| 5891 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5892 | long code; /* calc_binint returns long */ |
| 5893 | PyObject *py_code; /* code as a Python int */ |
| 5894 | PyObject *obj; /* the object to push */ |
| 5895 | PyObject *pair; /* (module_name, class_name) */ |
| 5896 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5897 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5898 | |
| 5899 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5900 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5901 | return -1; |
| 5902 | code = calc_binint(codebytes, nbytes); |
| 5903 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5904 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5905 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5906 | return -1; |
| 5907 | } |
| 5908 | |
| 5909 | /* Look for the code in the cache. */ |
| 5910 | py_code = PyLong_FromLong(code); |
| 5911 | if (py_code == NULL) |
| 5912 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5913 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5914 | if (obj != NULL) { |
| 5915 | /* Bingo. */ |
| 5916 | Py_DECREF(py_code); |
| 5917 | PDATA_APPEND(self->stack, obj, -1); |
| 5918 | return 0; |
| 5919 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5920 | if (PyErr_Occurred()) { |
| 5921 | Py_DECREF(py_code); |
| 5922 | return -1; |
| 5923 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5924 | |
| 5925 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5926 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5927 | if (pair == NULL) { |
| 5928 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5929 | if (!PyErr_Occurred()) { |
| 5930 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5931 | "code %ld", code); |
| 5932 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5933 | return -1; |
| 5934 | } |
| 5935 | /* Since the extension registry is manipulable via Python code, |
| 5936 | * confirm that pair is really a 2-tuple of strings. |
| 5937 | */ |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 5938 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) { |
| 5939 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5940 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 5941 | |
| 5942 | module_name = PyTuple_GET_ITEM(pair, 0); |
| 5943 | if (!PyUnicode_Check(module_name)) { |
| 5944 | goto error; |
| 5945 | } |
| 5946 | |
| 5947 | class_name = PyTuple_GET_ITEM(pair, 1); |
| 5948 | if (!PyUnicode_Check(class_name)) { |
| 5949 | goto error; |
| 5950 | } |
| 5951 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5952 | /* Load the object. */ |
| 5953 | obj = find_class(self, module_name, class_name); |
| 5954 | if (obj == NULL) { |
| 5955 | Py_DECREF(py_code); |
| 5956 | return -1; |
| 5957 | } |
| 5958 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5959 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5960 | Py_DECREF(py_code); |
| 5961 | if (code < 0) { |
| 5962 | Py_DECREF(obj); |
| 5963 | return -1; |
| 5964 | } |
| 5965 | PDATA_PUSH(self->stack, obj, -1); |
| 5966 | return 0; |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 5967 | |
| 5968 | error: |
| 5969 | Py_DECREF(py_code); |
| 5970 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5971 | "isn't a 2-tuple of strings", code); |
| 5972 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
| 5975 | static int |
| 5976 | load_put(UnpicklerObject *self) |
| 5977 | { |
| 5978 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5979 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5980 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5981 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5982 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5983 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5984 | return -1; |
| 5985 | if (len < 2) |
| 5986 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5987 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5988 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5989 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5990 | |
| 5991 | key = PyLong_FromString(s, NULL, 10); |
| 5992 | if (key == NULL) |
| 5993 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5994 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5995 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5996 | if (idx < 0) { |
| 5997 | if (!PyErr_Occurred()) |
| 5998 | PyErr_SetString(PyExc_ValueError, |
| 5999 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6000 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6001 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6002 | |
| 6003 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
| 6006 | static int |
| 6007 | load_binput(UnpicklerObject *self) |
| 6008 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6009 | PyObject *value; |
| 6010 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6011 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6012 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6013 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6014 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6015 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6016 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6017 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6018 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6019 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6020 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6021 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6022 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6023 | } |
| 6024 | |
| 6025 | static int |
| 6026 | load_long_binput(UnpicklerObject *self) |
| 6027 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6028 | PyObject *value; |
| 6029 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6030 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6031 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6032 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6033 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6034 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6035 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6036 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6037 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6038 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6039 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6040 | if (idx < 0) { |
| 6041 | PyErr_SetString(PyExc_ValueError, |
| 6042 | "negative LONG_BINPUT argument"); |
| 6043 | return -1; |
| 6044 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6045 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6046 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
| 6049 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6050 | load_memoize(UnpicklerObject *self) |
| 6051 | { |
| 6052 | PyObject *value; |
| 6053 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6054 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6055 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6056 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 6057 | |
| 6058 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 6059 | } |
| 6060 | |
| 6061 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6062 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6063 | { |
| 6064 | PyObject *value; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6065 | PyObject *slice; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6066 | PyObject *list; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6067 | PyObject *result; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6068 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6069 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6070 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6071 | if (x > len || x <= self->stack->fence) |
| 6072 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6073 | if (len == x) /* nothing to do */ |
| 6074 | return 0; |
| 6075 | |
| 6076 | list = self->stack->data[x - 1]; |
| 6077 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6078 | if (PyList_CheckExact(list)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6079 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6080 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6081 | |
| 6082 | slice = Pdata_poplist(self->stack, x); |
| 6083 | if (!slice) |
| 6084 | return -1; |
| 6085 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6086 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6087 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6088 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6089 | } |
| 6090 | else { |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6091 | PyObject *extend_func; |
| 6092 | _Py_IDENTIFIER(extend); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6093 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6094 | extend_func = _PyObject_GetAttrId(list, &PyId_extend); |
| 6095 | if (extend_func != NULL) { |
| 6096 | slice = Pdata_poplist(self->stack, x); |
| 6097 | if (!slice) { |
| 6098 | Py_DECREF(extend_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6099 | return -1; |
| 6100 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6101 | result = _Pickle_FastCall(extend_func, slice); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6102 | Py_DECREF(extend_func); |
| 6103 | if (result == NULL) |
| 6104 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6105 | Py_DECREF(result); |
| 6106 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6107 | else { |
| 6108 | PyObject *append_func; |
| 6109 | _Py_IDENTIFIER(append); |
| 6110 | |
| 6111 | /* Even if the PEP 307 requires extend() and append() methods, |
| 6112 | fall back on append() if the object has no extend() method |
| 6113 | for backward compatibility. */ |
| 6114 | PyErr_Clear(); |
| 6115 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
| 6116 | if (append_func == NULL) |
| 6117 | return -1; |
| 6118 | for (i = x; i < len; i++) { |
| 6119 | value = self->stack->data[i]; |
| 6120 | result = _Pickle_FastCall(append_func, value); |
| 6121 | if (result == NULL) { |
| 6122 | Pdata_clear(self->stack, i + 1); |
| 6123 | Py_SIZE(self->stack) = x; |
| 6124 | Py_DECREF(append_func); |
| 6125 | return -1; |
| 6126 | } |
| 6127 | Py_DECREF(result); |
| 6128 | } |
| 6129 | Py_SIZE(self->stack) = x; |
| 6130 | Py_DECREF(append_func); |
| 6131 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6132 | } |
| 6133 | |
| 6134 | return 0; |
| 6135 | } |
| 6136 | |
| 6137 | static int |
| 6138 | load_append(UnpicklerObject *self) |
| 6139 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6140 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 6141 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6142 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6143 | } |
| 6144 | |
| 6145 | static int |
| 6146 | load_appends(UnpicklerObject *self) |
| 6147 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6148 | Py_ssize_t i = marker(self); |
| 6149 | if (i < 0) |
| 6150 | return -1; |
| 6151 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6155 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6156 | { |
| 6157 | PyObject *value, *key; |
| 6158 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6159 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6160 | int status = 0; |
| 6161 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6162 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6163 | if (x > len || x <= self->stack->fence) |
| 6164 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6165 | if (len == x) /* nothing to do */ |
| 6166 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6167 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6168 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6169 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6170 | PyErr_SetString(st->UnpicklingError, |
| 6171 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6172 | return -1; |
| 6173 | } |
| 6174 | |
| 6175 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 6176 | that supports the __setitem__ attribute. */ |
| 6177 | dict = self->stack->data[x - 1]; |
| 6178 | |
| 6179 | for (i = x + 1; i < len; i += 2) { |
| 6180 | key = self->stack->data[i - 1]; |
| 6181 | value = self->stack->data[i]; |
| 6182 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 6183 | status = -1; |
| 6184 | break; |
| 6185 | } |
| 6186 | } |
| 6187 | |
| 6188 | Pdata_clear(self->stack, x); |
| 6189 | return status; |
| 6190 | } |
| 6191 | |
| 6192 | static int |
| 6193 | load_setitem(UnpicklerObject *self) |
| 6194 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6195 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6196 | } |
| 6197 | |
| 6198 | static int |
| 6199 | load_setitems(UnpicklerObject *self) |
| 6200 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6201 | Py_ssize_t i = marker(self); |
| 6202 | if (i < 0) |
| 6203 | return -1; |
| 6204 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6205 | } |
| 6206 | |
| 6207 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6208 | load_additems(UnpicklerObject *self) |
| 6209 | { |
| 6210 | PyObject *set; |
| 6211 | Py_ssize_t mark, len, i; |
| 6212 | |
| 6213 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6214 | if (mark < 0) |
| 6215 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6216 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6217 | if (mark > len || mark <= self->stack->fence) |
| 6218 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6219 | if (len == mark) /* nothing to do */ |
| 6220 | return 0; |
| 6221 | |
| 6222 | set = self->stack->data[mark - 1]; |
| 6223 | |
| 6224 | if (PySet_Check(set)) { |
| 6225 | PyObject *items; |
| 6226 | int status; |
| 6227 | |
| 6228 | items = Pdata_poptuple(self->stack, mark); |
| 6229 | if (items == NULL) |
| 6230 | return -1; |
| 6231 | |
| 6232 | status = _PySet_Update(set, items); |
| 6233 | Py_DECREF(items); |
| 6234 | return status; |
| 6235 | } |
| 6236 | else { |
| 6237 | PyObject *add_func; |
| 6238 | _Py_IDENTIFIER(add); |
| 6239 | |
| 6240 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6241 | if (add_func == NULL) |
| 6242 | return -1; |
| 6243 | for (i = mark; i < len; i++) { |
| 6244 | PyObject *result; |
| 6245 | PyObject *item; |
| 6246 | |
| 6247 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6248 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6249 | if (result == NULL) { |
| 6250 | Pdata_clear(self->stack, i + 1); |
| 6251 | Py_SIZE(self->stack) = mark; |
| 6252 | return -1; |
| 6253 | } |
| 6254 | Py_DECREF(result); |
| 6255 | } |
| 6256 | Py_SIZE(self->stack) = mark; |
| 6257 | } |
| 6258 | |
| 6259 | return 0; |
| 6260 | } |
| 6261 | |
| 6262 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6263 | load_build(UnpicklerObject *self) |
| 6264 | { |
| 6265 | PyObject *state, *inst, *slotstate; |
| 6266 | PyObject *setstate; |
| 6267 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6268 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6269 | |
| 6270 | /* Stack is ... instance, state. We want to leave instance at |
| 6271 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6272 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6273 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6274 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6275 | |
| 6276 | PDATA_POP(self->stack, state); |
| 6277 | if (state == NULL) |
| 6278 | return -1; |
| 6279 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6280 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6281 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6282 | if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) { |
| 6283 | Py_DECREF(state); |
| 6284 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6285 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6286 | if (setstate != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6287 | PyObject *result; |
| 6288 | |
| 6289 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6290 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6291 | Py_DECREF(setstate); |
| 6292 | if (result == NULL) |
| 6293 | return -1; |
| 6294 | Py_DECREF(result); |
| 6295 | return 0; |
| 6296 | } |
| 6297 | |
| 6298 | /* A default __setstate__. First see whether state embeds a |
| 6299 | * slot state dict too (a proto 2 addition). |
| 6300 | */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 6301 | if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6302 | PyObject *tmp = state; |
| 6303 | |
| 6304 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6305 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6306 | Py_INCREF(state); |
| 6307 | Py_INCREF(slotstate); |
| 6308 | Py_DECREF(tmp); |
| 6309 | } |
| 6310 | else |
| 6311 | slotstate = NULL; |
| 6312 | |
| 6313 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6314 | if (state != Py_None) { |
| 6315 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6316 | PyObject *d_key, *d_value; |
| 6317 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6318 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6319 | |
| 6320 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6321 | PickleState *st = _Pickle_GetGlobalState(); |
| 6322 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6323 | goto error; |
| 6324 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6325 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6326 | if (dict == NULL) |
| 6327 | goto error; |
| 6328 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6329 | i = 0; |
| 6330 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6331 | /* normally the keys for instance attributes are |
| 6332 | interned. we should try to do that here. */ |
| 6333 | Py_INCREF(d_key); |
| 6334 | if (PyUnicode_CheckExact(d_key)) |
| 6335 | PyUnicode_InternInPlace(&d_key); |
| 6336 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6337 | Py_DECREF(d_key); |
| 6338 | goto error; |
| 6339 | } |
| 6340 | Py_DECREF(d_key); |
| 6341 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6342 | Py_DECREF(dict); |
| 6343 | } |
| 6344 | |
| 6345 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6346 | if (slotstate != NULL) { |
| 6347 | PyObject *d_key, *d_value; |
| 6348 | Py_ssize_t i; |
| 6349 | |
| 6350 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6351 | PickleState *st = _Pickle_GetGlobalState(); |
| 6352 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6353 | "slot state is not a dictionary"); |
| 6354 | goto error; |
| 6355 | } |
| 6356 | i = 0; |
| 6357 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6358 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6359 | goto error; |
| 6360 | } |
| 6361 | } |
| 6362 | |
| 6363 | if (0) { |
| 6364 | error: |
| 6365 | status = -1; |
| 6366 | } |
| 6367 | |
| 6368 | Py_DECREF(state); |
| 6369 | Py_XDECREF(slotstate); |
| 6370 | return status; |
| 6371 | } |
| 6372 | |
| 6373 | static int |
| 6374 | load_mark(UnpicklerObject *self) |
| 6375 | { |
| 6376 | |
| 6377 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6378 | * object stack and a mark stack. Here we push a mark onto the |
| 6379 | * mark stack. |
| 6380 | */ |
| 6381 | |
Sergey Fedoseev | 86b8991 | 2018-08-25 12:54:40 +0500 | [diff] [blame] | 6382 | if (self->num_marks >= self->marks_size) { |
Sergey Fedoseev | 90555ec | 2018-08-25 15:41:58 +0500 | [diff] [blame] | 6383 | size_t alloc = ((size_t)self->num_marks << 1) + 20; |
| 6384 | Py_ssize_t *marks_new = self->marks; |
| 6385 | PyMem_RESIZE(marks_new, Py_ssize_t, alloc); |
| 6386 | if (marks_new == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6387 | PyErr_NoMemory(); |
| 6388 | return -1; |
| 6389 | } |
Sergey Fedoseev | 90555ec | 2018-08-25 15:41:58 +0500 | [diff] [blame] | 6390 | self->marks = marks_new; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6391 | self->marks_size = (Py_ssize_t)alloc; |
| 6392 | } |
| 6393 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6394 | self->stack->mark_set = 1; |
| 6395 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6396 | |
| 6397 | return 0; |
| 6398 | } |
| 6399 | |
| 6400 | static int |
| 6401 | load_reduce(UnpicklerObject *self) |
| 6402 | { |
| 6403 | PyObject *callable = NULL; |
| 6404 | PyObject *argtup = NULL; |
| 6405 | PyObject *obj = NULL; |
| 6406 | |
| 6407 | PDATA_POP(self->stack, argtup); |
| 6408 | if (argtup == NULL) |
| 6409 | return -1; |
| 6410 | PDATA_POP(self->stack, callable); |
| 6411 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6412 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6413 | Py_DECREF(callable); |
| 6414 | } |
| 6415 | Py_DECREF(argtup); |
| 6416 | |
| 6417 | if (obj == NULL) |
| 6418 | return -1; |
| 6419 | |
| 6420 | PDATA_PUSH(self->stack, obj, -1); |
| 6421 | return 0; |
| 6422 | } |
| 6423 | |
| 6424 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6425 | * is the first opcode for protocols >= 2. |
| 6426 | */ |
| 6427 | static int |
| 6428 | load_proto(UnpicklerObject *self) |
| 6429 | { |
| 6430 | char *s; |
| 6431 | int i; |
| 6432 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6433 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6434 | return -1; |
| 6435 | |
| 6436 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6437 | if (i <= HIGHEST_PROTOCOL) { |
| 6438 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6439 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6440 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6441 | |
| 6442 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6443 | return -1; |
| 6444 | } |
| 6445 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6446 | static int |
| 6447 | load_frame(UnpicklerObject *self) |
| 6448 | { |
| 6449 | char *s; |
| 6450 | Py_ssize_t frame_len; |
| 6451 | |
| 6452 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6453 | return -1; |
| 6454 | |
| 6455 | frame_len = calc_binsize(s, 8); |
| 6456 | if (frame_len < 0) { |
| 6457 | PyErr_Format(PyExc_OverflowError, |
| 6458 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6459 | PY_SSIZE_T_MAX); |
| 6460 | return -1; |
| 6461 | } |
| 6462 | |
| 6463 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6464 | return -1; |
| 6465 | |
| 6466 | /* Rewind to start of frame */ |
| 6467 | self->next_read_idx -= frame_len; |
| 6468 | return 0; |
| 6469 | } |
| 6470 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6471 | static PyObject * |
| 6472 | load(UnpicklerObject *self) |
| 6473 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6474 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6475 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6476 | |
| 6477 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6478 | self->stack->mark_set = 0; |
| 6479 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6480 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6481 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6482 | Pdata_clear(self->stack, 0); |
| 6483 | |
| 6484 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6485 | #define OP(opcode, load_func) \ |
| 6486 | case opcode: if (load_func(self) < 0) break; continue; |
| 6487 | |
| 6488 | #define OP_ARG(opcode, load_func, arg) \ |
| 6489 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6490 | |
| 6491 | while (1) { |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6492 | if (_Unpickler_Read(self, &s, 1) < 0) { |
| 6493 | PickleState *st = _Pickle_GetGlobalState(); |
| 6494 | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
| 6495 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 6496 | } |
| 6497 | return NULL; |
| 6498 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6499 | |
| 6500 | switch ((enum opcode)s[0]) { |
| 6501 | OP(NONE, load_none) |
| 6502 | OP(BININT, load_binint) |
| 6503 | OP(BININT1, load_binint1) |
| 6504 | OP(BININT2, load_binint2) |
| 6505 | OP(INT, load_int) |
| 6506 | OP(LONG, load_long) |
| 6507 | OP_ARG(LONG1, load_counted_long, 1) |
| 6508 | OP_ARG(LONG4, load_counted_long, 4) |
| 6509 | OP(FLOAT, load_float) |
| 6510 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6511 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6512 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6513 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6514 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6515 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6516 | OP(STRING, load_string) |
| 6517 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6518 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6519 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6520 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6521 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6522 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6523 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6524 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6525 | OP(TUPLE, load_tuple) |
| 6526 | OP(EMPTY_LIST, load_empty_list) |
| 6527 | OP(LIST, load_list) |
| 6528 | OP(EMPTY_DICT, load_empty_dict) |
| 6529 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6530 | OP(EMPTY_SET, load_empty_set) |
| 6531 | OP(ADDITEMS, load_additems) |
| 6532 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6533 | OP(OBJ, load_obj) |
| 6534 | OP(INST, load_inst) |
| 6535 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6536 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6537 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6538 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6539 | OP(APPEND, load_append) |
| 6540 | OP(APPENDS, load_appends) |
| 6541 | OP(BUILD, load_build) |
| 6542 | OP(DUP, load_dup) |
| 6543 | OP(BINGET, load_binget) |
| 6544 | OP(LONG_BINGET, load_long_binget) |
| 6545 | OP(GET, load_get) |
| 6546 | OP(MARK, load_mark) |
| 6547 | OP(BINPUT, load_binput) |
| 6548 | OP(LONG_BINPUT, load_long_binput) |
| 6549 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6550 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6551 | OP(POP, load_pop) |
| 6552 | OP(POP_MARK, load_pop_mark) |
| 6553 | OP(SETITEM, load_setitem) |
| 6554 | OP(SETITEMS, load_setitems) |
| 6555 | OP(PERSID, load_persid) |
| 6556 | OP(BINPERSID, load_binpersid) |
| 6557 | OP(REDUCE, load_reduce) |
| 6558 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6559 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6560 | OP_ARG(EXT1, load_extension, 1) |
| 6561 | OP_ARG(EXT2, load_extension, 2) |
| 6562 | OP_ARG(EXT4, load_extension, 4) |
| 6563 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6564 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6565 | |
| 6566 | case STOP: |
| 6567 | break; |
| 6568 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6569 | default: |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6570 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6571 | PickleState *st = _Pickle_GetGlobalState(); |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6572 | unsigned char c = (unsigned char) *s; |
| 6573 | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
| 6574 | PyErr_Format(st->UnpicklingError, |
| 6575 | "invalid load key, '%c'.", c); |
| 6576 | } |
| 6577 | else { |
| 6578 | PyErr_Format(st->UnpicklingError, |
| 6579 | "invalid load key, '\\x%02x'.", c); |
| 6580 | } |
| 6581 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6582 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6583 | } |
| 6584 | |
| 6585 | break; /* and we are done! */ |
| 6586 | } |
| 6587 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6588 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6589 | return NULL; |
| 6590 | } |
| 6591 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6592 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6593 | return NULL; |
| 6594 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6595 | PDATA_POP(self->stack, value); |
| 6596 | return value; |
| 6597 | } |
| 6598 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6599 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6600 | |
| 6601 | _pickle.Unpickler.load |
| 6602 | |
| 6603 | Load a pickle. |
| 6604 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6605 | Read a pickled object representation from the open file object given |
| 6606 | in the constructor, and return the reconstituted object hierarchy |
| 6607 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6608 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6609 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6610 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6611 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6612 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6613 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6614 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6615 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6616 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6617 | segfaulting if a subclass overridden __init__ with a function that does |
| 6618 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6619 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6620 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6621 | PickleState *st = _Pickle_GetGlobalState(); |
| 6622 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6623 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6624 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6625 | return NULL; |
| 6626 | } |
| 6627 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6628 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6629 | } |
| 6630 | |
| 6631 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6632 | function is used for loading any global (i.e., functions), not just |
| 6633 | classes. The name is kept only for backward compatibility. */ |
| 6634 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6635 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6636 | |
| 6637 | _pickle.Unpickler.find_class |
| 6638 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6639 | module_name: object |
| 6640 | global_name: object |
| 6641 | / |
| 6642 | |
| 6643 | Return an object from a specified module. |
| 6644 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6645 | If necessary, the module will be imported. Subclasses may override |
| 6646 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6647 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6648 | |
| 6649 | This method is called whenever a class or a function object is |
| 6650 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6651 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6652 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6653 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6654 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6655 | PyObject *module_name, |
| 6656 | PyObject *global_name) |
| 6657 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6658 | { |
| 6659 | PyObject *global; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6660 | PyObject *module; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6661 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6662 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6663 | Python 3.x. We do this only with old pickle protocols and when the |
| 6664 | user has not disabled the feature. */ |
| 6665 | if (self->proto < 3 && self->fix_imports) { |
| 6666 | PyObject *key; |
| 6667 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6668 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6669 | |
| 6670 | /* Check if the global (i.e., a function or a class) was renamed |
| 6671 | or moved to another module. */ |
| 6672 | key = PyTuple_Pack(2, module_name, global_name); |
| 6673 | if (key == NULL) |
| 6674 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6675 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6676 | Py_DECREF(key); |
| 6677 | if (item) { |
| 6678 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6679 | PyErr_Format(PyExc_RuntimeError, |
| 6680 | "_compat_pickle.NAME_MAPPING values should be " |
| 6681 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6682 | return NULL; |
| 6683 | } |
| 6684 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6685 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6686 | if (!PyUnicode_Check(module_name) || |
| 6687 | !PyUnicode_Check(global_name)) { |
| 6688 | PyErr_Format(PyExc_RuntimeError, |
| 6689 | "_compat_pickle.NAME_MAPPING values should be " |
| 6690 | "pairs of str, not (%.200s, %.200s)", |
| 6691 | Py_TYPE(module_name)->tp_name, |
| 6692 | Py_TYPE(global_name)->tp_name); |
| 6693 | return NULL; |
| 6694 | } |
| 6695 | } |
| 6696 | else if (PyErr_Occurred()) { |
| 6697 | return NULL; |
| 6698 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6699 | else { |
| 6700 | /* Check if the module was renamed. */ |
| 6701 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6702 | if (item) { |
| 6703 | if (!PyUnicode_Check(item)) { |
| 6704 | PyErr_Format(PyExc_RuntimeError, |
| 6705 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6706 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6707 | return NULL; |
| 6708 | } |
| 6709 | module_name = item; |
| 6710 | } |
| 6711 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6712 | return NULL; |
| 6713 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6714 | } |
| 6715 | } |
| 6716 | |
tjb900 | 4371c0a | 2019-02-18 23:30:51 +0800 | [diff] [blame] | 6717 | /* |
| 6718 | * we don't use PyImport_GetModule here, because it can return partially- |
| 6719 | * initialised modules, which then cause the getattribute to fail. |
| 6720 | */ |
| 6721 | module = PyImport_Import(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6722 | if (module == NULL) { |
tjb900 | 4371c0a | 2019-02-18 23:30:51 +0800 | [diff] [blame] | 6723 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6724 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6725 | global = getattribute(module, global_name, self->proto >= 4); |
| 6726 | Py_DECREF(module); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6727 | return global; |
| 6728 | } |
| 6729 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6730 | /*[clinic input] |
| 6731 | |
| 6732 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6733 | |
| 6734 | Returns size in memory, in bytes. |
| 6735 | [clinic start generated code]*/ |
| 6736 | |
| 6737 | static Py_ssize_t |
| 6738 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6739 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6740 | { |
| 6741 | Py_ssize_t res; |
| 6742 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 6743 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6744 | if (self->memo != NULL) |
| 6745 | res += self->memo_size * sizeof(PyObject *); |
| 6746 | if (self->marks != NULL) |
| 6747 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6748 | if (self->input_line != NULL) |
| 6749 | res += strlen(self->input_line) + 1; |
| 6750 | if (self->encoding != NULL) |
| 6751 | res += strlen(self->encoding) + 1; |
| 6752 | if (self->errors != NULL) |
| 6753 | res += strlen(self->errors) + 1; |
| 6754 | return res; |
| 6755 | } |
| 6756 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6757 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6758 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6759 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6760 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6761 | {NULL, NULL} /* sentinel */ |
| 6762 | }; |
| 6763 | |
| 6764 | static void |
| 6765 | Unpickler_dealloc(UnpicklerObject *self) |
| 6766 | { |
| 6767 | PyObject_GC_UnTrack((PyObject *)self); |
| 6768 | Py_XDECREF(self->readline); |
| 6769 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6770 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6771 | Py_XDECREF(self->stack); |
| 6772 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6773 | if (self->buffer.buf != NULL) { |
| 6774 | PyBuffer_Release(&self->buffer); |
| 6775 | self->buffer.buf = NULL; |
| 6776 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6777 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6778 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6779 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6780 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6781 | PyMem_Free(self->encoding); |
| 6782 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6783 | |
| 6784 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6785 | } |
| 6786 | |
| 6787 | static int |
| 6788 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6789 | { |
| 6790 | Py_VISIT(self->readline); |
| 6791 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6792 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6793 | Py_VISIT(self->stack); |
| 6794 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6795 | return 0; |
| 6796 | } |
| 6797 | |
| 6798 | static int |
| 6799 | Unpickler_clear(UnpicklerObject *self) |
| 6800 | { |
| 6801 | Py_CLEAR(self->readline); |
| 6802 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6803 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6804 | Py_CLEAR(self->stack); |
| 6805 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6806 | if (self->buffer.buf != NULL) { |
| 6807 | PyBuffer_Release(&self->buffer); |
| 6808 | self->buffer.buf = NULL; |
| 6809 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6810 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6811 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6812 | PyMem_Free(self->marks); |
| 6813 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6814 | PyMem_Free(self->input_line); |
| 6815 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6816 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6817 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6818 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6819 | self->errors = NULL; |
| 6820 | |
| 6821 | return 0; |
| 6822 | } |
| 6823 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6824 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6825 | |
| 6826 | _pickle.Unpickler.__init__ |
| 6827 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6828 | file: object |
| 6829 | * |
| 6830 | fix_imports: bool = True |
| 6831 | encoding: str = 'ASCII' |
| 6832 | errors: str = 'strict' |
| 6833 | |
| 6834 | This takes a binary file for reading a pickle data stream. |
| 6835 | |
| 6836 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6837 | protocol argument is needed. Bytes past the pickled object's |
| 6838 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6839 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6840 | The argument *file* must have two methods, a read() method that takes |
| 6841 | an integer argument, and a readline() method that requires no |
| 6842 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6843 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6844 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6845 | |
| 6846 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6847 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6848 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6849 | 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] | 6850 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6851 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6852 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6853 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6854 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6855 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6856 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6857 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6858 | int fix_imports, const char *encoding, |
| 6859 | const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6860 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6861 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6862 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6863 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6864 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6865 | if (self->read != NULL) |
| 6866 | (void)Unpickler_clear(self); |
| 6867 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6868 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6869 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6870 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6871 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6872 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6873 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6874 | self->fix_imports = fix_imports; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6875 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6876 | if (init_method_ref((PyObject *)self, &PyId_persistent_load, |
| 6877 | &self->pers_func, &self->pers_func_self) < 0) |
| 6878 | { |
| 6879 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6880 | } |
| 6881 | |
| 6882 | self->stack = (Pdata *)Pdata_New(); |
| 6883 | if (self->stack == NULL) |
Zackery Spytz | 4b430e5 | 2018-09-28 23:48:46 -0600 | [diff] [blame] | 6884 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6885 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6886 | self->memo_size = 32; |
| 6887 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6888 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6889 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6890 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6891 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6892 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6893 | return 0; |
| 6894 | } |
| 6895 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6896 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6897 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6898 | * avoid breaking code like: |
| 6899 | * unpickler.memo.clear() |
| 6900 | * and |
| 6901 | * unpickler.memo = saved_memo |
| 6902 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6903 | * it. Note that we don't implement the entire mapping API here. This is |
| 6904 | * intentional, as these should be treated as black-box implementation details. |
| 6905 | * |
| 6906 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6907 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6908 | */ |
| 6909 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6910 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6911 | _pickle.UnpicklerMemoProxy.clear |
| 6912 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6913 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6914 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6915 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6916 | static PyObject * |
| 6917 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6918 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6919 | { |
| 6920 | _Unpickler_MemoCleanup(self->unpickler); |
| 6921 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6922 | if (self->unpickler->memo == NULL) |
| 6923 | return NULL; |
| 6924 | Py_RETURN_NONE; |
| 6925 | } |
| 6926 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6927 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6928 | _pickle.UnpicklerMemoProxy.copy |
| 6929 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6930 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6931 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6932 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6933 | static PyObject * |
| 6934 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6935 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6936 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 6937 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6938 | PyObject *new_memo = PyDict_New(); |
| 6939 | if (new_memo == NULL) |
| 6940 | return NULL; |
| 6941 | |
| 6942 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6943 | int status; |
| 6944 | PyObject *key, *value; |
| 6945 | |
| 6946 | value = self->unpickler->memo[i]; |
| 6947 | if (value == NULL) |
| 6948 | continue; |
| 6949 | |
| 6950 | key = PyLong_FromSsize_t(i); |
| 6951 | if (key == NULL) |
| 6952 | goto error; |
| 6953 | status = PyDict_SetItem(new_memo, key, value); |
| 6954 | Py_DECREF(key); |
| 6955 | if (status < 0) |
| 6956 | goto error; |
| 6957 | } |
| 6958 | return new_memo; |
| 6959 | |
| 6960 | error: |
| 6961 | Py_DECREF(new_memo); |
| 6962 | return NULL; |
| 6963 | } |
| 6964 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6965 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6966 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6967 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6968 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6969 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6970 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6971 | static PyObject * |
| 6972 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6973 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6974 | { |
| 6975 | PyObject *reduce_value; |
| 6976 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6977 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6978 | if (contents == NULL) |
| 6979 | return NULL; |
| 6980 | |
| 6981 | reduce_value = PyTuple_New(2); |
| 6982 | if (reduce_value == NULL) { |
| 6983 | Py_DECREF(contents); |
| 6984 | return NULL; |
| 6985 | } |
| 6986 | constructor_args = PyTuple_New(1); |
| 6987 | if (constructor_args == NULL) { |
| 6988 | Py_DECREF(contents); |
| 6989 | Py_DECREF(reduce_value); |
| 6990 | return NULL; |
| 6991 | } |
| 6992 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6993 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6994 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6995 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6996 | return reduce_value; |
| 6997 | } |
| 6998 | |
| 6999 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7000 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 7001 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 7002 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7003 | {NULL, NULL} /* sentinel */ |
| 7004 | }; |
| 7005 | |
| 7006 | static void |
| 7007 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 7008 | { |
| 7009 | PyObject_GC_UnTrack(self); |
| 7010 | Py_XDECREF(self->unpickler); |
| 7011 | PyObject_GC_Del((PyObject *)self); |
| 7012 | } |
| 7013 | |
| 7014 | static int |
| 7015 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 7016 | visitproc visit, void *arg) |
| 7017 | { |
| 7018 | Py_VISIT(self->unpickler); |
| 7019 | return 0; |
| 7020 | } |
| 7021 | |
| 7022 | static int |
| 7023 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 7024 | { |
| 7025 | Py_CLEAR(self->unpickler); |
| 7026 | return 0; |
| 7027 | } |
| 7028 | |
| 7029 | static PyTypeObject UnpicklerMemoProxyType = { |
| 7030 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7031 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 7032 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 7033 | 0, |
| 7034 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 7035 | 0, /* tp_print */ |
| 7036 | 0, /* tp_getattr */ |
| 7037 | 0, /* tp_setattr */ |
| 7038 | 0, /* tp_compare */ |
| 7039 | 0, /* tp_repr */ |
| 7040 | 0, /* tp_as_number */ |
| 7041 | 0, /* tp_as_sequence */ |
| 7042 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 7043 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7044 | 0, /* tp_call */ |
| 7045 | 0, /* tp_str */ |
| 7046 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7047 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 7048 | 0, /* tp_as_buffer */ |
| 7049 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 7050 | 0, /* tp_doc */ |
| 7051 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 7052 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 7053 | 0, /* tp_richcompare */ |
| 7054 | 0, /* tp_weaklistoffset */ |
| 7055 | 0, /* tp_iter */ |
| 7056 | 0, /* tp_iternext */ |
| 7057 | unpicklerproxy_methods, /* tp_methods */ |
| 7058 | }; |
| 7059 | |
| 7060 | static PyObject * |
| 7061 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 7062 | { |
| 7063 | UnpicklerMemoProxyObject *self; |
| 7064 | |
| 7065 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 7066 | &UnpicklerMemoProxyType); |
| 7067 | if (self == NULL) |
| 7068 | return NULL; |
| 7069 | Py_INCREF(unpickler); |
| 7070 | self->unpickler = unpickler; |
| 7071 | PyObject_GC_Track(self); |
| 7072 | return (PyObject *)self; |
| 7073 | } |
| 7074 | |
| 7075 | /*****************************************************************************/ |
| 7076 | |
| 7077 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7078 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7079 | Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7080 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7081 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7082 | } |
| 7083 | |
| 7084 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7085 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7086 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7087 | PyObject **new_memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7088 | size_t new_memo_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7089 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7090 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7091 | PyErr_SetString(PyExc_TypeError, |
| 7092 | "attribute deletion is not supported"); |
| 7093 | return -1; |
| 7094 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7095 | |
| 7096 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 7097 | UnpicklerObject *unpickler = |
| 7098 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 7099 | |
| 7100 | new_memo_size = unpickler->memo_size; |
| 7101 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7102 | if (new_memo == NULL) |
| 7103 | return -1; |
| 7104 | |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7105 | for (size_t i = 0; i < new_memo_size; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7106 | Py_XINCREF(unpickler->memo[i]); |
| 7107 | new_memo[i] = unpickler->memo[i]; |
| 7108 | } |
| 7109 | } |
| 7110 | else if (PyDict_Check(obj)) { |
| 7111 | Py_ssize_t i = 0; |
| 7112 | PyObject *key, *value; |
| 7113 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 7114 | new_memo_size = PyDict_GET_SIZE(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7115 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7116 | if (new_memo == NULL) |
| 7117 | return -1; |
| 7118 | |
| 7119 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 7120 | Py_ssize_t idx; |
| 7121 | if (!PyLong_Check(key)) { |
| 7122 | PyErr_SetString(PyExc_TypeError, |
| 7123 | "memo key must be integers"); |
| 7124 | goto error; |
| 7125 | } |
| 7126 | idx = PyLong_AsSsize_t(key); |
| 7127 | if (idx == -1 && PyErr_Occurred()) |
| 7128 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7129 | if (idx < 0) { |
| 7130 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 7131 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7132 | goto error; |
| 7133 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7134 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 7135 | goto error; |
| 7136 | } |
| 7137 | } |
| 7138 | else { |
| 7139 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 7140 | "'memo' attribute must be an UnpicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7141 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7142 | return -1; |
| 7143 | } |
| 7144 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7145 | _Unpickler_MemoCleanup(self); |
| 7146 | self->memo_size = new_memo_size; |
| 7147 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7148 | |
| 7149 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7150 | |
| 7151 | error: |
| 7152 | if (new_memo_size) { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7153 | for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7154 | Py_XDECREF(new_memo[i]); |
| 7155 | } |
| 7156 | PyMem_FREE(new_memo); |
| 7157 | } |
| 7158 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7159 | } |
| 7160 | |
| 7161 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7162 | Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7163 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7164 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7165 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7166 | return NULL; |
| 7167 | } |
| 7168 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
| 7171 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7172 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7173 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7174 | if (value == NULL) { |
| 7175 | PyErr_SetString(PyExc_TypeError, |
| 7176 | "attribute deletion is not supported"); |
| 7177 | return -1; |
| 7178 | } |
| 7179 | if (!PyCallable_Check(value)) { |
| 7180 | PyErr_SetString(PyExc_TypeError, |
| 7181 | "persistent_load must be a callable taking " |
| 7182 | "one argument"); |
| 7183 | return -1; |
| 7184 | } |
| 7185 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7186 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7187 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 7188 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7189 | |
| 7190 | return 0; |
| 7191 | } |
| 7192 | |
| 7193 | static PyGetSetDef Unpickler_getsets[] = { |
| 7194 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 7195 | {"persistent_load", (getter)Unpickler_get_persload, |
| 7196 | (setter)Unpickler_set_persload}, |
| 7197 | {NULL} |
| 7198 | }; |
| 7199 | |
| 7200 | static PyTypeObject Unpickler_Type = { |
| 7201 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7202 | "_pickle.Unpickler", /*tp_name*/ |
| 7203 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 7204 | 0, /*tp_itemsize*/ |
| 7205 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 7206 | 0, /*tp_print*/ |
| 7207 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7208 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7209 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7210 | 0, /*tp_repr*/ |
| 7211 | 0, /*tp_as_number*/ |
| 7212 | 0, /*tp_as_sequence*/ |
| 7213 | 0, /*tp_as_mapping*/ |
| 7214 | 0, /*tp_hash*/ |
| 7215 | 0, /*tp_call*/ |
| 7216 | 0, /*tp_str*/ |
| 7217 | 0, /*tp_getattro*/ |
| 7218 | 0, /*tp_setattro*/ |
| 7219 | 0, /*tp_as_buffer*/ |
| 7220 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7221 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7222 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7223 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7224 | 0, /*tp_richcompare*/ |
| 7225 | 0, /*tp_weaklistoffset*/ |
| 7226 | 0, /*tp_iter*/ |
| 7227 | 0, /*tp_iternext*/ |
| 7228 | Unpickler_methods, /*tp_methods*/ |
| 7229 | 0, /*tp_members*/ |
| 7230 | Unpickler_getsets, /*tp_getset*/ |
| 7231 | 0, /*tp_base*/ |
| 7232 | 0, /*tp_dict*/ |
| 7233 | 0, /*tp_descr_get*/ |
| 7234 | 0, /*tp_descr_set*/ |
| 7235 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7236 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7237 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7238 | PyType_GenericNew, /*tp_new*/ |
| 7239 | PyObject_GC_Del, /*tp_free*/ |
| 7240 | 0, /*tp_is_gc*/ |
| 7241 | }; |
| 7242 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7243 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7244 | |
| 7245 | _pickle.dump |
| 7246 | |
| 7247 | obj: object |
| 7248 | file: object |
| 7249 | protocol: object = NULL |
| 7250 | * |
| 7251 | fix_imports: bool = True |
| 7252 | |
| 7253 | Write a pickled representation of obj to the open file object file. |
| 7254 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7255 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7256 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7257 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7258 | The optional *protocol* argument tells the pickler to use the given |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7259 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7260 | protocol is 4. It was introduced in Python 3.4, it is incompatible |
| 7261 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7262 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7263 | Specifying a negative protocol version selects the highest protocol |
| 7264 | version supported. The higher the protocol used, the more recent the |
| 7265 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7266 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7267 | The *file* argument must have a write() method that accepts a single |
| 7268 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7269 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7270 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7271 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7272 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7273 | to map the new Python 3 names to the old module names used in Python |
| 7274 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7275 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7276 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7277 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7278 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7279 | PyObject *protocol, int fix_imports) |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7280 | /*[clinic end generated code: output=a4774d5fde7d34de input=93f1408489a87472]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7281 | { |
| 7282 | PicklerObject *pickler = _Pickler_New(); |
| 7283 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7284 | if (pickler == NULL) |
| 7285 | return NULL; |
| 7286 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7287 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7288 | goto error; |
| 7289 | |
| 7290 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7291 | goto error; |
| 7292 | |
| 7293 | if (dump(pickler, obj) < 0) |
| 7294 | goto error; |
| 7295 | |
| 7296 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7297 | goto error; |
| 7298 | |
| 7299 | Py_DECREF(pickler); |
| 7300 | Py_RETURN_NONE; |
| 7301 | |
| 7302 | error: |
| 7303 | Py_XDECREF(pickler); |
| 7304 | return NULL; |
| 7305 | } |
| 7306 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7307 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7308 | |
| 7309 | _pickle.dumps |
| 7310 | |
| 7311 | obj: object |
| 7312 | protocol: object = NULL |
| 7313 | * |
| 7314 | fix_imports: bool = True |
| 7315 | |
| 7316 | Return the pickled representation of the object as a bytes object. |
| 7317 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7318 | The optional *protocol* argument tells the pickler to use the given |
| 7319 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7320 | protocol is 4. It was introduced in Python 3.4, it is incompatible |
| 7321 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7322 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7323 | Specifying a negative protocol version selects the highest protocol |
| 7324 | version supported. The higher the protocol used, the more recent the |
| 7325 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7326 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7327 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7328 | try to map the new Python 3 names to the old module names used in |
| 7329 | 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] | 7330 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7331 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7332 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7333 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7334 | int fix_imports) |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7335 | /*[clinic end generated code: output=d75d5cda456fd261 input=b6efb45a7d19b5ab]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7336 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7337 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7338 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7339 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7340 | if (pickler == NULL) |
| 7341 | return NULL; |
| 7342 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7343 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7344 | goto error; |
| 7345 | |
| 7346 | if (dump(pickler, obj) < 0) |
| 7347 | goto error; |
| 7348 | |
| 7349 | result = _Pickler_GetString(pickler); |
| 7350 | Py_DECREF(pickler); |
| 7351 | return result; |
| 7352 | |
| 7353 | error: |
| 7354 | Py_XDECREF(pickler); |
| 7355 | return NULL; |
| 7356 | } |
| 7357 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7358 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7359 | |
| 7360 | _pickle.load |
| 7361 | |
| 7362 | file: object |
| 7363 | * |
| 7364 | fix_imports: bool = True |
| 7365 | encoding: str = 'ASCII' |
| 7366 | errors: str = 'strict' |
| 7367 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7368 | 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] | 7369 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7370 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7371 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7372 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7373 | The protocol version of the pickle is detected automatically, so no |
| 7374 | protocol argument is needed. Bytes past the pickled object's |
| 7375 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7376 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7377 | The argument *file* must have two methods, a read() method that takes |
| 7378 | an integer argument, and a readline() method that requires no |
| 7379 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7380 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7381 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7382 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7383 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7384 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7385 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7386 | map the old Python 2 names to the new names used in Python 3. The |
| 7387 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7388 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7389 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7390 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7391 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7392 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7393 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7394 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7395 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7396 | /*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7397 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7398 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7399 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7400 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7401 | if (unpickler == NULL) |
| 7402 | return NULL; |
| 7403 | |
| 7404 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7405 | goto error; |
| 7406 | |
| 7407 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7408 | goto error; |
| 7409 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7410 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7411 | |
| 7412 | result = load(unpickler); |
| 7413 | Py_DECREF(unpickler); |
| 7414 | return result; |
| 7415 | |
| 7416 | error: |
| 7417 | Py_XDECREF(unpickler); |
| 7418 | return NULL; |
| 7419 | } |
| 7420 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7421 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7422 | |
| 7423 | _pickle.loads |
| 7424 | |
| 7425 | data: object |
| 7426 | * |
| 7427 | fix_imports: bool = True |
| 7428 | encoding: str = 'ASCII' |
| 7429 | errors: str = 'strict' |
| 7430 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7431 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7432 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7433 | The protocol version of the pickle is detected automatically, so no |
| 7434 | protocol argument is needed. Bytes past the pickled object's |
| 7435 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7436 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7437 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7438 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7439 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7440 | map the old Python 2 names to the new names used in Python 3. The |
| 7441 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7442 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7443 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7444 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7445 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7446 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7447 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7448 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7449 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7450 | /*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7451 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7452 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7453 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7454 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7455 | if (unpickler == NULL) |
| 7456 | return NULL; |
| 7457 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7458 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7459 | goto error; |
| 7460 | |
| 7461 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7462 | goto error; |
| 7463 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7464 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7465 | |
| 7466 | result = load(unpickler); |
| 7467 | Py_DECREF(unpickler); |
| 7468 | return result; |
| 7469 | |
| 7470 | error: |
| 7471 | Py_XDECREF(unpickler); |
| 7472 | return NULL; |
| 7473 | } |
| 7474 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7475 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7476 | _PICKLE_DUMP_METHODDEF |
| 7477 | _PICKLE_DUMPS_METHODDEF |
| 7478 | _PICKLE_LOAD_METHODDEF |
| 7479 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7480 | {NULL, NULL} /* sentinel */ |
| 7481 | }; |
| 7482 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7483 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7484 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7485 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7486 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7487 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7488 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7489 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7490 | static void |
| 7491 | pickle_free(PyObject *m) |
| 7492 | { |
| 7493 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7494 | } |
| 7495 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7496 | static int |
| 7497 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7498 | { |
| 7499 | PickleState *st = _Pickle_GetState(m); |
| 7500 | Py_VISIT(st->PickleError); |
| 7501 | Py_VISIT(st->PicklingError); |
| 7502 | Py_VISIT(st->UnpicklingError); |
| 7503 | Py_VISIT(st->dispatch_table); |
| 7504 | Py_VISIT(st->extension_registry); |
| 7505 | Py_VISIT(st->extension_cache); |
| 7506 | Py_VISIT(st->inverted_registry); |
| 7507 | Py_VISIT(st->name_mapping_2to3); |
| 7508 | Py_VISIT(st->import_mapping_2to3); |
| 7509 | Py_VISIT(st->name_mapping_3to2); |
| 7510 | Py_VISIT(st->import_mapping_3to2); |
| 7511 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7512 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7513 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7514 | } |
| 7515 | |
| 7516 | static struct PyModuleDef _picklemodule = { |
| 7517 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7518 | "_pickle", /* m_name */ |
| 7519 | pickle_module_doc, /* m_doc */ |
| 7520 | sizeof(PickleState), /* m_size */ |
| 7521 | pickle_methods, /* m_methods */ |
| 7522 | NULL, /* m_reload */ |
| 7523 | pickle_traverse, /* m_traverse */ |
| 7524 | pickle_clear, /* m_clear */ |
| 7525 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7526 | }; |
| 7527 | |
| 7528 | PyMODINIT_FUNC |
| 7529 | PyInit__pickle(void) |
| 7530 | { |
| 7531 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7532 | PickleState *st; |
| 7533 | |
| 7534 | m = PyState_FindModule(&_picklemodule); |
| 7535 | if (m) { |
| 7536 | Py_INCREF(m); |
| 7537 | return m; |
| 7538 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7539 | |
| 7540 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7541 | return NULL; |
| 7542 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7543 | return NULL; |
| 7544 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7545 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7546 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7547 | return NULL; |
| 7548 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7549 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7550 | |
| 7551 | /* Create the module and add the functions. */ |
| 7552 | m = PyModule_Create(&_picklemodule); |
| 7553 | if (m == NULL) |
| 7554 | return NULL; |
| 7555 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7556 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7557 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7558 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7559 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7560 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7561 | return NULL; |
| 7562 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7563 | st = _Pickle_GetState(m); |
| 7564 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7565 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7566 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7567 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7568 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7569 | st->PicklingError = \ |
| 7570 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7571 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7572 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7573 | st->UnpicklingError = \ |
| 7574 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7575 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7576 | return NULL; |
| 7577 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7578 | Py_INCREF(st->PickleError); |
| 7579 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7580 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7581 | Py_INCREF(st->PicklingError); |
| 7582 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7583 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7584 | Py_INCREF(st->UnpicklingError); |
| 7585 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7586 | return NULL; |
| 7587 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7588 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7589 | return NULL; |
| 7590 | |
| 7591 | return m; |
| 7592 | } |