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 | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 30 | HIGHEST_PROTOCOL = 5, |
Ł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', |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 107 | FRAME = '\x95', |
| 108 | |
| 109 | /* Protocol 5 */ |
| 110 | BYTEARRAY8 = '\x96', |
| 111 | NEXT_BUFFER = '\x97', |
| 112 | READONLY_BUFFER = '\x98' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 113 | }; |
| 114 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 115 | enum { |
| 116 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 117 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 118 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 119 | help anything either. */ |
| 120 | BATCHSIZE = 1000, |
| 121 | |
| 122 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 123 | checking for self-referential data-structures. */ |
| 124 | FAST_NESTING_LIMIT = 50, |
| 125 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 126 | /* Initial size of the write buffer of Pickler. */ |
| 127 | WRITE_BUF_SIZE = 4096, |
| 128 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 129 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 130 | PREFETCH = 8192 * 16, |
| 131 | |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 132 | FRAME_SIZE_MIN = 4, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 133 | FRAME_SIZE_TARGET = 64 * 1024, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 134 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 137 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 138 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 139 | /* State of the pickle module, per PEP 3121. */ |
| 140 | typedef struct { |
| 141 | /* Exception classes for pickle. */ |
| 142 | PyObject *PickleError; |
| 143 | PyObject *PicklingError; |
| 144 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 145 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 146 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 147 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 148 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 149 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 150 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 151 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 152 | PyObject *extension_registry; |
| 153 | /* copyreg._extension_cache, {code: object} */ |
| 154 | PyObject *extension_cache; |
| 155 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 156 | PyObject *inverted_registry; |
| 157 | |
| 158 | /* Import mappings for compatibility with Python 2.x */ |
| 159 | |
| 160 | /* _compat_pickle.NAME_MAPPING, |
| 161 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 162 | PyObject *name_mapping_2to3; |
| 163 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 164 | PyObject *import_mapping_2to3; |
| 165 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 166 | PyObject *name_mapping_3to2; |
| 167 | PyObject *import_mapping_3to2; |
| 168 | |
| 169 | /* codecs.encode, used for saving bytes in older protocols */ |
| 170 | PyObject *codecs_encode; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 171 | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
| 172 | PyObject *getattr; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 173 | /* functools.partial, used for implementing __newobj_ex__ with protocols |
| 174 | 2 and 3 */ |
| 175 | PyObject *partial; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 176 | } PickleState; |
| 177 | |
| 178 | /* Forward declaration of the _pickle module definition. */ |
| 179 | static struct PyModuleDef _picklemodule; |
| 180 | |
| 181 | /* Given a module object, get its per-module state. */ |
| 182 | static PickleState * |
| 183 | _Pickle_GetState(PyObject *module) |
| 184 | { |
| 185 | return (PickleState *)PyModule_GetState(module); |
| 186 | } |
| 187 | |
| 188 | /* Find the module instance imported in the currently running sub-interpreter |
| 189 | and get its state. */ |
| 190 | static PickleState * |
| 191 | _Pickle_GetGlobalState(void) |
| 192 | { |
| 193 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 194 | } |
| 195 | |
| 196 | /* Clear the given pickle module state. */ |
| 197 | static void |
| 198 | _Pickle_ClearState(PickleState *st) |
| 199 | { |
| 200 | Py_CLEAR(st->PickleError); |
| 201 | Py_CLEAR(st->PicklingError); |
| 202 | Py_CLEAR(st->UnpicklingError); |
| 203 | Py_CLEAR(st->dispatch_table); |
| 204 | Py_CLEAR(st->extension_registry); |
| 205 | Py_CLEAR(st->extension_cache); |
| 206 | Py_CLEAR(st->inverted_registry); |
| 207 | Py_CLEAR(st->name_mapping_2to3); |
| 208 | Py_CLEAR(st->import_mapping_2to3); |
| 209 | Py_CLEAR(st->name_mapping_3to2); |
| 210 | Py_CLEAR(st->import_mapping_3to2); |
| 211 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 212 | Py_CLEAR(st->getattr); |
Victor Stinner | 9ba97df | 2015-11-17 12:15:07 +0100 | [diff] [blame] | 213 | Py_CLEAR(st->partial); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | /* Initialize the given pickle module state. */ |
| 217 | static int |
| 218 | _Pickle_InitState(PickleState *st) |
| 219 | { |
| 220 | PyObject *copyreg = NULL; |
| 221 | PyObject *compat_pickle = NULL; |
| 222 | PyObject *codecs = NULL; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 223 | PyObject *functools = NULL; |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 224 | _Py_IDENTIFIER(getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 225 | |
Serhiy Storchaka | bb86bf4 | 2018-12-11 08:28:18 +0200 | [diff] [blame] | 226 | st->getattr = _PyEval_GetBuiltinId(&PyId_getattr); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 227 | if (st->getattr == NULL) |
| 228 | goto error; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 229 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 230 | copyreg = PyImport_ImportModule("copyreg"); |
| 231 | if (!copyreg) |
| 232 | goto error; |
| 233 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 234 | if (!st->dispatch_table) |
| 235 | goto error; |
| 236 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 237 | PyErr_Format(PyExc_RuntimeError, |
| 238 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 239 | Py_TYPE(st->dispatch_table)->tp_name); |
| 240 | goto error; |
| 241 | } |
| 242 | st->extension_registry = \ |
| 243 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 244 | if (!st->extension_registry) |
| 245 | goto error; |
| 246 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 247 | PyErr_Format(PyExc_RuntimeError, |
| 248 | "copyreg._extension_registry should be a dict, " |
| 249 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 250 | goto error; |
| 251 | } |
| 252 | st->inverted_registry = \ |
| 253 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 254 | if (!st->inverted_registry) |
| 255 | goto error; |
| 256 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 257 | PyErr_Format(PyExc_RuntimeError, |
| 258 | "copyreg._inverted_registry should be a dict, " |
| 259 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 260 | goto error; |
| 261 | } |
| 262 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 263 | if (!st->extension_cache) |
| 264 | goto error; |
| 265 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 266 | PyErr_Format(PyExc_RuntimeError, |
| 267 | "copyreg._extension_cache should be a dict, " |
| 268 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 269 | goto error; |
| 270 | } |
| 271 | Py_CLEAR(copyreg); |
| 272 | |
| 273 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 274 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 275 | if (!compat_pickle) |
| 276 | goto error; |
| 277 | st->name_mapping_2to3 = \ |
| 278 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 279 | if (!st->name_mapping_2to3) |
| 280 | goto error; |
| 281 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 282 | PyErr_Format(PyExc_RuntimeError, |
| 283 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 284 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 285 | goto error; |
| 286 | } |
| 287 | st->import_mapping_2to3 = \ |
| 288 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 289 | if (!st->import_mapping_2to3) |
| 290 | goto error; |
| 291 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 292 | PyErr_Format(PyExc_RuntimeError, |
| 293 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 294 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 295 | goto error; |
| 296 | } |
| 297 | /* ... and the 3.x -> 2.x mapping tables */ |
| 298 | st->name_mapping_3to2 = \ |
| 299 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 300 | if (!st->name_mapping_3to2) |
| 301 | goto error; |
| 302 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 303 | PyErr_Format(PyExc_RuntimeError, |
| 304 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 305 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 306 | goto error; |
| 307 | } |
| 308 | st->import_mapping_3to2 = \ |
| 309 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 310 | if (!st->import_mapping_3to2) |
| 311 | goto error; |
| 312 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 313 | PyErr_Format(PyExc_RuntimeError, |
| 314 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 315 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 316 | goto error; |
| 317 | } |
| 318 | Py_CLEAR(compat_pickle); |
| 319 | |
| 320 | codecs = PyImport_ImportModule("codecs"); |
| 321 | if (codecs == NULL) |
| 322 | goto error; |
| 323 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 324 | if (st->codecs_encode == NULL) { |
| 325 | goto error; |
| 326 | } |
| 327 | if (!PyCallable_Check(st->codecs_encode)) { |
| 328 | PyErr_Format(PyExc_RuntimeError, |
| 329 | "codecs.encode should be a callable, not %.200s", |
| 330 | Py_TYPE(st->codecs_encode)->tp_name); |
| 331 | goto error; |
| 332 | } |
| 333 | Py_CLEAR(codecs); |
| 334 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 335 | functools = PyImport_ImportModule("functools"); |
| 336 | if (!functools) |
| 337 | goto error; |
| 338 | st->partial = PyObject_GetAttrString(functools, "partial"); |
| 339 | if (!st->partial) |
| 340 | goto error; |
| 341 | Py_CLEAR(functools); |
| 342 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 343 | return 0; |
| 344 | |
| 345 | error: |
| 346 | Py_CLEAR(copyreg); |
| 347 | Py_CLEAR(compat_pickle); |
| 348 | Py_CLEAR(codecs); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 349 | Py_CLEAR(functools); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 350 | _Pickle_ClearState(st); |
| 351 | return -1; |
| 352 | } |
| 353 | |
| 354 | /* Helper for calling a function with a single argument quickly. |
| 355 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 356 | This function steals the reference of the given argument. */ |
| 357 | static PyObject * |
| 358 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 359 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 360 | PyObject *result; |
| 361 | |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 362 | result = _PyObject_CallOneArg(func, obj); |
Victor Stinner | 7521069 | 2016-08-19 18:59:15 +0200 | [diff] [blame] | 363 | Py_DECREF(obj); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 364 | return result; |
| 365 | } |
| 366 | |
| 367 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 368 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 369 | /* Retrieve and deconstruct a method for avoiding a reference cycle |
| 370 | (pickler -> bound method of pickler -> pickler) */ |
| 371 | static int |
| 372 | init_method_ref(PyObject *self, _Py_Identifier *name, |
| 373 | PyObject **method_func, PyObject **method_self) |
| 374 | { |
| 375 | PyObject *func, *func2; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 376 | int ret; |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 377 | |
| 378 | /* *method_func and *method_self should be consistent. All refcount decrements |
| 379 | should be occurred after setting *method_self and *method_func. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 380 | ret = _PyObject_LookupAttrId(self, name, &func); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 381 | if (func == NULL) { |
| 382 | *method_self = NULL; |
| 383 | Py_CLEAR(*method_func); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 384 | return ret; |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) { |
| 388 | /* Deconstruct a bound Python method */ |
| 389 | func2 = PyMethod_GET_FUNCTION(func); |
| 390 | Py_INCREF(func2); |
| 391 | *method_self = self; /* borrowed */ |
| 392 | Py_XSETREF(*method_func, func2); |
| 393 | Py_DECREF(func); |
| 394 | return 0; |
| 395 | } |
| 396 | else { |
| 397 | *method_self = NULL; |
| 398 | Py_XSETREF(*method_func, func); |
| 399 | return 0; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | /* Bind a method if it was deconstructed */ |
| 404 | static PyObject * |
| 405 | reconstruct_method(PyObject *func, PyObject *self) |
| 406 | { |
| 407 | if (self) { |
| 408 | return PyMethod_New(func, self); |
| 409 | } |
| 410 | else { |
| 411 | Py_INCREF(func); |
| 412 | return func; |
| 413 | } |
| 414 | } |
| 415 | |
| 416 | static PyObject * |
| 417 | call_method(PyObject *func, PyObject *self, PyObject *obj) |
| 418 | { |
| 419 | if (self) { |
| 420 | return PyObject_CallFunctionObjArgs(func, self, obj, NULL); |
| 421 | } |
| 422 | else { |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 423 | return _PyObject_CallOneArg(func, obj); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 424 | } |
| 425 | } |
| 426 | |
| 427 | /*************************************************************************/ |
| 428 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 429 | /* Internal data type used as the unpickling stack. */ |
| 430 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 431 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 432 | PyObject **data; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 433 | int mark_set; /* is MARK set? */ |
| 434 | Py_ssize_t fence; /* position of top MARK or 0 */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 435 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 436 | } Pdata; |
| 437 | |
| 438 | static void |
| 439 | Pdata_dealloc(Pdata *self) |
| 440 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 441 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 442 | while (--i >= 0) { |
| 443 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 444 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 445 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 446 | PyObject_Del(self); |
| 447 | } |
| 448 | |
| 449 | static PyTypeObject Pdata_Type = { |
| 450 | PyVarObject_HEAD_INIT(NULL, 0) |
| 451 | "_pickle.Pdata", /*tp_name*/ |
| 452 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 453 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 454 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 455 | }; |
| 456 | |
| 457 | static PyObject * |
| 458 | Pdata_New(void) |
| 459 | { |
| 460 | Pdata *self; |
| 461 | |
| 462 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 463 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 464 | Py_SIZE(self) = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 465 | self->mark_set = 0; |
| 466 | self->fence = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 467 | self->allocated = 8; |
| 468 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 469 | if (self->data) |
| 470 | return (PyObject *)self; |
| 471 | Py_DECREF(self); |
| 472 | return PyErr_NoMemory(); |
| 473 | } |
| 474 | |
| 475 | |
| 476 | /* Retain only the initial clearto items. If clearto >= the current |
| 477 | * number of items, this is a (non-erroneous) NOP. |
| 478 | */ |
| 479 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 480 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 481 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 482 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 483 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 484 | assert(clearto >= self->fence); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 485 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 486 | return 0; |
| 487 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 488 | while (--i >= clearto) { |
| 489 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 490 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 491 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | static int |
| 496 | Pdata_grow(Pdata *self) |
| 497 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 498 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 499 | size_t allocated = (size_t)self->allocated; |
| 500 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 501 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 502 | new_allocated = (allocated >> 3) + 6; |
| 503 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 504 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 505 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 506 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 507 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 508 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 509 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 510 | |
| 511 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 512 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 513 | return 0; |
| 514 | |
| 515 | nomemory: |
| 516 | PyErr_NoMemory(); |
| 517 | return -1; |
| 518 | } |
| 519 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 520 | static int |
| 521 | Pdata_stack_underflow(Pdata *self) |
| 522 | { |
| 523 | PickleState *st = _Pickle_GetGlobalState(); |
| 524 | PyErr_SetString(st->UnpicklingError, |
| 525 | self->mark_set ? |
| 526 | "unexpected MARK found" : |
| 527 | "unpickling stack underflow"); |
| 528 | return -1; |
| 529 | } |
| 530 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 531 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 532 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 533 | * is raised and V is set to NULL. |
| 534 | */ |
| 535 | static PyObject * |
| 536 | Pdata_pop(Pdata *self) |
| 537 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 538 | if (Py_SIZE(self) <= self->fence) { |
| 539 | Pdata_stack_underflow(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 540 | return NULL; |
| 541 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 542 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 543 | } |
| 544 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 545 | |
| 546 | static int |
| 547 | Pdata_push(Pdata *self, PyObject *obj) |
| 548 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 549 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 550 | return -1; |
| 551 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 552 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 553 | return 0; |
| 554 | } |
| 555 | |
| 556 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 557 | #define PDATA_PUSH(D, O, ER) do { \ |
| 558 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 559 | |
| 560 | /* Push an object on stack, adding a new reference to the object. */ |
| 561 | #define PDATA_APPEND(D, O, ER) do { \ |
| 562 | Py_INCREF((O)); \ |
| 563 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 564 | |
| 565 | static PyObject * |
| 566 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 567 | { |
| 568 | PyObject *tuple; |
| 569 | Py_ssize_t len, i, j; |
| 570 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 571 | if (start < self->fence) { |
| 572 | Pdata_stack_underflow(self); |
| 573 | return NULL; |
| 574 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 575 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 576 | tuple = PyTuple_New(len); |
| 577 | if (tuple == NULL) |
| 578 | return NULL; |
| 579 | for (i = start, j = 0; j < len; i++, j++) |
| 580 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 581 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 582 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 583 | return tuple; |
| 584 | } |
| 585 | |
| 586 | static PyObject * |
| 587 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 588 | { |
| 589 | PyObject *list; |
| 590 | Py_ssize_t len, i, j; |
| 591 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 592 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 593 | list = PyList_New(len); |
| 594 | if (list == NULL) |
| 595 | return NULL; |
| 596 | for (i = start, j = 0; j < len; i++, j++) |
| 597 | PyList_SET_ITEM(list, j, self->data[i]); |
| 598 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 599 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 600 | return list; |
| 601 | } |
| 602 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 603 | typedef struct { |
| 604 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 605 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 606 | } PyMemoEntry; |
| 607 | |
| 608 | typedef struct { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 609 | size_t mt_mask; |
| 610 | size_t mt_used; |
| 611 | size_t mt_allocated; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 612 | PyMemoEntry *mt_table; |
| 613 | } PyMemoTable; |
| 614 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 615 | typedef struct PicklerObject { |
| 616 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 617 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 618 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 619 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 620 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 621 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 622 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 623 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 624 | PyObject *reducer_override; /* hook for invoking user-defined callbacks |
| 625 | instead of save_global when pickling |
| 626 | functions and classes*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 627 | |
| 628 | PyObject *write; /* write() method of the output stream. */ |
| 629 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 630 | flushing to the stream. */ |
| 631 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 632 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 633 | int proto; /* Pickle protocol number, >= 0 */ |
| 634 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 635 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 636 | Py_ssize_t frame_start; /* Position in output_buffer where the |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 637 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 638 | is no frame currently open. */ |
| 639 | |
| 640 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 641 | int fast; /* Enable fast mode if set to a true value. |
| 642 | The fast mode disable the usage of memo, |
| 643 | therefore speeding the pickling process by |
| 644 | not generating superfluous PUT opcodes. It |
| 645 | should not be used if with self-referential |
| 646 | objects. */ |
| 647 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 648 | int fix_imports; /* Indicate whether Pickler should fix |
| 649 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 650 | PyObject *fast_memo; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 651 | PyObject *buffer_callback; /* Callback for out-of-band buffers, or NULL */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 652 | } PicklerObject; |
| 653 | |
| 654 | typedef struct UnpicklerObject { |
| 655 | PyObject_HEAD |
| 656 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 657 | |
| 658 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 659 | is unnecessary, since the keys are contiguous ints. */ |
| 660 | PyObject **memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 661 | size_t memo_size; /* Capacity of the memo array */ |
| 662 | size_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 663 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 664 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 665 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 666 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 667 | |
| 668 | Py_buffer buffer; |
| 669 | char *input_buffer; |
| 670 | char *input_line; |
| 671 | Py_ssize_t input_len; |
| 672 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 673 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 674 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 675 | PyObject *read; /* read() method of the input stream. */ |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 676 | PyObject *readinto; /* readinto() method of the input stream. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 677 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 678 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 679 | PyObject *buffers; /* iterable of out-of-band buffers, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 680 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 681 | char *encoding; /* Name of the encoding to be used for |
| 682 | decoding strings pickled using Python |
| 683 | 2.x. The default value is "ASCII" */ |
| 684 | char *errors; /* Name of errors handling scheme to used when |
| 685 | decoding strings. The default value is |
| 686 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 687 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 688 | objects. */ |
| 689 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 690 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 691 | int proto; /* Protocol of the pickle loaded. */ |
| 692 | int fix_imports; /* Indicate whether Unpickler should fix |
| 693 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 694 | } UnpicklerObject; |
| 695 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 696 | typedef struct { |
| 697 | PyObject_HEAD |
| 698 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 699 | } PicklerMemoProxyObject; |
| 700 | |
| 701 | typedef struct { |
| 702 | PyObject_HEAD |
| 703 | UnpicklerObject *unpickler; |
| 704 | } UnpicklerMemoProxyObject; |
| 705 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 706 | /* Forward declarations */ |
| 707 | static int save(PicklerObject *, PyObject *, int); |
| 708 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 709 | static PyTypeObject Pickler_Type; |
| 710 | static PyTypeObject Unpickler_Type; |
| 711 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 712 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 713 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 714 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 715 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 716 | 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] | 717 | a bunch of unnecessary object creation. This makes a huge performance |
| 718 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 719 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 720 | #define MT_MINSIZE 8 |
| 721 | #define PERTURB_SHIFT 5 |
| 722 | |
| 723 | |
| 724 | static PyMemoTable * |
| 725 | PyMemoTable_New(void) |
| 726 | { |
| 727 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 728 | if (memo == NULL) { |
| 729 | PyErr_NoMemory(); |
| 730 | return NULL; |
| 731 | } |
| 732 | |
| 733 | memo->mt_used = 0; |
| 734 | memo->mt_allocated = MT_MINSIZE; |
| 735 | memo->mt_mask = MT_MINSIZE - 1; |
| 736 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 737 | if (memo->mt_table == NULL) { |
| 738 | PyMem_FREE(memo); |
| 739 | PyErr_NoMemory(); |
| 740 | return NULL; |
| 741 | } |
| 742 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 743 | |
| 744 | return memo; |
| 745 | } |
| 746 | |
| 747 | static PyMemoTable * |
| 748 | PyMemoTable_Copy(PyMemoTable *self) |
| 749 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 750 | PyMemoTable *new = PyMemoTable_New(); |
| 751 | if (new == NULL) |
| 752 | return NULL; |
| 753 | |
| 754 | new->mt_used = self->mt_used; |
| 755 | new->mt_allocated = self->mt_allocated; |
| 756 | new->mt_mask = self->mt_mask; |
| 757 | /* The table we get from _New() is probably smaller than we wanted. |
| 758 | Free it and allocate one that's the right size. */ |
| 759 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 760 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 761 | if (new->mt_table == NULL) { |
| 762 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 763 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 764 | return NULL; |
| 765 | } |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 766 | for (size_t i = 0; i < self->mt_allocated; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 767 | Py_XINCREF(self->mt_table[i].me_key); |
| 768 | } |
| 769 | memcpy(new->mt_table, self->mt_table, |
| 770 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 771 | |
| 772 | return new; |
| 773 | } |
| 774 | |
| 775 | static Py_ssize_t |
| 776 | PyMemoTable_Size(PyMemoTable *self) |
| 777 | { |
| 778 | return self->mt_used; |
| 779 | } |
| 780 | |
| 781 | static int |
| 782 | PyMemoTable_Clear(PyMemoTable *self) |
| 783 | { |
| 784 | Py_ssize_t i = self->mt_allocated; |
| 785 | |
| 786 | while (--i >= 0) { |
| 787 | Py_XDECREF(self->mt_table[i].me_key); |
| 788 | } |
| 789 | self->mt_used = 0; |
| 790 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | static void |
| 795 | PyMemoTable_Del(PyMemoTable *self) |
| 796 | { |
| 797 | if (self == NULL) |
| 798 | return; |
| 799 | PyMemoTable_Clear(self); |
| 800 | |
| 801 | PyMem_FREE(self->mt_table); |
| 802 | PyMem_FREE(self); |
| 803 | } |
| 804 | |
| 805 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 806 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 807 | static PyMemoEntry * |
| 808 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 809 | { |
| 810 | size_t i; |
| 811 | size_t perturb; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 812 | size_t mask = self->mt_mask; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 813 | PyMemoEntry *table = self->mt_table; |
| 814 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 815 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 816 | |
| 817 | i = hash & mask; |
| 818 | entry = &table[i]; |
| 819 | if (entry->me_key == NULL || entry->me_key == key) |
| 820 | return entry; |
| 821 | |
| 822 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 823 | i = (i << 2) + i + perturb + 1; |
| 824 | entry = &table[i & mask]; |
| 825 | if (entry->me_key == NULL || entry->me_key == key) |
| 826 | return entry; |
| 827 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 828 | Py_UNREACHABLE(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | /* Returns -1 on failure, 0 on success. */ |
| 832 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 833 | _PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 834 | { |
| 835 | PyMemoEntry *oldtable = NULL; |
| 836 | PyMemoEntry *oldentry, *newentry; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 837 | size_t new_size = MT_MINSIZE; |
| 838 | size_t to_process; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 839 | |
| 840 | assert(min_size > 0); |
| 841 | |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 842 | if (min_size > PY_SSIZE_T_MAX) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 843 | PyErr_NoMemory(); |
| 844 | return -1; |
| 845 | } |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 846 | |
| 847 | /* Find the smallest valid table size >= min_size. */ |
| 848 | while (new_size < min_size) { |
| 849 | new_size <<= 1; |
| 850 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 851 | /* new_size needs to be a power of two. */ |
| 852 | assert((new_size & (new_size - 1)) == 0); |
| 853 | |
| 854 | /* Allocate new table. */ |
| 855 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 856 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 857 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 858 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 859 | PyErr_NoMemory(); |
| 860 | return -1; |
| 861 | } |
| 862 | self->mt_allocated = new_size; |
| 863 | self->mt_mask = new_size - 1; |
| 864 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 865 | |
| 866 | /* Copy entries from the old table. */ |
| 867 | to_process = self->mt_used; |
| 868 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 869 | if (oldentry->me_key != NULL) { |
| 870 | to_process--; |
| 871 | /* newentry is a pointer to a chunk of the new |
| 872 | mt_table, so we're setting the key:value pair |
| 873 | in-place. */ |
| 874 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 875 | newentry->me_key = oldentry->me_key; |
| 876 | newentry->me_value = oldentry->me_value; |
| 877 | } |
| 878 | } |
| 879 | |
| 880 | /* Deallocate the old table. */ |
| 881 | PyMem_FREE(oldtable); |
| 882 | return 0; |
| 883 | } |
| 884 | |
| 885 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 886 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 887 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 888 | { |
| 889 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 890 | if (entry->me_key == NULL) |
| 891 | return NULL; |
| 892 | return &entry->me_value; |
| 893 | } |
| 894 | |
| 895 | /* Returns -1 on failure, 0 on success. */ |
| 896 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 897 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 898 | { |
| 899 | PyMemoEntry *entry; |
| 900 | |
| 901 | assert(key != NULL); |
| 902 | |
| 903 | entry = _PyMemoTable_Lookup(self, key); |
| 904 | if (entry->me_key != NULL) { |
| 905 | entry->me_value = value; |
| 906 | return 0; |
| 907 | } |
| 908 | Py_INCREF(key); |
| 909 | entry->me_key = key; |
| 910 | entry->me_value = value; |
| 911 | self->mt_used++; |
| 912 | |
| 913 | /* If we added a key, we can safely resize. Otherwise just return! |
| 914 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 915 | * |
| 916 | * Quadrupling the size improves average table sparseness |
| 917 | * (reducing collisions) at the cost of some memory. It also halves |
| 918 | * the number of expensive resize operations in a growing memo table. |
| 919 | * |
| 920 | * Very large memo tables (over 50K items) use doubling instead. |
| 921 | * This may help applications with severe memory constraints. |
| 922 | */ |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 923 | 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] | 924 | return 0; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 925 | } |
| 926 | // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow. |
| 927 | size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used; |
| 928 | return _PyMemoTable_ResizeTable(self, desired_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 929 | } |
| 930 | |
| 931 | #undef MT_MINSIZE |
| 932 | #undef PERTURB_SHIFT |
| 933 | |
| 934 | /*************************************************************************/ |
| 935 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 937 | static int |
| 938 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 939 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 940 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 941 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 942 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 943 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 944 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 945 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 946 | return 0; |
| 947 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 948 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 949 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 950 | _write_size64(char *out, size_t value) |
| 951 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 952 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 953 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 954 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 955 | |
| 956 | for (i = 0; i < sizeof(size_t); i++) { |
| 957 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 958 | } |
| 959 | for (i = sizeof(size_t); i < 8; i++) { |
| 960 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 961 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 962 | } |
| 963 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 964 | static int |
| 965 | _Pickler_CommitFrame(PicklerObject *self) |
| 966 | { |
| 967 | size_t frame_len; |
| 968 | char *qdata; |
| 969 | |
| 970 | if (!self->framing || self->frame_start == -1) |
| 971 | return 0; |
| 972 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 973 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 974 | if (frame_len >= FRAME_SIZE_MIN) { |
| 975 | qdata[0] = FRAME; |
| 976 | _write_size64(qdata + 1, frame_len); |
| 977 | } |
| 978 | else { |
| 979 | memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len); |
| 980 | self->output_len -= FRAME_HEADER_SIZE; |
| 981 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 982 | self->frame_start = -1; |
| 983 | return 0; |
| 984 | } |
| 985 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 986 | static PyObject * |
| 987 | _Pickler_GetString(PicklerObject *self) |
| 988 | { |
| 989 | PyObject *output_buffer = self->output_buffer; |
| 990 | |
| 991 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 992 | |
| 993 | if (_Pickler_CommitFrame(self)) |
| 994 | return NULL; |
| 995 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 996 | self->output_buffer = NULL; |
| 997 | /* Resize down to exact size */ |
| 998 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 999 | return NULL; |
| 1000 | return output_buffer; |
| 1001 | } |
| 1002 | |
| 1003 | static int |
| 1004 | _Pickler_FlushToFile(PicklerObject *self) |
| 1005 | { |
| 1006 | PyObject *output, *result; |
| 1007 | |
| 1008 | assert(self->write != NULL); |
| 1009 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1010 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1011 | output = _Pickler_GetString(self); |
| 1012 | if (output == NULL) |
| 1013 | return -1; |
| 1014 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1015 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1016 | Py_XDECREF(result); |
| 1017 | return (result == NULL) ? -1 : 0; |
| 1018 | } |
| 1019 | |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1020 | static int |
| 1021 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 1022 | { |
| 1023 | Py_ssize_t frame_len; |
| 1024 | |
| 1025 | if (!self->framing || self->frame_start == -1) { |
| 1026 | return 0; |
| 1027 | } |
| 1028 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 1029 | if (frame_len >= FRAME_SIZE_TARGET) { |
| 1030 | if(_Pickler_CommitFrame(self)) { |
| 1031 | return -1; |
| 1032 | } |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 1033 | /* Flush the content of the committed frame to the underlying |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1034 | * file and reuse the pickler buffer for the next frame so as |
| 1035 | * to limit memory usage when dumping large complex objects to |
| 1036 | * a file. |
| 1037 | * |
| 1038 | * self->write is NULL when called via dumps. |
| 1039 | */ |
| 1040 | if (self->write != NULL) { |
| 1041 | if (_Pickler_FlushToFile(self) < 0) { |
| 1042 | return -1; |
| 1043 | } |
| 1044 | if (_Pickler_ClearBuffer(self) < 0) { |
| 1045 | return -1; |
| 1046 | } |
| 1047 | } |
| 1048 | } |
| 1049 | return 0; |
| 1050 | } |
| 1051 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1052 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1053 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1054 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1055 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1056 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1057 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1058 | |
| 1059 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1060 | need_new_frame = (self->framing && self->frame_start == -1); |
| 1061 | |
| 1062 | if (need_new_frame) |
| 1063 | n = data_len + FRAME_HEADER_SIZE; |
| 1064 | else |
| 1065 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1066 | |
| 1067 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1068 | if (required > self->max_output_len) { |
| 1069 | /* Make place in buffer for the pickle chunk */ |
| 1070 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 1071 | PyErr_NoMemory(); |
| 1072 | return -1; |
| 1073 | } |
| 1074 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 1075 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 1076 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1077 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1078 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1079 | if (need_new_frame) { |
| 1080 | /* Setup new frame */ |
| 1081 | Py_ssize_t frame_start = self->output_len; |
| 1082 | self->frame_start = frame_start; |
| 1083 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 1084 | /* Write an invalid value, for debugging */ |
| 1085 | buffer[frame_start + i] = 0xFE; |
| 1086 | } |
| 1087 | self->output_len += FRAME_HEADER_SIZE; |
| 1088 | } |
| 1089 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1090 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1091 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1092 | buffer[self->output_len + i] = s[i]; |
| 1093 | } |
| 1094 | } |
| 1095 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1096 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1097 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1098 | self->output_len += data_len; |
| 1099 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1100 | } |
| 1101 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1102 | static PicklerObject * |
| 1103 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1104 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1105 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1106 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1107 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1108 | if (self == NULL) |
| 1109 | return NULL; |
| 1110 | |
| 1111 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1112 | self->dispatch_table = NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1113 | self->buffer_callback = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1114 | self->write = NULL; |
| 1115 | self->proto = 0; |
| 1116 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1117 | self->framing = 0; |
| 1118 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1119 | self->fast = 0; |
| 1120 | self->fast_nesting = 0; |
| 1121 | self->fix_imports = 0; |
| 1122 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1123 | self->max_output_len = WRITE_BUF_SIZE; |
| 1124 | self->output_len = 0; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 1125 | self->reducer_override = NULL; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1126 | |
| 1127 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1128 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1129 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1130 | |
| 1131 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1132 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1133 | return NULL; |
| 1134 | } |
Zackery Spytz | 359bd4f | 2019-04-23 05:56:08 -0600 | [diff] [blame] | 1135 | |
| 1136 | PyObject_GC_Track(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1137 | return self; |
| 1138 | } |
| 1139 | |
| 1140 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1141 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1142 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1143 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1144 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1145 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1146 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1147 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1148 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1149 | proto = PyLong_AsLong(protocol); |
| 1150 | if (proto < 0) { |
| 1151 | if (proto == -1 && PyErr_Occurred()) |
| 1152 | return -1; |
| 1153 | proto = HIGHEST_PROTOCOL; |
| 1154 | } |
| 1155 | else if (proto > HIGHEST_PROTOCOL) { |
| 1156 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1157 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1158 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1159 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1160 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1161 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1162 | self->bin = proto > 0; |
| 1163 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1168 | be called once on a freshly created Pickler. */ |
| 1169 | static int |
| 1170 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1171 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1172 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1173 | assert(file != NULL); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1174 | if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) { |
| 1175 | return -1; |
| 1176 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1177 | if (self->write == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1178 | PyErr_SetString(PyExc_TypeError, |
| 1179 | "file must have a 'write' attribute"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1180 | return -1; |
| 1181 | } |
| 1182 | |
| 1183 | return 0; |
| 1184 | } |
| 1185 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1186 | static int |
| 1187 | _Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback) |
| 1188 | { |
| 1189 | if (buffer_callback == Py_None) { |
| 1190 | buffer_callback = NULL; |
| 1191 | } |
| 1192 | if (buffer_callback != NULL && self->proto < 5) { |
| 1193 | PyErr_SetString(PyExc_ValueError, |
| 1194 | "buffer_callback needs protocol >= 5"); |
| 1195 | return -1; |
| 1196 | } |
| 1197 | |
| 1198 | Py_XINCREF(buffer_callback); |
| 1199 | self->buffer_callback = buffer_callback; |
| 1200 | return 0; |
| 1201 | } |
| 1202 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1203 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1204 | own reference to `input`. */ |
| 1205 | static Py_ssize_t |
| 1206 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1207 | { |
| 1208 | if (self->buffer.buf != NULL) |
| 1209 | PyBuffer_Release(&self->buffer); |
| 1210 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1211 | return -1; |
| 1212 | self->input_buffer = self->buffer.buf; |
| 1213 | self->input_len = self->buffer.len; |
| 1214 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1215 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1216 | return self->input_len; |
| 1217 | } |
| 1218 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1219 | static int |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1220 | bad_readline(void) |
| 1221 | { |
| 1222 | PickleState *st = _Pickle_GetGlobalState(); |
| 1223 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
| 1224 | return -1; |
| 1225 | } |
| 1226 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1227 | /* Skip any consumed data that was only prefetched using peek() */ |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1228 | static int |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1229 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1230 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1231 | Py_ssize_t consumed; |
| 1232 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1233 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1234 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1235 | if (consumed <= 0) |
| 1236 | return 0; |
| 1237 | |
| 1238 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1239 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1240 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1241 | if (r == NULL) |
| 1242 | return -1; |
| 1243 | Py_DECREF(r); |
| 1244 | |
| 1245 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1246 | return 0; |
| 1247 | } |
| 1248 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1249 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1250 | |
| 1251 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1252 | may be multiple pickle objects arranged contiguously in the same input |
| 1253 | buffer. |
| 1254 | |
| 1255 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1256 | bytes from the input stream/buffer. |
| 1257 | |
| 1258 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1259 | failure; on success, returns the number of bytes read from the file. |
| 1260 | |
| 1261 | On success, self->input_len will be 0; this is intentional so that when |
| 1262 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1263 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1264 | size to tell you how much data you can process. */ |
| 1265 | static Py_ssize_t |
| 1266 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1267 | { |
| 1268 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1269 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1270 | |
| 1271 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1272 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1273 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1274 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1275 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1276 | if (n == READ_WHOLE_LINE) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 1277 | data = PyObject_CallNoArgs(self->readline); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1278 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1279 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1280 | PyObject *len; |
| 1281 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1282 | if (self->peek && n < PREFETCH) { |
| 1283 | len = PyLong_FromSsize_t(PREFETCH); |
| 1284 | if (len == NULL) |
| 1285 | return -1; |
| 1286 | data = _Pickle_FastCall(self->peek, len); |
| 1287 | if (data == NULL) { |
| 1288 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1289 | return -1; |
| 1290 | /* peek() is probably not supported by the given file object */ |
| 1291 | PyErr_Clear(); |
| 1292 | Py_CLEAR(self->peek); |
| 1293 | } |
| 1294 | else { |
| 1295 | read_size = _Unpickler_SetStringInput(self, data); |
| 1296 | Py_DECREF(data); |
| 1297 | self->prefetched_idx = 0; |
| 1298 | if (n <= read_size) |
| 1299 | return n; |
| 1300 | } |
| 1301 | } |
| 1302 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1303 | if (len == NULL) |
| 1304 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1305 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1306 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1307 | if (data == NULL) |
| 1308 | return -1; |
| 1309 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1310 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1311 | Py_DECREF(data); |
| 1312 | return read_size; |
| 1313 | } |
| 1314 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1315 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1316 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1317 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1318 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1319 | Py_ssize_t num_read; |
| 1320 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1321 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1322 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1323 | PickleState *st = _Pickle_GetGlobalState(); |
| 1324 | PyErr_SetString(st->UnpicklingError, |
| 1325 | "read would overflow (invalid bytecode)"); |
| 1326 | return -1; |
| 1327 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1328 | |
| 1329 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1330 | assert(self->next_read_idx + n > self->input_len); |
| 1331 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1332 | if (!self->read) |
| 1333 | return bad_readline(); |
| 1334 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1335 | /* Extend the buffer to satisfy desired size */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1336 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1337 | if (num_read < 0) |
| 1338 | return -1; |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1339 | if (num_read < n) |
| 1340 | return bad_readline(); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1341 | *s = self->input_buffer; |
| 1342 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1343 | return n; |
| 1344 | } |
| 1345 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1346 | /* Read `n` bytes from the unpickler's data source, storing the result in `buf`. |
| 1347 | * |
| 1348 | * This should only be used for non-small data reads where potentially |
| 1349 | * avoiding a copy is beneficial. This method does not try to prefetch |
| 1350 | * more data into the input buffer. |
| 1351 | * |
| 1352 | * _Unpickler_Read() is recommended in most cases. |
| 1353 | */ |
| 1354 | static Py_ssize_t |
| 1355 | _Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n) |
| 1356 | { |
| 1357 | assert(n != READ_WHOLE_LINE); |
| 1358 | |
| 1359 | /* Read from available buffer data, if any */ |
| 1360 | Py_ssize_t in_buffer = self->input_len - self->next_read_idx; |
| 1361 | if (in_buffer > 0) { |
| 1362 | Py_ssize_t to_read = Py_MIN(in_buffer, n); |
| 1363 | memcpy(buf, self->input_buffer + self->next_read_idx, to_read); |
| 1364 | self->next_read_idx += to_read; |
| 1365 | buf += to_read; |
| 1366 | n -= to_read; |
| 1367 | if (n == 0) { |
| 1368 | /* Entire read was satisfied from buffer */ |
| 1369 | return n; |
| 1370 | } |
| 1371 | } |
| 1372 | |
| 1373 | /* Read from file */ |
| 1374 | if (!self->readinto) { |
| 1375 | return bad_readline(); |
| 1376 | } |
| 1377 | if (_Unpickler_SkipConsumed(self) < 0) { |
| 1378 | return -1; |
| 1379 | } |
| 1380 | |
| 1381 | /* Call readinto() into user buffer */ |
| 1382 | PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE); |
| 1383 | if (buf_obj == NULL) { |
| 1384 | return -1; |
| 1385 | } |
| 1386 | PyObject *read_size_obj = _Pickle_FastCall(self->readinto, buf_obj); |
| 1387 | if (read_size_obj == NULL) { |
| 1388 | return -1; |
| 1389 | } |
| 1390 | Py_ssize_t read_size = PyLong_AsSsize_t(read_size_obj); |
| 1391 | Py_DECREF(read_size_obj); |
| 1392 | |
| 1393 | if (read_size < 0) { |
| 1394 | if (!PyErr_Occurred()) { |
| 1395 | PyErr_SetString(PyExc_ValueError, |
| 1396 | "readinto() returned negative size"); |
| 1397 | } |
| 1398 | return -1; |
| 1399 | } |
| 1400 | if (read_size < n) { |
| 1401 | return bad_readline(); |
| 1402 | } |
| 1403 | return n; |
| 1404 | } |
| 1405 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1406 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1407 | |
| 1408 | This should be used for all data reads, rather than accessing the unpickler's |
| 1409 | input buffer directly. This method deals correctly with reading from input |
| 1410 | streams, which the input buffer doesn't deal with. |
| 1411 | |
| 1412 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1413 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1414 | should use this function's return value to know how many bytes you can |
| 1415 | consume. |
| 1416 | |
| 1417 | Returns -1 (with an exception set) on failure. On success, return the |
| 1418 | number of chars read. */ |
| 1419 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1420 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1421 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1422 | (self)->next_read_idx += (n), \ |
| 1423 | (n)) \ |
| 1424 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1425 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1426 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1427 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1428 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1429 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1430 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1431 | if (input_line == NULL) { |
| 1432 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1433 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1434 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1435 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1436 | memcpy(input_line, line, len); |
| 1437 | input_line[len] = '\0'; |
| 1438 | self->input_line = input_line; |
| 1439 | *result = self->input_line; |
| 1440 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1443 | /* 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] | 1444 | before hitting \n, raise an error. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1445 | |
| 1446 | Returns the number of chars read, or -1 on failure. */ |
| 1447 | static Py_ssize_t |
| 1448 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1449 | { |
| 1450 | Py_ssize_t i, num_read; |
| 1451 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1452 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1453 | if (self->input_buffer[i] == '\n') { |
| 1454 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1455 | num_read = i - self->next_read_idx + 1; |
| 1456 | self->next_read_idx = i + 1; |
| 1457 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1458 | } |
| 1459 | } |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1460 | if (!self->read) |
| 1461 | return bad_readline(); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1462 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1463 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1464 | if (num_read < 0) |
| 1465 | return -1; |
| 1466 | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
| 1467 | return bad_readline(); |
| 1468 | self->next_read_idx = num_read; |
| 1469 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1473 | will be modified in place. */ |
| 1474 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1475 | _Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1476 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1477 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1478 | |
| 1479 | assert(new_size > self->memo_size); |
| 1480 | |
Sergey Fedoseev | 67b9cc8 | 2018-08-16 09:27:50 +0500 | [diff] [blame] | 1481 | PyObject **memo_new = self->memo; |
| 1482 | PyMem_RESIZE(memo_new, PyObject *, new_size); |
| 1483 | if (memo_new == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1484 | PyErr_NoMemory(); |
| 1485 | return -1; |
| 1486 | } |
Sergey Fedoseev | 67b9cc8 | 2018-08-16 09:27:50 +0500 | [diff] [blame] | 1487 | self->memo = memo_new; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1488 | for (i = self->memo_size; i < new_size; i++) |
| 1489 | self->memo[i] = NULL; |
| 1490 | self->memo_size = new_size; |
| 1491 | return 0; |
| 1492 | } |
| 1493 | |
| 1494 | /* Returns NULL if idx is out of bounds. */ |
| 1495 | static PyObject * |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1496 | _Unpickler_MemoGet(UnpicklerObject *self, size_t idx) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1497 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1498 | if (idx >= self->memo_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1499 | return NULL; |
| 1500 | |
| 1501 | return self->memo[idx]; |
| 1502 | } |
| 1503 | |
| 1504 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1505 | This takes its own reference to `value`. */ |
| 1506 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1507 | _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1508 | { |
| 1509 | PyObject *old_item; |
| 1510 | |
| 1511 | if (idx >= self->memo_size) { |
| 1512 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1513 | return -1; |
| 1514 | assert(idx < self->memo_size); |
| 1515 | } |
| 1516 | Py_INCREF(value); |
| 1517 | old_item = self->memo[idx]; |
| 1518 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1519 | if (old_item != NULL) { |
| 1520 | Py_DECREF(old_item); |
| 1521 | } |
| 1522 | else { |
| 1523 | self->memo_len++; |
| 1524 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1525 | return 0; |
| 1526 | } |
| 1527 | |
| 1528 | static PyObject ** |
| 1529 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1530 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1531 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1532 | if (memo == NULL) { |
| 1533 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1534 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1535 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1536 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1537 | return memo; |
| 1538 | } |
| 1539 | |
| 1540 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1541 | static void |
| 1542 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1543 | { |
| 1544 | Py_ssize_t i; |
| 1545 | PyObject **memo = self->memo; |
| 1546 | |
| 1547 | if (self->memo == NULL) |
| 1548 | return; |
| 1549 | self->memo = NULL; |
| 1550 | i = self->memo_size; |
| 1551 | while (--i >= 0) { |
| 1552 | Py_XDECREF(memo[i]); |
| 1553 | } |
| 1554 | PyMem_FREE(memo); |
| 1555 | } |
| 1556 | |
| 1557 | static UnpicklerObject * |
| 1558 | _Unpickler_New(void) |
| 1559 | { |
| 1560 | UnpicklerObject *self; |
| 1561 | |
| 1562 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1563 | if (self == NULL) |
| 1564 | return NULL; |
| 1565 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1566 | self->pers_func = NULL; |
| 1567 | self->input_buffer = NULL; |
| 1568 | self->input_line = NULL; |
| 1569 | self->input_len = 0; |
| 1570 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1571 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1572 | self->read = NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1573 | self->readinto = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1574 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1575 | self->peek = NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1576 | self->buffers = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1577 | self->encoding = NULL; |
| 1578 | self->errors = NULL; |
| 1579 | self->marks = NULL; |
| 1580 | self->num_marks = 0; |
| 1581 | self->marks_size = 0; |
| 1582 | self->proto = 0; |
| 1583 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1584 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1585 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1586 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1587 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1588 | self->stack = (Pdata *)Pdata_New(); |
| 1589 | |
| 1590 | if (self->memo == NULL || self->stack == NULL) { |
| 1591 | Py_DECREF(self); |
| 1592 | return NULL; |
| 1593 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1594 | |
Zackery Spytz | 359bd4f | 2019-04-23 05:56:08 -0600 | [diff] [blame] | 1595 | PyObject_GC_Track(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1596 | return self; |
| 1597 | } |
| 1598 | |
| 1599 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1600 | be called once on a freshly created Unpickler. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1601 | static int |
| 1602 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1603 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1604 | _Py_IDENTIFIER(peek); |
| 1605 | _Py_IDENTIFIER(read); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1606 | _Py_IDENTIFIER(readinto); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1607 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1608 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1609 | if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) { |
| 1610 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1611 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1612 | (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1613 | (void)_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1614 | (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1615 | if (!self->readline || !self->readinto || !self->read) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1616 | if (!PyErr_Occurred()) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1617 | PyErr_SetString(PyExc_TypeError, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1618 | "file must have 'read', 'readinto' and " |
| 1619 | "'readline' attributes"); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1620 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1621 | Py_CLEAR(self->read); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1622 | Py_CLEAR(self->readinto); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1623 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1624 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1625 | return -1; |
| 1626 | } |
| 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1631 | be called once on a freshly created Unpickler. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1632 | static int |
| 1633 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1634 | const char *encoding, |
| 1635 | const char *errors) |
| 1636 | { |
| 1637 | if (encoding == NULL) |
| 1638 | encoding = "ASCII"; |
| 1639 | if (errors == NULL) |
| 1640 | errors = "strict"; |
| 1641 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1642 | self->encoding = _PyMem_Strdup(encoding); |
| 1643 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1644 | if (self->encoding == NULL || self->errors == NULL) { |
| 1645 | PyErr_NoMemory(); |
| 1646 | return -1; |
| 1647 | } |
| 1648 | return 0; |
| 1649 | } |
| 1650 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1651 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1652 | be called once on a freshly created Unpickler. */ |
| 1653 | static int |
| 1654 | _Unpickler_SetBuffers(UnpicklerObject *self, PyObject *buffers) |
| 1655 | { |
| 1656 | if (buffers == NULL) { |
| 1657 | self->buffers = NULL; |
| 1658 | } |
| 1659 | else { |
| 1660 | self->buffers = PyObject_GetIter(buffers); |
| 1661 | if (self->buffers == NULL) { |
| 1662 | return -1; |
| 1663 | } |
| 1664 | } |
| 1665 | return 0; |
| 1666 | } |
| 1667 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1668 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1669 | static int |
| 1670 | memo_get(PicklerObject *self, PyObject *key) |
| 1671 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1672 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1673 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1674 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1675 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1676 | value = PyMemoTable_Get(self->memo, key); |
| 1677 | if (value == NULL) { |
| 1678 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1679 | return -1; |
| 1680 | } |
| 1681 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1682 | if (!self->bin) { |
| 1683 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1684 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1685 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1686 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1687 | } |
| 1688 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1689 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1690 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1691 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1692 | len = 2; |
| 1693 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1694 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1695 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1696 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1697 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1698 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1699 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1700 | len = 5; |
| 1701 | } |
| 1702 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1703 | PickleState *st = _Pickle_GetGlobalState(); |
| 1704 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1705 | "memo id too large for LONG_BINGET"); |
| 1706 | return -1; |
| 1707 | } |
| 1708 | } |
| 1709 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1710 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1711 | return -1; |
| 1712 | |
| 1713 | return 0; |
| 1714 | } |
| 1715 | |
| 1716 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1717 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1718 | static int |
| 1719 | memo_put(PicklerObject *self, PyObject *obj) |
| 1720 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1721 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1722 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1723 | Py_ssize_t idx; |
| 1724 | |
| 1725 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1726 | |
| 1727 | if (self->fast) |
| 1728 | return 0; |
| 1729 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1730 | idx = PyMemoTable_Size(self->memo); |
| 1731 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1732 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1733 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1734 | if (self->proto >= 4) { |
| 1735 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1736 | return -1; |
| 1737 | return 0; |
| 1738 | } |
| 1739 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1740 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1741 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1742 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1743 | len = strlen(pdata); |
| 1744 | } |
| 1745 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1746 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1747 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1748 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1749 | len = 2; |
| 1750 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1751 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1752 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1753 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1754 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1755 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1756 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1757 | len = 5; |
| 1758 | } |
| 1759 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1760 | PickleState *st = _Pickle_GetGlobalState(); |
| 1761 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1762 | "memo id too large for LONG_BINPUT"); |
| 1763 | return -1; |
| 1764 | } |
| 1765 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1766 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1767 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1768 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1769 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1770 | } |
| 1771 | |
| 1772 | static PyObject * |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1773 | get_dotted_path(PyObject *obj, PyObject *name) |
| 1774 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1775 | _Py_static_string(PyId_dot, "."); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1776 | PyObject *dotted_path; |
| 1777 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1778 | |
| 1779 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1780 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1781 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1782 | n = PyList_GET_SIZE(dotted_path); |
| 1783 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1784 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1785 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1786 | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1787 | if (obj == NULL) |
| 1788 | PyErr_Format(PyExc_AttributeError, |
| 1789 | "Can't pickle local object %R", name); |
| 1790 | else |
| 1791 | PyErr_Format(PyExc_AttributeError, |
| 1792 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1793 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1794 | return NULL; |
| 1795 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1796 | } |
| 1797 | return dotted_path; |
| 1798 | } |
| 1799 | |
| 1800 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1801 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1802 | { |
| 1803 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1804 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1805 | |
| 1806 | assert(PyList_CheckExact(names)); |
| 1807 | Py_INCREF(obj); |
| 1808 | n = PyList_GET_SIZE(names); |
| 1809 | for (i = 0; i < n; i++) { |
| 1810 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1811 | Py_XDECREF(parent); |
| 1812 | parent = obj; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1813 | (void)_PyObject_LookupAttr(parent, name, &obj); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1814 | if (obj == NULL) { |
| 1815 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1816 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1817 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1818 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1819 | if (pparent != NULL) |
| 1820 | *pparent = parent; |
| 1821 | else |
| 1822 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1823 | return obj; |
| 1824 | } |
| 1825 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1826 | |
| 1827 | static PyObject * |
| 1828 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1829 | { |
| 1830 | PyObject *dotted_path, *attr; |
| 1831 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1832 | if (allow_qualname) { |
| 1833 | dotted_path = get_dotted_path(obj, name); |
| 1834 | if (dotted_path == NULL) |
| 1835 | return NULL; |
| 1836 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1837 | Py_DECREF(dotted_path); |
| 1838 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1839 | else { |
| 1840 | (void)_PyObject_LookupAttr(obj, name, &attr); |
| 1841 | } |
| 1842 | if (attr == NULL && !PyErr_Occurred()) { |
| 1843 | PyErr_Format(PyExc_AttributeError, |
| 1844 | "Can't get attribute %R on %R", name, obj); |
| 1845 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1846 | return attr; |
| 1847 | } |
| 1848 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1849 | static int |
| 1850 | _checkmodule(PyObject *module_name, PyObject *module, |
| 1851 | PyObject *global, PyObject *dotted_path) |
| 1852 | { |
| 1853 | if (module == Py_None) { |
| 1854 | return -1; |
| 1855 | } |
| 1856 | if (PyUnicode_Check(module_name) && |
| 1857 | _PyUnicode_EqualToASCIIString(module_name, "__main__")) { |
| 1858 | return -1; |
| 1859 | } |
| 1860 | |
| 1861 | PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); |
| 1862 | if (candidate == NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1863 | return -1; |
| 1864 | } |
| 1865 | if (candidate != global) { |
| 1866 | Py_DECREF(candidate); |
| 1867 | return -1; |
| 1868 | } |
| 1869 | Py_DECREF(candidate); |
| 1870 | return 0; |
| 1871 | } |
| 1872 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1873 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1874 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1875 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1876 | PyObject *module_name; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1877 | PyObject *module = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1878 | Py_ssize_t i; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1879 | PyObject *modules; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1880 | _Py_IDENTIFIER(__module__); |
| 1881 | _Py_IDENTIFIER(modules); |
| 1882 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1883 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1884 | if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) { |
| 1885 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1886 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1887 | if (module_name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1888 | /* In some rare cases (e.g., bound methods of extension types), |
| 1889 | __module__ can be None. If it is so, then search sys.modules for |
| 1890 | the module of global. */ |
| 1891 | if (module_name != Py_None) |
| 1892 | return module_name; |
| 1893 | Py_CLEAR(module_name); |
| 1894 | } |
| 1895 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1896 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1897 | /* Fallback on walking sys.modules */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1898 | modules = _PySys_GetObjectId(&PyId_modules); |
| 1899 | if (modules == NULL) { |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1900 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1901 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1902 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1903 | if (PyDict_CheckExact(modules)) { |
| 1904 | i = 0; |
| 1905 | while (PyDict_Next(modules, &i, &module_name, &module)) { |
| 1906 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1907 | Py_INCREF(module_name); |
| 1908 | return module_name; |
| 1909 | } |
| 1910 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1911 | return NULL; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1912 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1913 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1914 | } |
| 1915 | else { |
| 1916 | PyObject *iterator = PyObject_GetIter(modules); |
| 1917 | if (iterator == NULL) { |
| 1918 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1919 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1920 | while ((module_name = PyIter_Next(iterator))) { |
| 1921 | module = PyObject_GetItem(modules, module_name); |
| 1922 | if (module == NULL) { |
| 1923 | Py_DECREF(module_name); |
| 1924 | Py_DECREF(iterator); |
| 1925 | return NULL; |
| 1926 | } |
| 1927 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1928 | Py_DECREF(module); |
| 1929 | Py_DECREF(iterator); |
| 1930 | return module_name; |
| 1931 | } |
| 1932 | Py_DECREF(module); |
| 1933 | Py_DECREF(module_name); |
| 1934 | if (PyErr_Occurred()) { |
| 1935 | Py_DECREF(iterator); |
| 1936 | return NULL; |
| 1937 | } |
| 1938 | } |
| 1939 | Py_DECREF(iterator); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1940 | } |
| 1941 | |
| 1942 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1943 | module_name = _PyUnicode_FromId(&PyId___main__); |
Victor Stinner | af46eb8 | 2017-09-05 23:30:16 +0200 | [diff] [blame] | 1944 | Py_XINCREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1945 | return module_name; |
| 1946 | } |
| 1947 | |
| 1948 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1949 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1950 | memoization disabled). If the nesting of a list or dict object exceed |
| 1951 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1952 | reference to the seen list or dict objects and check whether these objects |
| 1953 | are recursive. These are not strictly necessary, since save() has a |
| 1954 | hard-coded recursion limit, but they give a nicer error message than the |
| 1955 | typical RuntimeError. */ |
| 1956 | static int |
| 1957 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1958 | { |
| 1959 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1960 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1961 | PyObject *key = NULL; |
| 1962 | if (self->fast_memo == NULL) { |
| 1963 | self->fast_memo = PyDict_New(); |
| 1964 | if (self->fast_memo == NULL) { |
| 1965 | self->fast_nesting = -1; |
| 1966 | return 0; |
| 1967 | } |
| 1968 | } |
| 1969 | key = PyLong_FromVoidPtr(obj); |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1970 | if (key == NULL) { |
| 1971 | self->fast_nesting = -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1972 | return 0; |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1973 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1974 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1975 | Py_DECREF(key); |
| 1976 | PyErr_Format(PyExc_ValueError, |
| 1977 | "fast mode: can't pickle cyclic objects " |
| 1978 | "including object type %.200s at %p", |
| 1979 | obj->ob_type->tp_name, obj); |
| 1980 | self->fast_nesting = -1; |
| 1981 | return 0; |
| 1982 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1983 | if (PyErr_Occurred()) { |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1984 | Py_DECREF(key); |
| 1985 | self->fast_nesting = -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1986 | return 0; |
| 1987 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1988 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1989 | Py_DECREF(key); |
| 1990 | self->fast_nesting = -1; |
| 1991 | return 0; |
| 1992 | } |
| 1993 | Py_DECREF(key); |
| 1994 | } |
| 1995 | return 1; |
| 1996 | } |
| 1997 | |
| 1998 | static int |
| 1999 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 2000 | { |
| 2001 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 2002 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 2003 | if (key == NULL) |
| 2004 | return 0; |
| 2005 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 2006 | Py_DECREF(key); |
| 2007 | return 0; |
| 2008 | } |
| 2009 | Py_DECREF(key); |
| 2010 | } |
| 2011 | return 1; |
| 2012 | } |
| 2013 | |
| 2014 | static int |
| 2015 | save_none(PicklerObject *self, PyObject *obj) |
| 2016 | { |
| 2017 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2018 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2019 | return -1; |
| 2020 | |
| 2021 | return 0; |
| 2022 | } |
| 2023 | |
| 2024 | static int |
| 2025 | save_bool(PicklerObject *self, PyObject *obj) |
| 2026 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2027 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 2028 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2029 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2030 | return -1; |
| 2031 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 2032 | else { |
| 2033 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 2034 | * so that unpicklers written before bools were introduced unpickle them |
| 2035 | * as ints, but unpicklers after can recognize that bools were intended. |
| 2036 | * Note that protocol 2 added direct ways to pickle bools. |
| 2037 | */ |
| 2038 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 2039 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 2040 | return -1; |
| 2041 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2042 | return 0; |
| 2043 | } |
| 2044 | |
| 2045 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2046 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2047 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2048 | PyObject *repr = NULL; |
| 2049 | Py_ssize_t size; |
| 2050 | long val; |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2051 | int overflow; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2052 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2053 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2054 | val= PyLong_AsLongAndOverflow(obj, &overflow); |
| 2055 | if (!overflow && (sizeof(long) <= 4 || |
| 2056 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) |
| 2057 | { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2058 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2059 | |
| 2060 | Note: we can't use -0x80000000L in the above condition because some |
| 2061 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 2062 | before applying the unary minus when sizeof(long) <= 4. The |
| 2063 | resulting value stays unsigned which is commonly not what we want, |
| 2064 | so MSVC happily warns us about it. However, that result would have |
| 2065 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 2066 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2067 | char pdata[32]; |
| 2068 | Py_ssize_t len = 0; |
| 2069 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2070 | if (self->bin) { |
| 2071 | pdata[1] = (unsigned char)(val & 0xff); |
| 2072 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 2073 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 2074 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2075 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2076 | if ((pdata[4] != 0) || (pdata[3] != 0)) { |
| 2077 | pdata[0] = BININT; |
| 2078 | len = 5; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2079 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2080 | else if (pdata[2] != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2081 | pdata[0] = BININT2; |
| 2082 | len = 3; |
| 2083 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2084 | else { |
| 2085 | pdata[0] = BININT1; |
| 2086 | len = 2; |
| 2087 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2088 | } |
| 2089 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2090 | sprintf(pdata, "%c%ld\n", INT, val); |
| 2091 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2092 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2093 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2094 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2095 | |
| 2096 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2097 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2098 | assert(!PyErr_Occurred()); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2099 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2100 | if (self->proto >= 2) { |
| 2101 | /* Linear-time pickling. */ |
| 2102 | size_t nbits; |
| 2103 | size_t nbytes; |
| 2104 | unsigned char *pdata; |
| 2105 | char header[5]; |
| 2106 | int i; |
| 2107 | int sign = _PyLong_Sign(obj); |
| 2108 | |
| 2109 | if (sign == 0) { |
| 2110 | header[0] = LONG1; |
| 2111 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2112 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2113 | goto error; |
| 2114 | return 0; |
| 2115 | } |
| 2116 | nbits = _PyLong_NumBits(obj); |
| 2117 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 2118 | goto error; |
| 2119 | /* How many bytes do we need? There are nbits >> 3 full |
| 2120 | * bytes of data, and nbits & 7 leftover bits. If there |
| 2121 | * are any leftover bits, then we clearly need another |
| 2122 | * byte. Wnat's not so obvious is that we *probably* |
| 2123 | * need another byte even if there aren't any leftovers: |
| 2124 | * the most-significant bit of the most-significant byte |
| 2125 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2126 | * opposite of the one we need. The exception is ints |
| 2127 | * 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] | 2128 | * its own 256's-complement, so has the right sign bit |
| 2129 | * even without the extra byte. That's a pain to check |
| 2130 | * for in advance, though, so we always grab an extra |
| 2131 | * byte at the start, and cut it back later if possible. |
| 2132 | */ |
| 2133 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 2134 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2135 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2136 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2137 | goto error; |
| 2138 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2139 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2140 | if (repr == NULL) |
| 2141 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2142 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2143 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 2144 | pdata, nbytes, |
| 2145 | 1 /* little endian */ , 1 /* signed */ ); |
| 2146 | if (i < 0) |
| 2147 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2148 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2149 | * needed. This is so iff the MSB is all redundant sign |
| 2150 | * bits. |
| 2151 | */ |
| 2152 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2153 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2154 | pdata[nbytes - 1] == 0xff && |
| 2155 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 2156 | nbytes--; |
| 2157 | } |
| 2158 | |
| 2159 | if (nbytes < 256) { |
| 2160 | header[0] = LONG1; |
| 2161 | header[1] = (unsigned char)nbytes; |
| 2162 | size = 2; |
| 2163 | } |
| 2164 | else { |
| 2165 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2166 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2167 | for (i = 1; i < 5; i++) { |
| 2168 | header[i] = (unsigned char)(size & 0xff); |
| 2169 | size >>= 8; |
| 2170 | } |
| 2171 | size = 5; |
| 2172 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2173 | if (_Pickler_Write(self, header, size) < 0 || |
| 2174 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2175 | goto error; |
| 2176 | } |
| 2177 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2178 | const char long_op = LONG; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2179 | const char *string; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2180 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 2181 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 2182 | the number of digits), in both directions. We add a trailing 'L' |
| 2183 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2184 | |
| 2185 | repr = PyObject_Repr(obj); |
| 2186 | if (repr == NULL) |
| 2187 | goto error; |
| 2188 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2189 | string = PyUnicode_AsUTF8AndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2190 | if (string == NULL) |
| 2191 | goto error; |
| 2192 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2193 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 2194 | _Pickler_Write(self, string, size) < 0 || |
| 2195 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2196 | goto error; |
| 2197 | } |
| 2198 | |
| 2199 | if (0) { |
| 2200 | error: |
| 2201 | status = -1; |
| 2202 | } |
| 2203 | Py_XDECREF(repr); |
| 2204 | |
| 2205 | return status; |
| 2206 | } |
| 2207 | |
| 2208 | static int |
| 2209 | save_float(PicklerObject *self, PyObject *obj) |
| 2210 | { |
| 2211 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 2212 | |
| 2213 | if (self->bin) { |
| 2214 | char pdata[9]; |
| 2215 | pdata[0] = BINFLOAT; |
| 2216 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2217 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2218 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2219 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2220 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2221 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2222 | int result = -1; |
| 2223 | char *buf = NULL; |
| 2224 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2225 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2226 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2227 | goto done; |
| 2228 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2229 | 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] | 2230 | if (!buf) { |
| 2231 | PyErr_NoMemory(); |
| 2232 | goto done; |
| 2233 | } |
| 2234 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2235 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2236 | goto done; |
| 2237 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2238 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2239 | goto done; |
| 2240 | |
| 2241 | result = 0; |
| 2242 | done: |
| 2243 | PyMem_Free(buf); |
| 2244 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2245 | } |
| 2246 | |
| 2247 | return 0; |
| 2248 | } |
| 2249 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2250 | /* Perform direct write of the header and payload of the binary object. |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2251 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2252 | The large contiguous data is written directly into the underlying file |
| 2253 | object, bypassing the output_buffer of the Pickler. We intentionally |
| 2254 | do not insert a protocol 4 frame opcode to make it possible to optimize |
| 2255 | file.read calls in the loader. |
| 2256 | */ |
| 2257 | static int |
| 2258 | _Pickler_write_bytes(PicklerObject *self, |
| 2259 | const char *header, Py_ssize_t header_size, |
| 2260 | const char *data, Py_ssize_t data_size, |
| 2261 | PyObject *payload) |
| 2262 | { |
| 2263 | int bypass_buffer = (data_size >= FRAME_SIZE_TARGET); |
| 2264 | int framing = self->framing; |
| 2265 | |
| 2266 | if (bypass_buffer) { |
| 2267 | assert(self->output_buffer != NULL); |
| 2268 | /* Commit the previous frame. */ |
| 2269 | if (_Pickler_CommitFrame(self)) { |
| 2270 | return -1; |
| 2271 | } |
| 2272 | /* Disable framing temporarily */ |
| 2273 | self->framing = 0; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2274 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2275 | |
| 2276 | if (_Pickler_Write(self, header, header_size) < 0) { |
| 2277 | return -1; |
| 2278 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2279 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2280 | if (bypass_buffer && self->write != NULL) { |
| 2281 | /* Bypass the in-memory buffer to directly stream large data |
| 2282 | into the underlying file object. */ |
| 2283 | PyObject *result, *mem = NULL; |
| 2284 | /* Dump the output buffer to the file. */ |
| 2285 | if (_Pickler_FlushToFile(self) < 0) { |
| 2286 | return -1; |
| 2287 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2288 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2289 | /* Stream write the payload into the file without going through the |
| 2290 | output buffer. */ |
| 2291 | if (payload == NULL) { |
Serhiy Storchaka | 5b76bdb | 2018-01-13 00:28:31 +0200 | [diff] [blame] | 2292 | /* TODO: It would be better to use a memoryview with a linked |
| 2293 | original string if this is possible. */ |
| 2294 | payload = mem = PyBytes_FromStringAndSize(data, data_size); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2295 | if (payload == NULL) { |
| 2296 | return -1; |
| 2297 | } |
| 2298 | } |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 2299 | result = _PyObject_CallOneArg(self->write, payload); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2300 | Py_XDECREF(mem); |
| 2301 | if (result == NULL) { |
| 2302 | return -1; |
| 2303 | } |
| 2304 | Py_DECREF(result); |
| 2305 | |
| 2306 | /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */ |
| 2307 | if (_Pickler_ClearBuffer(self) < 0) { |
| 2308 | return -1; |
| 2309 | } |
| 2310 | } |
| 2311 | else { |
| 2312 | if (_Pickler_Write(self, data, data_size) < 0) { |
| 2313 | return -1; |
| 2314 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2315 | } |
| 2316 | |
| 2317 | /* Re-enable framing for subsequent calls to _Pickler_Write. */ |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2318 | self->framing = framing; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2319 | |
| 2320 | return 0; |
| 2321 | } |
| 2322 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2323 | static int |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2324 | _save_bytes_data(PicklerObject *self, PyObject *obj, const char *data, |
| 2325 | Py_ssize_t size) |
| 2326 | { |
| 2327 | assert(self->proto >= 3); |
| 2328 | |
| 2329 | char header[9]; |
| 2330 | Py_ssize_t len; |
| 2331 | |
| 2332 | if (size < 0) |
| 2333 | return -1; |
| 2334 | |
| 2335 | if (size <= 0xff) { |
| 2336 | header[0] = SHORT_BINBYTES; |
| 2337 | header[1] = (unsigned char)size; |
| 2338 | len = 2; |
| 2339 | } |
| 2340 | else if ((size_t)size <= 0xffffffffUL) { |
| 2341 | header[0] = BINBYTES; |
| 2342 | header[1] = (unsigned char)(size & 0xff); |
| 2343 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2344 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2345 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2346 | len = 5; |
| 2347 | } |
| 2348 | else if (self->proto >= 4) { |
| 2349 | header[0] = BINBYTES8; |
| 2350 | _write_size64(header + 1, size); |
| 2351 | len = 9; |
| 2352 | } |
| 2353 | else { |
| 2354 | PyErr_SetString(PyExc_OverflowError, |
| 2355 | "serializing a bytes object larger than 4 GiB " |
| 2356 | "requires pickle protocol 4 or higher"); |
| 2357 | return -1; |
| 2358 | } |
| 2359 | |
| 2360 | if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) { |
| 2361 | return -1; |
| 2362 | } |
| 2363 | |
| 2364 | if (memo_put(self, obj) < 0) { |
| 2365 | return -1; |
| 2366 | } |
| 2367 | |
| 2368 | return 0; |
| 2369 | } |
| 2370 | |
| 2371 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2372 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2373 | { |
| 2374 | if (self->proto < 3) { |
| 2375 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2376 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2377 | the __reduce__ method) to permit bytes object unpickling. |
| 2378 | |
| 2379 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2380 | 2 'bytes' is just an alias for 'str' (which has different |
| 2381 | parameters than the actual bytes object), we use codecs.encode |
| 2382 | to create the appropriate 'str' object when unpickled using |
| 2383 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2384 | using Python 3. Again this is a hack and we don't need to do this |
| 2385 | with newer protocols. */ |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 2386 | PyObject *reduce_value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2387 | int status; |
| 2388 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2389 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2390 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2391 | } |
| 2392 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2393 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2394 | PyObject *unicode_str = |
| 2395 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2396 | PyBytes_GET_SIZE(obj), |
| 2397 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2398 | _Py_IDENTIFIER(latin1); |
| 2399 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2400 | if (unicode_str == NULL) |
| 2401 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2402 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2403 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2404 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2405 | Py_DECREF(unicode_str); |
| 2406 | } |
| 2407 | |
| 2408 | if (reduce_value == NULL) |
| 2409 | return -1; |
| 2410 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2411 | /* save_reduce() will memoize the object automatically. */ |
| 2412 | status = save_reduce(self, reduce_value, obj); |
| 2413 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2414 | return status; |
| 2415 | } |
| 2416 | else { |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2417 | return _save_bytes_data(self, obj, PyBytes_AS_STRING(obj), |
| 2418 | PyBytes_GET_SIZE(obj)); |
| 2419 | } |
| 2420 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2422 | static int |
| 2423 | _save_bytearray_data(PicklerObject *self, PyObject *obj, const char *data, |
| 2424 | Py_ssize_t size) |
| 2425 | { |
| 2426 | assert(self->proto >= 5); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2427 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2428 | char header[9]; |
| 2429 | Py_ssize_t len; |
| 2430 | |
| 2431 | if (size < 0) |
| 2432 | return -1; |
| 2433 | |
| 2434 | header[0] = BYTEARRAY8; |
| 2435 | _write_size64(header + 1, size); |
| 2436 | len = 9; |
| 2437 | |
| 2438 | if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) { |
| 2439 | return -1; |
| 2440 | } |
| 2441 | |
| 2442 | if (memo_put(self, obj) < 0) { |
| 2443 | return -1; |
| 2444 | } |
| 2445 | |
| 2446 | return 0; |
| 2447 | } |
| 2448 | |
| 2449 | static int |
| 2450 | save_bytearray(PicklerObject *self, PyObject *obj) |
| 2451 | { |
| 2452 | if (self->proto < 5) { |
| 2453 | /* Older pickle protocols do not have an opcode for pickling |
| 2454 | * bytearrays. */ |
| 2455 | PyObject *reduce_value = NULL; |
| 2456 | int status; |
| 2457 | |
| 2458 | if (PyByteArray_GET_SIZE(obj) == 0) { |
| 2459 | reduce_value = Py_BuildValue("(O())", |
| 2460 | (PyObject *) &PyByteArray_Type); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2461 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2462 | else { |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2463 | PyObject *bytes_obj = PyBytes_FromObject(obj); |
| 2464 | if (bytes_obj != NULL) { |
| 2465 | reduce_value = Py_BuildValue("(O(O))", |
| 2466 | (PyObject *) &PyByteArray_Type, |
| 2467 | bytes_obj); |
| 2468 | Py_DECREF(bytes_obj); |
| 2469 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2470 | } |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2471 | if (reduce_value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2472 | return -1; |
| 2473 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2474 | /* save_reduce() will memoize the object automatically. */ |
| 2475 | status = save_reduce(self, reduce_value, obj); |
| 2476 | Py_DECREF(reduce_value); |
| 2477 | return status; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2478 | } |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2479 | else { |
| 2480 | return _save_bytearray_data(self, obj, PyByteArray_AS_STRING(obj), |
| 2481 | PyByteArray_GET_SIZE(obj)); |
| 2482 | } |
| 2483 | } |
| 2484 | |
| 2485 | static int |
| 2486 | save_picklebuffer(PicklerObject *self, PyObject *obj) |
| 2487 | { |
| 2488 | if (self->proto < 5) { |
| 2489 | PickleState *st = _Pickle_GetGlobalState(); |
| 2490 | PyErr_SetString(st->PicklingError, |
| 2491 | "PickleBuffer can only pickled with protocol >= 5"); |
| 2492 | return -1; |
| 2493 | } |
| 2494 | const Py_buffer* view = PyPickleBuffer_GetBuffer(obj); |
| 2495 | if (view == NULL) { |
| 2496 | return -1; |
| 2497 | } |
| 2498 | if (view->suboffsets != NULL || !PyBuffer_IsContiguous(view, 'A')) { |
| 2499 | PickleState *st = _Pickle_GetGlobalState(); |
| 2500 | PyErr_SetString(st->PicklingError, |
| 2501 | "PickleBuffer can not be pickled when " |
| 2502 | "pointing to a non-contiguous buffer"); |
| 2503 | return -1; |
| 2504 | } |
| 2505 | int in_band = 1; |
| 2506 | if (self->buffer_callback != NULL) { |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 2507 | PyObject *ret = _PyObject_CallOneArg(self->buffer_callback, obj); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2508 | if (ret == NULL) { |
| 2509 | return -1; |
| 2510 | } |
| 2511 | in_band = PyObject_IsTrue(ret); |
| 2512 | Py_DECREF(ret); |
| 2513 | if (in_band == -1) { |
| 2514 | return -1; |
| 2515 | } |
| 2516 | } |
| 2517 | if (in_band) { |
| 2518 | /* Write data in-band */ |
| 2519 | if (view->readonly) { |
| 2520 | return _save_bytes_data(self, obj, (const char*) view->buf, |
| 2521 | view->len); |
| 2522 | } |
| 2523 | else { |
| 2524 | return _save_bytearray_data(self, obj, (const char*) view->buf, |
| 2525 | view->len); |
| 2526 | } |
| 2527 | } |
| 2528 | else { |
| 2529 | /* Write data out-of-band */ |
| 2530 | const char next_buffer_op = NEXT_BUFFER; |
| 2531 | if (_Pickler_Write(self, &next_buffer_op, 1) < 0) { |
| 2532 | return -1; |
| 2533 | } |
| 2534 | if (view->readonly) { |
| 2535 | const char readonly_buffer_op = READONLY_BUFFER; |
| 2536 | if (_Pickler_Write(self, &readonly_buffer_op, 1) < 0) { |
| 2537 | return -1; |
| 2538 | } |
| 2539 | } |
| 2540 | } |
| 2541 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2542 | } |
| 2543 | |
| 2544 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2545 | backslash and newline characters to \uXXXX escapes. */ |
| 2546 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2547 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2548 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2549 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2550 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2551 | void *data; |
| 2552 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2553 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2554 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2555 | if (PyUnicode_READY(obj)) |
| 2556 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2557 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2558 | _PyBytesWriter_Init(&writer); |
| 2559 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2560 | size = PyUnicode_GET_LENGTH(obj); |
| 2561 | data = PyUnicode_DATA(obj); |
| 2562 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2563 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2564 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2565 | if (p == NULL) |
| 2566 | goto error; |
| 2567 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2568 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2569 | for (i=0; i < size; i++) { |
| 2570 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2571 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2572 | if (ch >= 0x10000) { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2573 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2574 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2575 | if (p == NULL) |
| 2576 | goto error; |
| 2577 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2578 | *p++ = '\\'; |
| 2579 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2580 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2581 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2582 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2583 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2584 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2585 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2586 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2587 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2588 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2589 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Serhiy Storchaka | 38ab7d4 | 2019-05-31 11:29:39 +0300 | [diff] [blame] | 2590 | else if (ch >= 256 || |
| 2591 | ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' || |
| 2592 | ch == 0x1a) |
| 2593 | { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2594 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2595 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2596 | if (p == NULL) |
| 2597 | goto error; |
| 2598 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2599 | *p++ = '\\'; |
| 2600 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2601 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2602 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2603 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2604 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2605 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2606 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2607 | else |
| 2608 | *p++ = (char) ch; |
| 2609 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2610 | |
| 2611 | return _PyBytesWriter_Finish(&writer, p); |
| 2612 | |
| 2613 | error: |
| 2614 | _PyBytesWriter_Dealloc(&writer); |
| 2615 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
| 2618 | static int |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2619 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2620 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2621 | char header[9]; |
| 2622 | Py_ssize_t len; |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2623 | PyObject *encoded = NULL; |
| 2624 | Py_ssize_t size; |
| 2625 | const char *data; |
| 2626 | |
| 2627 | if (PyUnicode_READY(obj)) |
| 2628 | return -1; |
| 2629 | |
| 2630 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2631 | if (data == NULL) { |
| 2632 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2633 | "surrogatepass" error handler. */ |
| 2634 | PyErr_Clear(); |
| 2635 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2636 | if (encoded == NULL) |
| 2637 | return -1; |
| 2638 | |
| 2639 | data = PyBytes_AS_STRING(encoded); |
| 2640 | size = PyBytes_GET_SIZE(encoded); |
| 2641 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2642 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2643 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2644 | if (size <= 0xff && self->proto >= 4) { |
| 2645 | header[0] = SHORT_BINUNICODE; |
| 2646 | header[1] = (unsigned char)(size & 0xff); |
| 2647 | len = 2; |
| 2648 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2649 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2650 | header[0] = BINUNICODE; |
| 2651 | header[1] = (unsigned char)(size & 0xff); |
| 2652 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2653 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2654 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2655 | len = 5; |
| 2656 | } |
| 2657 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2658 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2659 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2660 | len = 9; |
| 2661 | } |
| 2662 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2663 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2664 | "serializing a string larger than 4 GiB " |
| 2665 | "requires pickle protocol 4 or higher"); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2666 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2667 | return -1; |
| 2668 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2669 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2670 | if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) { |
| 2671 | Py_XDECREF(encoded); |
| 2672 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2673 | } |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2674 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2675 | return 0; |
| 2676 | } |
| 2677 | |
| 2678 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2679 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2680 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2681 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2682 | if (write_unicode_binary(self, obj) < 0) |
| 2683 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2684 | } |
| 2685 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2686 | PyObject *encoded; |
| 2687 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2688 | const char unicode_op = UNICODE; |
| 2689 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2690 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2691 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2692 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2693 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2694 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2695 | Py_DECREF(encoded); |
| 2696 | return -1; |
| 2697 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2698 | |
| 2699 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2700 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2701 | Py_DECREF(encoded); |
| 2702 | return -1; |
| 2703 | } |
| 2704 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2705 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2706 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2707 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2708 | } |
| 2709 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2710 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2711 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2712 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2713 | } |
| 2714 | |
| 2715 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2716 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2717 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2718 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2719 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2720 | |
| 2721 | assert(PyTuple_Size(t) == len); |
| 2722 | |
| 2723 | for (i = 0; i < len; i++) { |
| 2724 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2725 | |
| 2726 | if (element == NULL) |
| 2727 | return -1; |
| 2728 | if (save(self, element, 0) < 0) |
| 2729 | return -1; |
| 2730 | } |
| 2731 | |
| 2732 | return 0; |
| 2733 | } |
| 2734 | |
| 2735 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2736 | * used across protocols to minimize the space needed to pickle them. |
| 2737 | * Tuples are also the only builtin immutable type that can be recursive |
| 2738 | * (a tuple can be reached from itself), and that requires some subtle |
| 2739 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2740 | */ |
| 2741 | static int |
| 2742 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2743 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2744 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2745 | |
| 2746 | const char mark_op = MARK; |
| 2747 | const char tuple_op = TUPLE; |
| 2748 | const char pop_op = POP; |
| 2749 | const char pop_mark_op = POP_MARK; |
| 2750 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2751 | |
| 2752 | if ((len = PyTuple_Size(obj)) < 0) |
| 2753 | return -1; |
| 2754 | |
| 2755 | if (len == 0) { |
| 2756 | char pdata[2]; |
| 2757 | |
| 2758 | if (self->proto) { |
| 2759 | pdata[0] = EMPTY_TUPLE; |
| 2760 | len = 1; |
| 2761 | } |
| 2762 | else { |
| 2763 | pdata[0] = MARK; |
| 2764 | pdata[1] = TUPLE; |
| 2765 | len = 2; |
| 2766 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2767 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2768 | return -1; |
| 2769 | return 0; |
| 2770 | } |
| 2771 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2772 | /* 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] | 2773 | * saving the tuple elements, the tuple must be recursive, in |
| 2774 | * which case we'll pop everything we put on the stack, and fetch |
| 2775 | * its value from the memo. |
| 2776 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2777 | if (len <= 3 && self->proto >= 2) { |
| 2778 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2779 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2780 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2781 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2782 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2783 | /* pop the len elements */ |
| 2784 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2785 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2786 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2787 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2788 | if (memo_get(self, obj) < 0) |
| 2789 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2790 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2791 | return 0; |
| 2792 | } |
| 2793 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2794 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2795 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2796 | } |
| 2797 | goto memoize; |
| 2798 | } |
| 2799 | |
| 2800 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2801 | * Generate MARK e1 e2 ... TUPLE |
| 2802 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2803 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2804 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2805 | |
| 2806 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2807 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2808 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2809 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2810 | /* pop the stack stuff we pushed */ |
| 2811 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2812 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2813 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2814 | } |
| 2815 | else { |
| 2816 | /* Note that we pop one more than len, to remove |
| 2817 | * the MARK too. |
| 2818 | */ |
| 2819 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2820 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2821 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2822 | } |
| 2823 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2824 | if (memo_get(self, obj) < 0) |
| 2825 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2826 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2827 | return 0; |
| 2828 | } |
| 2829 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2830 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2831 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2832 | } |
| 2833 | |
| 2834 | memoize: |
| 2835 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2836 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2837 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2838 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2839 | } |
| 2840 | |
| 2841 | /* iter is an iterator giving items, and we batch up chunks of |
| 2842 | * MARK item item ... item APPENDS |
| 2843 | * opcode sequences. Calling code should have arranged to first create an |
| 2844 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2845 | * Returns 0 on success, <0 on error. |
| 2846 | */ |
| 2847 | static int |
| 2848 | batch_list(PicklerObject *self, PyObject *iter) |
| 2849 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2850 | PyObject *obj = NULL; |
| 2851 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2852 | int i, n; |
| 2853 | |
| 2854 | const char mark_op = MARK; |
| 2855 | const char append_op = APPEND; |
| 2856 | const char appends_op = APPENDS; |
| 2857 | |
| 2858 | assert(iter != NULL); |
| 2859 | |
| 2860 | /* XXX: I think this function could be made faster by avoiding the |
| 2861 | iterator interface and fetching objects directly from list using |
| 2862 | PyList_GET_ITEM. |
| 2863 | */ |
| 2864 | |
| 2865 | if (self->proto == 0) { |
| 2866 | /* APPENDS isn't available; do one at a time. */ |
| 2867 | for (;;) { |
| 2868 | obj = PyIter_Next(iter); |
| 2869 | if (obj == NULL) { |
| 2870 | if (PyErr_Occurred()) |
| 2871 | return -1; |
| 2872 | break; |
| 2873 | } |
| 2874 | i = save(self, obj, 0); |
| 2875 | Py_DECREF(obj); |
| 2876 | if (i < 0) |
| 2877 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2878 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2879 | return -1; |
| 2880 | } |
| 2881 | return 0; |
| 2882 | } |
| 2883 | |
| 2884 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2885 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2886 | /* Get first item */ |
| 2887 | firstitem = PyIter_Next(iter); |
| 2888 | if (firstitem == NULL) { |
| 2889 | if (PyErr_Occurred()) |
| 2890 | goto error; |
| 2891 | |
| 2892 | /* nothing more to add */ |
| 2893 | break; |
| 2894 | } |
| 2895 | |
| 2896 | /* Try to get a second item */ |
| 2897 | obj = PyIter_Next(iter); |
| 2898 | if (obj == NULL) { |
| 2899 | if (PyErr_Occurred()) |
| 2900 | goto error; |
| 2901 | |
| 2902 | /* Only one item to write */ |
| 2903 | if (save(self, firstitem, 0) < 0) |
| 2904 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2905 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2906 | goto error; |
| 2907 | Py_CLEAR(firstitem); |
| 2908 | break; |
| 2909 | } |
| 2910 | |
| 2911 | /* More than one item to write */ |
| 2912 | |
| 2913 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2914 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2915 | goto error; |
| 2916 | |
| 2917 | if (save(self, firstitem, 0) < 0) |
| 2918 | goto error; |
| 2919 | Py_CLEAR(firstitem); |
| 2920 | n = 1; |
| 2921 | |
| 2922 | /* Fetch and save up to BATCHSIZE items */ |
| 2923 | while (obj) { |
| 2924 | if (save(self, obj, 0) < 0) |
| 2925 | goto error; |
| 2926 | Py_CLEAR(obj); |
| 2927 | n += 1; |
| 2928 | |
| 2929 | if (n == BATCHSIZE) |
| 2930 | break; |
| 2931 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2932 | obj = PyIter_Next(iter); |
| 2933 | if (obj == NULL) { |
| 2934 | if (PyErr_Occurred()) |
| 2935 | goto error; |
| 2936 | break; |
| 2937 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2938 | } |
| 2939 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2940 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2941 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2942 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2943 | } while (n == BATCHSIZE); |
| 2944 | return 0; |
| 2945 | |
| 2946 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2947 | Py_XDECREF(firstitem); |
| 2948 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2949 | return -1; |
| 2950 | } |
| 2951 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2952 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2953 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2954 | * MARK item item ... item APPENDS |
| 2955 | * opcode sequences. Calling code should have arranged to first create an |
| 2956 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2957 | * Returns 0 on success, -1 on error. |
| 2958 | * |
| 2959 | * This version is considerably faster than batch_list(), if less general. |
| 2960 | * |
| 2961 | * Note that this only works for protocols > 0. |
| 2962 | */ |
| 2963 | static int |
| 2964 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2965 | { |
| 2966 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2967 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2968 | |
| 2969 | const char append_op = APPEND; |
| 2970 | const char appends_op = APPENDS; |
| 2971 | const char mark_op = MARK; |
| 2972 | |
| 2973 | assert(obj != NULL); |
| 2974 | assert(self->proto > 0); |
| 2975 | assert(PyList_CheckExact(obj)); |
| 2976 | |
| 2977 | if (PyList_GET_SIZE(obj) == 1) { |
| 2978 | item = PyList_GET_ITEM(obj, 0); |
| 2979 | if (save(self, item, 0) < 0) |
| 2980 | return -1; |
| 2981 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2982 | return -1; |
| 2983 | return 0; |
| 2984 | } |
| 2985 | |
| 2986 | /* Write in batches of BATCHSIZE. */ |
| 2987 | total = 0; |
| 2988 | do { |
| 2989 | this_batch = 0; |
| 2990 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2991 | return -1; |
| 2992 | while (total < PyList_GET_SIZE(obj)) { |
| 2993 | item = PyList_GET_ITEM(obj, total); |
| 2994 | if (save(self, item, 0) < 0) |
| 2995 | return -1; |
| 2996 | total++; |
| 2997 | if (++this_batch == BATCHSIZE) |
| 2998 | break; |
| 2999 | } |
| 3000 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 3001 | return -1; |
| 3002 | |
| 3003 | } while (total < PyList_GET_SIZE(obj)); |
| 3004 | |
| 3005 | return 0; |
| 3006 | } |
| 3007 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3008 | static int |
| 3009 | save_list(PicklerObject *self, PyObject *obj) |
| 3010 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3011 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3012 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3013 | int status = 0; |
| 3014 | |
| 3015 | if (self->fast && !fast_save_enter(self, obj)) |
| 3016 | goto error; |
| 3017 | |
| 3018 | /* Create an empty list. */ |
| 3019 | if (self->bin) { |
| 3020 | header[0] = EMPTY_LIST; |
| 3021 | len = 1; |
| 3022 | } |
| 3023 | else { |
| 3024 | header[0] = MARK; |
| 3025 | header[1] = LIST; |
| 3026 | len = 2; |
| 3027 | } |
| 3028 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3029 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3030 | goto error; |
| 3031 | |
| 3032 | /* Get list length, and bow out early if empty. */ |
| 3033 | if ((len = PyList_Size(obj)) < 0) |
| 3034 | goto error; |
| 3035 | |
| 3036 | if (memo_put(self, obj) < 0) |
| 3037 | goto error; |
| 3038 | |
| 3039 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3040 | /* Materialize the list elements. */ |
| 3041 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3042 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3043 | goto error; |
| 3044 | status = batch_list_exact(self, obj); |
| 3045 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3046 | } else { |
| 3047 | PyObject *iter = PyObject_GetIter(obj); |
| 3048 | if (iter == NULL) |
| 3049 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3050 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3051 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3052 | Py_DECREF(iter); |
| 3053 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3054 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3055 | status = batch_list(self, iter); |
| 3056 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3057 | Py_DECREF(iter); |
| 3058 | } |
| 3059 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3060 | if (0) { |
| 3061 | error: |
| 3062 | status = -1; |
| 3063 | } |
| 3064 | |
| 3065 | if (self->fast && !fast_save_leave(self, obj)) |
| 3066 | status = -1; |
| 3067 | |
| 3068 | return status; |
| 3069 | } |
| 3070 | |
| 3071 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 3072 | * MARK key value ... key value SETITEMS |
| 3073 | * opcode sequences. Calling code should have arranged to first create an |
| 3074 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 3075 | * Returns 0 on success, <0 on error. |
| 3076 | * |
| 3077 | * This is very much like batch_list(). The difference between saving |
| 3078 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 3079 | * the C level, though, that attempts to combine these routines were too |
| 3080 | * ugly to bear. |
| 3081 | */ |
| 3082 | static int |
| 3083 | batch_dict(PicklerObject *self, PyObject *iter) |
| 3084 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3085 | PyObject *obj = NULL; |
| 3086 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3087 | int i, n; |
| 3088 | |
| 3089 | const char mark_op = MARK; |
| 3090 | const char setitem_op = SETITEM; |
| 3091 | const char setitems_op = SETITEMS; |
| 3092 | |
| 3093 | assert(iter != NULL); |
| 3094 | |
| 3095 | if (self->proto == 0) { |
| 3096 | /* SETITEMS isn't available; do one at a time. */ |
| 3097 | for (;;) { |
| 3098 | obj = PyIter_Next(iter); |
| 3099 | if (obj == NULL) { |
| 3100 | if (PyErr_Occurred()) |
| 3101 | return -1; |
| 3102 | break; |
| 3103 | } |
| 3104 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 3105 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 3106 | "iterator must return 2-tuples"); |
| 3107 | return -1; |
| 3108 | } |
| 3109 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 3110 | if (i >= 0) |
| 3111 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 3112 | Py_DECREF(obj); |
| 3113 | if (i < 0) |
| 3114 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3115 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3116 | return -1; |
| 3117 | } |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
| 3121 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 3122 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3123 | /* Get first item */ |
| 3124 | firstitem = PyIter_Next(iter); |
| 3125 | if (firstitem == NULL) { |
| 3126 | if (PyErr_Occurred()) |
| 3127 | goto error; |
| 3128 | |
| 3129 | /* nothing more to add */ |
| 3130 | break; |
| 3131 | } |
| 3132 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 3133 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 3134 | "iterator must return 2-tuples"); |
| 3135 | goto error; |
| 3136 | } |
| 3137 | |
| 3138 | /* Try to get a second item */ |
| 3139 | obj = PyIter_Next(iter); |
| 3140 | if (obj == NULL) { |
| 3141 | if (PyErr_Occurred()) |
| 3142 | goto error; |
| 3143 | |
| 3144 | /* Only one item to write */ |
| 3145 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 3146 | goto error; |
| 3147 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 3148 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3149 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3150 | goto error; |
| 3151 | Py_CLEAR(firstitem); |
| 3152 | break; |
| 3153 | } |
| 3154 | |
| 3155 | /* More than one item to write */ |
| 3156 | |
| 3157 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3158 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3159 | goto error; |
| 3160 | |
| 3161 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 3162 | goto error; |
| 3163 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 3164 | goto error; |
| 3165 | Py_CLEAR(firstitem); |
| 3166 | n = 1; |
| 3167 | |
| 3168 | /* Fetch and save up to BATCHSIZE items */ |
| 3169 | while (obj) { |
| 3170 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 3171 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 3172 | "iterator must return 2-tuples"); |
| 3173 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3174 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3175 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 3176 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 3177 | goto error; |
| 3178 | Py_CLEAR(obj); |
| 3179 | n += 1; |
| 3180 | |
| 3181 | if (n == BATCHSIZE) |
| 3182 | break; |
| 3183 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3184 | obj = PyIter_Next(iter); |
| 3185 | if (obj == NULL) { |
| 3186 | if (PyErr_Occurred()) |
| 3187 | goto error; |
| 3188 | break; |
| 3189 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3192 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3193 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3194 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3195 | } while (n == BATCHSIZE); |
| 3196 | return 0; |
| 3197 | |
| 3198 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3199 | Py_XDECREF(firstitem); |
| 3200 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3201 | return -1; |
| 3202 | } |
| 3203 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3204 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 3205 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 3206 | * MARK key value ... key value SETITEMS |
| 3207 | * opcode sequences. Calling code should have arranged to first create an |
| 3208 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 3209 | * Returns 0 on success, -1 on error. |
| 3210 | * |
| 3211 | * Note that this currently doesn't work for protocol 0. |
| 3212 | */ |
| 3213 | static int |
| 3214 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 3215 | { |
| 3216 | PyObject *key = NULL, *value = NULL; |
| 3217 | int i; |
| 3218 | Py_ssize_t dict_size, ppos = 0; |
| 3219 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 3220 | const char mark_op = MARK; |
| 3221 | const char setitem_op = SETITEM; |
| 3222 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3223 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3224 | assert(obj != NULL && PyDict_CheckExact(obj)); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3225 | assert(self->proto > 0); |
| 3226 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3227 | dict_size = PyDict_GET_SIZE(obj); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3228 | |
| 3229 | /* Special-case len(d) == 1 to save space. */ |
| 3230 | if (dict_size == 1) { |
| 3231 | PyDict_Next(obj, &ppos, &key, &value); |
| 3232 | if (save(self, key, 0) < 0) |
| 3233 | return -1; |
| 3234 | if (save(self, value, 0) < 0) |
| 3235 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3236 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3237 | return -1; |
| 3238 | return 0; |
| 3239 | } |
| 3240 | |
| 3241 | /* Write in batches of BATCHSIZE. */ |
| 3242 | do { |
| 3243 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3244 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3245 | return -1; |
| 3246 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 3247 | if (save(self, key, 0) < 0) |
| 3248 | return -1; |
| 3249 | if (save(self, value, 0) < 0) |
| 3250 | return -1; |
| 3251 | if (++i == BATCHSIZE) |
| 3252 | break; |
| 3253 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3254 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3255 | return -1; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3256 | if (PyDict_GET_SIZE(obj) != dict_size) { |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3257 | PyErr_Format( |
| 3258 | PyExc_RuntimeError, |
| 3259 | "dictionary changed size during iteration"); |
| 3260 | return -1; |
| 3261 | } |
| 3262 | |
| 3263 | } while (i == BATCHSIZE); |
| 3264 | return 0; |
| 3265 | } |
| 3266 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3267 | static int |
| 3268 | save_dict(PicklerObject *self, PyObject *obj) |
| 3269 | { |
| 3270 | PyObject *items, *iter; |
| 3271 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3272 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3273 | int status = 0; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3274 | assert(PyDict_Check(obj)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3275 | |
| 3276 | if (self->fast && !fast_save_enter(self, obj)) |
| 3277 | goto error; |
| 3278 | |
| 3279 | /* Create an empty dict. */ |
| 3280 | if (self->bin) { |
| 3281 | header[0] = EMPTY_DICT; |
| 3282 | len = 1; |
| 3283 | } |
| 3284 | else { |
| 3285 | header[0] = MARK; |
| 3286 | header[1] = DICT; |
| 3287 | len = 2; |
| 3288 | } |
| 3289 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3290 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3291 | goto error; |
| 3292 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3293 | if (memo_put(self, obj) < 0) |
| 3294 | goto error; |
| 3295 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3296 | if (PyDict_GET_SIZE(obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3297 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3298 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 3299 | /* We can take certain shortcuts if we know this is a dict and |
| 3300 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3301 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3302 | goto error; |
| 3303 | status = batch_dict_exact(self, obj); |
| 3304 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3305 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3306 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3307 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3308 | items = _PyObject_CallMethodIdNoArgs(obj, &PyId_items); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3309 | if (items == NULL) |
| 3310 | goto error; |
| 3311 | iter = PyObject_GetIter(items); |
| 3312 | Py_DECREF(items); |
| 3313 | if (iter == NULL) |
| 3314 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3315 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3316 | Py_DECREF(iter); |
| 3317 | goto error; |
| 3318 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3319 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3320 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3321 | Py_DECREF(iter); |
| 3322 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | if (0) { |
| 3326 | error: |
| 3327 | status = -1; |
| 3328 | } |
| 3329 | |
| 3330 | if (self->fast && !fast_save_leave(self, obj)) |
| 3331 | status = -1; |
| 3332 | |
| 3333 | return status; |
| 3334 | } |
| 3335 | |
| 3336 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3337 | save_set(PicklerObject *self, PyObject *obj) |
| 3338 | { |
| 3339 | PyObject *item; |
| 3340 | int i; |
| 3341 | Py_ssize_t set_size, ppos = 0; |
| 3342 | Py_hash_t hash; |
| 3343 | |
| 3344 | const char empty_set_op = EMPTY_SET; |
| 3345 | const char mark_op = MARK; |
| 3346 | const char additems_op = ADDITEMS; |
| 3347 | |
| 3348 | if (self->proto < 4) { |
| 3349 | PyObject *items; |
| 3350 | PyObject *reduce_value; |
| 3351 | int status; |
| 3352 | |
| 3353 | items = PySequence_List(obj); |
| 3354 | if (items == NULL) { |
| 3355 | return -1; |
| 3356 | } |
| 3357 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 3358 | Py_DECREF(items); |
| 3359 | if (reduce_value == NULL) { |
| 3360 | return -1; |
| 3361 | } |
| 3362 | /* save_reduce() will memoize the object automatically. */ |
| 3363 | status = save_reduce(self, reduce_value, obj); |
| 3364 | Py_DECREF(reduce_value); |
| 3365 | return status; |
| 3366 | } |
| 3367 | |
| 3368 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 3369 | return -1; |
| 3370 | |
| 3371 | if (memo_put(self, obj) < 0) |
| 3372 | return -1; |
| 3373 | |
| 3374 | set_size = PySet_GET_SIZE(obj); |
| 3375 | if (set_size == 0) |
| 3376 | return 0; /* nothing to do */ |
| 3377 | |
| 3378 | /* Write in batches of BATCHSIZE. */ |
| 3379 | do { |
| 3380 | i = 0; |
| 3381 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3382 | return -1; |
| 3383 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 3384 | if (save(self, item, 0) < 0) |
| 3385 | return -1; |
| 3386 | if (++i == BATCHSIZE) |
| 3387 | break; |
| 3388 | } |
| 3389 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 3390 | return -1; |
| 3391 | if (PySet_GET_SIZE(obj) != set_size) { |
| 3392 | PyErr_Format( |
| 3393 | PyExc_RuntimeError, |
| 3394 | "set changed size during iteration"); |
| 3395 | return -1; |
| 3396 | } |
| 3397 | } while (i == BATCHSIZE); |
| 3398 | |
| 3399 | return 0; |
| 3400 | } |
| 3401 | |
| 3402 | static int |
| 3403 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 3404 | { |
| 3405 | PyObject *iter; |
| 3406 | |
| 3407 | const char mark_op = MARK; |
| 3408 | const char frozenset_op = FROZENSET; |
| 3409 | |
| 3410 | if (self->fast && !fast_save_enter(self, obj)) |
| 3411 | return -1; |
| 3412 | |
| 3413 | if (self->proto < 4) { |
| 3414 | PyObject *items; |
| 3415 | PyObject *reduce_value; |
| 3416 | int status; |
| 3417 | |
| 3418 | items = PySequence_List(obj); |
| 3419 | if (items == NULL) { |
| 3420 | return -1; |
| 3421 | } |
| 3422 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3423 | items); |
| 3424 | Py_DECREF(items); |
| 3425 | if (reduce_value == NULL) { |
| 3426 | return -1; |
| 3427 | } |
| 3428 | /* save_reduce() will memoize the object automatically. */ |
| 3429 | status = save_reduce(self, reduce_value, obj); |
| 3430 | Py_DECREF(reduce_value); |
| 3431 | return status; |
| 3432 | } |
| 3433 | |
| 3434 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3435 | return -1; |
| 3436 | |
| 3437 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3438 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3439 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3440 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3441 | for (;;) { |
| 3442 | PyObject *item; |
| 3443 | |
| 3444 | item = PyIter_Next(iter); |
| 3445 | if (item == NULL) { |
| 3446 | if (PyErr_Occurred()) { |
| 3447 | Py_DECREF(iter); |
| 3448 | return -1; |
| 3449 | } |
| 3450 | break; |
| 3451 | } |
| 3452 | if (save(self, item, 0) < 0) { |
| 3453 | Py_DECREF(item); |
| 3454 | Py_DECREF(iter); |
| 3455 | return -1; |
| 3456 | } |
| 3457 | Py_DECREF(item); |
| 3458 | } |
| 3459 | Py_DECREF(iter); |
| 3460 | |
| 3461 | /* If the object is already in the memo, this means it is |
| 3462 | recursive. In this case, throw away everything we put on the |
| 3463 | stack, and fetch the object back from the memo. */ |
| 3464 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3465 | const char pop_mark_op = POP_MARK; |
| 3466 | |
| 3467 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3468 | return -1; |
| 3469 | if (memo_get(self, obj) < 0) |
| 3470 | return -1; |
| 3471 | return 0; |
| 3472 | } |
| 3473 | |
| 3474 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3475 | return -1; |
| 3476 | if (memo_put(self, obj) < 0) |
| 3477 | return -1; |
| 3478 | |
| 3479 | return 0; |
| 3480 | } |
| 3481 | |
| 3482 | static int |
| 3483 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3484 | { |
| 3485 | PyObject *key; |
| 3486 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3487 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3488 | |
| 3489 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3490 | if (key == NULL) |
| 3491 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3492 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3493 | Py_DECREF(key); |
| 3494 | if (item) { |
| 3495 | PyObject *fixed_module_name; |
| 3496 | PyObject *fixed_global_name; |
| 3497 | |
| 3498 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3499 | PyErr_Format(PyExc_RuntimeError, |
| 3500 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3501 | "should be 2-tuples, not %.200s", |
| 3502 | Py_TYPE(item)->tp_name); |
| 3503 | return -1; |
| 3504 | } |
| 3505 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3506 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3507 | if (!PyUnicode_Check(fixed_module_name) || |
| 3508 | !PyUnicode_Check(fixed_global_name)) { |
| 3509 | PyErr_Format(PyExc_RuntimeError, |
| 3510 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3511 | "should be pairs of str, not (%.200s, %.200s)", |
| 3512 | Py_TYPE(fixed_module_name)->tp_name, |
| 3513 | Py_TYPE(fixed_global_name)->tp_name); |
| 3514 | return -1; |
| 3515 | } |
| 3516 | |
| 3517 | Py_CLEAR(*module_name); |
| 3518 | Py_CLEAR(*global_name); |
| 3519 | Py_INCREF(fixed_module_name); |
| 3520 | Py_INCREF(fixed_global_name); |
| 3521 | *module_name = fixed_module_name; |
| 3522 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3523 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3524 | } |
| 3525 | else if (PyErr_Occurred()) { |
| 3526 | return -1; |
| 3527 | } |
| 3528 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3529 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3530 | if (item) { |
| 3531 | if (!PyUnicode_Check(item)) { |
| 3532 | PyErr_Format(PyExc_RuntimeError, |
| 3533 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3534 | "should be strings, not %.200s", |
| 3535 | Py_TYPE(item)->tp_name); |
| 3536 | return -1; |
| 3537 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3538 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3539 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3540 | } |
| 3541 | else if (PyErr_Occurred()) { |
| 3542 | return -1; |
| 3543 | } |
| 3544 | |
| 3545 | return 0; |
| 3546 | } |
| 3547 | |
| 3548 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3549 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3550 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3551 | PyObject *global_name = NULL; |
| 3552 | PyObject *module_name = NULL; |
| 3553 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3554 | PyObject *parent = NULL; |
| 3555 | PyObject *dotted_path = NULL; |
| 3556 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3557 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3558 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3559 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3560 | _Py_IDENTIFIER(__name__); |
| 3561 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3562 | |
| 3563 | const char global_op = GLOBAL; |
| 3564 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3565 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3566 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3567 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3568 | } |
| 3569 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3570 | if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0) |
| 3571 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3572 | if (global_name == NULL) { |
| 3573 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3574 | if (global_name == NULL) |
| 3575 | goto error; |
| 3576 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3579 | dotted_path = get_dotted_path(module, global_name); |
| 3580 | if (dotted_path == NULL) |
| 3581 | goto error; |
| 3582 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3583 | if (module_name == NULL) |
| 3584 | goto error; |
| 3585 | |
| 3586 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3587 | relative imports. |
| 3588 | |
| 3589 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3590 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3591 | custom import functions (IMHO, this would be a nice security |
| 3592 | feature). The import C API would need to be extended to support the |
| 3593 | extra parameters of __import__ to fix that. */ |
| 3594 | module = PyImport_Import(module_name); |
| 3595 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3596 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3597 | "Can't pickle %R: import of module %R failed", |
| 3598 | obj, module_name); |
| 3599 | goto error; |
| 3600 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3601 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3602 | Py_INCREF(lastname); |
| 3603 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3604 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3605 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3606 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3607 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3608 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3609 | goto error; |
| 3610 | } |
| 3611 | if (cls != obj) { |
| 3612 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3613 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3614 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3615 | obj, module_name, global_name); |
| 3616 | goto error; |
| 3617 | } |
| 3618 | Py_DECREF(cls); |
| 3619 | |
| 3620 | if (self->proto >= 2) { |
| 3621 | /* See whether this is in the extension registry, and if |
| 3622 | * so generate an EXT opcode. |
| 3623 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3624 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3625 | PyObject *code_obj; /* extension code as Python object */ |
| 3626 | long code; /* extension code as C value */ |
| 3627 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3628 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3629 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3630 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3631 | if (extension_key == NULL) { |
| 3632 | goto error; |
| 3633 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3634 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3635 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3636 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3637 | /* The object is not registered in the extension registry. |
| 3638 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3639 | if (code_obj == NULL) { |
| 3640 | if (PyErr_Occurred()) { |
| 3641 | goto error; |
| 3642 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3643 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3644 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3645 | |
| 3646 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3647 | of the value returned by the extension_registry. It should for |
| 3648 | consistency. */ |
| 3649 | |
| 3650 | /* Verify code_obj has the right type and value. */ |
| 3651 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3652 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3653 | "Can't pickle %R: extension code %R isn't an integer", |
| 3654 | obj, code_obj); |
| 3655 | goto error; |
| 3656 | } |
| 3657 | code = PyLong_AS_LONG(code_obj); |
| 3658 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3659 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3660 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3661 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3662 | goto error; |
| 3663 | } |
| 3664 | |
| 3665 | /* Generate an EXT opcode. */ |
| 3666 | if (code <= 0xff) { |
| 3667 | pdata[0] = EXT1; |
| 3668 | pdata[1] = (unsigned char)code; |
| 3669 | n = 2; |
| 3670 | } |
| 3671 | else if (code <= 0xffff) { |
| 3672 | pdata[0] = EXT2; |
| 3673 | pdata[1] = (unsigned char)(code & 0xff); |
| 3674 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3675 | n = 3; |
| 3676 | } |
| 3677 | else { |
| 3678 | pdata[0] = EXT4; |
| 3679 | pdata[1] = (unsigned char)(code & 0xff); |
| 3680 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3681 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3682 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3683 | n = 5; |
| 3684 | } |
| 3685 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3686 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3687 | goto error; |
| 3688 | } |
| 3689 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3690 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3691 | if (parent == module) { |
| 3692 | Py_INCREF(lastname); |
| 3693 | Py_DECREF(global_name); |
| 3694 | global_name = lastname; |
| 3695 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3696 | if (self->proto >= 4) { |
| 3697 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3698 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3699 | if (save(self, module_name, 0) < 0) |
| 3700 | goto error; |
| 3701 | if (save(self, global_name, 0) < 0) |
| 3702 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3703 | |
| 3704 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3705 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3706 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3707 | else if (parent != module) { |
| 3708 | PickleState *st = _Pickle_GetGlobalState(); |
| 3709 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3710 | st->getattr, parent, lastname); |
Alexey Izbyshev | f8c06b0 | 2018-08-22 07:51:25 +0300 | [diff] [blame] | 3711 | if (reduce_value == NULL) |
| 3712 | goto error; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3713 | status = save_reduce(self, reduce_value, NULL); |
| 3714 | Py_DECREF(reduce_value); |
| 3715 | if (status < 0) |
| 3716 | goto error; |
| 3717 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3718 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3719 | /* Generate a normal global opcode if we are using a pickle |
| 3720 | protocol < 4, or if the object is not registered in the |
| 3721 | extension registry. */ |
| 3722 | PyObject *encoded; |
| 3723 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3724 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3725 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3726 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3727 | |
| 3728 | /* For protocol < 3 and if the user didn't request against doing |
| 3729 | so, we convert module names to the old 2.x module names. */ |
| 3730 | if (self->proto < 3 && self->fix_imports) { |
| 3731 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3732 | goto error; |
| 3733 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3734 | } |
| 3735 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3736 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3737 | both the module name and the global name using UTF-8. We do so |
| 3738 | only when we are using the pickle protocol newer than version |
| 3739 | 3. This is to ensure compatibility with older Unpickler running |
| 3740 | on Python 2.x. */ |
| 3741 | if (self->proto == 3) { |
| 3742 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3743 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3744 | else { |
| 3745 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3746 | } |
| 3747 | encoded = unicode_encoder(module_name); |
| 3748 | if (encoded == NULL) { |
| 3749 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3750 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3751 | "can't pickle module identifier '%S' using " |
| 3752 | "pickle protocol %i", |
| 3753 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3754 | goto error; |
| 3755 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3756 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3757 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3758 | Py_DECREF(encoded); |
| 3759 | goto error; |
| 3760 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3761 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3762 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3763 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3764 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3765 | /* Save the name of the module. */ |
| 3766 | encoded = unicode_encoder(global_name); |
| 3767 | if (encoded == NULL) { |
| 3768 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3769 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3770 | "can't pickle global identifier '%S' using " |
| 3771 | "pickle protocol %i", |
| 3772 | global_name, self->proto); |
| 3773 | goto error; |
| 3774 | } |
| 3775 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3776 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3777 | Py_DECREF(encoded); |
| 3778 | goto error; |
| 3779 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3780 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3781 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3782 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3783 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3784 | /* Memoize the object. */ |
| 3785 | if (memo_put(self, obj) < 0) |
| 3786 | goto error; |
| 3787 | } |
| 3788 | |
| 3789 | if (0) { |
| 3790 | error: |
| 3791 | status = -1; |
| 3792 | } |
| 3793 | Py_XDECREF(module_name); |
| 3794 | Py_XDECREF(global_name); |
| 3795 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3796 | Py_XDECREF(parent); |
| 3797 | Py_XDECREF(dotted_path); |
| 3798 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3799 | |
| 3800 | return status; |
| 3801 | } |
| 3802 | |
| 3803 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3804 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3805 | { |
| 3806 | PyObject *reduce_value; |
| 3807 | int status; |
| 3808 | |
| 3809 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3810 | if (reduce_value == NULL) { |
| 3811 | return -1; |
| 3812 | } |
| 3813 | status = save_reduce(self, reduce_value, obj); |
| 3814 | Py_DECREF(reduce_value); |
| 3815 | return status; |
| 3816 | } |
| 3817 | |
| 3818 | static int |
| 3819 | save_type(PicklerObject *self, PyObject *obj) |
| 3820 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3821 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3822 | return save_singleton_type(self, obj, Py_None); |
| 3823 | } |
| 3824 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3825 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3826 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3827 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3828 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3829 | } |
| 3830 | return save_global(self, obj, NULL); |
| 3831 | } |
| 3832 | |
| 3833 | static int |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3834 | save_pers(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3835 | { |
| 3836 | PyObject *pid = NULL; |
| 3837 | int status = 0; |
| 3838 | |
| 3839 | const char persid_op = PERSID; |
| 3840 | const char binpersid_op = BINPERSID; |
| 3841 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3842 | pid = call_method(self->pers_func, self->pers_func_self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3843 | if (pid == NULL) |
| 3844 | return -1; |
| 3845 | |
| 3846 | if (pid != Py_None) { |
| 3847 | if (self->bin) { |
| 3848 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3849 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3850 | goto error; |
| 3851 | } |
| 3852 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3853 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3854 | |
| 3855 | pid_str = PyObject_Str(pid); |
| 3856 | if (pid_str == NULL) |
| 3857 | goto error; |
| 3858 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3859 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3860 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3861 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3862 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3863 | "persistent IDs in protocol 0 must be " |
| 3864 | "ASCII strings"); |
| 3865 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3866 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3867 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3868 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3869 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3870 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3871 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3872 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3873 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3874 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3875 | } |
| 3876 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3877 | } |
| 3878 | status = 1; |
| 3879 | } |
| 3880 | |
| 3881 | if (0) { |
| 3882 | error: |
| 3883 | status = -1; |
| 3884 | } |
| 3885 | Py_XDECREF(pid); |
| 3886 | |
| 3887 | return status; |
| 3888 | } |
| 3889 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3890 | static PyObject * |
| 3891 | get_class(PyObject *obj) |
| 3892 | { |
| 3893 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3894 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3895 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3896 | if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) { |
| 3897 | cls = (PyObject *) Py_TYPE(obj); |
| 3898 | Py_INCREF(cls); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3899 | } |
| 3900 | return cls; |
| 3901 | } |
| 3902 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3903 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3904 | * appropriate __reduce__ method for obj. |
| 3905 | */ |
| 3906 | static int |
| 3907 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3908 | { |
| 3909 | PyObject *callable; |
| 3910 | PyObject *argtup; |
| 3911 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3912 | PyObject *listitems = Py_None; |
| 3913 | PyObject *dictitems = Py_None; |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3914 | PyObject *state_setter = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3915 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3916 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3917 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3918 | |
| 3919 | const char reduce_op = REDUCE; |
| 3920 | const char build_op = BUILD; |
| 3921 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3922 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3923 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3924 | size = PyTuple_Size(args); |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3925 | if (size < 2 || size > 6) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3926 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3927 | "__reduce__ must contain 2 through 6 elements"); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3928 | return -1; |
| 3929 | } |
| 3930 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3931 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6, |
| 3932 | &callable, &argtup, &state, &listitems, &dictitems, |
| 3933 | &state_setter)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3934 | return -1; |
| 3935 | |
| 3936 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3937 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3938 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3939 | return -1; |
| 3940 | } |
| 3941 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3942 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3943 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3944 | return -1; |
| 3945 | } |
| 3946 | |
| 3947 | if (state == Py_None) |
| 3948 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3949 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3950 | if (listitems == Py_None) |
| 3951 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3952 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3953 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3954 | "returned by __reduce__ must be an iterator, not %s", |
| 3955 | Py_TYPE(listitems)->tp_name); |
| 3956 | return -1; |
| 3957 | } |
| 3958 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3959 | if (dictitems == Py_None) |
| 3960 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3961 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3962 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3963 | "returned by __reduce__ must be an iterator, not %s", |
| 3964 | Py_TYPE(dictitems)->tp_name); |
| 3965 | return -1; |
| 3966 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3967 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3968 | if (state_setter == Py_None) |
| 3969 | state_setter = NULL; |
| 3970 | else if (!PyCallable_Check(state_setter)) { |
| 3971 | PyErr_Format(st->PicklingError, "sixth element of the tuple " |
| 3972 | "returned by __reduce__ must be a function, not %s", |
| 3973 | Py_TYPE(state_setter)->tp_name); |
| 3974 | return -1; |
| 3975 | } |
| 3976 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3977 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3978 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3979 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3980 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3981 | if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) { |
| 3982 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3983 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3984 | if (name != NULL && PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3985 | _Py_IDENTIFIER(__newobj_ex__); |
Serhiy Storchaka | f0f35a6 | 2017-01-09 10:09:43 +0200 | [diff] [blame] | 3986 | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
| 3987 | name, &PyId___newobj_ex__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3988 | if (!use_newobj_ex) { |
| 3989 | _Py_IDENTIFIER(__newobj__); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 3990 | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3991 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3992 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3993 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3994 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3995 | |
| 3996 | if (use_newobj_ex) { |
| 3997 | PyObject *cls; |
| 3998 | PyObject *args; |
| 3999 | PyObject *kwargs; |
| 4000 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4001 | if (PyTuple_GET_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4002 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4003 | "length of the NEWOBJ_EX argument tuple must be " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4004 | "exactly 3, not %zd", PyTuple_GET_SIZE(argtup)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4005 | return -1; |
| 4006 | } |
| 4007 | |
| 4008 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 4009 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4010 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4011 | "first item from NEWOBJ_EX argument tuple must " |
| 4012 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 4013 | return -1; |
| 4014 | } |
| 4015 | args = PyTuple_GET_ITEM(argtup, 1); |
| 4016 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4017 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4018 | "second item from NEWOBJ_EX argument tuple must " |
| 4019 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 4020 | return -1; |
| 4021 | } |
| 4022 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 4023 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4024 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4025 | "third item from NEWOBJ_EX argument tuple must " |
| 4026 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 4027 | return -1; |
| 4028 | } |
| 4029 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4030 | if (self->proto >= 4) { |
| 4031 | if (save(self, cls, 0) < 0 || |
| 4032 | save(self, args, 0) < 0 || |
| 4033 | save(self, kwargs, 0) < 0 || |
| 4034 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 4035 | return -1; |
| 4036 | } |
| 4037 | } |
| 4038 | else { |
| 4039 | PyObject *newargs; |
| 4040 | PyObject *cls_new; |
| 4041 | Py_ssize_t i; |
| 4042 | _Py_IDENTIFIER(__new__); |
| 4043 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4044 | newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4045 | if (newargs == NULL) |
| 4046 | return -1; |
| 4047 | |
| 4048 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 4049 | if (cls_new == NULL) { |
| 4050 | Py_DECREF(newargs); |
| 4051 | return -1; |
| 4052 | } |
| 4053 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 4054 | Py_INCREF(cls); |
| 4055 | PyTuple_SET_ITEM(newargs, 1, cls); |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4056 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4057 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 4058 | Py_INCREF(item); |
| 4059 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 4060 | } |
| 4061 | |
| 4062 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 4063 | Py_DECREF(newargs); |
| 4064 | if (callable == NULL) |
| 4065 | return -1; |
| 4066 | |
| 4067 | newargs = PyTuple_New(0); |
| 4068 | if (newargs == NULL) { |
| 4069 | Py_DECREF(callable); |
| 4070 | return -1; |
| 4071 | } |
| 4072 | |
| 4073 | if (save(self, callable, 0) < 0 || |
| 4074 | save(self, newargs, 0) < 0 || |
| 4075 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 4076 | Py_DECREF(newargs); |
| 4077 | Py_DECREF(callable); |
| 4078 | return -1; |
| 4079 | } |
| 4080 | Py_DECREF(newargs); |
| 4081 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4082 | } |
| 4083 | } |
| 4084 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4085 | PyObject *cls; |
| 4086 | PyObject *newargtup; |
| 4087 | PyObject *obj_class; |
| 4088 | int p; |
| 4089 | |
| 4090 | /* Sanity checks. */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4091 | if (PyTuple_GET_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4092 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4093 | return -1; |
| 4094 | } |
| 4095 | |
| 4096 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4097 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4098 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4099 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4100 | return -1; |
| 4101 | } |
| 4102 | |
| 4103 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4104 | obj_class = get_class(obj); |
Zackery Spytz | 25d3897 | 2018-12-05 11:29:20 -0700 | [diff] [blame] | 4105 | if (obj_class == NULL) { |
| 4106 | return -1; |
| 4107 | } |
| 4108 | p = obj_class != cls; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4109 | Py_DECREF(obj_class); |
| 4110 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4111 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4112 | "__newobj__ args has the wrong class"); |
| 4113 | return -1; |
| 4114 | } |
| 4115 | } |
| 4116 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 4117 | what happen if the value returned by the __reduce__() method of |
| 4118 | some extension type contains another object of the same type. Ouch! |
| 4119 | |
| 4120 | Here is a quick example, that I ran into, to illustrate what I |
| 4121 | mean: |
| 4122 | |
| 4123 | >>> import pickle, copyreg |
| 4124 | >>> copyreg.dispatch_table.pop(complex) |
| 4125 | >>> pickle.dumps(1+2j) |
| 4126 | Traceback (most recent call last): |
| 4127 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 4128 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4129 | |
| 4130 | Removing the complex class from copyreg.dispatch_table made the |
| 4131 | __reduce_ex__() method emit another complex object: |
| 4132 | |
| 4133 | >>> (1+1j).__reduce_ex__(2) |
| 4134 | (<function __newobj__ at 0xb7b71c3c>, |
| 4135 | (<class 'complex'>, (1+1j)), None, None, None) |
| 4136 | |
| 4137 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 4138 | ensued. Of course, the bug was in the complex class which had a |
| 4139 | broken __getnewargs__() that emitted another complex object. But, |
| 4140 | the point, here, is it is quite easy to end up with a broken reduce |
| 4141 | function. */ |
| 4142 | |
| 4143 | /* Save the class and its __new__ arguments. */ |
| 4144 | if (save(self, cls, 0) < 0) |
| 4145 | return -1; |
| 4146 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4147 | newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4148 | if (newargtup == NULL) |
| 4149 | return -1; |
| 4150 | |
| 4151 | p = save(self, newargtup, 0); |
| 4152 | Py_DECREF(newargtup); |
| 4153 | if (p < 0) |
| 4154 | return -1; |
| 4155 | |
| 4156 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4157 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4158 | return -1; |
| 4159 | } |
| 4160 | else { /* Not using NEWOBJ. */ |
| 4161 | if (save(self, callable, 0) < 0 || |
| 4162 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4163 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4164 | return -1; |
| 4165 | } |
| 4166 | |
| 4167 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 4168 | the caller do not want to memoize the object. Not particularly useful, |
| 4169 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 4170 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4171 | if (obj != NULL) { |
| 4172 | /* If the object is already in the memo, this means it is |
| 4173 | recursive. In this case, throw away everything we put on the |
| 4174 | stack, and fetch the object back from the memo. */ |
| 4175 | if (PyMemoTable_Get(self->memo, obj)) { |
| 4176 | const char pop_op = POP; |
| 4177 | |
| 4178 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 4179 | return -1; |
| 4180 | if (memo_get(self, obj) < 0) |
| 4181 | return -1; |
| 4182 | |
| 4183 | return 0; |
| 4184 | } |
| 4185 | else if (memo_put(self, obj) < 0) |
| 4186 | return -1; |
| 4187 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4188 | |
| 4189 | if (listitems && batch_list(self, listitems) < 0) |
| 4190 | return -1; |
| 4191 | |
| 4192 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 4193 | return -1; |
| 4194 | |
| 4195 | if (state) { |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 4196 | if (state_setter == NULL) { |
| 4197 | if (save(self, state, 0) < 0 || |
| 4198 | _Pickler_Write(self, &build_op, 1) < 0) |
| 4199 | return -1; |
| 4200 | } |
| 4201 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4202 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 4203 | /* If a state_setter is specified, call it instead of load_build to |
| 4204 | * update obj's with its previous state. |
| 4205 | * The first 4 save/write instructions push state_setter and its |
| 4206 | * tuple of expected arguments (obj, state) onto the stack. The |
| 4207 | * REDUCE opcode triggers the state_setter(obj, state) function |
| 4208 | * call. Finally, because state-updating routines only do in-place |
| 4209 | * modification, the whole operation has to be stack-transparent. |
| 4210 | * Thus, we finally pop the call's output from the stack.*/ |
| 4211 | |
| 4212 | const char tupletwo_op = TUPLE2; |
| 4213 | const char pop_op = POP; |
| 4214 | if (save(self, state_setter, 0) < 0 || |
| 4215 | save(self, obj, 0) < 0 || save(self, state, 0) < 0 || |
| 4216 | _Pickler_Write(self, &tupletwo_op, 1) < 0 || |
| 4217 | _Pickler_Write(self, &reduce_op, 1) < 0 || |
| 4218 | _Pickler_Write(self, &pop_op, 1) < 0) |
| 4219 | return -1; |
| 4220 | } |
| 4221 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4222 | return 0; |
| 4223 | } |
| 4224 | |
| 4225 | static int |
| 4226 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 4227 | { |
| 4228 | PyTypeObject *type; |
| 4229 | PyObject *reduce_func = NULL; |
| 4230 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4231 | int status = 0; |
| 4232 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4233 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 4234 | return -1; |
| 4235 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4236 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 4237 | on its returned object. */ |
| 4238 | if (!pers_save && self->pers_func) { |
| 4239 | /* save_pers() returns: |
| 4240 | -1 to signal an error; |
| 4241 | 0 if it did nothing successfully; |
| 4242 | 1 if a persistent id was saved. |
| 4243 | */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4244 | if ((status = save_pers(self, obj)) != 0) |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4245 | return status; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4246 | } |
| 4247 | |
| 4248 | type = Py_TYPE(obj); |
| 4249 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4250 | /* The old cPickle had an optimization that used switch-case statement |
| 4251 | dispatching on the first letter of the type name. This has was removed |
| 4252 | since benchmarks shown that this optimization was actually slowing |
| 4253 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4254 | |
| 4255 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 4256 | |
| 4257 | if (obj == Py_None) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4258 | return save_none(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4259 | } |
| 4260 | else if (obj == Py_False || obj == Py_True) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4261 | return save_bool(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4262 | } |
| 4263 | else if (type == &PyLong_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4264 | return save_long(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4265 | } |
| 4266 | else if (type == &PyFloat_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4267 | return save_float(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4268 | } |
| 4269 | |
| 4270 | /* Check the memo to see if it has the object. If so, generate |
| 4271 | a GET (or BINGET) opcode, instead of pickling the object |
| 4272 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4273 | if (PyMemoTable_Get(self->memo, obj)) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4274 | return memo_get(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4275 | } |
| 4276 | |
| 4277 | if (type == &PyBytes_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4278 | return save_bytes(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4279 | } |
| 4280 | else if (type == &PyUnicode_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4281 | return save_unicode(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4282 | } |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4283 | |
| 4284 | /* We're only calling Py_EnterRecursiveCall here so that atomic |
| 4285 | types above are pickled faster. */ |
| 4286 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 4287 | return -1; |
| 4288 | } |
| 4289 | |
| 4290 | if (type == &PyDict_Type) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4291 | status = save_dict(self, obj); |
| 4292 | goto done; |
| 4293 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4294 | else if (type == &PySet_Type) { |
| 4295 | status = save_set(self, obj); |
| 4296 | goto done; |
| 4297 | } |
| 4298 | else if (type == &PyFrozenSet_Type) { |
| 4299 | status = save_frozenset(self, obj); |
| 4300 | goto done; |
| 4301 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4302 | else if (type == &PyList_Type) { |
| 4303 | status = save_list(self, obj); |
| 4304 | goto done; |
| 4305 | } |
| 4306 | else if (type == &PyTuple_Type) { |
| 4307 | status = save_tuple(self, obj); |
| 4308 | goto done; |
| 4309 | } |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4310 | else if (type == &PyByteArray_Type) { |
| 4311 | status = save_bytearray(self, obj); |
| 4312 | goto done; |
| 4313 | } |
| 4314 | else if (type == &PyPickleBuffer_Type) { |
| 4315 | status = save_picklebuffer(self, obj); |
| 4316 | goto done; |
| 4317 | } |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4318 | |
| 4319 | /* Now, check reducer_override. If it returns NotImplemented, |
| 4320 | * fallback to save_type or save_global, and then perhaps to the |
| 4321 | * regular reduction mechanism. |
| 4322 | */ |
| 4323 | if (self->reducer_override != NULL) { |
Jeroen Demeyer | 196a530 | 2019-07-04 12:31:34 +0200 | [diff] [blame] | 4324 | reduce_value = _PyObject_CallOneArg(self->reducer_override, obj); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4325 | if (reduce_value == NULL) { |
| 4326 | goto error; |
| 4327 | } |
| 4328 | if (reduce_value != Py_NotImplemented) { |
| 4329 | goto reduce; |
| 4330 | } |
| 4331 | Py_DECREF(reduce_value); |
| 4332 | reduce_value = NULL; |
| 4333 | } |
| 4334 | |
| 4335 | if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 4336 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4337 | goto done; |
| 4338 | } |
| 4339 | else if (type == &PyFunction_Type) { |
| 4340 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 4341 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4342 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4343 | |
| 4344 | /* XXX: This part needs some unit tests. */ |
| 4345 | |
| 4346 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4347 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 4348 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4349 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4350 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4351 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4352 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 4353 | (PyObject *)type); |
| 4354 | if (reduce_func == NULL) { |
| 4355 | if (PyErr_Occurred()) { |
| 4356 | goto error; |
| 4357 | } |
| 4358 | } else { |
| 4359 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 4360 | Increase the reference count to be consistent with |
| 4361 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 4362 | Py_INCREF(reduce_func); |
| 4363 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4364 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4365 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 4366 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4367 | if (reduce_func == NULL) { |
| 4368 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4369 | PyErr_Clear(); |
| 4370 | else |
| 4371 | goto error; |
| 4372 | } |
| 4373 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4374 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4375 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4376 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4377 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 4378 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 4379 | status = save_global(self, obj, NULL); |
| 4380 | goto done; |
| 4381 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4382 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4383 | _Py_IDENTIFIER(__reduce__); |
| 4384 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4385 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4386 | |
| 4387 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 4388 | automatically defined as __reduce__. While this is convenient, this |
| 4389 | make it impossible to know which method was actually called. Of |
| 4390 | course, this is not a big deal. But still, it would be nice to let |
| 4391 | the user know which method was called when something go |
| 4392 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 4393 | don't actually have to check for a __reduce__ method. */ |
| 4394 | |
| 4395 | /* Check for a __reduce_ex__ method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4396 | if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) { |
| 4397 | goto error; |
| 4398 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4399 | if (reduce_func != NULL) { |
| 4400 | PyObject *proto; |
| 4401 | proto = PyLong_FromLong(self->proto); |
| 4402 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4403 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4404 | } |
| 4405 | } |
| 4406 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4407 | PickleState *st = _Pickle_GetGlobalState(); |
| 4408 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4409 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4410 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4411 | if (reduce_func != NULL) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 4412 | reduce_value = PyObject_CallNoArgs(reduce_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4413 | } |
| 4414 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4415 | PyErr_Format(st->PicklingError, |
| 4416 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4417 | type->tp_name, obj); |
| 4418 | goto error; |
| 4419 | } |
| 4420 | } |
| 4421 | } |
| 4422 | |
| 4423 | if (reduce_value == NULL) |
| 4424 | goto error; |
| 4425 | |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4426 | reduce: |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4427 | if (PyUnicode_Check(reduce_value)) { |
| 4428 | status = save_global(self, obj, reduce_value); |
| 4429 | goto done; |
| 4430 | } |
| 4431 | |
| 4432 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4433 | PickleState *st = _Pickle_GetGlobalState(); |
| 4434 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4435 | "__reduce__ must return a string or tuple"); |
| 4436 | goto error; |
| 4437 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4438 | |
| 4439 | status = save_reduce(self, reduce_value, obj); |
| 4440 | |
| 4441 | if (0) { |
| 4442 | error: |
| 4443 | status = -1; |
| 4444 | } |
| 4445 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4446 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 4447 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4448 | Py_XDECREF(reduce_func); |
| 4449 | Py_XDECREF(reduce_value); |
| 4450 | |
| 4451 | return status; |
| 4452 | } |
| 4453 | |
| 4454 | static int |
| 4455 | dump(PicklerObject *self, PyObject *obj) |
| 4456 | { |
| 4457 | const char stop_op = STOP; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4458 | PyObject *tmp; |
| 4459 | _Py_IDENTIFIER(reducer_override); |
| 4460 | |
| 4461 | if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override, |
| 4462 | &tmp) < 0) { |
| 4463 | return -1; |
| 4464 | } |
| 4465 | /* Cache the reducer_override method, if it exists. */ |
| 4466 | if (tmp != NULL) { |
| 4467 | Py_XSETREF(self->reducer_override, tmp); |
| 4468 | } |
| 4469 | else { |
| 4470 | Py_CLEAR(self->reducer_override); |
| 4471 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4472 | |
| 4473 | if (self->proto >= 2) { |
| 4474 | char header[2]; |
| 4475 | |
| 4476 | header[0] = PROTO; |
| 4477 | assert(self->proto >= 0 && self->proto < 256); |
| 4478 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4479 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4480 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4481 | if (self->proto >= 4) |
| 4482 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4483 | } |
| 4484 | |
| 4485 | if (save(self, obj, 0) < 0 || |
Serhiy Storchaka | c869529 | 2018-04-04 00:11:27 +0300 | [diff] [blame] | 4486 | _Pickler_Write(self, &stop_op, 1) < 0 || |
| 4487 | _Pickler_CommitFrame(self) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4488 | return -1; |
Serhiy Storchaka | c869529 | 2018-04-04 00:11:27 +0300 | [diff] [blame] | 4489 | self->framing = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4490 | return 0; |
| 4491 | } |
| 4492 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4493 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4494 | |
| 4495 | _pickle.Pickler.clear_memo |
| 4496 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4497 | Clears the pickler's "memo". |
| 4498 | |
| 4499 | The memo is the data structure that remembers which objects the |
| 4500 | pickler has already seen, so that shared or recursive objects are |
| 4501 | pickled by reference and not by value. This method is useful when |
| 4502 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4503 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4504 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4505 | static PyObject * |
| 4506 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4507 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4508 | { |
| 4509 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4510 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4511 | |
| 4512 | Py_RETURN_NONE; |
| 4513 | } |
| 4514 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4515 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4516 | |
| 4517 | _pickle.Pickler.dump |
| 4518 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4519 | obj: object |
| 4520 | / |
| 4521 | |
| 4522 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4523 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4524 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4525 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4526 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4527 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4528 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4529 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4530 | Developers often forget to call __init__() in their subclasses, which |
| 4531 | would trigger a segfault without this check. */ |
| 4532 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4533 | PickleState *st = _Pickle_GetGlobalState(); |
| 4534 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4535 | "Pickler.__init__() was not called by %s.__init__()", |
| 4536 | Py_TYPE(self)->tp_name); |
| 4537 | return NULL; |
| 4538 | } |
| 4539 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4540 | if (_Pickler_ClearBuffer(self) < 0) |
| 4541 | return NULL; |
| 4542 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4543 | if (dump(self, obj) < 0) |
| 4544 | return NULL; |
| 4545 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4546 | if (_Pickler_FlushToFile(self) < 0) |
| 4547 | return NULL; |
| 4548 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4549 | Py_RETURN_NONE; |
| 4550 | } |
| 4551 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4552 | /*[clinic input] |
| 4553 | |
| 4554 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4555 | |
| 4556 | Returns size in memory, in bytes. |
| 4557 | [clinic start generated code]*/ |
| 4558 | |
| 4559 | static Py_ssize_t |
| 4560 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4561 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4562 | { |
| 4563 | Py_ssize_t res, s; |
| 4564 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4565 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4566 | if (self->memo != NULL) { |
| 4567 | res += sizeof(PyMemoTable); |
| 4568 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4569 | } |
| 4570 | if (self->output_buffer != NULL) { |
| 4571 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4572 | if (s == -1) |
| 4573 | return -1; |
| 4574 | res += s; |
| 4575 | } |
| 4576 | return res; |
| 4577 | } |
| 4578 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4579 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4580 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4581 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4582 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4583 | {NULL, NULL} /* sentinel */ |
| 4584 | }; |
| 4585 | |
| 4586 | static void |
| 4587 | Pickler_dealloc(PicklerObject *self) |
| 4588 | { |
| 4589 | PyObject_GC_UnTrack(self); |
| 4590 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4591 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4592 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4593 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4594 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4595 | Py_XDECREF(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4596 | Py_XDECREF(self->reducer_override); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4597 | Py_XDECREF(self->buffer_callback); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4598 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4599 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4600 | |
| 4601 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4602 | } |
| 4603 | |
| 4604 | static int |
| 4605 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4606 | { |
| 4607 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4608 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4609 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4610 | Py_VISIT(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4611 | Py_VISIT(self->reducer_override); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4612 | Py_VISIT(self->buffer_callback); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4613 | return 0; |
| 4614 | } |
| 4615 | |
| 4616 | static int |
| 4617 | Pickler_clear(PicklerObject *self) |
| 4618 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4619 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4620 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4621 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4622 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4623 | Py_CLEAR(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4624 | Py_CLEAR(self->reducer_override); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4625 | Py_CLEAR(self->buffer_callback); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4626 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4627 | if (self->memo != NULL) { |
| 4628 | PyMemoTable *memo = self->memo; |
| 4629 | self->memo = NULL; |
| 4630 | PyMemoTable_Del(memo); |
| 4631 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4632 | return 0; |
| 4633 | } |
| 4634 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4635 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4636 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4637 | |
| 4638 | _pickle.Pickler.__init__ |
| 4639 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4640 | file: object |
| 4641 | protocol: object = NULL |
| 4642 | fix_imports: bool = True |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4643 | buffer_callback: object = NULL |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4644 | |
| 4645 | This takes a binary file for writing a pickle data stream. |
| 4646 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4647 | The optional *protocol* argument tells the pickler to use the given |
| 4648 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4649 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4650 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4651 | Specifying a negative protocol version selects the highest protocol |
| 4652 | version supported. The higher the protocol used, the more recent the |
| 4653 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4654 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4655 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4656 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4657 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4658 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4659 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4660 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4661 | to map the new Python 3 names to the old module names used in Python |
| 4662 | 2, so that the pickle data stream is readable with Python 2. |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4663 | |
| 4664 | If *buffer_callback* is None (the default), buffer views are |
| 4665 | serialized into *file* as part of the pickle stream. |
| 4666 | |
| 4667 | If *buffer_callback* is not None, then it can be called any number |
| 4668 | of times with a buffer view. If the callback returns a false value |
| 4669 | (such as None), the given buffer is out-of-band; otherwise the |
| 4670 | buffer is serialized in-band, i.e. inside the pickle stream. |
| 4671 | |
| 4672 | It is an error if *buffer_callback* is not None and *protocol* |
| 4673 | is None or smaller than 5. |
| 4674 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4675 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4676 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4677 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4678 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4679 | PyObject *protocol, int fix_imports, |
| 4680 | PyObject *buffer_callback) |
| 4681 | /*[clinic end generated code: output=0abedc50590d259b input=9a43a1c50ab91652]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4682 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4683 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4684 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4685 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4686 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4687 | if (self->write != NULL) |
| 4688 | (void)Pickler_clear(self); |
| 4689 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4690 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4691 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4692 | |
| 4693 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4694 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4695 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4696 | if (_Pickler_SetBufferCallback(self, buffer_callback) < 0) |
| 4697 | return -1; |
| 4698 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4699 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4700 | if (self->memo == NULL) { |
| 4701 | self->memo = PyMemoTable_New(); |
| 4702 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4703 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4704 | } |
| 4705 | self->output_len = 0; |
| 4706 | if (self->output_buffer == NULL) { |
| 4707 | self->max_output_len = WRITE_BUF_SIZE; |
| 4708 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4709 | self->max_output_len); |
| 4710 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4711 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4712 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4713 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4714 | self->fast = 0; |
| 4715 | self->fast_nesting = 0; |
| 4716 | self->fast_memo = NULL; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4717 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4718 | if (init_method_ref((PyObject *)self, &PyId_persistent_id, |
| 4719 | &self->pers_func, &self->pers_func_self) < 0) |
| 4720 | { |
| 4721 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4722 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4723 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4724 | if (_PyObject_LookupAttrId((PyObject *)self, |
| 4725 | &PyId_dispatch_table, &self->dispatch_table) < 0) { |
| 4726 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4727 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4728 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4729 | return 0; |
| 4730 | } |
| 4731 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4732 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4733 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4734 | * avoid breaking code like: |
| 4735 | * pickler.memo.clear() |
| 4736 | * and |
| 4737 | * pickler.memo = saved_memo |
| 4738 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4739 | * it. Note that we don't implement the entire mapping API here. This is |
| 4740 | * intentional, as these should be treated as black-box implementation details. |
| 4741 | */ |
| 4742 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4743 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4744 | _pickle.PicklerMemoProxy.clear |
| 4745 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4746 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4747 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4748 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4749 | static PyObject * |
| 4750 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4751 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4752 | { |
| 4753 | if (self->pickler->memo) |
| 4754 | PyMemoTable_Clear(self->pickler->memo); |
| 4755 | Py_RETURN_NONE; |
| 4756 | } |
| 4757 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4758 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4759 | _pickle.PicklerMemoProxy.copy |
| 4760 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4761 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4762 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4763 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4764 | static PyObject * |
| 4765 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4766 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4767 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4768 | PyMemoTable *memo; |
| 4769 | PyObject *new_memo = PyDict_New(); |
| 4770 | if (new_memo == NULL) |
| 4771 | return NULL; |
| 4772 | |
| 4773 | memo = self->pickler->memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 4774 | for (size_t i = 0; i < memo->mt_allocated; ++i) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4775 | PyMemoEntry entry = memo->mt_table[i]; |
| 4776 | if (entry.me_key != NULL) { |
| 4777 | int status; |
| 4778 | PyObject *key, *value; |
| 4779 | |
| 4780 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4781 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4782 | |
| 4783 | if (key == NULL || value == NULL) { |
| 4784 | Py_XDECREF(key); |
| 4785 | Py_XDECREF(value); |
| 4786 | goto error; |
| 4787 | } |
| 4788 | status = PyDict_SetItem(new_memo, key, value); |
| 4789 | Py_DECREF(key); |
| 4790 | Py_DECREF(value); |
| 4791 | if (status < 0) |
| 4792 | goto error; |
| 4793 | } |
| 4794 | } |
| 4795 | return new_memo; |
| 4796 | |
| 4797 | error: |
| 4798 | Py_XDECREF(new_memo); |
| 4799 | return NULL; |
| 4800 | } |
| 4801 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4802 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4803 | _pickle.PicklerMemoProxy.__reduce__ |
| 4804 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4805 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4806 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4807 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4808 | static PyObject * |
| 4809 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4810 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4811 | { |
| 4812 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4813 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4814 | if (contents == NULL) |
| 4815 | return NULL; |
| 4816 | |
| 4817 | reduce_value = PyTuple_New(2); |
| 4818 | if (reduce_value == NULL) { |
| 4819 | Py_DECREF(contents); |
| 4820 | return NULL; |
| 4821 | } |
| 4822 | dict_args = PyTuple_New(1); |
| 4823 | if (dict_args == NULL) { |
| 4824 | Py_DECREF(contents); |
| 4825 | Py_DECREF(reduce_value); |
| 4826 | return NULL; |
| 4827 | } |
| 4828 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4829 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4830 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4831 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4832 | return reduce_value; |
| 4833 | } |
| 4834 | |
| 4835 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4836 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4837 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4838 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4839 | {NULL, NULL} /* sentinel */ |
| 4840 | }; |
| 4841 | |
| 4842 | static void |
| 4843 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4844 | { |
| 4845 | PyObject_GC_UnTrack(self); |
| 4846 | Py_XDECREF(self->pickler); |
| 4847 | PyObject_GC_Del((PyObject *)self); |
| 4848 | } |
| 4849 | |
| 4850 | static int |
| 4851 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4852 | visitproc visit, void *arg) |
| 4853 | { |
| 4854 | Py_VISIT(self->pickler); |
| 4855 | return 0; |
| 4856 | } |
| 4857 | |
| 4858 | static int |
| 4859 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4860 | { |
| 4861 | Py_CLEAR(self->pickler); |
| 4862 | return 0; |
| 4863 | } |
| 4864 | |
| 4865 | static PyTypeObject PicklerMemoProxyType = { |
| 4866 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4867 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4868 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4869 | 0, |
| 4870 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4871 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4872 | 0, /* tp_getattr */ |
| 4873 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4874 | 0, /* tp_as_async */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4875 | 0, /* tp_repr */ |
| 4876 | 0, /* tp_as_number */ |
| 4877 | 0, /* tp_as_sequence */ |
| 4878 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4879 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4880 | 0, /* tp_call */ |
| 4881 | 0, /* tp_str */ |
| 4882 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4883 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4884 | 0, /* tp_as_buffer */ |
| 4885 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4886 | 0, /* tp_doc */ |
| 4887 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4888 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4889 | 0, /* tp_richcompare */ |
| 4890 | 0, /* tp_weaklistoffset */ |
| 4891 | 0, /* tp_iter */ |
| 4892 | 0, /* tp_iternext */ |
| 4893 | picklerproxy_methods, /* tp_methods */ |
| 4894 | }; |
| 4895 | |
| 4896 | static PyObject * |
| 4897 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4898 | { |
| 4899 | PicklerMemoProxyObject *self; |
| 4900 | |
| 4901 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4902 | if (self == NULL) |
| 4903 | return NULL; |
| 4904 | Py_INCREF(pickler); |
| 4905 | self->pickler = pickler; |
| 4906 | PyObject_GC_Track(self); |
| 4907 | return (PyObject *)self; |
| 4908 | } |
| 4909 | |
| 4910 | /*****************************************************************************/ |
| 4911 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4912 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4913 | Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4914 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4915 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4916 | } |
| 4917 | |
| 4918 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4919 | Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4920 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4921 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4922 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4923 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4924 | PyErr_SetString(PyExc_TypeError, |
| 4925 | "attribute deletion is not supported"); |
| 4926 | return -1; |
| 4927 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4928 | |
| 4929 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4930 | PicklerObject *pickler = |
| 4931 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4932 | |
| 4933 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4934 | if (new_memo == NULL) |
| 4935 | return -1; |
| 4936 | } |
| 4937 | else if (PyDict_Check(obj)) { |
| 4938 | Py_ssize_t i = 0; |
| 4939 | PyObject *key, *value; |
| 4940 | |
| 4941 | new_memo = PyMemoTable_New(); |
| 4942 | if (new_memo == NULL) |
| 4943 | return -1; |
| 4944 | |
| 4945 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4946 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4947 | PyObject *memo_obj; |
| 4948 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4949 | if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4950 | PyErr_SetString(PyExc_TypeError, |
| 4951 | "'memo' values must be 2-item tuples"); |
| 4952 | goto error; |
| 4953 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4954 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4955 | if (memo_id == -1 && PyErr_Occurred()) |
| 4956 | goto error; |
| 4957 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4958 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4959 | goto error; |
| 4960 | } |
| 4961 | } |
| 4962 | else { |
| 4963 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 4964 | "'memo' attribute must be a PicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4965 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4966 | return -1; |
| 4967 | } |
| 4968 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4969 | PyMemoTable_Del(self->memo); |
| 4970 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4971 | |
| 4972 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4973 | |
| 4974 | error: |
| 4975 | if (new_memo) |
| 4976 | PyMemoTable_Del(new_memo); |
| 4977 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4978 | } |
| 4979 | |
| 4980 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4981 | Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4982 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4983 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4984 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4985 | return NULL; |
| 4986 | } |
| 4987 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4991 | Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4992 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4993 | if (value == NULL) { |
| 4994 | PyErr_SetString(PyExc_TypeError, |
| 4995 | "attribute deletion is not supported"); |
| 4996 | return -1; |
| 4997 | } |
| 4998 | if (!PyCallable_Check(value)) { |
| 4999 | PyErr_SetString(PyExc_TypeError, |
| 5000 | "persistent_id must be a callable taking one argument"); |
| 5001 | return -1; |
| 5002 | } |
| 5003 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5004 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5005 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 5006 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5007 | |
| 5008 | return 0; |
| 5009 | } |
| 5010 | |
| 5011 | static PyMemberDef Pickler_members[] = { |
| 5012 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 5013 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 5014 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5015 | {NULL} |
| 5016 | }; |
| 5017 | |
| 5018 | static PyGetSetDef Pickler_getsets[] = { |
| 5019 | {"memo", (getter)Pickler_get_memo, |
| 5020 | (setter)Pickler_set_memo}, |
| 5021 | {"persistent_id", (getter)Pickler_get_persid, |
| 5022 | (setter)Pickler_set_persid}, |
| 5023 | {NULL} |
| 5024 | }; |
| 5025 | |
| 5026 | static PyTypeObject Pickler_Type = { |
| 5027 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5028 | "_pickle.Pickler" , /*tp_name*/ |
| 5029 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 5030 | 0, /*tp_itemsize*/ |
| 5031 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5032 | 0, /*tp_vectorcall_offset*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5033 | 0, /*tp_getattr*/ |
| 5034 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5035 | 0, /*tp_as_async*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5036 | 0, /*tp_repr*/ |
| 5037 | 0, /*tp_as_number*/ |
| 5038 | 0, /*tp_as_sequence*/ |
| 5039 | 0, /*tp_as_mapping*/ |
| 5040 | 0, /*tp_hash*/ |
| 5041 | 0, /*tp_call*/ |
| 5042 | 0, /*tp_str*/ |
| 5043 | 0, /*tp_getattro*/ |
| 5044 | 0, /*tp_setattro*/ |
| 5045 | 0, /*tp_as_buffer*/ |
| 5046 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 5047 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5048 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 5049 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 5050 | 0, /*tp_richcompare*/ |
| 5051 | 0, /*tp_weaklistoffset*/ |
| 5052 | 0, /*tp_iter*/ |
| 5053 | 0, /*tp_iternext*/ |
| 5054 | Pickler_methods, /*tp_methods*/ |
| 5055 | Pickler_members, /*tp_members*/ |
| 5056 | Pickler_getsets, /*tp_getset*/ |
| 5057 | 0, /*tp_base*/ |
| 5058 | 0, /*tp_dict*/ |
| 5059 | 0, /*tp_descr_get*/ |
| 5060 | 0, /*tp_descr_set*/ |
| 5061 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 5062 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5063 | PyType_GenericAlloc, /*tp_alloc*/ |
| 5064 | PyType_GenericNew, /*tp_new*/ |
| 5065 | PyObject_GC_Del, /*tp_free*/ |
| 5066 | 0, /*tp_is_gc*/ |
| 5067 | }; |
| 5068 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5069 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5070 | |
| 5071 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 5072 | using directly the C version of find_class(), when find_class() is not |
| 5073 | overridden by a subclass. Although, this could become rather hackish. A |
| 5074 | simpler optimization would be to call the C function when self is not a |
| 5075 | subclass instance. */ |
| 5076 | static PyObject * |
| 5077 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 5078 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5079 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5080 | |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 5081 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
| 5082 | module_name, global_name, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5083 | } |
| 5084 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5085 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5086 | marker(UnpicklerObject *self) |
| 5087 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5088 | Py_ssize_t mark; |
| 5089 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5090 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5091 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5092 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5093 | return -1; |
| 5094 | } |
| 5095 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5096 | mark = self->marks[--self->num_marks]; |
| 5097 | self->stack->mark_set = self->num_marks != 0; |
| 5098 | self->stack->fence = self->num_marks ? |
| 5099 | self->marks[self->num_marks - 1] : 0; |
| 5100 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5101 | } |
| 5102 | |
| 5103 | static int |
| 5104 | load_none(UnpicklerObject *self) |
| 5105 | { |
| 5106 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5107 | return 0; |
| 5108 | } |
| 5109 | |
| 5110 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5111 | load_int(UnpicklerObject *self) |
| 5112 | { |
| 5113 | PyObject *value; |
| 5114 | char *endptr, *s; |
| 5115 | Py_ssize_t len; |
| 5116 | long x; |
| 5117 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5118 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5119 | return -1; |
| 5120 | if (len < 2) |
| 5121 | return bad_readline(); |
| 5122 | |
| 5123 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5124 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5125 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5126 | x = strtol(s, &endptr, 0); |
| 5127 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5128 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5129 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5130 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5131 | errno = 0; |
| 5132 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5133 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5134 | if (value == NULL) { |
| 5135 | PyErr_SetString(PyExc_ValueError, |
| 5136 | "could not convert string to int"); |
| 5137 | return -1; |
| 5138 | } |
| 5139 | } |
| 5140 | else { |
| 5141 | if (len == 3 && (x == 0 || x == 1)) { |
| 5142 | if ((value = PyBool_FromLong(x)) == NULL) |
| 5143 | return -1; |
| 5144 | } |
| 5145 | else { |
| 5146 | if ((value = PyLong_FromLong(x)) == NULL) |
| 5147 | return -1; |
| 5148 | } |
| 5149 | } |
| 5150 | |
| 5151 | PDATA_PUSH(self->stack, value, -1); |
| 5152 | return 0; |
| 5153 | } |
| 5154 | |
| 5155 | static int |
| 5156 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 5157 | { |
| 5158 | assert(boolean == Py_True || boolean == Py_False); |
| 5159 | PDATA_APPEND(self->stack, boolean, -1); |
| 5160 | return 0; |
| 5161 | } |
| 5162 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5163 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 5164 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 5165 | */ |
| 5166 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5167 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5168 | { |
| 5169 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5170 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5171 | size_t x = 0; |
| 5172 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 5173 | if (nbytes > (int)sizeof(size_t)) { |
| 5174 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 5175 | * have 64-bit size that can't be represented on 32-bit platform. |
| 5176 | */ |
| 5177 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 5178 | if (s[i]) |
| 5179 | return -1; |
| 5180 | } |
| 5181 | nbytes = (int)sizeof(size_t); |
| 5182 | } |
| 5183 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5184 | x |= (size_t) s[i] << (8 * i); |
| 5185 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5186 | |
| 5187 | if (x > PY_SSIZE_T_MAX) |
| 5188 | return -1; |
| 5189 | else |
| 5190 | return (Py_ssize_t) x; |
| 5191 | } |
| 5192 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5193 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 5194 | * 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] | 5195 | * 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] | 5196 | * of x-platform bugs. |
| 5197 | */ |
| 5198 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5199 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5200 | { |
| 5201 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 5202 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5203 | long x = 0; |
| 5204 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5205 | for (i = 0; i < nbytes; i++) { |
| 5206 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5207 | } |
| 5208 | |
| 5209 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 5210 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 5211 | * to extend a BININT's sign bit to the full width. |
| 5212 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5213 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5214 | x |= -(x & (1L << 31)); |
| 5215 | } |
| 5216 | |
| 5217 | return x; |
| 5218 | } |
| 5219 | |
| 5220 | static int |
| 5221 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 5222 | { |
| 5223 | PyObject *value; |
| 5224 | long x; |
| 5225 | |
| 5226 | x = calc_binint(s, size); |
| 5227 | |
| 5228 | if ((value = PyLong_FromLong(x)) == NULL) |
| 5229 | return -1; |
| 5230 | |
| 5231 | PDATA_PUSH(self->stack, value, -1); |
| 5232 | return 0; |
| 5233 | } |
| 5234 | |
| 5235 | static int |
| 5236 | load_binint(UnpicklerObject *self) |
| 5237 | { |
| 5238 | char *s; |
| 5239 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5240 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5241 | return -1; |
| 5242 | |
| 5243 | return load_binintx(self, s, 4); |
| 5244 | } |
| 5245 | |
| 5246 | static int |
| 5247 | load_binint1(UnpicklerObject *self) |
| 5248 | { |
| 5249 | char *s; |
| 5250 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5251 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5252 | return -1; |
| 5253 | |
| 5254 | return load_binintx(self, s, 1); |
| 5255 | } |
| 5256 | |
| 5257 | static int |
| 5258 | load_binint2(UnpicklerObject *self) |
| 5259 | { |
| 5260 | char *s; |
| 5261 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5262 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5263 | return -1; |
| 5264 | |
| 5265 | return load_binintx(self, s, 2); |
| 5266 | } |
| 5267 | |
| 5268 | static int |
| 5269 | load_long(UnpicklerObject *self) |
| 5270 | { |
| 5271 | PyObject *value; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5272 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5273 | Py_ssize_t len; |
| 5274 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5275 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5276 | return -1; |
| 5277 | if (len < 2) |
| 5278 | return bad_readline(); |
| 5279 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 5280 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 5281 | the 'L' before calling PyLong_FromString. In order to maintain |
| 5282 | compatibility with Python 3.0.0, we don't actually *require* |
| 5283 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5284 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 5285 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 5286 | /* XXX: Should the base argument explicitly set to 10? */ |
| 5287 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 5288 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5289 | return -1; |
| 5290 | |
| 5291 | PDATA_PUSH(self->stack, value, -1); |
| 5292 | return 0; |
| 5293 | } |
| 5294 | |
| 5295 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 5296 | * data following. |
| 5297 | */ |
| 5298 | static int |
| 5299 | load_counted_long(UnpicklerObject *self, int size) |
| 5300 | { |
| 5301 | PyObject *value; |
| 5302 | char *nbytes; |
| 5303 | char *pdata; |
| 5304 | |
| 5305 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5306 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5307 | return -1; |
| 5308 | |
| 5309 | size = calc_binint(nbytes, size); |
| 5310 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5311 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5312 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5313 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5314 | "LONG pickle has negative byte count"); |
| 5315 | return -1; |
| 5316 | } |
| 5317 | |
| 5318 | if (size == 0) |
| 5319 | value = PyLong_FromLong(0L); |
| 5320 | else { |
| 5321 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5322 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5323 | return -1; |
| 5324 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 5325 | 1 /* little endian */ , 1 /* signed */ ); |
| 5326 | } |
| 5327 | if (value == NULL) |
| 5328 | return -1; |
| 5329 | PDATA_PUSH(self->stack, value, -1); |
| 5330 | return 0; |
| 5331 | } |
| 5332 | |
| 5333 | static int |
| 5334 | load_float(UnpicklerObject *self) |
| 5335 | { |
| 5336 | PyObject *value; |
| 5337 | char *endptr, *s; |
| 5338 | Py_ssize_t len; |
| 5339 | double d; |
| 5340 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5341 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5342 | return -1; |
| 5343 | if (len < 2) |
| 5344 | return bad_readline(); |
| 5345 | |
| 5346 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5347 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 5348 | if (d == -1.0 && PyErr_Occurred()) |
| 5349 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5350 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5351 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 5352 | return -1; |
| 5353 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5354 | value = PyFloat_FromDouble(d); |
| 5355 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5356 | return -1; |
| 5357 | |
| 5358 | PDATA_PUSH(self->stack, value, -1); |
| 5359 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5360 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5361 | |
| 5362 | static int |
| 5363 | load_binfloat(UnpicklerObject *self) |
| 5364 | { |
| 5365 | PyObject *value; |
| 5366 | double x; |
| 5367 | char *s; |
| 5368 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5369 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5370 | return -1; |
| 5371 | |
| 5372 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 5373 | if (x == -1.0 && PyErr_Occurred()) |
| 5374 | return -1; |
| 5375 | |
| 5376 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 5377 | return -1; |
| 5378 | |
| 5379 | PDATA_PUSH(self->stack, value, -1); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
| 5383 | static int |
| 5384 | load_string(UnpicklerObject *self) |
| 5385 | { |
| 5386 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5387 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5388 | Py_ssize_t len; |
| 5389 | char *s, *p; |
| 5390 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5391 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5392 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5393 | /* Strip the newline */ |
| 5394 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5395 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5396 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5397 | p = s + 1; |
| 5398 | len -= 2; |
| 5399 | } |
| 5400 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5401 | PickleState *st = _Pickle_GetGlobalState(); |
| 5402 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5403 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5404 | return -1; |
| 5405 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5406 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5407 | |
| 5408 | /* Use the PyBytes API to decode the string, since that is what is used |
| 5409 | to encode, and then coerce the result to Unicode. */ |
| 5410 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5411 | if (bytes == NULL) |
| 5412 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5413 | |
| 5414 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 5415 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5416 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5417 | obj = bytes; |
| 5418 | } |
| 5419 | else { |
| 5420 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 5421 | Py_DECREF(bytes); |
| 5422 | if (obj == NULL) { |
| 5423 | return -1; |
| 5424 | } |
| 5425 | } |
| 5426 | |
| 5427 | PDATA_PUSH(self->stack, obj, -1); |
| 5428 | return 0; |
| 5429 | } |
| 5430 | |
| 5431 | static int |
| 5432 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 5433 | { |
| 5434 | PyObject *obj; |
| 5435 | Py_ssize_t size; |
| 5436 | char *s; |
| 5437 | |
| 5438 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5439 | return -1; |
| 5440 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5441 | size = calc_binsize(s, nbytes); |
| 5442 | if (size < 0) { |
| 5443 | PickleState *st = _Pickle_GetGlobalState(); |
| 5444 | PyErr_Format(st->UnpicklingError, |
| 5445 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 5446 | PY_SSIZE_T_MAX); |
| 5447 | return -1; |
| 5448 | } |
| 5449 | |
| 5450 | if (_Unpickler_Read(self, &s, size) < 0) |
| 5451 | return -1; |
| 5452 | |
| 5453 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 5454 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5455 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5456 | obj = PyBytes_FromStringAndSize(s, size); |
| 5457 | } |
| 5458 | else { |
| 5459 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 5460 | } |
| 5461 | if (obj == NULL) { |
| 5462 | return -1; |
| 5463 | } |
| 5464 | |
| 5465 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5466 | return 0; |
| 5467 | } |
| 5468 | |
| 5469 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5470 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5471 | { |
| 5472 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5473 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5474 | char *s; |
| 5475 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5476 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5477 | return -1; |
| 5478 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5479 | size = calc_binsize(s, nbytes); |
| 5480 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5481 | PyErr_Format(PyExc_OverflowError, |
| 5482 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5483 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5484 | return -1; |
| 5485 | } |
| 5486 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 5487 | bytes = PyBytes_FromStringAndSize(NULL, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5488 | if (bytes == NULL) |
| 5489 | return -1; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 5490 | if (_Unpickler_ReadInto(self, PyBytes_AS_STRING(bytes), size) < 0) { |
| 5491 | Py_DECREF(bytes); |
| 5492 | return -1; |
| 5493 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5494 | |
| 5495 | PDATA_PUSH(self->stack, bytes, -1); |
| 5496 | return 0; |
| 5497 | } |
| 5498 | |
| 5499 | static int |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 5500 | load_counted_bytearray(UnpicklerObject *self) |
| 5501 | { |
| 5502 | PyObject *bytearray; |
| 5503 | Py_ssize_t size; |
| 5504 | char *s; |
| 5505 | |
| 5506 | if (_Unpickler_Read(self, &s, 8) < 0) { |
| 5507 | return -1; |
| 5508 | } |
| 5509 | |
| 5510 | size = calc_binsize(s, 8); |
| 5511 | if (size < 0) { |
| 5512 | PyErr_Format(PyExc_OverflowError, |
| 5513 | "BYTEARRAY8 exceeds system's maximum size of %zd bytes", |
| 5514 | PY_SSIZE_T_MAX); |
| 5515 | return -1; |
| 5516 | } |
| 5517 | |
| 5518 | bytearray = PyByteArray_FromStringAndSize(NULL, size); |
| 5519 | if (bytearray == NULL) { |
| 5520 | return -1; |
| 5521 | } |
| 5522 | if (_Unpickler_ReadInto(self, PyByteArray_AS_STRING(bytearray), size) < 0) { |
| 5523 | Py_DECREF(bytearray); |
| 5524 | return -1; |
| 5525 | } |
| 5526 | |
| 5527 | PDATA_PUSH(self->stack, bytearray, -1); |
| 5528 | return 0; |
| 5529 | } |
| 5530 | |
| 5531 | static int |
| 5532 | load_next_buffer(UnpicklerObject *self) |
| 5533 | { |
| 5534 | if (self->buffers == NULL) { |
| 5535 | PickleState *st = _Pickle_GetGlobalState(); |
| 5536 | PyErr_SetString(st->UnpicklingError, |
| 5537 | "pickle stream refers to out-of-band data " |
| 5538 | "but no *buffers* argument was given"); |
| 5539 | return -1; |
| 5540 | } |
| 5541 | PyObject *buf = PyIter_Next(self->buffers); |
| 5542 | if (buf == NULL) { |
| 5543 | if (!PyErr_Occurred()) { |
| 5544 | PickleState *st = _Pickle_GetGlobalState(); |
| 5545 | PyErr_SetString(st->UnpicklingError, |
| 5546 | "not enough out-of-band buffers"); |
| 5547 | } |
| 5548 | return -1; |
| 5549 | } |
| 5550 | |
| 5551 | PDATA_PUSH(self->stack, buf, -1); |
| 5552 | return 0; |
| 5553 | } |
| 5554 | |
| 5555 | static int |
| 5556 | load_readonly_buffer(UnpicklerObject *self) |
| 5557 | { |
| 5558 | Py_ssize_t len = Py_SIZE(self->stack); |
| 5559 | if (len <= self->stack->fence) { |
| 5560 | return Pdata_stack_underflow(self->stack); |
| 5561 | } |
| 5562 | |
| 5563 | PyObject *obj = self->stack->data[len - 1]; |
| 5564 | PyObject *view = PyMemoryView_FromObject(obj); |
| 5565 | if (view == NULL) { |
| 5566 | return -1; |
| 5567 | } |
| 5568 | if (!PyMemoryView_GET_BUFFER(view)->readonly) { |
| 5569 | /* Original object is writable */ |
| 5570 | PyMemoryView_GET_BUFFER(view)->readonly = 1; |
| 5571 | self->stack->data[len - 1] = view; |
| 5572 | Py_DECREF(obj); |
| 5573 | } |
| 5574 | else { |
| 5575 | /* Original object is read-only, no need to replace it */ |
| 5576 | Py_DECREF(view); |
| 5577 | } |
| 5578 | return 0; |
| 5579 | } |
| 5580 | |
| 5581 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5582 | load_unicode(UnpicklerObject *self) |
| 5583 | { |
| 5584 | PyObject *str; |
| 5585 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5586 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5587 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5588 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5589 | return -1; |
| 5590 | if (len < 1) |
| 5591 | return bad_readline(); |
| 5592 | |
| 5593 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5594 | if (str == NULL) |
| 5595 | return -1; |
| 5596 | |
| 5597 | PDATA_PUSH(self->stack, str, -1); |
| 5598 | return 0; |
| 5599 | } |
| 5600 | |
| 5601 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5602 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5603 | { |
| 5604 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5605 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5606 | char *s; |
| 5607 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5608 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5609 | return -1; |
| 5610 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5611 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5612 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5613 | PyErr_Format(PyExc_OverflowError, |
| 5614 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5615 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5616 | return -1; |
| 5617 | } |
| 5618 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5619 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5620 | return -1; |
| 5621 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5622 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5623 | if (str == NULL) |
| 5624 | return -1; |
| 5625 | |
| 5626 | PDATA_PUSH(self->stack, str, -1); |
| 5627 | return 0; |
| 5628 | } |
| 5629 | |
| 5630 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5631 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5632 | { |
| 5633 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5634 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5635 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5636 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5637 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5638 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5639 | if (tuple == NULL) |
| 5640 | return -1; |
| 5641 | PDATA_PUSH(self->stack, tuple, -1); |
| 5642 | return 0; |
| 5643 | } |
| 5644 | |
| 5645 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5646 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5647 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5648 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5649 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5650 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5651 | return -1; |
| 5652 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5653 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5654 | } |
| 5655 | |
| 5656 | static int |
| 5657 | load_empty_list(UnpicklerObject *self) |
| 5658 | { |
| 5659 | PyObject *list; |
| 5660 | |
| 5661 | if ((list = PyList_New(0)) == NULL) |
| 5662 | return -1; |
| 5663 | PDATA_PUSH(self->stack, list, -1); |
| 5664 | return 0; |
| 5665 | } |
| 5666 | |
| 5667 | static int |
| 5668 | load_empty_dict(UnpicklerObject *self) |
| 5669 | { |
| 5670 | PyObject *dict; |
| 5671 | |
| 5672 | if ((dict = PyDict_New()) == NULL) |
| 5673 | return -1; |
| 5674 | PDATA_PUSH(self->stack, dict, -1); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
| 5678 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5679 | load_empty_set(UnpicklerObject *self) |
| 5680 | { |
| 5681 | PyObject *set; |
| 5682 | |
| 5683 | if ((set = PySet_New(NULL)) == NULL) |
| 5684 | return -1; |
| 5685 | PDATA_PUSH(self->stack, set, -1); |
| 5686 | return 0; |
| 5687 | } |
| 5688 | |
| 5689 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5690 | load_list(UnpicklerObject *self) |
| 5691 | { |
| 5692 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5693 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5694 | |
| 5695 | if ((i = marker(self)) < 0) |
| 5696 | return -1; |
| 5697 | |
| 5698 | list = Pdata_poplist(self->stack, i); |
| 5699 | if (list == NULL) |
| 5700 | return -1; |
| 5701 | PDATA_PUSH(self->stack, list, -1); |
| 5702 | return 0; |
| 5703 | } |
| 5704 | |
| 5705 | static int |
| 5706 | load_dict(UnpicklerObject *self) |
| 5707 | { |
| 5708 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5709 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5710 | |
| 5711 | if ((i = marker(self)) < 0) |
| 5712 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5713 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5714 | |
| 5715 | if ((dict = PyDict_New()) == NULL) |
| 5716 | return -1; |
| 5717 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5718 | if ((j - i) % 2 != 0) { |
| 5719 | PickleState *st = _Pickle_GetGlobalState(); |
| 5720 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5721 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5722 | return -1; |
| 5723 | } |
| 5724 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5725 | for (k = i + 1; k < j; k += 2) { |
| 5726 | key = self->stack->data[k - 1]; |
| 5727 | value = self->stack->data[k]; |
| 5728 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5729 | Py_DECREF(dict); |
| 5730 | return -1; |
| 5731 | } |
| 5732 | } |
| 5733 | Pdata_clear(self->stack, i); |
| 5734 | PDATA_PUSH(self->stack, dict, -1); |
| 5735 | return 0; |
| 5736 | } |
| 5737 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5738 | static int |
| 5739 | load_frozenset(UnpicklerObject *self) |
| 5740 | { |
| 5741 | PyObject *items; |
| 5742 | PyObject *frozenset; |
| 5743 | Py_ssize_t i; |
| 5744 | |
| 5745 | if ((i = marker(self)) < 0) |
| 5746 | return -1; |
| 5747 | |
| 5748 | items = Pdata_poptuple(self->stack, i); |
| 5749 | if (items == NULL) |
| 5750 | return -1; |
| 5751 | |
| 5752 | frozenset = PyFrozenSet_New(items); |
| 5753 | Py_DECREF(items); |
| 5754 | if (frozenset == NULL) |
| 5755 | return -1; |
| 5756 | |
| 5757 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5758 | return 0; |
| 5759 | } |
| 5760 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5761 | static PyObject * |
| 5762 | instantiate(PyObject *cls, PyObject *args) |
| 5763 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5764 | /* Caller must assure args are a tuple. Normally, args come from |
| 5765 | Pdata_poptuple which packs objects from the top of the stack |
| 5766 | into a newly created tuple. */ |
| 5767 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5768 | if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { |
| 5769 | _Py_IDENTIFIER(__getinitargs__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5770 | _Py_IDENTIFIER(__new__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5771 | PyObject *func; |
| 5772 | if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) { |
| 5773 | return NULL; |
| 5774 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5775 | if (func == NULL) { |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 5776 | return _PyObject_CallMethodIdOneArg(cls, &PyId___new__, cls); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5777 | } |
| 5778 | Py_DECREF(func); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5779 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5780 | return PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5781 | } |
| 5782 | |
| 5783 | static int |
| 5784 | load_obj(UnpicklerObject *self) |
| 5785 | { |
| 5786 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5787 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5788 | |
| 5789 | if ((i = marker(self)) < 0) |
| 5790 | return -1; |
| 5791 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5792 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5793 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5794 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5795 | args = Pdata_poptuple(self->stack, i + 1); |
| 5796 | if (args == NULL) |
| 5797 | return -1; |
| 5798 | |
| 5799 | PDATA_POP(self->stack, cls); |
| 5800 | if (cls) { |
| 5801 | obj = instantiate(cls, args); |
| 5802 | Py_DECREF(cls); |
| 5803 | } |
| 5804 | Py_DECREF(args); |
| 5805 | if (obj == NULL) |
| 5806 | return -1; |
| 5807 | |
| 5808 | PDATA_PUSH(self->stack, obj, -1); |
| 5809 | return 0; |
| 5810 | } |
| 5811 | |
| 5812 | static int |
| 5813 | load_inst(UnpicklerObject *self) |
| 5814 | { |
| 5815 | PyObject *cls = NULL; |
| 5816 | PyObject *args = NULL; |
| 5817 | PyObject *obj = NULL; |
| 5818 | PyObject *module_name; |
| 5819 | PyObject *class_name; |
| 5820 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5821 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5822 | char *s; |
| 5823 | |
| 5824 | if ((i = marker(self)) < 0) |
| 5825 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5826 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5827 | return -1; |
| 5828 | if (len < 2) |
| 5829 | return bad_readline(); |
| 5830 | |
| 5831 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5832 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5833 | supported by older protocols on Python 2.x. */ |
| 5834 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5835 | if (module_name == NULL) |
| 5836 | return -1; |
| 5837 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5838 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5839 | if (len < 2) { |
| 5840 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5841 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5842 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5843 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5844 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5845 | cls = find_class(self, module_name, class_name); |
| 5846 | Py_DECREF(class_name); |
| 5847 | } |
| 5848 | } |
| 5849 | Py_DECREF(module_name); |
| 5850 | |
| 5851 | if (cls == NULL) |
| 5852 | return -1; |
| 5853 | |
| 5854 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5855 | obj = instantiate(cls, args); |
| 5856 | Py_DECREF(args); |
| 5857 | } |
| 5858 | Py_DECREF(cls); |
| 5859 | |
| 5860 | if (obj == NULL) |
| 5861 | return -1; |
| 5862 | |
| 5863 | PDATA_PUSH(self->stack, obj, -1); |
| 5864 | return 0; |
| 5865 | } |
| 5866 | |
| 5867 | static int |
| 5868 | load_newobj(UnpicklerObject *self) |
| 5869 | { |
| 5870 | PyObject *args = NULL; |
| 5871 | PyObject *clsraw = NULL; |
| 5872 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5873 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5874 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5875 | |
| 5876 | /* Stack is ... cls argtuple, and we want to call |
| 5877 | * cls.__new__(cls, *argtuple). |
| 5878 | */ |
| 5879 | PDATA_POP(self->stack, args); |
| 5880 | if (args == NULL) |
| 5881 | goto error; |
| 5882 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5883 | PyErr_SetString(st->UnpicklingError, |
| 5884 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5885 | goto error; |
| 5886 | } |
| 5887 | |
| 5888 | PDATA_POP(self->stack, clsraw); |
| 5889 | cls = (PyTypeObject *)clsraw; |
| 5890 | if (cls == NULL) |
| 5891 | goto error; |
| 5892 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5893 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5894 | "isn't a type object"); |
| 5895 | goto error; |
| 5896 | } |
| 5897 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5898 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5899 | "has NULL tp_new"); |
| 5900 | goto error; |
| 5901 | } |
| 5902 | |
| 5903 | /* Call __new__. */ |
| 5904 | obj = cls->tp_new(cls, args, NULL); |
| 5905 | if (obj == NULL) |
| 5906 | goto error; |
| 5907 | |
| 5908 | Py_DECREF(args); |
| 5909 | Py_DECREF(clsraw); |
| 5910 | PDATA_PUSH(self->stack, obj, -1); |
| 5911 | return 0; |
| 5912 | |
| 5913 | error: |
| 5914 | Py_XDECREF(args); |
| 5915 | Py_XDECREF(clsraw); |
| 5916 | return -1; |
| 5917 | } |
| 5918 | |
| 5919 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5920 | load_newobj_ex(UnpicklerObject *self) |
| 5921 | { |
| 5922 | PyObject *cls, *args, *kwargs; |
| 5923 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5924 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5925 | |
| 5926 | PDATA_POP(self->stack, kwargs); |
| 5927 | if (kwargs == NULL) { |
| 5928 | return -1; |
| 5929 | } |
| 5930 | PDATA_POP(self->stack, args); |
| 5931 | if (args == NULL) { |
| 5932 | Py_DECREF(kwargs); |
| 5933 | return -1; |
| 5934 | } |
| 5935 | PDATA_POP(self->stack, cls); |
| 5936 | if (cls == NULL) { |
| 5937 | Py_DECREF(kwargs); |
| 5938 | Py_DECREF(args); |
| 5939 | return -1; |
| 5940 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5941 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5942 | if (!PyType_Check(cls)) { |
| 5943 | Py_DECREF(kwargs); |
| 5944 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5945 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5946 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5947 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5948 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5949 | return -1; |
| 5950 | } |
| 5951 | |
| 5952 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5953 | Py_DECREF(kwargs); |
| 5954 | Py_DECREF(args); |
| 5955 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5956 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5957 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5958 | return -1; |
| 5959 | } |
| 5960 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5961 | Py_DECREF(kwargs); |
| 5962 | Py_DECREF(args); |
| 5963 | Py_DECREF(cls); |
| 5964 | if (obj == NULL) { |
| 5965 | return -1; |
| 5966 | } |
| 5967 | PDATA_PUSH(self->stack, obj, -1); |
| 5968 | return 0; |
| 5969 | } |
| 5970 | |
| 5971 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5972 | load_global(UnpicklerObject *self) |
| 5973 | { |
| 5974 | PyObject *global = NULL; |
| 5975 | PyObject *module_name; |
| 5976 | PyObject *global_name; |
| 5977 | Py_ssize_t len; |
| 5978 | char *s; |
| 5979 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5980 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5981 | return -1; |
| 5982 | if (len < 2) |
| 5983 | return bad_readline(); |
| 5984 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5985 | if (!module_name) |
| 5986 | return -1; |
| 5987 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5988 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5989 | if (len < 2) { |
| 5990 | Py_DECREF(module_name); |
| 5991 | return bad_readline(); |
| 5992 | } |
| 5993 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5994 | if (global_name) { |
| 5995 | global = find_class(self, module_name, global_name); |
| 5996 | Py_DECREF(global_name); |
| 5997 | } |
| 5998 | } |
| 5999 | Py_DECREF(module_name); |
| 6000 | |
| 6001 | if (global == NULL) |
| 6002 | return -1; |
| 6003 | PDATA_PUSH(self->stack, global, -1); |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
| 6007 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6008 | load_stack_global(UnpicklerObject *self) |
| 6009 | { |
| 6010 | PyObject *global; |
| 6011 | PyObject *module_name; |
| 6012 | PyObject *global_name; |
| 6013 | |
| 6014 | PDATA_POP(self->stack, global_name); |
| 6015 | PDATA_POP(self->stack, module_name); |
| 6016 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 6017 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6018 | PickleState *st = _Pickle_GetGlobalState(); |
| 6019 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6020 | Py_XDECREF(global_name); |
| 6021 | Py_XDECREF(module_name); |
| 6022 | return -1; |
| 6023 | } |
| 6024 | global = find_class(self, module_name, global_name); |
| 6025 | Py_DECREF(global_name); |
| 6026 | Py_DECREF(module_name); |
| 6027 | if (global == NULL) |
| 6028 | return -1; |
| 6029 | PDATA_PUSH(self->stack, global, -1); |
| 6030 | return 0; |
| 6031 | } |
| 6032 | |
| 6033 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6034 | load_persid(UnpicklerObject *self) |
| 6035 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6036 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6037 | Py_ssize_t len; |
| 6038 | char *s; |
| 6039 | |
| 6040 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6041 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6042 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 6043 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6044 | return bad_readline(); |
| 6045 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 6046 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 6047 | if (pid == NULL) { |
| 6048 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 6049 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 6050 | "persistent IDs in protocol 0 must be " |
| 6051 | "ASCII strings"); |
| 6052 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6053 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 6054 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6055 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6056 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 6057 | Py_DECREF(pid); |
| 6058 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6059 | return -1; |
| 6060 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6061 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6062 | return 0; |
| 6063 | } |
| 6064 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6065 | PickleState *st = _Pickle_GetGlobalState(); |
| 6066 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6067 | "A load persistent id instruction was encountered,\n" |
| 6068 | "but no persistent_load function was specified."); |
| 6069 | return -1; |
| 6070 | } |
| 6071 | } |
| 6072 | |
| 6073 | static int |
| 6074 | load_binpersid(UnpicklerObject *self) |
| 6075 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6076 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6077 | |
| 6078 | if (self->pers_func) { |
| 6079 | PDATA_POP(self->stack, pid); |
| 6080 | if (pid == NULL) |
| 6081 | return -1; |
| 6082 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6083 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 6084 | Py_DECREF(pid); |
| 6085 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6086 | return -1; |
| 6087 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6088 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6089 | return 0; |
| 6090 | } |
| 6091 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6092 | PickleState *st = _Pickle_GetGlobalState(); |
| 6093 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6094 | "A load persistent id instruction was encountered,\n" |
| 6095 | "but no persistent_load function was specified."); |
| 6096 | return -1; |
| 6097 | } |
| 6098 | } |
| 6099 | |
| 6100 | static int |
| 6101 | load_pop(UnpicklerObject *self) |
| 6102 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6103 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6104 | |
| 6105 | /* Note that we split the (pickle.py) stack into two stacks, |
| 6106 | * an object stack and a mark stack. We have to be clever and |
| 6107 | * 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] | 6108 | * mark stack first, and only signalling a stack underflow if |
| 6109 | * the object stack is empty and the mark stack doesn't match |
| 6110 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6111 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 6112 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6113 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6114 | self->stack->mark_set = self->num_marks != 0; |
| 6115 | self->stack->fence = self->num_marks ? |
| 6116 | self->marks[self->num_marks - 1] : 0; |
| 6117 | } else if (len <= self->stack->fence) |
| 6118 | return Pdata_stack_underflow(self->stack); |
| 6119 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6120 | len--; |
| 6121 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6122 | Py_SIZE(self->stack) = len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6123 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6124 | return 0; |
| 6125 | } |
| 6126 | |
| 6127 | static int |
| 6128 | load_pop_mark(UnpicklerObject *self) |
| 6129 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6130 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6131 | |
| 6132 | if ((i = marker(self)) < 0) |
| 6133 | return -1; |
| 6134 | |
| 6135 | Pdata_clear(self->stack, i); |
| 6136 | |
| 6137 | return 0; |
| 6138 | } |
| 6139 | |
| 6140 | static int |
| 6141 | load_dup(UnpicklerObject *self) |
| 6142 | { |
| 6143 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6144 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6145 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6146 | if (len <= self->stack->fence) |
| 6147 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6148 | last = self->stack->data[len - 1]; |
| 6149 | PDATA_APPEND(self->stack, last, -1); |
| 6150 | return 0; |
| 6151 | } |
| 6152 | |
| 6153 | static int |
| 6154 | load_get(UnpicklerObject *self) |
| 6155 | { |
| 6156 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6157 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6158 | Py_ssize_t len; |
| 6159 | char *s; |
| 6160 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6161 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6162 | return -1; |
| 6163 | if (len < 2) |
| 6164 | return bad_readline(); |
| 6165 | |
| 6166 | key = PyLong_FromString(s, NULL, 10); |
| 6167 | if (key == NULL) |
| 6168 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6169 | idx = PyLong_AsSsize_t(key); |
| 6170 | if (idx == -1 && PyErr_Occurred()) { |
| 6171 | Py_DECREF(key); |
| 6172 | return -1; |
| 6173 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6174 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6175 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6176 | if (value == NULL) { |
| 6177 | if (!PyErr_Occurred()) |
| 6178 | PyErr_SetObject(PyExc_KeyError, key); |
| 6179 | Py_DECREF(key); |
| 6180 | return -1; |
| 6181 | } |
| 6182 | Py_DECREF(key); |
| 6183 | |
| 6184 | PDATA_APPEND(self->stack, value, -1); |
| 6185 | return 0; |
| 6186 | } |
| 6187 | |
| 6188 | static int |
| 6189 | load_binget(UnpicklerObject *self) |
| 6190 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6191 | PyObject *value; |
| 6192 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6193 | char *s; |
| 6194 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6195 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6196 | return -1; |
| 6197 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6198 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6199 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6200 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6201 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6202 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6203 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6204 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6205 | Py_DECREF(key); |
| 6206 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6207 | return -1; |
| 6208 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6209 | |
| 6210 | PDATA_APPEND(self->stack, value, -1); |
| 6211 | return 0; |
| 6212 | } |
| 6213 | |
| 6214 | static int |
| 6215 | load_long_binget(UnpicklerObject *self) |
| 6216 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6217 | PyObject *value; |
| 6218 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6219 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6220 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6221 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6222 | return -1; |
| 6223 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6224 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6225 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6226 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6227 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6228 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6229 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6230 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6231 | Py_DECREF(key); |
| 6232 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6233 | return -1; |
| 6234 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6235 | |
| 6236 | PDATA_APPEND(self->stack, value, -1); |
| 6237 | return 0; |
| 6238 | } |
| 6239 | |
| 6240 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 6241 | * the number of bytes following the opcode, holding the index (code) value. |
| 6242 | */ |
| 6243 | static int |
| 6244 | load_extension(UnpicklerObject *self, int nbytes) |
| 6245 | { |
| 6246 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 6247 | long code; /* calc_binint returns long */ |
| 6248 | PyObject *py_code; /* code as a Python int */ |
| 6249 | PyObject *obj; /* the object to push */ |
| 6250 | PyObject *pair; /* (module_name, class_name) */ |
| 6251 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6252 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6253 | |
| 6254 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6255 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6256 | return -1; |
| 6257 | code = calc_binint(codebytes, nbytes); |
| 6258 | if (code <= 0) { /* note that 0 is forbidden */ |
| 6259 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6260 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6261 | return -1; |
| 6262 | } |
| 6263 | |
| 6264 | /* Look for the code in the cache. */ |
| 6265 | py_code = PyLong_FromLong(code); |
| 6266 | if (py_code == NULL) |
| 6267 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6268 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6269 | if (obj != NULL) { |
| 6270 | /* Bingo. */ |
| 6271 | Py_DECREF(py_code); |
| 6272 | PDATA_APPEND(self->stack, obj, -1); |
| 6273 | return 0; |
| 6274 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6275 | if (PyErr_Occurred()) { |
| 6276 | Py_DECREF(py_code); |
| 6277 | return -1; |
| 6278 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6279 | |
| 6280 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6281 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6282 | if (pair == NULL) { |
| 6283 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6284 | if (!PyErr_Occurred()) { |
| 6285 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 6286 | "code %ld", code); |
| 6287 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6288 | return -1; |
| 6289 | } |
| 6290 | /* Since the extension registry is manipulable via Python code, |
| 6291 | * confirm that pair is really a 2-tuple of strings. |
| 6292 | */ |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6293 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) { |
| 6294 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6295 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6296 | |
| 6297 | module_name = PyTuple_GET_ITEM(pair, 0); |
| 6298 | if (!PyUnicode_Check(module_name)) { |
| 6299 | goto error; |
| 6300 | } |
| 6301 | |
| 6302 | class_name = PyTuple_GET_ITEM(pair, 1); |
| 6303 | if (!PyUnicode_Check(class_name)) { |
| 6304 | goto error; |
| 6305 | } |
| 6306 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6307 | /* Load the object. */ |
| 6308 | obj = find_class(self, module_name, class_name); |
| 6309 | if (obj == NULL) { |
| 6310 | Py_DECREF(py_code); |
| 6311 | return -1; |
| 6312 | } |
| 6313 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6314 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6315 | Py_DECREF(py_code); |
| 6316 | if (code < 0) { |
| 6317 | Py_DECREF(obj); |
| 6318 | return -1; |
| 6319 | } |
| 6320 | PDATA_PUSH(self->stack, obj, -1); |
| 6321 | return 0; |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6322 | |
| 6323 | error: |
| 6324 | Py_DECREF(py_code); |
| 6325 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 6326 | "isn't a 2-tuple of strings", code); |
| 6327 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6328 | } |
| 6329 | |
| 6330 | static int |
| 6331 | load_put(UnpicklerObject *self) |
| 6332 | { |
| 6333 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6334 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6335 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 6336 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6337 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6338 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6339 | return -1; |
| 6340 | if (len < 2) |
| 6341 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6342 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6343 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6344 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6345 | |
| 6346 | key = PyLong_FromString(s, NULL, 10); |
| 6347 | if (key == NULL) |
| 6348 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6349 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6350 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6351 | if (idx < 0) { |
| 6352 | if (!PyErr_Occurred()) |
| 6353 | PyErr_SetString(PyExc_ValueError, |
| 6354 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6355 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6356 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6357 | |
| 6358 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6359 | } |
| 6360 | |
| 6361 | static int |
| 6362 | load_binput(UnpicklerObject *self) |
| 6363 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6364 | PyObject *value; |
| 6365 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6366 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6367 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6368 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6369 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6370 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6371 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6372 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6373 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6374 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6375 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6376 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6377 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6378 | } |
| 6379 | |
| 6380 | static int |
| 6381 | load_long_binput(UnpicklerObject *self) |
| 6382 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6383 | PyObject *value; |
| 6384 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6385 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6386 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6387 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6388 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6389 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6390 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6391 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6392 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6393 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6394 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6395 | if (idx < 0) { |
| 6396 | PyErr_SetString(PyExc_ValueError, |
| 6397 | "negative LONG_BINPUT argument"); |
| 6398 | return -1; |
| 6399 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6400 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6401 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6402 | } |
| 6403 | |
| 6404 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6405 | load_memoize(UnpicklerObject *self) |
| 6406 | { |
| 6407 | PyObject *value; |
| 6408 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6409 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6410 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6411 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 6412 | |
| 6413 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 6414 | } |
| 6415 | |
| 6416 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6417 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6418 | { |
| 6419 | PyObject *value; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6420 | PyObject *slice; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6421 | PyObject *list; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6422 | PyObject *result; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6423 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6424 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6425 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6426 | if (x > len || x <= self->stack->fence) |
| 6427 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6428 | if (len == x) /* nothing to do */ |
| 6429 | return 0; |
| 6430 | |
| 6431 | list = self->stack->data[x - 1]; |
| 6432 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6433 | if (PyList_CheckExact(list)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6434 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6435 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6436 | |
| 6437 | slice = Pdata_poplist(self->stack, x); |
| 6438 | if (!slice) |
| 6439 | return -1; |
| 6440 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6441 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6442 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6443 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6444 | } |
| 6445 | else { |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6446 | PyObject *extend_func; |
| 6447 | _Py_IDENTIFIER(extend); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6448 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6449 | extend_func = _PyObject_GetAttrId(list, &PyId_extend); |
| 6450 | if (extend_func != NULL) { |
| 6451 | slice = Pdata_poplist(self->stack, x); |
| 6452 | if (!slice) { |
| 6453 | Py_DECREF(extend_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6454 | return -1; |
| 6455 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6456 | result = _Pickle_FastCall(extend_func, slice); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6457 | Py_DECREF(extend_func); |
| 6458 | if (result == NULL) |
| 6459 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6460 | Py_DECREF(result); |
| 6461 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6462 | else { |
| 6463 | PyObject *append_func; |
| 6464 | _Py_IDENTIFIER(append); |
| 6465 | |
| 6466 | /* Even if the PEP 307 requires extend() and append() methods, |
| 6467 | fall back on append() if the object has no extend() method |
| 6468 | for backward compatibility. */ |
| 6469 | PyErr_Clear(); |
| 6470 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
| 6471 | if (append_func == NULL) |
| 6472 | return -1; |
| 6473 | for (i = x; i < len; i++) { |
| 6474 | value = self->stack->data[i]; |
| 6475 | result = _Pickle_FastCall(append_func, value); |
| 6476 | if (result == NULL) { |
| 6477 | Pdata_clear(self->stack, i + 1); |
| 6478 | Py_SIZE(self->stack) = x; |
| 6479 | Py_DECREF(append_func); |
| 6480 | return -1; |
| 6481 | } |
| 6482 | Py_DECREF(result); |
| 6483 | } |
| 6484 | Py_SIZE(self->stack) = x; |
| 6485 | Py_DECREF(append_func); |
| 6486 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6487 | } |
| 6488 | |
| 6489 | return 0; |
| 6490 | } |
| 6491 | |
| 6492 | static int |
| 6493 | load_append(UnpicklerObject *self) |
| 6494 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6495 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 6496 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6497 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6498 | } |
| 6499 | |
| 6500 | static int |
| 6501 | load_appends(UnpicklerObject *self) |
| 6502 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6503 | Py_ssize_t i = marker(self); |
| 6504 | if (i < 0) |
| 6505 | return -1; |
| 6506 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6507 | } |
| 6508 | |
| 6509 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6510 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6511 | { |
| 6512 | PyObject *value, *key; |
| 6513 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6514 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6515 | int status = 0; |
| 6516 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6517 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6518 | if (x > len || x <= self->stack->fence) |
| 6519 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6520 | if (len == x) /* nothing to do */ |
| 6521 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6522 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6523 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6524 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6525 | PyErr_SetString(st->UnpicklingError, |
| 6526 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6527 | return -1; |
| 6528 | } |
| 6529 | |
| 6530 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 6531 | that supports the __setitem__ attribute. */ |
| 6532 | dict = self->stack->data[x - 1]; |
| 6533 | |
| 6534 | for (i = x + 1; i < len; i += 2) { |
| 6535 | key = self->stack->data[i - 1]; |
| 6536 | value = self->stack->data[i]; |
| 6537 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 6538 | status = -1; |
| 6539 | break; |
| 6540 | } |
| 6541 | } |
| 6542 | |
| 6543 | Pdata_clear(self->stack, x); |
| 6544 | return status; |
| 6545 | } |
| 6546 | |
| 6547 | static int |
| 6548 | load_setitem(UnpicklerObject *self) |
| 6549 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6550 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | static int |
| 6554 | load_setitems(UnpicklerObject *self) |
| 6555 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6556 | Py_ssize_t i = marker(self); |
| 6557 | if (i < 0) |
| 6558 | return -1; |
| 6559 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6563 | load_additems(UnpicklerObject *self) |
| 6564 | { |
| 6565 | PyObject *set; |
| 6566 | Py_ssize_t mark, len, i; |
| 6567 | |
| 6568 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6569 | if (mark < 0) |
| 6570 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6571 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6572 | if (mark > len || mark <= self->stack->fence) |
| 6573 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6574 | if (len == mark) /* nothing to do */ |
| 6575 | return 0; |
| 6576 | |
| 6577 | set = self->stack->data[mark - 1]; |
| 6578 | |
| 6579 | if (PySet_Check(set)) { |
| 6580 | PyObject *items; |
| 6581 | int status; |
| 6582 | |
| 6583 | items = Pdata_poptuple(self->stack, mark); |
| 6584 | if (items == NULL) |
| 6585 | return -1; |
| 6586 | |
| 6587 | status = _PySet_Update(set, items); |
| 6588 | Py_DECREF(items); |
| 6589 | return status; |
| 6590 | } |
| 6591 | else { |
| 6592 | PyObject *add_func; |
| 6593 | _Py_IDENTIFIER(add); |
| 6594 | |
| 6595 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6596 | if (add_func == NULL) |
| 6597 | return -1; |
| 6598 | for (i = mark; i < len; i++) { |
| 6599 | PyObject *result; |
| 6600 | PyObject *item; |
| 6601 | |
| 6602 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6603 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6604 | if (result == NULL) { |
| 6605 | Pdata_clear(self->stack, i + 1); |
| 6606 | Py_SIZE(self->stack) = mark; |
| 6607 | return -1; |
| 6608 | } |
| 6609 | Py_DECREF(result); |
| 6610 | } |
| 6611 | Py_SIZE(self->stack) = mark; |
| 6612 | } |
| 6613 | |
| 6614 | return 0; |
| 6615 | } |
| 6616 | |
| 6617 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6618 | load_build(UnpicklerObject *self) |
| 6619 | { |
| 6620 | PyObject *state, *inst, *slotstate; |
| 6621 | PyObject *setstate; |
| 6622 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6623 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6624 | |
| 6625 | /* Stack is ... instance, state. We want to leave instance at |
| 6626 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6627 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6628 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6629 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6630 | |
| 6631 | PDATA_POP(self->stack, state); |
| 6632 | if (state == NULL) |
| 6633 | return -1; |
| 6634 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6635 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6636 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6637 | if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) { |
| 6638 | Py_DECREF(state); |
| 6639 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6640 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6641 | if (setstate != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6642 | PyObject *result; |
| 6643 | |
| 6644 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6645 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6646 | Py_DECREF(setstate); |
| 6647 | if (result == NULL) |
| 6648 | return -1; |
| 6649 | Py_DECREF(result); |
| 6650 | return 0; |
| 6651 | } |
| 6652 | |
| 6653 | /* A default __setstate__. First see whether state embeds a |
| 6654 | * slot state dict too (a proto 2 addition). |
| 6655 | */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 6656 | if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6657 | PyObject *tmp = state; |
| 6658 | |
| 6659 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6660 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6661 | Py_INCREF(state); |
| 6662 | Py_INCREF(slotstate); |
| 6663 | Py_DECREF(tmp); |
| 6664 | } |
| 6665 | else |
| 6666 | slotstate = NULL; |
| 6667 | |
| 6668 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6669 | if (state != Py_None) { |
| 6670 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6671 | PyObject *d_key, *d_value; |
| 6672 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6673 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6674 | |
| 6675 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6676 | PickleState *st = _Pickle_GetGlobalState(); |
| 6677 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6678 | goto error; |
| 6679 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6680 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6681 | if (dict == NULL) |
| 6682 | goto error; |
| 6683 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6684 | i = 0; |
| 6685 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6686 | /* normally the keys for instance attributes are |
| 6687 | interned. we should try to do that here. */ |
| 6688 | Py_INCREF(d_key); |
| 6689 | if (PyUnicode_CheckExact(d_key)) |
| 6690 | PyUnicode_InternInPlace(&d_key); |
| 6691 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6692 | Py_DECREF(d_key); |
| 6693 | goto error; |
| 6694 | } |
| 6695 | Py_DECREF(d_key); |
| 6696 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6697 | Py_DECREF(dict); |
| 6698 | } |
| 6699 | |
| 6700 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6701 | if (slotstate != NULL) { |
| 6702 | PyObject *d_key, *d_value; |
| 6703 | Py_ssize_t i; |
| 6704 | |
| 6705 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6706 | PickleState *st = _Pickle_GetGlobalState(); |
| 6707 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6708 | "slot state is not a dictionary"); |
| 6709 | goto error; |
| 6710 | } |
| 6711 | i = 0; |
| 6712 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6713 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6714 | goto error; |
| 6715 | } |
| 6716 | } |
| 6717 | |
| 6718 | if (0) { |
| 6719 | error: |
| 6720 | status = -1; |
| 6721 | } |
| 6722 | |
| 6723 | Py_DECREF(state); |
| 6724 | Py_XDECREF(slotstate); |
| 6725 | return status; |
| 6726 | } |
| 6727 | |
| 6728 | static int |
| 6729 | load_mark(UnpicklerObject *self) |
| 6730 | { |
| 6731 | |
| 6732 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6733 | * object stack and a mark stack. Here we push a mark onto the |
| 6734 | * mark stack. |
| 6735 | */ |
| 6736 | |
Sergey Fedoseev | 86b8991 | 2018-08-25 12:54:40 +0500 | [diff] [blame] | 6737 | if (self->num_marks >= self->marks_size) { |
Sergey Fedoseev | 90555ec | 2018-08-25 15:41:58 +0500 | [diff] [blame] | 6738 | size_t alloc = ((size_t)self->num_marks << 1) + 20; |
| 6739 | Py_ssize_t *marks_new = self->marks; |
| 6740 | PyMem_RESIZE(marks_new, Py_ssize_t, alloc); |
| 6741 | if (marks_new == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6742 | PyErr_NoMemory(); |
| 6743 | return -1; |
| 6744 | } |
Sergey Fedoseev | 90555ec | 2018-08-25 15:41:58 +0500 | [diff] [blame] | 6745 | self->marks = marks_new; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6746 | self->marks_size = (Py_ssize_t)alloc; |
| 6747 | } |
| 6748 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6749 | self->stack->mark_set = 1; |
| 6750 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6751 | |
| 6752 | return 0; |
| 6753 | } |
| 6754 | |
| 6755 | static int |
| 6756 | load_reduce(UnpicklerObject *self) |
| 6757 | { |
| 6758 | PyObject *callable = NULL; |
| 6759 | PyObject *argtup = NULL; |
| 6760 | PyObject *obj = NULL; |
| 6761 | |
| 6762 | PDATA_POP(self->stack, argtup); |
| 6763 | if (argtup == NULL) |
| 6764 | return -1; |
| 6765 | PDATA_POP(self->stack, callable); |
| 6766 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6767 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6768 | Py_DECREF(callable); |
| 6769 | } |
| 6770 | Py_DECREF(argtup); |
| 6771 | |
| 6772 | if (obj == NULL) |
| 6773 | return -1; |
| 6774 | |
| 6775 | PDATA_PUSH(self->stack, obj, -1); |
| 6776 | return 0; |
| 6777 | } |
| 6778 | |
| 6779 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6780 | * is the first opcode for protocols >= 2. |
| 6781 | */ |
| 6782 | static int |
| 6783 | load_proto(UnpicklerObject *self) |
| 6784 | { |
| 6785 | char *s; |
| 6786 | int i; |
| 6787 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6788 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6789 | return -1; |
| 6790 | |
| 6791 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6792 | if (i <= HIGHEST_PROTOCOL) { |
| 6793 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6794 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6795 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6796 | |
| 6797 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6798 | return -1; |
| 6799 | } |
| 6800 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6801 | static int |
| 6802 | load_frame(UnpicklerObject *self) |
| 6803 | { |
| 6804 | char *s; |
| 6805 | Py_ssize_t frame_len; |
| 6806 | |
| 6807 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6808 | return -1; |
| 6809 | |
| 6810 | frame_len = calc_binsize(s, 8); |
| 6811 | if (frame_len < 0) { |
| 6812 | PyErr_Format(PyExc_OverflowError, |
| 6813 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6814 | PY_SSIZE_T_MAX); |
| 6815 | return -1; |
| 6816 | } |
| 6817 | |
| 6818 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6819 | return -1; |
| 6820 | |
| 6821 | /* Rewind to start of frame */ |
| 6822 | self->next_read_idx -= frame_len; |
| 6823 | return 0; |
| 6824 | } |
| 6825 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6826 | static PyObject * |
| 6827 | load(UnpicklerObject *self) |
| 6828 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6829 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6830 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6831 | |
| 6832 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6833 | self->stack->mark_set = 0; |
| 6834 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6835 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6836 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6837 | Pdata_clear(self->stack, 0); |
| 6838 | |
| 6839 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6840 | #define OP(opcode, load_func) \ |
| 6841 | case opcode: if (load_func(self) < 0) break; continue; |
| 6842 | |
| 6843 | #define OP_ARG(opcode, load_func, arg) \ |
| 6844 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6845 | |
| 6846 | while (1) { |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6847 | if (_Unpickler_Read(self, &s, 1) < 0) { |
| 6848 | PickleState *st = _Pickle_GetGlobalState(); |
| 6849 | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
| 6850 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 6851 | } |
| 6852 | return NULL; |
| 6853 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6854 | |
| 6855 | switch ((enum opcode)s[0]) { |
| 6856 | OP(NONE, load_none) |
| 6857 | OP(BININT, load_binint) |
| 6858 | OP(BININT1, load_binint1) |
| 6859 | OP(BININT2, load_binint2) |
| 6860 | OP(INT, load_int) |
| 6861 | OP(LONG, load_long) |
| 6862 | OP_ARG(LONG1, load_counted_long, 1) |
| 6863 | OP_ARG(LONG4, load_counted_long, 4) |
| 6864 | OP(FLOAT, load_float) |
| 6865 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6866 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6867 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6868 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 6869 | OP(BYTEARRAY8, load_counted_bytearray) |
| 6870 | OP(NEXT_BUFFER, load_next_buffer) |
| 6871 | OP(READONLY_BUFFER, load_readonly_buffer) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6872 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6873 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6874 | OP(STRING, load_string) |
| 6875 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6876 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6877 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6878 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6879 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6880 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6881 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6882 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6883 | OP(TUPLE, load_tuple) |
| 6884 | OP(EMPTY_LIST, load_empty_list) |
| 6885 | OP(LIST, load_list) |
| 6886 | OP(EMPTY_DICT, load_empty_dict) |
| 6887 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6888 | OP(EMPTY_SET, load_empty_set) |
| 6889 | OP(ADDITEMS, load_additems) |
| 6890 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6891 | OP(OBJ, load_obj) |
| 6892 | OP(INST, load_inst) |
| 6893 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6894 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6895 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6896 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6897 | OP(APPEND, load_append) |
| 6898 | OP(APPENDS, load_appends) |
| 6899 | OP(BUILD, load_build) |
| 6900 | OP(DUP, load_dup) |
| 6901 | OP(BINGET, load_binget) |
| 6902 | OP(LONG_BINGET, load_long_binget) |
| 6903 | OP(GET, load_get) |
| 6904 | OP(MARK, load_mark) |
| 6905 | OP(BINPUT, load_binput) |
| 6906 | OP(LONG_BINPUT, load_long_binput) |
| 6907 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6908 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6909 | OP(POP, load_pop) |
| 6910 | OP(POP_MARK, load_pop_mark) |
| 6911 | OP(SETITEM, load_setitem) |
| 6912 | OP(SETITEMS, load_setitems) |
| 6913 | OP(PERSID, load_persid) |
| 6914 | OP(BINPERSID, load_binpersid) |
| 6915 | OP(REDUCE, load_reduce) |
| 6916 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6917 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6918 | OP_ARG(EXT1, load_extension, 1) |
| 6919 | OP_ARG(EXT2, load_extension, 2) |
| 6920 | OP_ARG(EXT4, load_extension, 4) |
| 6921 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6922 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6923 | |
| 6924 | case STOP: |
| 6925 | break; |
| 6926 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6927 | default: |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6928 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6929 | PickleState *st = _Pickle_GetGlobalState(); |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6930 | unsigned char c = (unsigned char) *s; |
| 6931 | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
| 6932 | PyErr_Format(st->UnpicklingError, |
| 6933 | "invalid load key, '%c'.", c); |
| 6934 | } |
| 6935 | else { |
| 6936 | PyErr_Format(st->UnpicklingError, |
| 6937 | "invalid load key, '\\x%02x'.", c); |
| 6938 | } |
| 6939 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6940 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6941 | } |
| 6942 | |
| 6943 | break; /* and we are done! */ |
| 6944 | } |
| 6945 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6946 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6947 | return NULL; |
| 6948 | } |
| 6949 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6950 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6951 | return NULL; |
| 6952 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6953 | PDATA_POP(self->stack, value); |
| 6954 | return value; |
| 6955 | } |
| 6956 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6957 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6958 | |
| 6959 | _pickle.Unpickler.load |
| 6960 | |
| 6961 | Load a pickle. |
| 6962 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6963 | Read a pickled object representation from the open file object given |
| 6964 | in the constructor, and return the reconstituted object hierarchy |
| 6965 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6966 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6967 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6968 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6969 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6970 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6971 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6972 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6973 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6974 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6975 | segfaulting if a subclass overridden __init__ with a function that does |
| 6976 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6977 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6978 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6979 | PickleState *st = _Pickle_GetGlobalState(); |
| 6980 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6981 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6982 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6983 | return NULL; |
| 6984 | } |
| 6985 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6986 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6987 | } |
| 6988 | |
| 6989 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6990 | function is used for loading any global (i.e., functions), not just |
| 6991 | classes. The name is kept only for backward compatibility. */ |
| 6992 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6993 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6994 | |
| 6995 | _pickle.Unpickler.find_class |
| 6996 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6997 | module_name: object |
| 6998 | global_name: object |
| 6999 | / |
| 7000 | |
| 7001 | Return an object from a specified module. |
| 7002 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7003 | If necessary, the module will be imported. Subclasses may override |
| 7004 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 7005 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7006 | |
| 7007 | This method is called whenever a class or a function object is |
| 7008 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7009 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7010 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7011 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7012 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 7013 | PyObject *module_name, |
| 7014 | PyObject *global_name) |
| 7015 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7016 | { |
| 7017 | PyObject *global; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7018 | PyObject *module; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7019 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 7020 | if (PySys_Audit("pickle.find_class", "OO", |
| 7021 | module_name, global_name) < 0) { |
| 7022 | return NULL; |
| 7023 | } |
| 7024 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7025 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 7026 | Python 3.x. We do this only with old pickle protocols and when the |
| 7027 | user has not disabled the feature. */ |
| 7028 | if (self->proto < 3 && self->fix_imports) { |
| 7029 | PyObject *key; |
| 7030 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7031 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7032 | |
| 7033 | /* Check if the global (i.e., a function or a class) was renamed |
| 7034 | or moved to another module. */ |
| 7035 | key = PyTuple_Pack(2, module_name, global_name); |
| 7036 | if (key == NULL) |
| 7037 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7038 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7039 | Py_DECREF(key); |
| 7040 | if (item) { |
| 7041 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 7042 | PyErr_Format(PyExc_RuntimeError, |
| 7043 | "_compat_pickle.NAME_MAPPING values should be " |
| 7044 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 7045 | return NULL; |
| 7046 | } |
| 7047 | module_name = PyTuple_GET_ITEM(item, 0); |
| 7048 | global_name = PyTuple_GET_ITEM(item, 1); |
| 7049 | if (!PyUnicode_Check(module_name) || |
| 7050 | !PyUnicode_Check(global_name)) { |
| 7051 | PyErr_Format(PyExc_RuntimeError, |
| 7052 | "_compat_pickle.NAME_MAPPING values should be " |
| 7053 | "pairs of str, not (%.200s, %.200s)", |
| 7054 | Py_TYPE(module_name)->tp_name, |
| 7055 | Py_TYPE(global_name)->tp_name); |
| 7056 | return NULL; |
| 7057 | } |
| 7058 | } |
| 7059 | else if (PyErr_Occurred()) { |
| 7060 | return NULL; |
| 7061 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 7062 | else { |
| 7063 | /* Check if the module was renamed. */ |
| 7064 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 7065 | if (item) { |
| 7066 | if (!PyUnicode_Check(item)) { |
| 7067 | PyErr_Format(PyExc_RuntimeError, |
| 7068 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 7069 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 7070 | return NULL; |
| 7071 | } |
| 7072 | module_name = item; |
| 7073 | } |
| 7074 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7075 | return NULL; |
| 7076 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7077 | } |
| 7078 | } |
| 7079 | |
tjb900 | 4371c0a | 2019-02-18 23:30:51 +0800 | [diff] [blame] | 7080 | /* |
| 7081 | * we don't use PyImport_GetModule here, because it can return partially- |
| 7082 | * initialised modules, which then cause the getattribute to fail. |
| 7083 | */ |
| 7084 | module = PyImport_Import(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7085 | if (module == NULL) { |
tjb900 | 4371c0a | 2019-02-18 23:30:51 +0800 | [diff] [blame] | 7086 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7087 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 7088 | global = getattribute(module, global_name, self->proto >= 4); |
| 7089 | Py_DECREF(module); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7090 | return global; |
| 7091 | } |
| 7092 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 7093 | /*[clinic input] |
| 7094 | |
| 7095 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 7096 | |
| 7097 | Returns size in memory, in bytes. |
| 7098 | [clinic start generated code]*/ |
| 7099 | |
| 7100 | static Py_ssize_t |
| 7101 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 7102 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 7103 | { |
| 7104 | Py_ssize_t res; |
| 7105 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 7106 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 7107 | if (self->memo != NULL) |
| 7108 | res += self->memo_size * sizeof(PyObject *); |
| 7109 | if (self->marks != NULL) |
| 7110 | res += self->marks_size * sizeof(Py_ssize_t); |
| 7111 | if (self->input_line != NULL) |
| 7112 | res += strlen(self->input_line) + 1; |
| 7113 | if (self->encoding != NULL) |
| 7114 | res += strlen(self->encoding) + 1; |
| 7115 | if (self->errors != NULL) |
| 7116 | res += strlen(self->errors) + 1; |
| 7117 | return res; |
| 7118 | } |
| 7119 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7120 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7121 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 7122 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 7123 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7124 | {NULL, NULL} /* sentinel */ |
| 7125 | }; |
| 7126 | |
| 7127 | static void |
| 7128 | Unpickler_dealloc(UnpicklerObject *self) |
| 7129 | { |
| 7130 | PyObject_GC_UnTrack((PyObject *)self); |
| 7131 | Py_XDECREF(self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7132 | Py_XDECREF(self->readinto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7133 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 7134 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7135 | Py_XDECREF(self->stack); |
| 7136 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7137 | Py_XDECREF(self->buffers); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7138 | if (self->buffer.buf != NULL) { |
| 7139 | PyBuffer_Release(&self->buffer); |
| 7140 | self->buffer.buf = NULL; |
| 7141 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7142 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7143 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7144 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7145 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 7146 | PyMem_Free(self->encoding); |
| 7147 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7148 | |
| 7149 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 7150 | } |
| 7151 | |
| 7152 | static int |
| 7153 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 7154 | { |
| 7155 | Py_VISIT(self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7156 | Py_VISIT(self->readinto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7157 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 7158 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7159 | Py_VISIT(self->stack); |
| 7160 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7161 | Py_VISIT(self->buffers); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7162 | return 0; |
| 7163 | } |
| 7164 | |
| 7165 | static int |
| 7166 | Unpickler_clear(UnpicklerObject *self) |
| 7167 | { |
| 7168 | Py_CLEAR(self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7169 | Py_CLEAR(self->readinto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7170 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 7171 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7172 | Py_CLEAR(self->stack); |
| 7173 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7174 | Py_CLEAR(self->buffers); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7175 | if (self->buffer.buf != NULL) { |
| 7176 | PyBuffer_Release(&self->buffer); |
| 7177 | self->buffer.buf = NULL; |
| 7178 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7179 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7180 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7181 | PyMem_Free(self->marks); |
| 7182 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7183 | PyMem_Free(self->input_line); |
| 7184 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 7185 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7186 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 7187 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7188 | self->errors = NULL; |
| 7189 | |
| 7190 | return 0; |
| 7191 | } |
| 7192 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7193 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7194 | |
| 7195 | _pickle.Unpickler.__init__ |
| 7196 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7197 | file: object |
| 7198 | * |
| 7199 | fix_imports: bool = True |
| 7200 | encoding: str = 'ASCII' |
| 7201 | errors: str = 'strict' |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7202 | buffers: object = NULL |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7203 | |
| 7204 | This takes a binary file for reading a pickle data stream. |
| 7205 | |
| 7206 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7207 | protocol argument is needed. Bytes past the pickled object's |
| 7208 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7209 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7210 | The argument *file* must have two methods, a read() method that takes |
| 7211 | an integer argument, and a readline() method that requires no |
| 7212 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7213 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7214 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7215 | |
| 7216 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7217 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7218 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7219 | 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] | 7220 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7221 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7222 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7223 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7224 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7225 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7226 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7227 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 7228 | int fix_imports, const char *encoding, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7229 | const char *errors, PyObject *buffers) |
| 7230 | /*[clinic end generated code: output=09f0192649ea3f85 input=da4b62d9edb68700]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7231 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 7232 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7233 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7234 | /* In case of multiple __init__() calls, clear previous content. */ |
| 7235 | if (self->read != NULL) |
| 7236 | (void)Unpickler_clear(self); |
| 7237 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7238 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7239 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7240 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7241 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7242 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7243 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7244 | if (_Unpickler_SetBuffers(self, buffers) < 0) |
| 7245 | return -1; |
| 7246 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7247 | self->fix_imports = fix_imports; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7248 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7249 | if (init_method_ref((PyObject *)self, &PyId_persistent_load, |
| 7250 | &self->pers_func, &self->pers_func_self) < 0) |
| 7251 | { |
| 7252 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
| 7255 | self->stack = (Pdata *)Pdata_New(); |
| 7256 | if (self->stack == NULL) |
Zackery Spytz | 4b430e5 | 2018-09-28 23:48:46 -0600 | [diff] [blame] | 7257 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7258 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7259 | self->memo_size = 32; |
| 7260 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7261 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7262 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7263 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7264 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 7265 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7266 | return 0; |
| 7267 | } |
| 7268 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7269 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7270 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 7271 | * avoid breaking code like: |
| 7272 | * unpickler.memo.clear() |
| 7273 | * and |
| 7274 | * unpickler.memo = saved_memo |
| 7275 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 7276 | * it. Note that we don't implement the entire mapping API here. This is |
| 7277 | * intentional, as these should be treated as black-box implementation details. |
| 7278 | * |
| 7279 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 7280 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7281 | */ |
| 7282 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7283 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7284 | _pickle.UnpicklerMemoProxy.clear |
| 7285 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7286 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7287 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7288 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7289 | static PyObject * |
| 7290 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7291 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7292 | { |
| 7293 | _Unpickler_MemoCleanup(self->unpickler); |
| 7294 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 7295 | if (self->unpickler->memo == NULL) |
| 7296 | return NULL; |
| 7297 | Py_RETURN_NONE; |
| 7298 | } |
| 7299 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7300 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7301 | _pickle.UnpicklerMemoProxy.copy |
| 7302 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7303 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7304 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7305 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7306 | static PyObject * |
| 7307 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7308 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7309 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7310 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7311 | PyObject *new_memo = PyDict_New(); |
| 7312 | if (new_memo == NULL) |
| 7313 | return NULL; |
| 7314 | |
| 7315 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 7316 | int status; |
| 7317 | PyObject *key, *value; |
| 7318 | |
| 7319 | value = self->unpickler->memo[i]; |
| 7320 | if (value == NULL) |
| 7321 | continue; |
| 7322 | |
| 7323 | key = PyLong_FromSsize_t(i); |
| 7324 | if (key == NULL) |
| 7325 | goto error; |
| 7326 | status = PyDict_SetItem(new_memo, key, value); |
| 7327 | Py_DECREF(key); |
| 7328 | if (status < 0) |
| 7329 | goto error; |
| 7330 | } |
| 7331 | return new_memo; |
| 7332 | |
| 7333 | error: |
| 7334 | Py_DECREF(new_memo); |
| 7335 | return NULL; |
| 7336 | } |
| 7337 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7338 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7339 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 7340 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7341 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7342 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7343 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7344 | static PyObject * |
| 7345 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7346 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7347 | { |
| 7348 | PyObject *reduce_value; |
| 7349 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7350 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7351 | if (contents == NULL) |
| 7352 | return NULL; |
| 7353 | |
| 7354 | reduce_value = PyTuple_New(2); |
| 7355 | if (reduce_value == NULL) { |
| 7356 | Py_DECREF(contents); |
| 7357 | return NULL; |
| 7358 | } |
| 7359 | constructor_args = PyTuple_New(1); |
| 7360 | if (constructor_args == NULL) { |
| 7361 | Py_DECREF(contents); |
| 7362 | Py_DECREF(reduce_value); |
| 7363 | return NULL; |
| 7364 | } |
| 7365 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 7366 | Py_INCREF((PyObject *)&PyDict_Type); |
| 7367 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 7368 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 7369 | return reduce_value; |
| 7370 | } |
| 7371 | |
| 7372 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7373 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 7374 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 7375 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7376 | {NULL, NULL} /* sentinel */ |
| 7377 | }; |
| 7378 | |
| 7379 | static void |
| 7380 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 7381 | { |
| 7382 | PyObject_GC_UnTrack(self); |
| 7383 | Py_XDECREF(self->unpickler); |
| 7384 | PyObject_GC_Del((PyObject *)self); |
| 7385 | } |
| 7386 | |
| 7387 | static int |
| 7388 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 7389 | visitproc visit, void *arg) |
| 7390 | { |
| 7391 | Py_VISIT(self->unpickler); |
| 7392 | return 0; |
| 7393 | } |
| 7394 | |
| 7395 | static int |
| 7396 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 7397 | { |
| 7398 | Py_CLEAR(self->unpickler); |
| 7399 | return 0; |
| 7400 | } |
| 7401 | |
| 7402 | static PyTypeObject UnpicklerMemoProxyType = { |
| 7403 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7404 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 7405 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 7406 | 0, |
| 7407 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7408 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7409 | 0, /* tp_getattr */ |
| 7410 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7411 | 0, /* tp_as_async */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7412 | 0, /* tp_repr */ |
| 7413 | 0, /* tp_as_number */ |
| 7414 | 0, /* tp_as_sequence */ |
| 7415 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 7416 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7417 | 0, /* tp_call */ |
| 7418 | 0, /* tp_str */ |
| 7419 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7420 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 7421 | 0, /* tp_as_buffer */ |
| 7422 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 7423 | 0, /* tp_doc */ |
| 7424 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 7425 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 7426 | 0, /* tp_richcompare */ |
| 7427 | 0, /* tp_weaklistoffset */ |
| 7428 | 0, /* tp_iter */ |
| 7429 | 0, /* tp_iternext */ |
| 7430 | unpicklerproxy_methods, /* tp_methods */ |
| 7431 | }; |
| 7432 | |
| 7433 | static PyObject * |
| 7434 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 7435 | { |
| 7436 | UnpicklerMemoProxyObject *self; |
| 7437 | |
| 7438 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 7439 | &UnpicklerMemoProxyType); |
| 7440 | if (self == NULL) |
| 7441 | return NULL; |
| 7442 | Py_INCREF(unpickler); |
| 7443 | self->unpickler = unpickler; |
| 7444 | PyObject_GC_Track(self); |
| 7445 | return (PyObject *)self; |
| 7446 | } |
| 7447 | |
| 7448 | /*****************************************************************************/ |
| 7449 | |
| 7450 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7451 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7452 | Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7453 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7454 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7455 | } |
| 7456 | |
| 7457 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7458 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7459 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7460 | PyObject **new_memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7461 | size_t new_memo_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7462 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7463 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7464 | PyErr_SetString(PyExc_TypeError, |
| 7465 | "attribute deletion is not supported"); |
| 7466 | return -1; |
| 7467 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7468 | |
| 7469 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 7470 | UnpicklerObject *unpickler = |
| 7471 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 7472 | |
| 7473 | new_memo_size = unpickler->memo_size; |
| 7474 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7475 | if (new_memo == NULL) |
| 7476 | return -1; |
| 7477 | |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7478 | for (size_t i = 0; i < new_memo_size; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7479 | Py_XINCREF(unpickler->memo[i]); |
| 7480 | new_memo[i] = unpickler->memo[i]; |
| 7481 | } |
| 7482 | } |
| 7483 | else if (PyDict_Check(obj)) { |
| 7484 | Py_ssize_t i = 0; |
| 7485 | PyObject *key, *value; |
| 7486 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 7487 | new_memo_size = PyDict_GET_SIZE(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7488 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7489 | if (new_memo == NULL) |
| 7490 | return -1; |
| 7491 | |
| 7492 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 7493 | Py_ssize_t idx; |
| 7494 | if (!PyLong_Check(key)) { |
| 7495 | PyErr_SetString(PyExc_TypeError, |
| 7496 | "memo key must be integers"); |
| 7497 | goto error; |
| 7498 | } |
| 7499 | idx = PyLong_AsSsize_t(key); |
| 7500 | if (idx == -1 && PyErr_Occurred()) |
| 7501 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7502 | if (idx < 0) { |
| 7503 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 7504 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7505 | goto error; |
| 7506 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7507 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 7508 | goto error; |
| 7509 | } |
| 7510 | } |
| 7511 | else { |
| 7512 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 7513 | "'memo' attribute must be an UnpicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7514 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7515 | return -1; |
| 7516 | } |
| 7517 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7518 | _Unpickler_MemoCleanup(self); |
| 7519 | self->memo_size = new_memo_size; |
| 7520 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7521 | |
| 7522 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7523 | |
| 7524 | error: |
| 7525 | if (new_memo_size) { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7526 | for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7527 | Py_XDECREF(new_memo[i]); |
| 7528 | } |
| 7529 | PyMem_FREE(new_memo); |
| 7530 | } |
| 7531 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7532 | } |
| 7533 | |
| 7534 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7535 | Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7536 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7537 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7538 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7539 | return NULL; |
| 7540 | } |
| 7541 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7542 | } |
| 7543 | |
| 7544 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7545 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7546 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7547 | if (value == NULL) { |
| 7548 | PyErr_SetString(PyExc_TypeError, |
| 7549 | "attribute deletion is not supported"); |
| 7550 | return -1; |
| 7551 | } |
| 7552 | if (!PyCallable_Check(value)) { |
| 7553 | PyErr_SetString(PyExc_TypeError, |
| 7554 | "persistent_load must be a callable taking " |
| 7555 | "one argument"); |
| 7556 | return -1; |
| 7557 | } |
| 7558 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7559 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7560 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 7561 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7562 | |
| 7563 | return 0; |
| 7564 | } |
| 7565 | |
| 7566 | static PyGetSetDef Unpickler_getsets[] = { |
| 7567 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 7568 | {"persistent_load", (getter)Unpickler_get_persload, |
| 7569 | (setter)Unpickler_set_persload}, |
| 7570 | {NULL} |
| 7571 | }; |
| 7572 | |
| 7573 | static PyTypeObject Unpickler_Type = { |
| 7574 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7575 | "_pickle.Unpickler", /*tp_name*/ |
| 7576 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 7577 | 0, /*tp_itemsize*/ |
| 7578 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7579 | 0, /*tp_vectorcall_offset*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7580 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7581 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7582 | 0, /*tp_as_async*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7583 | 0, /*tp_repr*/ |
| 7584 | 0, /*tp_as_number*/ |
| 7585 | 0, /*tp_as_sequence*/ |
| 7586 | 0, /*tp_as_mapping*/ |
| 7587 | 0, /*tp_hash*/ |
| 7588 | 0, /*tp_call*/ |
| 7589 | 0, /*tp_str*/ |
| 7590 | 0, /*tp_getattro*/ |
| 7591 | 0, /*tp_setattro*/ |
| 7592 | 0, /*tp_as_buffer*/ |
| 7593 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7594 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7595 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7596 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7597 | 0, /*tp_richcompare*/ |
| 7598 | 0, /*tp_weaklistoffset*/ |
| 7599 | 0, /*tp_iter*/ |
| 7600 | 0, /*tp_iternext*/ |
| 7601 | Unpickler_methods, /*tp_methods*/ |
| 7602 | 0, /*tp_members*/ |
| 7603 | Unpickler_getsets, /*tp_getset*/ |
| 7604 | 0, /*tp_base*/ |
| 7605 | 0, /*tp_dict*/ |
| 7606 | 0, /*tp_descr_get*/ |
| 7607 | 0, /*tp_descr_set*/ |
| 7608 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7609 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7610 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7611 | PyType_GenericNew, /*tp_new*/ |
| 7612 | PyObject_GC_Del, /*tp_free*/ |
| 7613 | 0, /*tp_is_gc*/ |
| 7614 | }; |
| 7615 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7616 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7617 | |
| 7618 | _pickle.dump |
| 7619 | |
| 7620 | obj: object |
| 7621 | file: object |
| 7622 | protocol: object = NULL |
| 7623 | * |
| 7624 | fix_imports: bool = True |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7625 | buffer_callback: object = NULL |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7626 | |
| 7627 | Write a pickled representation of obj to the open file object file. |
| 7628 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7629 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7630 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7631 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7632 | The optional *protocol* argument tells the pickler to use the given |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7633 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7634 | protocol is 4. It was introduced in Python 3.4, it is incompatible |
| 7635 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7636 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7637 | Specifying a negative protocol version selects the highest protocol |
| 7638 | version supported. The higher the protocol used, the more recent the |
| 7639 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7640 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7641 | The *file* argument must have a write() method that accepts a single |
| 7642 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7643 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7644 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7645 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7646 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7647 | to map the new Python 3 names to the old module names used in Python |
| 7648 | 2, so that the pickle data stream is readable with Python 2. |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7649 | |
| 7650 | If *buffer_callback* is None (the default), buffer views are serialized |
| 7651 | into *file* as part of the pickle stream. It is an error if |
| 7652 | *buffer_callback* is not None and *protocol* is None or smaller than 5. |
| 7653 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7654 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7655 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7656 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7657 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7658 | PyObject *protocol, int fix_imports, |
| 7659 | PyObject *buffer_callback) |
| 7660 | /*[clinic end generated code: output=706186dba996490c input=2f035f02cc0f9547]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7661 | { |
| 7662 | PicklerObject *pickler = _Pickler_New(); |
| 7663 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7664 | if (pickler == NULL) |
| 7665 | return NULL; |
| 7666 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7667 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7668 | goto error; |
| 7669 | |
| 7670 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7671 | goto error; |
| 7672 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7673 | if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0) |
| 7674 | goto error; |
| 7675 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7676 | if (dump(pickler, obj) < 0) |
| 7677 | goto error; |
| 7678 | |
| 7679 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7680 | goto error; |
| 7681 | |
| 7682 | Py_DECREF(pickler); |
| 7683 | Py_RETURN_NONE; |
| 7684 | |
| 7685 | error: |
| 7686 | Py_XDECREF(pickler); |
| 7687 | return NULL; |
| 7688 | } |
| 7689 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7690 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7691 | |
| 7692 | _pickle.dumps |
| 7693 | |
| 7694 | obj: object |
| 7695 | protocol: object = NULL |
| 7696 | * |
| 7697 | fix_imports: bool = True |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7698 | buffer_callback: object = NULL |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7699 | |
| 7700 | Return the pickled representation of the object as a bytes object. |
| 7701 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7702 | The optional *protocol* argument tells the pickler to use the given |
| 7703 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7704 | protocol is 4. It was introduced in Python 3.4, it is incompatible |
| 7705 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7706 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7707 | Specifying a negative protocol version selects the highest protocol |
| 7708 | version supported. The higher the protocol used, the more recent the |
| 7709 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7710 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7711 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7712 | try to map the new Python 3 names to the old module names used in |
| 7713 | Python 2, so that the pickle data stream is readable with Python 2. |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7714 | |
| 7715 | If *buffer_callback* is None (the default), buffer views are serialized |
| 7716 | into *file* as part of the pickle stream. It is an error if |
| 7717 | *buffer_callback* is not None and *protocol* is None or smaller than 5. |
| 7718 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7719 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7720 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7721 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7722 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7723 | int fix_imports, PyObject *buffer_callback) |
| 7724 | /*[clinic end generated code: output=fbab0093a5580fdf input=001f167df711b9f1]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7725 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7726 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7727 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7728 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7729 | if (pickler == NULL) |
| 7730 | return NULL; |
| 7731 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7732 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7733 | goto error; |
| 7734 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7735 | if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0) |
| 7736 | goto error; |
| 7737 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7738 | if (dump(pickler, obj) < 0) |
| 7739 | goto error; |
| 7740 | |
| 7741 | result = _Pickler_GetString(pickler); |
| 7742 | Py_DECREF(pickler); |
| 7743 | return result; |
| 7744 | |
| 7745 | error: |
| 7746 | Py_XDECREF(pickler); |
| 7747 | return NULL; |
| 7748 | } |
| 7749 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7750 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7751 | |
| 7752 | _pickle.load |
| 7753 | |
| 7754 | file: object |
| 7755 | * |
| 7756 | fix_imports: bool = True |
| 7757 | encoding: str = 'ASCII' |
| 7758 | errors: str = 'strict' |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7759 | buffers: object = NULL |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7760 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7761 | 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] | 7762 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7763 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7764 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7765 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7766 | The protocol version of the pickle is detected automatically, so no |
| 7767 | protocol argument is needed. Bytes past the pickled object's |
| 7768 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7769 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7770 | The argument *file* must have two methods, a read() method that takes |
| 7771 | an integer argument, and a readline() method that requires no |
| 7772 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7773 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7774 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7775 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7776 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7777 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7778 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7779 | map the old Python 2 names to the new names used in Python 3. The |
| 7780 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7781 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7782 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7783 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7784 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7785 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7786 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7787 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7788 | const char *encoding, const char *errors, |
| 7789 | PyObject *buffers) |
| 7790 | /*[clinic end generated code: output=250452d141c23e76 input=29fae982fe778156]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7791 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7792 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7793 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7794 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7795 | if (unpickler == NULL) |
| 7796 | return NULL; |
| 7797 | |
| 7798 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7799 | goto error; |
| 7800 | |
| 7801 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7802 | goto error; |
| 7803 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7804 | if (_Unpickler_SetBuffers(unpickler, buffers) < 0) |
| 7805 | goto error; |
| 7806 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7807 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7808 | |
| 7809 | result = load(unpickler); |
| 7810 | Py_DECREF(unpickler); |
| 7811 | return result; |
| 7812 | |
| 7813 | error: |
| 7814 | Py_XDECREF(unpickler); |
| 7815 | return NULL; |
| 7816 | } |
| 7817 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7818 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7819 | |
| 7820 | _pickle.loads |
| 7821 | |
| 7822 | data: object |
| 7823 | * |
| 7824 | fix_imports: bool = True |
| 7825 | encoding: str = 'ASCII' |
| 7826 | errors: str = 'strict' |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7827 | buffers: object = NULL |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7828 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7829 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7830 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7831 | The protocol version of the pickle is detected automatically, so no |
| 7832 | protocol argument is needed. Bytes past the pickled object's |
| 7833 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7834 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7835 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7836 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7837 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7838 | map the old Python 2 names to the new names used in Python 3. The |
| 7839 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7840 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7841 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7842 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7843 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7844 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7845 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7846 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7847 | const char *encoding, const char *errors, |
| 7848 | PyObject *buffers) |
| 7849 | /*[clinic end generated code: output=82ac1e6b588e6d02 input=c6004393f8276867]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7850 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7851 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7852 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7853 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7854 | if (unpickler == NULL) |
| 7855 | return NULL; |
| 7856 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7857 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7858 | goto error; |
| 7859 | |
| 7860 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7861 | goto error; |
| 7862 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7863 | if (_Unpickler_SetBuffers(unpickler, buffers) < 0) |
| 7864 | goto error; |
| 7865 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7866 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7867 | |
| 7868 | result = load(unpickler); |
| 7869 | Py_DECREF(unpickler); |
| 7870 | return result; |
| 7871 | |
| 7872 | error: |
| 7873 | Py_XDECREF(unpickler); |
| 7874 | return NULL; |
| 7875 | } |
| 7876 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7877 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7878 | _PICKLE_DUMP_METHODDEF |
| 7879 | _PICKLE_DUMPS_METHODDEF |
| 7880 | _PICKLE_LOAD_METHODDEF |
| 7881 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7882 | {NULL, NULL} /* sentinel */ |
| 7883 | }; |
| 7884 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7885 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7886 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7887 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7888 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7889 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7890 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7891 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7892 | static void |
| 7893 | pickle_free(PyObject *m) |
| 7894 | { |
| 7895 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7896 | } |
| 7897 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7898 | static int |
| 7899 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7900 | { |
| 7901 | PickleState *st = _Pickle_GetState(m); |
| 7902 | Py_VISIT(st->PickleError); |
| 7903 | Py_VISIT(st->PicklingError); |
| 7904 | Py_VISIT(st->UnpicklingError); |
| 7905 | Py_VISIT(st->dispatch_table); |
| 7906 | Py_VISIT(st->extension_registry); |
| 7907 | Py_VISIT(st->extension_cache); |
| 7908 | Py_VISIT(st->inverted_registry); |
| 7909 | Py_VISIT(st->name_mapping_2to3); |
| 7910 | Py_VISIT(st->import_mapping_2to3); |
| 7911 | Py_VISIT(st->name_mapping_3to2); |
| 7912 | Py_VISIT(st->import_mapping_3to2); |
| 7913 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7914 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7915 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7916 | } |
| 7917 | |
| 7918 | static struct PyModuleDef _picklemodule = { |
| 7919 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7920 | "_pickle", /* m_name */ |
| 7921 | pickle_module_doc, /* m_doc */ |
| 7922 | sizeof(PickleState), /* m_size */ |
| 7923 | pickle_methods, /* m_methods */ |
| 7924 | NULL, /* m_reload */ |
| 7925 | pickle_traverse, /* m_traverse */ |
| 7926 | pickle_clear, /* m_clear */ |
| 7927 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7928 | }; |
| 7929 | |
| 7930 | PyMODINIT_FUNC |
| 7931 | PyInit__pickle(void) |
| 7932 | { |
| 7933 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7934 | PickleState *st; |
| 7935 | |
| 7936 | m = PyState_FindModule(&_picklemodule); |
| 7937 | if (m) { |
| 7938 | Py_INCREF(m); |
| 7939 | return m; |
| 7940 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7941 | |
| 7942 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7943 | return NULL; |
| 7944 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7945 | return NULL; |
| 7946 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7947 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7948 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7949 | return NULL; |
| 7950 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7951 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7952 | |
| 7953 | /* Create the module and add the functions. */ |
| 7954 | m = PyModule_Create(&_picklemodule); |
| 7955 | if (m == NULL) |
| 7956 | return NULL; |
| 7957 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7958 | /* Add types */ |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7959 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7960 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7961 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7962 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7963 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7964 | return NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7965 | Py_INCREF(&PyPickleBuffer_Type); |
| 7966 | if (PyModule_AddObject(m, "PickleBuffer", |
| 7967 | (PyObject *)&PyPickleBuffer_Type) < 0) |
| 7968 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7969 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7970 | st = _Pickle_GetState(m); |
| 7971 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7972 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7973 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7974 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7975 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7976 | st->PicklingError = \ |
| 7977 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7978 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7979 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7980 | st->UnpicklingError = \ |
| 7981 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7982 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7983 | return NULL; |
| 7984 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7985 | Py_INCREF(st->PickleError); |
| 7986 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7987 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7988 | Py_INCREF(st->PicklingError); |
| 7989 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7990 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7991 | Py_INCREF(st->UnpicklingError); |
| 7992 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7993 | return NULL; |
| 7994 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7995 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7996 | return NULL; |
| 7997 | |
| 7998 | return m; |
| 7999 | } |