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" |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 12 | #include "structmember.h" // PyMemberDef |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 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 | |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [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 { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [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; |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 464 | Py_SET_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 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 491 | Py_SET_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 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 542 | Py_SET_SIZE(self, Py_SIZE(self) - 1); |
| 543 | return self->data[Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 544 | } |
| 545 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 546 | |
| 547 | static int |
| 548 | Pdata_push(Pdata *self, PyObject *obj) |
| 549 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 550 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 551 | return -1; |
| 552 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 553 | self->data[Py_SIZE(self)] = obj; |
| 554 | Py_SET_SIZE(self, Py_SIZE(self) + 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 559 | #define PDATA_PUSH(D, O, ER) do { \ |
| 560 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 561 | |
| 562 | /* Push an object on stack, adding a new reference to the object. */ |
| 563 | #define PDATA_APPEND(D, O, ER) do { \ |
| 564 | Py_INCREF((O)); \ |
| 565 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 566 | |
| 567 | static PyObject * |
| 568 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 569 | { |
| 570 | PyObject *tuple; |
| 571 | Py_ssize_t len, i, j; |
| 572 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 573 | if (start < self->fence) { |
| 574 | Pdata_stack_underflow(self); |
| 575 | return NULL; |
| 576 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 577 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 578 | tuple = PyTuple_New(len); |
| 579 | if (tuple == NULL) |
| 580 | return NULL; |
| 581 | for (i = start, j = 0; j < len; i++, j++) |
| 582 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 583 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 584 | Py_SET_SIZE(self, start); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 585 | return tuple; |
| 586 | } |
| 587 | |
| 588 | static PyObject * |
| 589 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 590 | { |
| 591 | PyObject *list; |
| 592 | Py_ssize_t len, i, j; |
| 593 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 594 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 595 | list = PyList_New(len); |
| 596 | if (list == NULL) |
| 597 | return NULL; |
| 598 | for (i = start, j = 0; j < len; i++, j++) |
| 599 | PyList_SET_ITEM(list, j, self->data[i]); |
| 600 | |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 601 | Py_SET_SIZE(self, start); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 602 | return list; |
| 603 | } |
| 604 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 605 | typedef struct { |
| 606 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 607 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 608 | } PyMemoEntry; |
| 609 | |
| 610 | typedef struct { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 611 | size_t mt_mask; |
| 612 | size_t mt_used; |
| 613 | size_t mt_allocated; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 614 | PyMemoEntry *mt_table; |
| 615 | } PyMemoTable; |
| 616 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 617 | typedef struct PicklerObject { |
| 618 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 619 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 620 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 621 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 622 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 623 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 624 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 625 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 626 | PyObject *reducer_override; /* hook for invoking user-defined callbacks |
| 627 | instead of save_global when pickling |
| 628 | functions and classes*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 629 | |
| 630 | PyObject *write; /* write() method of the output stream. */ |
| 631 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 632 | flushing to the stream. */ |
| 633 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 634 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 635 | int proto; /* Pickle protocol number, >= 0 */ |
| 636 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 637 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 638 | Py_ssize_t frame_start; /* Position in output_buffer where the |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 639 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 640 | is no frame currently open. */ |
| 641 | |
| 642 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 643 | int fast; /* Enable fast mode if set to a true value. |
| 644 | The fast mode disable the usage of memo, |
| 645 | therefore speeding the pickling process by |
| 646 | not generating superfluous PUT opcodes. It |
| 647 | should not be used if with self-referential |
| 648 | objects. */ |
| 649 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 650 | int fix_imports; /* Indicate whether Pickler should fix |
| 651 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 652 | PyObject *fast_memo; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 653 | PyObject *buffer_callback; /* Callback for out-of-band buffers, or NULL */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 654 | } PicklerObject; |
| 655 | |
| 656 | typedef struct UnpicklerObject { |
| 657 | PyObject_HEAD |
| 658 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 659 | |
| 660 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 661 | is unnecessary, since the keys are contiguous ints. */ |
| 662 | PyObject **memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 663 | size_t memo_size; /* Capacity of the memo array */ |
| 664 | size_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 665 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 666 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 667 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 668 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 669 | |
| 670 | Py_buffer buffer; |
| 671 | char *input_buffer; |
| 672 | char *input_line; |
| 673 | Py_ssize_t input_len; |
| 674 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 675 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 676 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 677 | PyObject *read; /* read() method of the input stream. */ |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 678 | PyObject *readinto; /* readinto() method of the input stream. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 679 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 680 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 681 | PyObject *buffers; /* iterable of out-of-band buffers, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 682 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 683 | char *encoding; /* Name of the encoding to be used for |
| 684 | decoding strings pickled using Python |
| 685 | 2.x. The default value is "ASCII" */ |
| 686 | char *errors; /* Name of errors handling scheme to used when |
| 687 | decoding strings. The default value is |
| 688 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 689 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 690 | objects. */ |
| 691 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 692 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 693 | int proto; /* Protocol of the pickle loaded. */ |
| 694 | int fix_imports; /* Indicate whether Unpickler should fix |
| 695 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 696 | } UnpicklerObject; |
| 697 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 698 | typedef struct { |
| 699 | PyObject_HEAD |
| 700 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 701 | } PicklerMemoProxyObject; |
| 702 | |
| 703 | typedef struct { |
| 704 | PyObject_HEAD |
| 705 | UnpicklerObject *unpickler; |
| 706 | } UnpicklerMemoProxyObject; |
| 707 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 708 | /* Forward declarations */ |
| 709 | static int save(PicklerObject *, PyObject *, int); |
| 710 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 711 | static PyTypeObject Pickler_Type; |
| 712 | static PyTypeObject Unpickler_Type; |
| 713 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 714 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 715 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 716 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 717 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 718 | 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] | 719 | a bunch of unnecessary object creation. This makes a huge performance |
| 720 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 721 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 722 | #define MT_MINSIZE 8 |
| 723 | #define PERTURB_SHIFT 5 |
| 724 | |
| 725 | |
| 726 | static PyMemoTable * |
| 727 | PyMemoTable_New(void) |
| 728 | { |
| 729 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 730 | if (memo == NULL) { |
| 731 | PyErr_NoMemory(); |
| 732 | return NULL; |
| 733 | } |
| 734 | |
| 735 | memo->mt_used = 0; |
| 736 | memo->mt_allocated = MT_MINSIZE; |
| 737 | memo->mt_mask = MT_MINSIZE - 1; |
| 738 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 739 | if (memo->mt_table == NULL) { |
| 740 | PyMem_FREE(memo); |
| 741 | PyErr_NoMemory(); |
| 742 | return NULL; |
| 743 | } |
| 744 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 745 | |
| 746 | return memo; |
| 747 | } |
| 748 | |
| 749 | static PyMemoTable * |
| 750 | PyMemoTable_Copy(PyMemoTable *self) |
| 751 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 752 | PyMemoTable *new = PyMemoTable_New(); |
| 753 | if (new == NULL) |
| 754 | return NULL; |
| 755 | |
| 756 | new->mt_used = self->mt_used; |
| 757 | new->mt_allocated = self->mt_allocated; |
| 758 | new->mt_mask = self->mt_mask; |
| 759 | /* The table we get from _New() is probably smaller than we wanted. |
| 760 | Free it and allocate one that's the right size. */ |
| 761 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 762 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 763 | if (new->mt_table == NULL) { |
| 764 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 765 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 766 | return NULL; |
| 767 | } |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 768 | for (size_t i = 0; i < self->mt_allocated; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 769 | Py_XINCREF(self->mt_table[i].me_key); |
| 770 | } |
| 771 | memcpy(new->mt_table, self->mt_table, |
| 772 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 773 | |
| 774 | return new; |
| 775 | } |
| 776 | |
| 777 | static Py_ssize_t |
| 778 | PyMemoTable_Size(PyMemoTable *self) |
| 779 | { |
| 780 | return self->mt_used; |
| 781 | } |
| 782 | |
| 783 | static int |
| 784 | PyMemoTable_Clear(PyMemoTable *self) |
| 785 | { |
| 786 | Py_ssize_t i = self->mt_allocated; |
| 787 | |
| 788 | while (--i >= 0) { |
| 789 | Py_XDECREF(self->mt_table[i].me_key); |
| 790 | } |
| 791 | self->mt_used = 0; |
| 792 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | static void |
| 797 | PyMemoTable_Del(PyMemoTable *self) |
| 798 | { |
| 799 | if (self == NULL) |
| 800 | return; |
| 801 | PyMemoTable_Clear(self); |
| 802 | |
| 803 | PyMem_FREE(self->mt_table); |
| 804 | PyMem_FREE(self); |
| 805 | } |
| 806 | |
| 807 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 808 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 809 | static PyMemoEntry * |
| 810 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 811 | { |
| 812 | size_t i; |
| 813 | size_t perturb; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 814 | size_t mask = self->mt_mask; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 815 | PyMemoEntry *table = self->mt_table; |
| 816 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 817 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 818 | |
| 819 | i = hash & mask; |
| 820 | entry = &table[i]; |
| 821 | if (entry->me_key == NULL || entry->me_key == key) |
| 822 | return entry; |
| 823 | |
| 824 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 825 | i = (i << 2) + i + perturb + 1; |
| 826 | entry = &table[i & mask]; |
| 827 | if (entry->me_key == NULL || entry->me_key == key) |
| 828 | return entry; |
| 829 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 830 | Py_UNREACHABLE(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | /* Returns -1 on failure, 0 on success. */ |
| 834 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 835 | _PyMemoTable_ResizeTable(PyMemoTable *self, size_t min_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 836 | { |
| 837 | PyMemoEntry *oldtable = NULL; |
| 838 | PyMemoEntry *oldentry, *newentry; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 839 | size_t new_size = MT_MINSIZE; |
| 840 | size_t to_process; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 841 | |
| 842 | assert(min_size > 0); |
| 843 | |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 844 | if (min_size > PY_SSIZE_T_MAX) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 845 | PyErr_NoMemory(); |
| 846 | return -1; |
| 847 | } |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 848 | |
| 849 | /* Find the smallest valid table size >= min_size. */ |
| 850 | while (new_size < min_size) { |
| 851 | new_size <<= 1; |
| 852 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 853 | /* new_size needs to be a power of two. */ |
| 854 | assert((new_size & (new_size - 1)) == 0); |
| 855 | |
| 856 | /* Allocate new table. */ |
| 857 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 858 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 859 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 860 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 861 | PyErr_NoMemory(); |
| 862 | return -1; |
| 863 | } |
| 864 | self->mt_allocated = new_size; |
| 865 | self->mt_mask = new_size - 1; |
| 866 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 867 | |
| 868 | /* Copy entries from the old table. */ |
| 869 | to_process = self->mt_used; |
| 870 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 871 | if (oldentry->me_key != NULL) { |
| 872 | to_process--; |
| 873 | /* newentry is a pointer to a chunk of the new |
| 874 | mt_table, so we're setting the key:value pair |
| 875 | in-place. */ |
| 876 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 877 | newentry->me_key = oldentry->me_key; |
| 878 | newentry->me_value = oldentry->me_value; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | /* Deallocate the old table. */ |
| 883 | PyMem_FREE(oldtable); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 888 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 889 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 890 | { |
| 891 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 892 | if (entry->me_key == NULL) |
| 893 | return NULL; |
| 894 | return &entry->me_value; |
| 895 | } |
| 896 | |
| 897 | /* Returns -1 on failure, 0 on success. */ |
| 898 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 899 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 900 | { |
| 901 | PyMemoEntry *entry; |
| 902 | |
| 903 | assert(key != NULL); |
| 904 | |
| 905 | entry = _PyMemoTable_Lookup(self, key); |
| 906 | if (entry->me_key != NULL) { |
| 907 | entry->me_value = value; |
| 908 | return 0; |
| 909 | } |
| 910 | Py_INCREF(key); |
| 911 | entry->me_key = key; |
| 912 | entry->me_value = value; |
| 913 | self->mt_used++; |
| 914 | |
| 915 | /* If we added a key, we can safely resize. Otherwise just return! |
| 916 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 917 | * |
| 918 | * Quadrupling the size improves average table sparseness |
| 919 | * (reducing collisions) at the cost of some memory. It also halves |
| 920 | * the number of expensive resize operations in a growing memo table. |
| 921 | * |
| 922 | * Very large memo tables (over 50K items) use doubling instead. |
| 923 | * This may help applications with severe memory constraints. |
| 924 | */ |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 925 | 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] | 926 | return 0; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 927 | } |
| 928 | // self->mt_used is always < PY_SSIZE_T_MAX, so this can't overflow. |
| 929 | size_t desired_size = (self->mt_used > 50000 ? 2 : 4) * self->mt_used; |
| 930 | return _PyMemoTable_ResizeTable(self, desired_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 931 | } |
| 932 | |
| 933 | #undef MT_MINSIZE |
| 934 | #undef PERTURB_SHIFT |
| 935 | |
| 936 | /*************************************************************************/ |
| 937 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 938 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 939 | static int |
| 940 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 941 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 942 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 943 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 944 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 945 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 946 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 947 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 948 | return 0; |
| 949 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 950 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 951 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 952 | _write_size64(char *out, size_t value) |
| 953 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 954 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 955 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 956 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 957 | |
| 958 | for (i = 0; i < sizeof(size_t); i++) { |
| 959 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 960 | } |
| 961 | for (i = sizeof(size_t); i < 8; i++) { |
| 962 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 963 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 964 | } |
| 965 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 966 | static int |
| 967 | _Pickler_CommitFrame(PicklerObject *self) |
| 968 | { |
| 969 | size_t frame_len; |
| 970 | char *qdata; |
| 971 | |
| 972 | if (!self->framing || self->frame_start == -1) |
| 973 | return 0; |
| 974 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 975 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
Serhiy Storchaka | 1211c9a | 2018-01-20 16:42:44 +0200 | [diff] [blame] | 976 | if (frame_len >= FRAME_SIZE_MIN) { |
| 977 | qdata[0] = FRAME; |
| 978 | _write_size64(qdata + 1, frame_len); |
| 979 | } |
| 980 | else { |
| 981 | memmove(qdata, qdata + FRAME_HEADER_SIZE, frame_len); |
| 982 | self->output_len -= FRAME_HEADER_SIZE; |
| 983 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 984 | self->frame_start = -1; |
| 985 | return 0; |
| 986 | } |
| 987 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 988 | static PyObject * |
| 989 | _Pickler_GetString(PicklerObject *self) |
| 990 | { |
| 991 | PyObject *output_buffer = self->output_buffer; |
| 992 | |
| 993 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 994 | |
| 995 | if (_Pickler_CommitFrame(self)) |
| 996 | return NULL; |
| 997 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 998 | self->output_buffer = NULL; |
| 999 | /* Resize down to exact size */ |
| 1000 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 1001 | return NULL; |
| 1002 | return output_buffer; |
| 1003 | } |
| 1004 | |
| 1005 | static int |
| 1006 | _Pickler_FlushToFile(PicklerObject *self) |
| 1007 | { |
| 1008 | PyObject *output, *result; |
| 1009 | |
| 1010 | assert(self->write != NULL); |
| 1011 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1012 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1013 | output = _Pickler_GetString(self); |
| 1014 | if (output == NULL) |
| 1015 | return -1; |
| 1016 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1017 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1018 | Py_XDECREF(result); |
| 1019 | return (result == NULL) ? -1 : 0; |
| 1020 | } |
| 1021 | |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1022 | static int |
| 1023 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 1024 | { |
| 1025 | Py_ssize_t frame_len; |
| 1026 | |
| 1027 | if (!self->framing || self->frame_start == -1) { |
| 1028 | return 0; |
| 1029 | } |
| 1030 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 1031 | if (frame_len >= FRAME_SIZE_TARGET) { |
| 1032 | if(_Pickler_CommitFrame(self)) { |
| 1033 | return -1; |
| 1034 | } |
Leo Arias | c3d9508 | 2018-02-03 18:36:10 -0600 | [diff] [blame] | 1035 | /* Flush the content of the committed frame to the underlying |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 1036 | * file and reuse the pickler buffer for the next frame so as |
| 1037 | * to limit memory usage when dumping large complex objects to |
| 1038 | * a file. |
| 1039 | * |
| 1040 | * self->write is NULL when called via dumps. |
| 1041 | */ |
| 1042 | if (self->write != NULL) { |
| 1043 | if (_Pickler_FlushToFile(self) < 0) { |
| 1044 | return -1; |
| 1045 | } |
| 1046 | if (_Pickler_ClearBuffer(self) < 0) { |
| 1047 | return -1; |
| 1048 | } |
| 1049 | } |
| 1050 | } |
| 1051 | return 0; |
| 1052 | } |
| 1053 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1054 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1055 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1056 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1057 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1058 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1059 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1060 | |
| 1061 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1062 | need_new_frame = (self->framing && self->frame_start == -1); |
| 1063 | |
| 1064 | if (need_new_frame) |
| 1065 | n = data_len + FRAME_HEADER_SIZE; |
| 1066 | else |
| 1067 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1068 | |
| 1069 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1070 | if (required > self->max_output_len) { |
| 1071 | /* Make place in buffer for the pickle chunk */ |
| 1072 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 1073 | PyErr_NoMemory(); |
| 1074 | return -1; |
| 1075 | } |
| 1076 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 1077 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 1078 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1079 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1080 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1081 | if (need_new_frame) { |
| 1082 | /* Setup new frame */ |
| 1083 | Py_ssize_t frame_start = self->output_len; |
| 1084 | self->frame_start = frame_start; |
| 1085 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 1086 | /* Write an invalid value, for debugging */ |
| 1087 | buffer[frame_start + i] = 0xFE; |
| 1088 | } |
| 1089 | self->output_len += FRAME_HEADER_SIZE; |
| 1090 | } |
| 1091 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1092 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1093 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1094 | buffer[self->output_len + i] = s[i]; |
| 1095 | } |
| 1096 | } |
| 1097 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1098 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1099 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1100 | self->output_len += data_len; |
| 1101 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1104 | static PicklerObject * |
| 1105 | _Pickler_New(void) |
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 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1108 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1109 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1110 | if (self == NULL) |
| 1111 | return NULL; |
| 1112 | |
| 1113 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1114 | self->dispatch_table = NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1115 | self->buffer_callback = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1116 | self->write = NULL; |
| 1117 | self->proto = 0; |
| 1118 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1119 | self->framing = 0; |
| 1120 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1121 | self->fast = 0; |
| 1122 | self->fast_nesting = 0; |
| 1123 | self->fix_imports = 0; |
| 1124 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1125 | self->max_output_len = WRITE_BUF_SIZE; |
| 1126 | self->output_len = 0; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 1127 | self->reducer_override = NULL; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1128 | |
| 1129 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1130 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1131 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1132 | |
| 1133 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1134 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1135 | return NULL; |
| 1136 | } |
Zackery Spytz | 359bd4f | 2019-04-23 05:56:08 -0600 | [diff] [blame] | 1137 | |
| 1138 | PyObject_GC_Track(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1139 | return self; |
| 1140 | } |
| 1141 | |
| 1142 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1143 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
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 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1146 | |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 1147 | if (protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1148 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1149 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1150 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1151 | proto = PyLong_AsLong(protocol); |
| 1152 | if (proto < 0) { |
| 1153 | if (proto == -1 && PyErr_Occurred()) |
| 1154 | return -1; |
| 1155 | proto = HIGHEST_PROTOCOL; |
| 1156 | } |
| 1157 | else if (proto > HIGHEST_PROTOCOL) { |
| 1158 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1159 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1160 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1161 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1162 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1163 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1164 | self->bin = proto > 0; |
| 1165 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1166 | return 0; |
| 1167 | } |
| 1168 | |
| 1169 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1170 | be called once on a freshly created Pickler. */ |
| 1171 | static int |
| 1172 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1173 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1174 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1175 | assert(file != NULL); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1176 | if (_PyObject_LookupAttrId(file, &PyId_write, &self->write) < 0) { |
| 1177 | return -1; |
| 1178 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1179 | if (self->write == NULL) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1180 | PyErr_SetString(PyExc_TypeError, |
| 1181 | "file must have a 'write' attribute"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1182 | return -1; |
| 1183 | } |
| 1184 | |
| 1185 | return 0; |
| 1186 | } |
| 1187 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1188 | static int |
| 1189 | _Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback) |
| 1190 | { |
| 1191 | if (buffer_callback == Py_None) { |
| 1192 | buffer_callback = NULL; |
| 1193 | } |
| 1194 | if (buffer_callback != NULL && self->proto < 5) { |
| 1195 | PyErr_SetString(PyExc_ValueError, |
| 1196 | "buffer_callback needs protocol >= 5"); |
| 1197 | return -1; |
| 1198 | } |
| 1199 | |
| 1200 | Py_XINCREF(buffer_callback); |
| 1201 | self->buffer_callback = buffer_callback; |
| 1202 | return 0; |
| 1203 | } |
| 1204 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1205 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1206 | own reference to `input`. */ |
| 1207 | static Py_ssize_t |
| 1208 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1209 | { |
| 1210 | if (self->buffer.buf != NULL) |
| 1211 | PyBuffer_Release(&self->buffer); |
| 1212 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1213 | return -1; |
| 1214 | self->input_buffer = self->buffer.buf; |
| 1215 | self->input_len = self->buffer.len; |
| 1216 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1217 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1218 | return self->input_len; |
| 1219 | } |
| 1220 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1221 | static int |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1222 | bad_readline(void) |
| 1223 | { |
| 1224 | PickleState *st = _Pickle_GetGlobalState(); |
| 1225 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
| 1226 | return -1; |
| 1227 | } |
| 1228 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1229 | /* Skip any consumed data that was only prefetched using peek() */ |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1230 | static int |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1231 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1232 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1233 | Py_ssize_t consumed; |
| 1234 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1235 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1236 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1237 | if (consumed <= 0) |
| 1238 | return 0; |
| 1239 | |
| 1240 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1241 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1242 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1243 | if (r == NULL) |
| 1244 | return -1; |
| 1245 | Py_DECREF(r); |
| 1246 | |
| 1247 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1248 | return 0; |
| 1249 | } |
| 1250 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1251 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1252 | |
| 1253 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1254 | may be multiple pickle objects arranged contiguously in the same input |
| 1255 | buffer. |
| 1256 | |
| 1257 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1258 | bytes from the input stream/buffer. |
| 1259 | |
| 1260 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1261 | failure; on success, returns the number of bytes read from the file. |
| 1262 | |
| 1263 | On success, self->input_len will be 0; this is intentional so that when |
| 1264 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1265 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1266 | size to tell you how much data you can process. */ |
| 1267 | static Py_ssize_t |
| 1268 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1269 | { |
| 1270 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1271 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1272 | |
| 1273 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1274 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1275 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1276 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1277 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1278 | if (n == READ_WHOLE_LINE) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 1279 | data = PyObject_CallNoArgs(self->readline); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1280 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1281 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1282 | PyObject *len; |
| 1283 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1284 | if (self->peek && n < PREFETCH) { |
| 1285 | len = PyLong_FromSsize_t(PREFETCH); |
| 1286 | if (len == NULL) |
| 1287 | return -1; |
| 1288 | data = _Pickle_FastCall(self->peek, len); |
| 1289 | if (data == NULL) { |
| 1290 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1291 | return -1; |
| 1292 | /* peek() is probably not supported by the given file object */ |
| 1293 | PyErr_Clear(); |
| 1294 | Py_CLEAR(self->peek); |
| 1295 | } |
| 1296 | else { |
| 1297 | read_size = _Unpickler_SetStringInput(self, data); |
| 1298 | Py_DECREF(data); |
| 1299 | self->prefetched_idx = 0; |
| 1300 | if (n <= read_size) |
| 1301 | return n; |
| 1302 | } |
| 1303 | } |
| 1304 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1305 | if (len == NULL) |
| 1306 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1307 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1308 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1309 | if (data == NULL) |
| 1310 | return -1; |
| 1311 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1312 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1313 | Py_DECREF(data); |
| 1314 | return read_size; |
| 1315 | } |
| 1316 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1317 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1318 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1319 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1320 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1321 | Py_ssize_t num_read; |
| 1322 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1323 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1324 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1325 | PickleState *st = _Pickle_GetGlobalState(); |
| 1326 | PyErr_SetString(st->UnpicklingError, |
| 1327 | "read would overflow (invalid bytecode)"); |
| 1328 | return -1; |
| 1329 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1330 | |
| 1331 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1332 | assert(self->next_read_idx + n > self->input_len); |
| 1333 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1334 | if (!self->read) |
| 1335 | return bad_readline(); |
| 1336 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1337 | /* Extend the buffer to satisfy desired size */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1338 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1339 | if (num_read < 0) |
| 1340 | return -1; |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1341 | if (num_read < n) |
| 1342 | return bad_readline(); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1343 | *s = self->input_buffer; |
| 1344 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1345 | return n; |
| 1346 | } |
| 1347 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1348 | /* Read `n` bytes from the unpickler's data source, storing the result in `buf`. |
| 1349 | * |
| 1350 | * This should only be used for non-small data reads where potentially |
| 1351 | * avoiding a copy is beneficial. This method does not try to prefetch |
| 1352 | * more data into the input buffer. |
| 1353 | * |
| 1354 | * _Unpickler_Read() is recommended in most cases. |
| 1355 | */ |
| 1356 | static Py_ssize_t |
| 1357 | _Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n) |
| 1358 | { |
| 1359 | assert(n != READ_WHOLE_LINE); |
| 1360 | |
| 1361 | /* Read from available buffer data, if any */ |
| 1362 | Py_ssize_t in_buffer = self->input_len - self->next_read_idx; |
| 1363 | if (in_buffer > 0) { |
| 1364 | Py_ssize_t to_read = Py_MIN(in_buffer, n); |
| 1365 | memcpy(buf, self->input_buffer + self->next_read_idx, to_read); |
| 1366 | self->next_read_idx += to_read; |
| 1367 | buf += to_read; |
| 1368 | n -= to_read; |
| 1369 | if (n == 0) { |
| 1370 | /* Entire read was satisfied from buffer */ |
| 1371 | return n; |
| 1372 | } |
| 1373 | } |
| 1374 | |
| 1375 | /* Read from file */ |
Antoine Pitrou | 9f37872 | 2020-02-23 23:33:53 +0100 | [diff] [blame] | 1376 | if (!self->read) { |
| 1377 | /* We're unpickling memory, this means the input is truncated */ |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1378 | return bad_readline(); |
| 1379 | } |
| 1380 | if (_Unpickler_SkipConsumed(self) < 0) { |
| 1381 | return -1; |
| 1382 | } |
| 1383 | |
Antoine Pitrou | 9f37872 | 2020-02-23 23:33:53 +0100 | [diff] [blame] | 1384 | if (!self->readinto) { |
| 1385 | /* readinto() not supported on file-like object, fall back to read() |
| 1386 | * and copy into destination buffer (bpo-39681) */ |
| 1387 | PyObject* len = PyLong_FromSsize_t(n); |
| 1388 | if (len == NULL) { |
| 1389 | return -1; |
| 1390 | } |
| 1391 | PyObject* data = _Pickle_FastCall(self->read, len); |
| 1392 | if (data == NULL) { |
| 1393 | return -1; |
| 1394 | } |
| 1395 | if (!PyBytes_Check(data)) { |
| 1396 | PyErr_Format(PyExc_ValueError, |
| 1397 | "read() returned non-bytes object (%R)", |
| 1398 | Py_TYPE(data)); |
| 1399 | Py_DECREF(data); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | Py_ssize_t read_size = PyBytes_GET_SIZE(data); |
| 1403 | if (read_size < n) { |
| 1404 | Py_DECREF(data); |
| 1405 | return bad_readline(); |
| 1406 | } |
| 1407 | memcpy(buf, PyBytes_AS_STRING(data), n); |
| 1408 | Py_DECREF(data); |
| 1409 | return n; |
| 1410 | } |
| 1411 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1412 | /* Call readinto() into user buffer */ |
| 1413 | PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE); |
| 1414 | if (buf_obj == NULL) { |
| 1415 | return -1; |
| 1416 | } |
| 1417 | PyObject *read_size_obj = _Pickle_FastCall(self->readinto, buf_obj); |
| 1418 | if (read_size_obj == NULL) { |
| 1419 | return -1; |
| 1420 | } |
| 1421 | Py_ssize_t read_size = PyLong_AsSsize_t(read_size_obj); |
| 1422 | Py_DECREF(read_size_obj); |
| 1423 | |
| 1424 | if (read_size < 0) { |
| 1425 | if (!PyErr_Occurred()) { |
| 1426 | PyErr_SetString(PyExc_ValueError, |
| 1427 | "readinto() returned negative size"); |
| 1428 | } |
| 1429 | return -1; |
| 1430 | } |
| 1431 | if (read_size < n) { |
| 1432 | return bad_readline(); |
| 1433 | } |
| 1434 | return n; |
| 1435 | } |
| 1436 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1437 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1438 | |
| 1439 | This should be used for all data reads, rather than accessing the unpickler's |
| 1440 | input buffer directly. This method deals correctly with reading from input |
| 1441 | streams, which the input buffer doesn't deal with. |
| 1442 | |
| 1443 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1444 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1445 | should use this function's return value to know how many bytes you can |
| 1446 | consume. |
| 1447 | |
| 1448 | Returns -1 (with an exception set) on failure. On success, return the |
| 1449 | number of chars read. */ |
| 1450 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1451 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1452 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1453 | (self)->next_read_idx += (n), \ |
| 1454 | (n)) \ |
| 1455 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1456 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1457 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1458 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1459 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1460 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1461 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1462 | if (input_line == NULL) { |
| 1463 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1464 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1465 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1466 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1467 | memcpy(input_line, line, len); |
| 1468 | input_line[len] = '\0'; |
| 1469 | self->input_line = input_line; |
| 1470 | *result = self->input_line; |
| 1471 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1474 | /* 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] | 1475 | before hitting \n, raise an error. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1476 | |
| 1477 | Returns the number of chars read, or -1 on failure. */ |
| 1478 | static Py_ssize_t |
| 1479 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1480 | { |
| 1481 | Py_ssize_t i, num_read; |
| 1482 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1483 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1484 | if (self->input_buffer[i] == '\n') { |
| 1485 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1486 | num_read = i - self->next_read_idx + 1; |
| 1487 | self->next_read_idx = i + 1; |
| 1488 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1489 | } |
| 1490 | } |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1491 | if (!self->read) |
| 1492 | return bad_readline(); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1493 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1494 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1495 | if (num_read < 0) |
| 1496 | return -1; |
| 1497 | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
| 1498 | return bad_readline(); |
| 1499 | self->next_read_idx = num_read; |
| 1500 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1504 | will be modified in place. */ |
| 1505 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1506 | _Unpickler_ResizeMemoList(UnpicklerObject *self, size_t new_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1507 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1508 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1509 | |
| 1510 | assert(new_size > self->memo_size); |
| 1511 | |
Sergey Fedoseev | 67b9cc8 | 2018-08-16 09:27:50 +0500 | [diff] [blame] | 1512 | PyObject **memo_new = self->memo; |
| 1513 | PyMem_RESIZE(memo_new, PyObject *, new_size); |
| 1514 | if (memo_new == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1515 | PyErr_NoMemory(); |
| 1516 | return -1; |
| 1517 | } |
Sergey Fedoseev | 67b9cc8 | 2018-08-16 09:27:50 +0500 | [diff] [blame] | 1518 | self->memo = memo_new; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1519 | for (i = self->memo_size; i < new_size; i++) |
| 1520 | self->memo[i] = NULL; |
| 1521 | self->memo_size = new_size; |
| 1522 | return 0; |
| 1523 | } |
| 1524 | |
| 1525 | /* Returns NULL if idx is out of bounds. */ |
| 1526 | static PyObject * |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1527 | _Unpickler_MemoGet(UnpicklerObject *self, size_t idx) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1528 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1529 | if (idx >= self->memo_size) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1530 | return NULL; |
| 1531 | |
| 1532 | return self->memo[idx]; |
| 1533 | } |
| 1534 | |
| 1535 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1536 | This takes its own reference to `value`. */ |
| 1537 | static int |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 1538 | _Unpickler_MemoPut(UnpicklerObject *self, size_t idx, PyObject *value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1539 | { |
| 1540 | PyObject *old_item; |
| 1541 | |
| 1542 | if (idx >= self->memo_size) { |
| 1543 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1544 | return -1; |
| 1545 | assert(idx < self->memo_size); |
| 1546 | } |
| 1547 | Py_INCREF(value); |
| 1548 | old_item = self->memo[idx]; |
| 1549 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1550 | if (old_item != NULL) { |
| 1551 | Py_DECREF(old_item); |
| 1552 | } |
| 1553 | else { |
| 1554 | self->memo_len++; |
| 1555 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1556 | return 0; |
| 1557 | } |
| 1558 | |
| 1559 | static PyObject ** |
| 1560 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1561 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1562 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1563 | if (memo == NULL) { |
| 1564 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1565 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1566 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1567 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1568 | return memo; |
| 1569 | } |
| 1570 | |
| 1571 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1572 | static void |
| 1573 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1574 | { |
| 1575 | Py_ssize_t i; |
| 1576 | PyObject **memo = self->memo; |
| 1577 | |
| 1578 | if (self->memo == NULL) |
| 1579 | return; |
| 1580 | self->memo = NULL; |
| 1581 | i = self->memo_size; |
| 1582 | while (--i >= 0) { |
| 1583 | Py_XDECREF(memo[i]); |
| 1584 | } |
| 1585 | PyMem_FREE(memo); |
| 1586 | } |
| 1587 | |
| 1588 | static UnpicklerObject * |
| 1589 | _Unpickler_New(void) |
| 1590 | { |
| 1591 | UnpicklerObject *self; |
| 1592 | |
| 1593 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1594 | if (self == NULL) |
| 1595 | return NULL; |
| 1596 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1597 | self->pers_func = NULL; |
| 1598 | self->input_buffer = NULL; |
| 1599 | self->input_line = NULL; |
| 1600 | self->input_len = 0; |
| 1601 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1602 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1603 | self->read = NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1604 | self->readinto = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1605 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1606 | self->peek = NULL; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1607 | self->buffers = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1608 | self->encoding = NULL; |
| 1609 | self->errors = NULL; |
| 1610 | self->marks = NULL; |
| 1611 | self->num_marks = 0; |
| 1612 | self->marks_size = 0; |
| 1613 | self->proto = 0; |
| 1614 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1615 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1616 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1617 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1618 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1619 | self->stack = (Pdata *)Pdata_New(); |
| 1620 | |
| 1621 | if (self->memo == NULL || self->stack == NULL) { |
| 1622 | Py_DECREF(self); |
| 1623 | return NULL; |
| 1624 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1625 | |
Zackery Spytz | 359bd4f | 2019-04-23 05:56:08 -0600 | [diff] [blame] | 1626 | PyObject_GC_Track(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1627 | return self; |
| 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_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1634 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1635 | _Py_IDENTIFIER(peek); |
| 1636 | _Py_IDENTIFIER(read); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1637 | _Py_IDENTIFIER(readinto); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1638 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1639 | |
Antoine Pitrou | 9f37872 | 2020-02-23 23:33:53 +0100 | [diff] [blame] | 1640 | /* Optional file methods */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1641 | if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) { |
| 1642 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1643 | } |
Antoine Pitrou | 9f37872 | 2020-02-23 23:33:53 +0100 | [diff] [blame] | 1644 | if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) < 0) { |
| 1645 | return -1; |
| 1646 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1647 | (void)_PyObject_LookupAttrId(file, &PyId_read, &self->read); |
| 1648 | (void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline); |
Antoine Pitrou | 9f37872 | 2020-02-23 23:33:53 +0100 | [diff] [blame] | 1649 | if (!self->readline || !self->read) { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1650 | if (!PyErr_Occurred()) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1651 | PyErr_SetString(PyExc_TypeError, |
Antoine Pitrou | 9f37872 | 2020-02-23 23:33:53 +0100 | [diff] [blame] | 1652 | "file must have 'read' and 'readline' attributes"); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1653 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1654 | Py_CLEAR(self->read); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1655 | Py_CLEAR(self->readinto); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1656 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1657 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1658 | return -1; |
| 1659 | } |
| 1660 | return 0; |
| 1661 | } |
| 1662 | |
| 1663 | /* 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] | 1664 | be called once on a freshly created Unpickler. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1665 | static int |
| 1666 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1667 | const char *encoding, |
| 1668 | const char *errors) |
| 1669 | { |
| 1670 | if (encoding == NULL) |
| 1671 | encoding = "ASCII"; |
| 1672 | if (errors == NULL) |
| 1673 | errors = "strict"; |
| 1674 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1675 | self->encoding = _PyMem_Strdup(encoding); |
| 1676 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1677 | if (self->encoding == NULL || self->errors == NULL) { |
| 1678 | PyErr_NoMemory(); |
| 1679 | return -1; |
| 1680 | } |
| 1681 | return 0; |
| 1682 | } |
| 1683 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1684 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1685 | be called once on a freshly created Unpickler. */ |
| 1686 | static int |
| 1687 | _Unpickler_SetBuffers(UnpicklerObject *self, PyObject *buffers) |
| 1688 | { |
Markus Mohrhard | 898318b | 2019-07-26 00:00:34 +0800 | [diff] [blame] | 1689 | if (buffers == NULL || buffers == Py_None) { |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 1690 | self->buffers = NULL; |
| 1691 | } |
| 1692 | else { |
| 1693 | self->buffers = PyObject_GetIter(buffers); |
| 1694 | if (self->buffers == NULL) { |
| 1695 | return -1; |
| 1696 | } |
| 1697 | } |
| 1698 | return 0; |
| 1699 | } |
| 1700 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1701 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1702 | static int |
| 1703 | memo_get(PicklerObject *self, PyObject *key) |
| 1704 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1705 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1706 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1707 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1708 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1709 | value = PyMemoTable_Get(self->memo, key); |
| 1710 | if (value == NULL) { |
| 1711 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1712 | return -1; |
| 1713 | } |
| 1714 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1715 | if (!self->bin) { |
| 1716 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1717 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Victor Stinner | d36cf5f | 2020-06-10 18:38:05 +0200 | [diff] [blame] | 1718 | "%zd\n", *value); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1719 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1720 | } |
| 1721 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1722 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1723 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1724 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1725 | len = 2; |
| 1726 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1727 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1728 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1729 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1730 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1731 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1732 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1733 | len = 5; |
| 1734 | } |
| 1735 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1736 | PickleState *st = _Pickle_GetGlobalState(); |
| 1737 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1738 | "memo id too large for LONG_BINGET"); |
| 1739 | return -1; |
| 1740 | } |
| 1741 | } |
| 1742 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1743 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1744 | return -1; |
| 1745 | |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
| 1749 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1750 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1751 | static int |
| 1752 | memo_put(PicklerObject *self, PyObject *obj) |
| 1753 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1754 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1755 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1756 | Py_ssize_t idx; |
| 1757 | |
| 1758 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1759 | |
| 1760 | if (self->fast) |
| 1761 | return 0; |
| 1762 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1763 | idx = PyMemoTable_Size(self->memo); |
| 1764 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1765 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1766 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1767 | if (self->proto >= 4) { |
| 1768 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1769 | return -1; |
| 1770 | return 0; |
| 1771 | } |
| 1772 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1773 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1774 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Victor Stinner | d36cf5f | 2020-06-10 18:38:05 +0200 | [diff] [blame] | 1775 | "%zd\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1776 | len = strlen(pdata); |
| 1777 | } |
| 1778 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1779 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1780 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1781 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1782 | len = 2; |
| 1783 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1784 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1785 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1786 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1787 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1788 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1789 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1790 | len = 5; |
| 1791 | } |
| 1792 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1793 | PickleState *st = _Pickle_GetGlobalState(); |
| 1794 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1795 | "memo id too large for LONG_BINPUT"); |
| 1796 | return -1; |
| 1797 | } |
| 1798 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1799 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1800 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1801 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1802 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1803 | } |
| 1804 | |
| 1805 | static PyObject * |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1806 | get_dotted_path(PyObject *obj, PyObject *name) |
| 1807 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1808 | _Py_static_string(PyId_dot, "."); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1809 | PyObject *dotted_path; |
| 1810 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1811 | |
| 1812 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1813 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1814 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1815 | n = PyList_GET_SIZE(dotted_path); |
| 1816 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1817 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1818 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1819 | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1820 | if (obj == NULL) |
| 1821 | PyErr_Format(PyExc_AttributeError, |
| 1822 | "Can't pickle local object %R", name); |
| 1823 | else |
| 1824 | PyErr_Format(PyExc_AttributeError, |
| 1825 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1826 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1827 | return NULL; |
| 1828 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1829 | } |
| 1830 | return dotted_path; |
| 1831 | } |
| 1832 | |
| 1833 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1834 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1835 | { |
| 1836 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1837 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1838 | |
| 1839 | assert(PyList_CheckExact(names)); |
| 1840 | Py_INCREF(obj); |
| 1841 | n = PyList_GET_SIZE(names); |
| 1842 | for (i = 0; i < n; i++) { |
| 1843 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1844 | Py_XDECREF(parent); |
| 1845 | parent = obj; |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1846 | (void)_PyObject_LookupAttr(parent, name, &obj); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1847 | if (obj == NULL) { |
| 1848 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1849 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1850 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1851 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1852 | if (pparent != NULL) |
| 1853 | *pparent = parent; |
| 1854 | else |
| 1855 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1856 | return obj; |
| 1857 | } |
| 1858 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1859 | |
| 1860 | static PyObject * |
| 1861 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1862 | { |
| 1863 | PyObject *dotted_path, *attr; |
| 1864 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1865 | if (allow_qualname) { |
| 1866 | dotted_path = get_dotted_path(obj, name); |
| 1867 | if (dotted_path == NULL) |
| 1868 | return NULL; |
| 1869 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1870 | Py_DECREF(dotted_path); |
| 1871 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1872 | else { |
| 1873 | (void)_PyObject_LookupAttr(obj, name, &attr); |
| 1874 | } |
| 1875 | if (attr == NULL && !PyErr_Occurred()) { |
| 1876 | PyErr_Format(PyExc_AttributeError, |
| 1877 | "Can't get attribute %R on %R", name, obj); |
| 1878 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1879 | return attr; |
| 1880 | } |
| 1881 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1882 | static int |
| 1883 | _checkmodule(PyObject *module_name, PyObject *module, |
| 1884 | PyObject *global, PyObject *dotted_path) |
| 1885 | { |
| 1886 | if (module == Py_None) { |
| 1887 | return -1; |
| 1888 | } |
| 1889 | if (PyUnicode_Check(module_name) && |
| 1890 | _PyUnicode_EqualToASCIIString(module_name, "__main__")) { |
| 1891 | return -1; |
| 1892 | } |
| 1893 | |
| 1894 | PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); |
| 1895 | if (candidate == NULL) { |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1896 | return -1; |
| 1897 | } |
| 1898 | if (candidate != global) { |
| 1899 | Py_DECREF(candidate); |
| 1900 | return -1; |
| 1901 | } |
| 1902 | Py_DECREF(candidate); |
| 1903 | return 0; |
| 1904 | } |
| 1905 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1906 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1907 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1908 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1909 | PyObject *module_name; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1910 | PyObject *module = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1911 | Py_ssize_t i; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1912 | PyObject *modules; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1913 | _Py_IDENTIFIER(__module__); |
| 1914 | _Py_IDENTIFIER(modules); |
| 1915 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1916 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1917 | if (_PyObject_LookupAttrId(global, &PyId___module__, &module_name) < 0) { |
| 1918 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1919 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 1920 | if (module_name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1921 | /* In some rare cases (e.g., bound methods of extension types), |
| 1922 | __module__ can be None. If it is so, then search sys.modules for |
| 1923 | the module of global. */ |
| 1924 | if (module_name != Py_None) |
| 1925 | return module_name; |
| 1926 | Py_CLEAR(module_name); |
| 1927 | } |
| 1928 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1929 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1930 | /* Fallback on walking sys.modules */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1931 | modules = _PySys_GetObjectId(&PyId_modules); |
| 1932 | if (modules == NULL) { |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1933 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1934 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1935 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1936 | if (PyDict_CheckExact(modules)) { |
| 1937 | i = 0; |
| 1938 | while (PyDict_Next(modules, &i, &module_name, &module)) { |
| 1939 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1940 | Py_INCREF(module_name); |
| 1941 | return module_name; |
| 1942 | } |
| 1943 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1944 | return NULL; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1945 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1946 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1947 | } |
| 1948 | else { |
| 1949 | PyObject *iterator = PyObject_GetIter(modules); |
| 1950 | if (iterator == NULL) { |
| 1951 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1952 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1953 | while ((module_name = PyIter_Next(iterator))) { |
| 1954 | module = PyObject_GetItem(modules, module_name); |
| 1955 | if (module == NULL) { |
| 1956 | Py_DECREF(module_name); |
| 1957 | Py_DECREF(iterator); |
| 1958 | return NULL; |
| 1959 | } |
| 1960 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1961 | Py_DECREF(module); |
| 1962 | Py_DECREF(iterator); |
| 1963 | return module_name; |
| 1964 | } |
| 1965 | Py_DECREF(module); |
| 1966 | Py_DECREF(module_name); |
| 1967 | if (PyErr_Occurred()) { |
| 1968 | Py_DECREF(iterator); |
| 1969 | return NULL; |
| 1970 | } |
| 1971 | } |
| 1972 | Py_DECREF(iterator); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1973 | } |
| 1974 | |
| 1975 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1976 | module_name = _PyUnicode_FromId(&PyId___main__); |
Victor Stinner | af46eb8 | 2017-09-05 23:30:16 +0200 | [diff] [blame] | 1977 | Py_XINCREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1978 | return module_name; |
| 1979 | } |
| 1980 | |
| 1981 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1982 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1983 | memoization disabled). If the nesting of a list or dict object exceed |
| 1984 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1985 | reference to the seen list or dict objects and check whether these objects |
| 1986 | are recursive. These are not strictly necessary, since save() has a |
| 1987 | hard-coded recursion limit, but they give a nicer error message than the |
| 1988 | typical RuntimeError. */ |
| 1989 | static int |
| 1990 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1991 | { |
| 1992 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1993 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1994 | PyObject *key = NULL; |
| 1995 | if (self->fast_memo == NULL) { |
| 1996 | self->fast_memo = PyDict_New(); |
| 1997 | if (self->fast_memo == NULL) { |
| 1998 | self->fast_nesting = -1; |
| 1999 | return 0; |
| 2000 | } |
| 2001 | } |
| 2002 | key = PyLong_FromVoidPtr(obj); |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 2003 | if (key == NULL) { |
| 2004 | self->fast_nesting = -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2005 | return 0; |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 2006 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 2007 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2008 | Py_DECREF(key); |
| 2009 | PyErr_Format(PyExc_ValueError, |
| 2010 | "fast mode: can't pickle cyclic objects " |
| 2011 | "including object type %.200s at %p", |
Victor Stinner | daa9756 | 2020-02-07 03:37:06 +0100 | [diff] [blame] | 2012 | Py_TYPE(obj)->tp_name, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2013 | self->fast_nesting = -1; |
| 2014 | return 0; |
| 2015 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 2016 | if (PyErr_Occurred()) { |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 2017 | Py_DECREF(key); |
| 2018 | self->fast_nesting = -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 2019 | return 0; |
| 2020 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2021 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 2022 | Py_DECREF(key); |
| 2023 | self->fast_nesting = -1; |
| 2024 | return 0; |
| 2025 | } |
| 2026 | Py_DECREF(key); |
| 2027 | } |
| 2028 | return 1; |
| 2029 | } |
| 2030 | |
| 2031 | static int |
| 2032 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 2033 | { |
| 2034 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 2035 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 2036 | if (key == NULL) |
| 2037 | return 0; |
| 2038 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 2039 | Py_DECREF(key); |
| 2040 | return 0; |
| 2041 | } |
| 2042 | Py_DECREF(key); |
| 2043 | } |
| 2044 | return 1; |
| 2045 | } |
| 2046 | |
| 2047 | static int |
| 2048 | save_none(PicklerObject *self, PyObject *obj) |
| 2049 | { |
| 2050 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2051 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2052 | return -1; |
| 2053 | |
| 2054 | return 0; |
| 2055 | } |
| 2056 | |
| 2057 | static int |
| 2058 | save_bool(PicklerObject *self, PyObject *obj) |
| 2059 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2060 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 2061 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2062 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2063 | return -1; |
| 2064 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 2065 | else { |
| 2066 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 2067 | * so that unpicklers written before bools were introduced unpickle them |
| 2068 | * as ints, but unpicklers after can recognize that bools were intended. |
| 2069 | * Note that protocol 2 added direct ways to pickle bools. |
| 2070 | */ |
| 2071 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 2072 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 2073 | return -1; |
| 2074 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2075 | return 0; |
| 2076 | } |
| 2077 | |
| 2078 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2079 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2080 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2081 | PyObject *repr = NULL; |
| 2082 | Py_ssize_t size; |
| 2083 | long val; |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2084 | int overflow; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2085 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2086 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2087 | val= PyLong_AsLongAndOverflow(obj, &overflow); |
| 2088 | if (!overflow && (sizeof(long) <= 4 || |
| 2089 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) |
| 2090 | { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 2091 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2092 | |
| 2093 | Note: we can't use -0x80000000L in the above condition because some |
| 2094 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 2095 | before applying the unary minus when sizeof(long) <= 4. The |
| 2096 | resulting value stays unsigned which is commonly not what we want, |
| 2097 | so MSVC happily warns us about it. However, that result would have |
| 2098 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 2099 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2100 | char pdata[32]; |
| 2101 | Py_ssize_t len = 0; |
| 2102 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2103 | if (self->bin) { |
| 2104 | pdata[1] = (unsigned char)(val & 0xff); |
| 2105 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 2106 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 2107 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2108 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2109 | if ((pdata[4] != 0) || (pdata[3] != 0)) { |
| 2110 | pdata[0] = BININT; |
| 2111 | len = 5; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2112 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2113 | else if (pdata[2] != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2114 | pdata[0] = BININT2; |
| 2115 | len = 3; |
| 2116 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2117 | else { |
| 2118 | pdata[0] = BININT1; |
| 2119 | len = 2; |
| 2120 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2121 | } |
| 2122 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2123 | sprintf(pdata, "%c%ld\n", INT, val); |
| 2124 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2125 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2126 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2127 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 2128 | |
| 2129 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2130 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2131 | assert(!PyErr_Occurred()); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2132 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2133 | if (self->proto >= 2) { |
| 2134 | /* Linear-time pickling. */ |
| 2135 | size_t nbits; |
| 2136 | size_t nbytes; |
| 2137 | unsigned char *pdata; |
| 2138 | char header[5]; |
| 2139 | int i; |
| 2140 | int sign = _PyLong_Sign(obj); |
| 2141 | |
| 2142 | if (sign == 0) { |
| 2143 | header[0] = LONG1; |
| 2144 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2145 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2146 | goto error; |
| 2147 | return 0; |
| 2148 | } |
| 2149 | nbits = _PyLong_NumBits(obj); |
| 2150 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 2151 | goto error; |
| 2152 | /* How many bytes do we need? There are nbits >> 3 full |
| 2153 | * bytes of data, and nbits & 7 leftover bits. If there |
| 2154 | * are any leftover bits, then we clearly need another |
Min ho Kim | 96e12d5 | 2019-07-22 06:12:33 +1000 | [diff] [blame] | 2155 | * byte. What's not so obvious is that we *probably* |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2156 | * need another byte even if there aren't any leftovers: |
| 2157 | * the most-significant bit of the most-significant byte |
| 2158 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2159 | * opposite of the one we need. The exception is ints |
| 2160 | * 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] | 2161 | * its own 256's-complement, so has the right sign bit |
| 2162 | * even without the extra byte. That's a pain to check |
| 2163 | * for in advance, though, so we always grab an extra |
| 2164 | * byte at the start, and cut it back later if possible. |
| 2165 | */ |
| 2166 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 2167 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2168 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2169 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2170 | goto error; |
| 2171 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2172 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2173 | if (repr == NULL) |
| 2174 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2175 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2176 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 2177 | pdata, nbytes, |
| 2178 | 1 /* little endian */ , 1 /* signed */ ); |
| 2179 | if (i < 0) |
| 2180 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2181 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2182 | * needed. This is so iff the MSB is all redundant sign |
| 2183 | * bits. |
| 2184 | */ |
| 2185 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2186 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2187 | pdata[nbytes - 1] == 0xff && |
| 2188 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 2189 | nbytes--; |
| 2190 | } |
| 2191 | |
| 2192 | if (nbytes < 256) { |
| 2193 | header[0] = LONG1; |
| 2194 | header[1] = (unsigned char)nbytes; |
| 2195 | size = 2; |
| 2196 | } |
| 2197 | else { |
| 2198 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2199 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2200 | for (i = 1; i < 5; i++) { |
| 2201 | header[i] = (unsigned char)(size & 0xff); |
| 2202 | size >>= 8; |
| 2203 | } |
| 2204 | size = 5; |
| 2205 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2206 | if (_Pickler_Write(self, header, size) < 0 || |
| 2207 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2208 | goto error; |
| 2209 | } |
| 2210 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2211 | const char long_op = LONG; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2212 | const char *string; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2213 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 2214 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 2215 | the number of digits), in both directions. We add a trailing 'L' |
| 2216 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2217 | |
| 2218 | repr = PyObject_Repr(obj); |
| 2219 | if (repr == NULL) |
| 2220 | goto error; |
| 2221 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2222 | string = PyUnicode_AsUTF8AndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2223 | if (string == NULL) |
| 2224 | goto error; |
| 2225 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2226 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 2227 | _Pickler_Write(self, string, size) < 0 || |
| 2228 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2229 | goto error; |
| 2230 | } |
| 2231 | |
| 2232 | if (0) { |
| 2233 | error: |
| 2234 | status = -1; |
| 2235 | } |
| 2236 | Py_XDECREF(repr); |
| 2237 | |
| 2238 | return status; |
| 2239 | } |
| 2240 | |
| 2241 | static int |
| 2242 | save_float(PicklerObject *self, PyObject *obj) |
| 2243 | { |
| 2244 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 2245 | |
| 2246 | if (self->bin) { |
| 2247 | char pdata[9]; |
| 2248 | pdata[0] = BINFLOAT; |
| 2249 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2250 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2251 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2252 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2253 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2254 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2255 | int result = -1; |
| 2256 | char *buf = NULL; |
| 2257 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2258 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2259 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2260 | goto done; |
| 2261 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2262 | 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] | 2263 | if (!buf) { |
| 2264 | PyErr_NoMemory(); |
| 2265 | goto done; |
| 2266 | } |
| 2267 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2268 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2269 | goto done; |
| 2270 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2271 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2272 | goto done; |
| 2273 | |
| 2274 | result = 0; |
| 2275 | done: |
| 2276 | PyMem_Free(buf); |
| 2277 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2278 | } |
| 2279 | |
| 2280 | return 0; |
| 2281 | } |
| 2282 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2283 | /* Perform direct write of the header and payload of the binary object. |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2284 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2285 | The large contiguous data is written directly into the underlying file |
| 2286 | object, bypassing the output_buffer of the Pickler. We intentionally |
| 2287 | do not insert a protocol 4 frame opcode to make it possible to optimize |
| 2288 | file.read calls in the loader. |
| 2289 | */ |
| 2290 | static int |
| 2291 | _Pickler_write_bytes(PicklerObject *self, |
| 2292 | const char *header, Py_ssize_t header_size, |
| 2293 | const char *data, Py_ssize_t data_size, |
| 2294 | PyObject *payload) |
| 2295 | { |
| 2296 | int bypass_buffer = (data_size >= FRAME_SIZE_TARGET); |
| 2297 | int framing = self->framing; |
| 2298 | |
| 2299 | if (bypass_buffer) { |
| 2300 | assert(self->output_buffer != NULL); |
| 2301 | /* Commit the previous frame. */ |
| 2302 | if (_Pickler_CommitFrame(self)) { |
| 2303 | return -1; |
| 2304 | } |
| 2305 | /* Disable framing temporarily */ |
| 2306 | self->framing = 0; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2307 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2308 | |
| 2309 | if (_Pickler_Write(self, header, header_size) < 0) { |
| 2310 | return -1; |
| 2311 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2312 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2313 | if (bypass_buffer && self->write != NULL) { |
| 2314 | /* Bypass the in-memory buffer to directly stream large data |
| 2315 | into the underlying file object. */ |
| 2316 | PyObject *result, *mem = NULL; |
| 2317 | /* Dump the output buffer to the file. */ |
| 2318 | if (_Pickler_FlushToFile(self) < 0) { |
| 2319 | return -1; |
| 2320 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2321 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2322 | /* Stream write the payload into the file without going through the |
| 2323 | output buffer. */ |
| 2324 | if (payload == NULL) { |
Serhiy Storchaka | 5b76bdb | 2018-01-13 00:28:31 +0200 | [diff] [blame] | 2325 | /* TODO: It would be better to use a memoryview with a linked |
| 2326 | original string if this is possible. */ |
| 2327 | payload = mem = PyBytes_FromStringAndSize(data, data_size); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2328 | if (payload == NULL) { |
| 2329 | return -1; |
| 2330 | } |
| 2331 | } |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 2332 | result = PyObject_CallOneArg(self->write, payload); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2333 | Py_XDECREF(mem); |
| 2334 | if (result == NULL) { |
| 2335 | return -1; |
| 2336 | } |
| 2337 | Py_DECREF(result); |
| 2338 | |
| 2339 | /* Reinitialize the buffer for subsequent calls to _Pickler_Write. */ |
| 2340 | if (_Pickler_ClearBuffer(self) < 0) { |
| 2341 | return -1; |
| 2342 | } |
| 2343 | } |
| 2344 | else { |
| 2345 | if (_Pickler_Write(self, data, data_size) < 0) { |
| 2346 | return -1; |
| 2347 | } |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | /* Re-enable framing for subsequent calls to _Pickler_Write. */ |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2351 | self->framing = framing; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2352 | |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2356 | static int |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2357 | _save_bytes_data(PicklerObject *self, PyObject *obj, const char *data, |
| 2358 | Py_ssize_t size) |
| 2359 | { |
| 2360 | assert(self->proto >= 3); |
| 2361 | |
| 2362 | char header[9]; |
| 2363 | Py_ssize_t len; |
| 2364 | |
| 2365 | if (size < 0) |
| 2366 | return -1; |
| 2367 | |
| 2368 | if (size <= 0xff) { |
| 2369 | header[0] = SHORT_BINBYTES; |
| 2370 | header[1] = (unsigned char)size; |
| 2371 | len = 2; |
| 2372 | } |
| 2373 | else if ((size_t)size <= 0xffffffffUL) { |
| 2374 | header[0] = BINBYTES; |
| 2375 | header[1] = (unsigned char)(size & 0xff); |
| 2376 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2377 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2378 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2379 | len = 5; |
| 2380 | } |
| 2381 | else if (self->proto >= 4) { |
| 2382 | header[0] = BINBYTES8; |
| 2383 | _write_size64(header + 1, size); |
| 2384 | len = 9; |
| 2385 | } |
| 2386 | else { |
| 2387 | PyErr_SetString(PyExc_OverflowError, |
| 2388 | "serializing a bytes object larger than 4 GiB " |
| 2389 | "requires pickle protocol 4 or higher"); |
| 2390 | return -1; |
| 2391 | } |
| 2392 | |
| 2393 | if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) { |
| 2394 | return -1; |
| 2395 | } |
| 2396 | |
| 2397 | if (memo_put(self, obj) < 0) { |
| 2398 | return -1; |
| 2399 | } |
| 2400 | |
| 2401 | return 0; |
| 2402 | } |
| 2403 | |
| 2404 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2405 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2406 | { |
| 2407 | if (self->proto < 3) { |
| 2408 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2409 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2410 | the __reduce__ method) to permit bytes object unpickling. |
| 2411 | |
| 2412 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2413 | 2 'bytes' is just an alias for 'str' (which has different |
| 2414 | parameters than the actual bytes object), we use codecs.encode |
| 2415 | to create the appropriate 'str' object when unpickled using |
| 2416 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2417 | using Python 3. Again this is a hack and we don't need to do this |
| 2418 | with newer protocols. */ |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 2419 | PyObject *reduce_value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2420 | int status; |
| 2421 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2422 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2423 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2424 | } |
| 2425 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2426 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2427 | PyObject *unicode_str = |
| 2428 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2429 | PyBytes_GET_SIZE(obj), |
| 2430 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2431 | _Py_IDENTIFIER(latin1); |
| 2432 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2433 | if (unicode_str == NULL) |
| 2434 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2435 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2436 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2437 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2438 | Py_DECREF(unicode_str); |
| 2439 | } |
| 2440 | |
| 2441 | if (reduce_value == NULL) |
| 2442 | return -1; |
| 2443 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2444 | /* save_reduce() will memoize the object automatically. */ |
| 2445 | status = save_reduce(self, reduce_value, obj); |
| 2446 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2447 | return status; |
| 2448 | } |
| 2449 | else { |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2450 | return _save_bytes_data(self, obj, PyBytes_AS_STRING(obj), |
| 2451 | PyBytes_GET_SIZE(obj)); |
| 2452 | } |
| 2453 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2454 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2455 | static int |
| 2456 | _save_bytearray_data(PicklerObject *self, PyObject *obj, const char *data, |
| 2457 | Py_ssize_t size) |
| 2458 | { |
| 2459 | assert(self->proto >= 5); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2460 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2461 | char header[9]; |
| 2462 | Py_ssize_t len; |
| 2463 | |
| 2464 | if (size < 0) |
| 2465 | return -1; |
| 2466 | |
| 2467 | header[0] = BYTEARRAY8; |
| 2468 | _write_size64(header + 1, size); |
| 2469 | len = 9; |
| 2470 | |
| 2471 | if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) { |
| 2472 | return -1; |
| 2473 | } |
| 2474 | |
| 2475 | if (memo_put(self, obj) < 0) { |
| 2476 | return -1; |
| 2477 | } |
| 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
| 2482 | static int |
| 2483 | save_bytearray(PicklerObject *self, PyObject *obj) |
| 2484 | { |
| 2485 | if (self->proto < 5) { |
| 2486 | /* Older pickle protocols do not have an opcode for pickling |
| 2487 | * bytearrays. */ |
| 2488 | PyObject *reduce_value = NULL; |
| 2489 | int status; |
| 2490 | |
| 2491 | if (PyByteArray_GET_SIZE(obj) == 0) { |
| 2492 | reduce_value = Py_BuildValue("(O())", |
| 2493 | (PyObject *) &PyByteArray_Type); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2494 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2495 | else { |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2496 | PyObject *bytes_obj = PyBytes_FromObject(obj); |
| 2497 | if (bytes_obj != NULL) { |
| 2498 | reduce_value = Py_BuildValue("(O(O))", |
| 2499 | (PyObject *) &PyByteArray_Type, |
| 2500 | bytes_obj); |
| 2501 | Py_DECREF(bytes_obj); |
| 2502 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2503 | } |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2504 | if (reduce_value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2505 | return -1; |
| 2506 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2507 | /* save_reduce() will memoize the object automatically. */ |
| 2508 | status = save_reduce(self, reduce_value, obj); |
| 2509 | Py_DECREF(reduce_value); |
| 2510 | return status; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2511 | } |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2512 | else { |
| 2513 | return _save_bytearray_data(self, obj, PyByteArray_AS_STRING(obj), |
| 2514 | PyByteArray_GET_SIZE(obj)); |
| 2515 | } |
| 2516 | } |
| 2517 | |
| 2518 | static int |
| 2519 | save_picklebuffer(PicklerObject *self, PyObject *obj) |
| 2520 | { |
| 2521 | if (self->proto < 5) { |
| 2522 | PickleState *st = _Pickle_GetGlobalState(); |
| 2523 | PyErr_SetString(st->PicklingError, |
| 2524 | "PickleBuffer can only pickled with protocol >= 5"); |
| 2525 | return -1; |
| 2526 | } |
| 2527 | const Py_buffer* view = PyPickleBuffer_GetBuffer(obj); |
| 2528 | if (view == NULL) { |
| 2529 | return -1; |
| 2530 | } |
| 2531 | if (view->suboffsets != NULL || !PyBuffer_IsContiguous(view, 'A')) { |
| 2532 | PickleState *st = _Pickle_GetGlobalState(); |
| 2533 | PyErr_SetString(st->PicklingError, |
| 2534 | "PickleBuffer can not be pickled when " |
| 2535 | "pointing to a non-contiguous buffer"); |
| 2536 | return -1; |
| 2537 | } |
| 2538 | int in_band = 1; |
| 2539 | if (self->buffer_callback != NULL) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 2540 | PyObject *ret = PyObject_CallOneArg(self->buffer_callback, obj); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2541 | if (ret == NULL) { |
| 2542 | return -1; |
| 2543 | } |
| 2544 | in_band = PyObject_IsTrue(ret); |
| 2545 | Py_DECREF(ret); |
| 2546 | if (in_band == -1) { |
| 2547 | return -1; |
| 2548 | } |
| 2549 | } |
| 2550 | if (in_band) { |
| 2551 | /* Write data in-band */ |
| 2552 | if (view->readonly) { |
| 2553 | return _save_bytes_data(self, obj, (const char*) view->buf, |
| 2554 | view->len); |
| 2555 | } |
| 2556 | else { |
| 2557 | return _save_bytearray_data(self, obj, (const char*) view->buf, |
| 2558 | view->len); |
| 2559 | } |
| 2560 | } |
| 2561 | else { |
| 2562 | /* Write data out-of-band */ |
| 2563 | const char next_buffer_op = NEXT_BUFFER; |
| 2564 | if (_Pickler_Write(self, &next_buffer_op, 1) < 0) { |
| 2565 | return -1; |
| 2566 | } |
| 2567 | if (view->readonly) { |
| 2568 | const char readonly_buffer_op = READONLY_BUFFER; |
| 2569 | if (_Pickler_Write(self, &readonly_buffer_op, 1) < 0) { |
| 2570 | return -1; |
| 2571 | } |
| 2572 | } |
| 2573 | } |
| 2574 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2575 | } |
| 2576 | |
| 2577 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2578 | backslash and newline characters to \uXXXX escapes. */ |
| 2579 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2580 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2581 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2582 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2583 | Py_ssize_t i, size; |
Serhiy Storchaka | cd8295f | 2020-04-11 10:48:40 +0300 | [diff] [blame] | 2584 | const void *data; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2585 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2586 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2587 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2588 | if (PyUnicode_READY(obj)) |
| 2589 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2590 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2591 | _PyBytesWriter_Init(&writer); |
| 2592 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2593 | size = PyUnicode_GET_LENGTH(obj); |
| 2594 | data = PyUnicode_DATA(obj); |
| 2595 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2596 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2597 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2598 | if (p == NULL) |
| 2599 | goto error; |
| 2600 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2601 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2602 | for (i=0; i < size; i++) { |
| 2603 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2604 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2605 | if (ch >= 0x10000) { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2606 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2607 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2608 | if (p == NULL) |
| 2609 | goto error; |
| 2610 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2611 | *p++ = '\\'; |
| 2612 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2613 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2614 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2615 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2616 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2617 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2618 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2619 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2620 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2621 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2622 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Serhiy Storchaka | 38ab7d4 | 2019-05-31 11:29:39 +0300 | [diff] [blame] | 2623 | else if (ch >= 256 || |
| 2624 | ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' || |
| 2625 | ch == 0x1a) |
| 2626 | { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2627 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2628 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2629 | if (p == NULL) |
| 2630 | goto error; |
| 2631 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2632 | *p++ = '\\'; |
| 2633 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2634 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2635 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2636 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2637 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2638 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2639 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2640 | else |
| 2641 | *p++ = (char) ch; |
| 2642 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2643 | |
| 2644 | return _PyBytesWriter_Finish(&writer, p); |
| 2645 | |
| 2646 | error: |
| 2647 | _PyBytesWriter_Dealloc(&writer); |
| 2648 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | static int |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2652 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2653 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2654 | char header[9]; |
| 2655 | Py_ssize_t len; |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2656 | PyObject *encoded = NULL; |
| 2657 | Py_ssize_t size; |
| 2658 | const char *data; |
| 2659 | |
| 2660 | if (PyUnicode_READY(obj)) |
| 2661 | return -1; |
| 2662 | |
| 2663 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2664 | if (data == NULL) { |
| 2665 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2666 | "surrogatepass" error handler. */ |
| 2667 | PyErr_Clear(); |
| 2668 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2669 | if (encoded == NULL) |
| 2670 | return -1; |
| 2671 | |
| 2672 | data = PyBytes_AS_STRING(encoded); |
| 2673 | size = PyBytes_GET_SIZE(encoded); |
| 2674 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2675 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2676 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2677 | if (size <= 0xff && self->proto >= 4) { |
| 2678 | header[0] = SHORT_BINUNICODE; |
| 2679 | header[1] = (unsigned char)(size & 0xff); |
| 2680 | len = 2; |
| 2681 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2682 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2683 | header[0] = BINUNICODE; |
| 2684 | header[1] = (unsigned char)(size & 0xff); |
| 2685 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2686 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2687 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2688 | len = 5; |
| 2689 | } |
| 2690 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2691 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2692 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2693 | len = 9; |
| 2694 | } |
| 2695 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2696 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 2697 | "serializing a string larger than 4 GiB " |
| 2698 | "requires pickle protocol 4 or higher"); |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2699 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2700 | return -1; |
| 2701 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2702 | |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2703 | if (_Pickler_write_bytes(self, header, len, data, size, encoded) < 0) { |
| 2704 | Py_XDECREF(encoded); |
| 2705 | return -1; |
Olivier Grisel | 3cd7c6e | 2018-01-06 16:18:54 +0100 | [diff] [blame] | 2706 | } |
Serhiy Storchaka | 0a2da50 | 2018-01-11 13:03:20 +0200 | [diff] [blame] | 2707 | Py_XDECREF(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2708 | return 0; |
| 2709 | } |
| 2710 | |
| 2711 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2712 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2713 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2714 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2715 | if (write_unicode_binary(self, obj) < 0) |
| 2716 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2717 | } |
| 2718 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2719 | PyObject *encoded; |
| 2720 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2721 | const char unicode_op = UNICODE; |
| 2722 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2723 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2724 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2725 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2726 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2727 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2728 | Py_DECREF(encoded); |
| 2729 | return -1; |
| 2730 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2731 | |
| 2732 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2733 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2734 | Py_DECREF(encoded); |
| 2735 | return -1; |
| 2736 | } |
| 2737 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2738 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2739 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2740 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2741 | } |
| 2742 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2743 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2744 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2745 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2746 | } |
| 2747 | |
| 2748 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2749 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2750 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2751 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2752 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2753 | |
| 2754 | assert(PyTuple_Size(t) == len); |
| 2755 | |
| 2756 | for (i = 0; i < len; i++) { |
| 2757 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2758 | |
| 2759 | if (element == NULL) |
| 2760 | return -1; |
| 2761 | if (save(self, element, 0) < 0) |
| 2762 | return -1; |
| 2763 | } |
| 2764 | |
| 2765 | return 0; |
| 2766 | } |
| 2767 | |
| 2768 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2769 | * used across protocols to minimize the space needed to pickle them. |
| 2770 | * Tuples are also the only builtin immutable type that can be recursive |
| 2771 | * (a tuple can be reached from itself), and that requires some subtle |
| 2772 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2773 | */ |
| 2774 | static int |
| 2775 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2776 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2777 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2778 | |
| 2779 | const char mark_op = MARK; |
| 2780 | const char tuple_op = TUPLE; |
| 2781 | const char pop_op = POP; |
| 2782 | const char pop_mark_op = POP_MARK; |
| 2783 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2784 | |
| 2785 | if ((len = PyTuple_Size(obj)) < 0) |
| 2786 | return -1; |
| 2787 | |
| 2788 | if (len == 0) { |
| 2789 | char pdata[2]; |
| 2790 | |
| 2791 | if (self->proto) { |
| 2792 | pdata[0] = EMPTY_TUPLE; |
| 2793 | len = 1; |
| 2794 | } |
| 2795 | else { |
| 2796 | pdata[0] = MARK; |
| 2797 | pdata[1] = TUPLE; |
| 2798 | len = 2; |
| 2799 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2800 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2801 | return -1; |
| 2802 | return 0; |
| 2803 | } |
| 2804 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2805 | /* 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] | 2806 | * saving the tuple elements, the tuple must be recursive, in |
| 2807 | * which case we'll pop everything we put on the stack, and fetch |
| 2808 | * its value from the memo. |
| 2809 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2810 | if (len <= 3 && self->proto >= 2) { |
| 2811 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2812 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2813 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2814 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2815 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2816 | /* pop the len elements */ |
| 2817 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2818 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2819 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2820 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2821 | if (memo_get(self, obj) < 0) |
| 2822 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2823 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2824 | return 0; |
| 2825 | } |
| 2826 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2827 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2828 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2829 | } |
| 2830 | goto memoize; |
| 2831 | } |
| 2832 | |
| 2833 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2834 | * Generate MARK e1 e2 ... TUPLE |
| 2835 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2836 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2837 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2838 | |
| 2839 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2840 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2841 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2842 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2843 | /* pop the stack stuff we pushed */ |
| 2844 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2845 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2846 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2847 | } |
| 2848 | else { |
| 2849 | /* Note that we pop one more than len, to remove |
| 2850 | * the MARK too. |
| 2851 | */ |
| 2852 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2853 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2854 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2855 | } |
| 2856 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2857 | if (memo_get(self, obj) < 0) |
| 2858 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2859 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2860 | return 0; |
| 2861 | } |
| 2862 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2863 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2864 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2865 | } |
| 2866 | |
| 2867 | memoize: |
| 2868 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2869 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2870 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2871 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | /* iter is an iterator giving items, and we batch up chunks of |
| 2875 | * MARK item item ... item APPENDS |
| 2876 | * opcode sequences. Calling code should have arranged to first create an |
| 2877 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2878 | * Returns 0 on success, <0 on error. |
| 2879 | */ |
| 2880 | static int |
| 2881 | batch_list(PicklerObject *self, PyObject *iter) |
| 2882 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2883 | PyObject *obj = NULL; |
| 2884 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2885 | int i, n; |
| 2886 | |
| 2887 | const char mark_op = MARK; |
| 2888 | const char append_op = APPEND; |
| 2889 | const char appends_op = APPENDS; |
| 2890 | |
| 2891 | assert(iter != NULL); |
| 2892 | |
| 2893 | /* XXX: I think this function could be made faster by avoiding the |
| 2894 | iterator interface and fetching objects directly from list using |
| 2895 | PyList_GET_ITEM. |
| 2896 | */ |
| 2897 | |
| 2898 | if (self->proto == 0) { |
| 2899 | /* APPENDS isn't available; do one at a time. */ |
| 2900 | for (;;) { |
| 2901 | obj = PyIter_Next(iter); |
| 2902 | if (obj == NULL) { |
| 2903 | if (PyErr_Occurred()) |
| 2904 | return -1; |
| 2905 | break; |
| 2906 | } |
| 2907 | i = save(self, obj, 0); |
| 2908 | Py_DECREF(obj); |
| 2909 | if (i < 0) |
| 2910 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2911 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2912 | return -1; |
| 2913 | } |
| 2914 | return 0; |
| 2915 | } |
| 2916 | |
| 2917 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2918 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2919 | /* Get first item */ |
| 2920 | firstitem = PyIter_Next(iter); |
| 2921 | if (firstitem == NULL) { |
| 2922 | if (PyErr_Occurred()) |
| 2923 | goto error; |
| 2924 | |
| 2925 | /* nothing more to add */ |
| 2926 | break; |
| 2927 | } |
| 2928 | |
| 2929 | /* Try to get a second item */ |
| 2930 | obj = PyIter_Next(iter); |
| 2931 | if (obj == NULL) { |
| 2932 | if (PyErr_Occurred()) |
| 2933 | goto error; |
| 2934 | |
| 2935 | /* Only one item to write */ |
| 2936 | if (save(self, firstitem, 0) < 0) |
| 2937 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2938 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2939 | goto error; |
| 2940 | Py_CLEAR(firstitem); |
| 2941 | break; |
| 2942 | } |
| 2943 | |
| 2944 | /* More than one item to write */ |
| 2945 | |
| 2946 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2947 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2948 | goto error; |
| 2949 | |
| 2950 | if (save(self, firstitem, 0) < 0) |
| 2951 | goto error; |
| 2952 | Py_CLEAR(firstitem); |
| 2953 | n = 1; |
| 2954 | |
| 2955 | /* Fetch and save up to BATCHSIZE items */ |
| 2956 | while (obj) { |
| 2957 | if (save(self, obj, 0) < 0) |
| 2958 | goto error; |
| 2959 | Py_CLEAR(obj); |
| 2960 | n += 1; |
| 2961 | |
| 2962 | if (n == BATCHSIZE) |
| 2963 | break; |
| 2964 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2965 | obj = PyIter_Next(iter); |
| 2966 | if (obj == NULL) { |
| 2967 | if (PyErr_Occurred()) |
| 2968 | goto error; |
| 2969 | break; |
| 2970 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2971 | } |
| 2972 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2973 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2974 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2975 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2976 | } while (n == BATCHSIZE); |
| 2977 | return 0; |
| 2978 | |
| 2979 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2980 | Py_XDECREF(firstitem); |
| 2981 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2982 | return -1; |
| 2983 | } |
| 2984 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2985 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2986 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2987 | * MARK item item ... item APPENDS |
| 2988 | * opcode sequences. Calling code should have arranged to first create an |
| 2989 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2990 | * Returns 0 on success, -1 on error. |
| 2991 | * |
| 2992 | * This version is considerably faster than batch_list(), if less general. |
| 2993 | * |
| 2994 | * Note that this only works for protocols > 0. |
| 2995 | */ |
| 2996 | static int |
| 2997 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2998 | { |
| 2999 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3000 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3001 | |
| 3002 | const char append_op = APPEND; |
| 3003 | const char appends_op = APPENDS; |
| 3004 | const char mark_op = MARK; |
| 3005 | |
| 3006 | assert(obj != NULL); |
| 3007 | assert(self->proto > 0); |
| 3008 | assert(PyList_CheckExact(obj)); |
| 3009 | |
| 3010 | if (PyList_GET_SIZE(obj) == 1) { |
| 3011 | item = PyList_GET_ITEM(obj, 0); |
| 3012 | if (save(self, item, 0) < 0) |
| 3013 | return -1; |
| 3014 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 3015 | return -1; |
| 3016 | return 0; |
| 3017 | } |
| 3018 | |
| 3019 | /* Write in batches of BATCHSIZE. */ |
| 3020 | total = 0; |
| 3021 | do { |
| 3022 | this_batch = 0; |
| 3023 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3024 | return -1; |
| 3025 | while (total < PyList_GET_SIZE(obj)) { |
| 3026 | item = PyList_GET_ITEM(obj, total); |
| 3027 | if (save(self, item, 0) < 0) |
| 3028 | return -1; |
| 3029 | total++; |
| 3030 | if (++this_batch == BATCHSIZE) |
| 3031 | break; |
| 3032 | } |
| 3033 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 3034 | return -1; |
| 3035 | |
| 3036 | } while (total < PyList_GET_SIZE(obj)); |
| 3037 | |
| 3038 | return 0; |
| 3039 | } |
| 3040 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3041 | static int |
| 3042 | save_list(PicklerObject *self, PyObject *obj) |
| 3043 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3044 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3045 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3046 | int status = 0; |
| 3047 | |
| 3048 | if (self->fast && !fast_save_enter(self, obj)) |
| 3049 | goto error; |
| 3050 | |
| 3051 | /* Create an empty list. */ |
| 3052 | if (self->bin) { |
| 3053 | header[0] = EMPTY_LIST; |
| 3054 | len = 1; |
| 3055 | } |
| 3056 | else { |
| 3057 | header[0] = MARK; |
| 3058 | header[1] = LIST; |
| 3059 | len = 2; |
| 3060 | } |
| 3061 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3062 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3063 | goto error; |
| 3064 | |
| 3065 | /* Get list length, and bow out early if empty. */ |
| 3066 | if ((len = PyList_Size(obj)) < 0) |
| 3067 | goto error; |
| 3068 | |
| 3069 | if (memo_put(self, obj) < 0) |
| 3070 | goto error; |
| 3071 | |
| 3072 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3073 | /* Materialize the list elements. */ |
| 3074 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3075 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3076 | goto error; |
| 3077 | status = batch_list_exact(self, obj); |
| 3078 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3079 | } else { |
| 3080 | PyObject *iter = PyObject_GetIter(obj); |
| 3081 | if (iter == NULL) |
| 3082 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3083 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3084 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3085 | Py_DECREF(iter); |
| 3086 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3087 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3088 | status = batch_list(self, iter); |
| 3089 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3090 | Py_DECREF(iter); |
| 3091 | } |
| 3092 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3093 | if (0) { |
| 3094 | error: |
| 3095 | status = -1; |
| 3096 | } |
| 3097 | |
| 3098 | if (self->fast && !fast_save_leave(self, obj)) |
| 3099 | status = -1; |
| 3100 | |
| 3101 | return status; |
| 3102 | } |
| 3103 | |
| 3104 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 3105 | * MARK key value ... key value SETITEMS |
| 3106 | * opcode sequences. Calling code should have arranged to first create an |
| 3107 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 3108 | * Returns 0 on success, <0 on error. |
| 3109 | * |
| 3110 | * This is very much like batch_list(). The difference between saving |
| 3111 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 3112 | * the C level, though, that attempts to combine these routines were too |
| 3113 | * ugly to bear. |
| 3114 | */ |
| 3115 | static int |
| 3116 | batch_dict(PicklerObject *self, PyObject *iter) |
| 3117 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3118 | PyObject *obj = NULL; |
| 3119 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3120 | int i, n; |
| 3121 | |
| 3122 | const char mark_op = MARK; |
| 3123 | const char setitem_op = SETITEM; |
| 3124 | const char setitems_op = SETITEMS; |
| 3125 | |
| 3126 | assert(iter != NULL); |
| 3127 | |
| 3128 | if (self->proto == 0) { |
| 3129 | /* SETITEMS isn't available; do one at a time. */ |
| 3130 | for (;;) { |
| 3131 | obj = PyIter_Next(iter); |
| 3132 | if (obj == NULL) { |
| 3133 | if (PyErr_Occurred()) |
| 3134 | return -1; |
| 3135 | break; |
| 3136 | } |
| 3137 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 3138 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 3139 | "iterator must return 2-tuples"); |
| 3140 | return -1; |
| 3141 | } |
| 3142 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 3143 | if (i >= 0) |
| 3144 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 3145 | Py_DECREF(obj); |
| 3146 | if (i < 0) |
| 3147 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3148 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3149 | return -1; |
| 3150 | } |
| 3151 | return 0; |
| 3152 | } |
| 3153 | |
| 3154 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 3155 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3156 | /* Get first item */ |
| 3157 | firstitem = PyIter_Next(iter); |
| 3158 | if (firstitem == NULL) { |
| 3159 | if (PyErr_Occurred()) |
| 3160 | goto error; |
| 3161 | |
| 3162 | /* nothing more to add */ |
| 3163 | break; |
| 3164 | } |
| 3165 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 3166 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 3167 | "iterator must return 2-tuples"); |
| 3168 | goto error; |
| 3169 | } |
| 3170 | |
| 3171 | /* Try to get a second item */ |
| 3172 | obj = PyIter_Next(iter); |
| 3173 | if (obj == NULL) { |
| 3174 | if (PyErr_Occurred()) |
| 3175 | goto error; |
| 3176 | |
| 3177 | /* Only one item to write */ |
| 3178 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 3179 | goto error; |
| 3180 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 3181 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3182 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3183 | goto error; |
| 3184 | Py_CLEAR(firstitem); |
| 3185 | break; |
| 3186 | } |
| 3187 | |
| 3188 | /* More than one item to write */ |
| 3189 | |
| 3190 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3191 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3192 | goto error; |
| 3193 | |
| 3194 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 3195 | goto error; |
| 3196 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 3197 | goto error; |
| 3198 | Py_CLEAR(firstitem); |
| 3199 | n = 1; |
| 3200 | |
| 3201 | /* Fetch and save up to BATCHSIZE items */ |
| 3202 | while (obj) { |
| 3203 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 3204 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 3205 | "iterator must return 2-tuples"); |
| 3206 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3207 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3208 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 3209 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 3210 | goto error; |
| 3211 | Py_CLEAR(obj); |
| 3212 | n += 1; |
| 3213 | |
| 3214 | if (n == BATCHSIZE) |
| 3215 | break; |
| 3216 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3217 | obj = PyIter_Next(iter); |
| 3218 | if (obj == NULL) { |
| 3219 | if (PyErr_Occurred()) |
| 3220 | goto error; |
| 3221 | break; |
| 3222 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3223 | } |
| 3224 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3225 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3226 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3227 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3228 | } while (n == BATCHSIZE); |
| 3229 | return 0; |
| 3230 | |
| 3231 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 3232 | Py_XDECREF(firstitem); |
| 3233 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3234 | return -1; |
| 3235 | } |
| 3236 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3237 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 3238 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 3239 | * MARK key value ... key value SETITEMS |
| 3240 | * opcode sequences. Calling code should have arranged to first create an |
| 3241 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 3242 | * Returns 0 on success, -1 on error. |
| 3243 | * |
| 3244 | * Note that this currently doesn't work for protocol 0. |
| 3245 | */ |
| 3246 | static int |
| 3247 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 3248 | { |
| 3249 | PyObject *key = NULL, *value = NULL; |
| 3250 | int i; |
| 3251 | Py_ssize_t dict_size, ppos = 0; |
| 3252 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 3253 | const char mark_op = MARK; |
| 3254 | const char setitem_op = SETITEM; |
| 3255 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3256 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3257 | assert(obj != NULL && PyDict_CheckExact(obj)); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3258 | assert(self->proto > 0); |
| 3259 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3260 | dict_size = PyDict_GET_SIZE(obj); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3261 | |
| 3262 | /* Special-case len(d) == 1 to save space. */ |
| 3263 | if (dict_size == 1) { |
| 3264 | PyDict_Next(obj, &ppos, &key, &value); |
| 3265 | if (save(self, key, 0) < 0) |
| 3266 | return -1; |
| 3267 | if (save(self, value, 0) < 0) |
| 3268 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3269 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3270 | return -1; |
| 3271 | return 0; |
| 3272 | } |
| 3273 | |
| 3274 | /* Write in batches of BATCHSIZE. */ |
| 3275 | do { |
| 3276 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3277 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3278 | return -1; |
| 3279 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 3280 | if (save(self, key, 0) < 0) |
| 3281 | return -1; |
| 3282 | if (save(self, value, 0) < 0) |
| 3283 | return -1; |
| 3284 | if (++i == BATCHSIZE) |
| 3285 | break; |
| 3286 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3287 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3288 | return -1; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3289 | if (PyDict_GET_SIZE(obj) != dict_size) { |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3290 | PyErr_Format( |
| 3291 | PyExc_RuntimeError, |
| 3292 | "dictionary changed size during iteration"); |
| 3293 | return -1; |
| 3294 | } |
| 3295 | |
| 3296 | } while (i == BATCHSIZE); |
| 3297 | return 0; |
| 3298 | } |
| 3299 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3300 | static int |
| 3301 | save_dict(PicklerObject *self, PyObject *obj) |
| 3302 | { |
| 3303 | PyObject *items, *iter; |
| 3304 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3305 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3306 | int status = 0; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3307 | assert(PyDict_Check(obj)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3308 | |
| 3309 | if (self->fast && !fast_save_enter(self, obj)) |
| 3310 | goto error; |
| 3311 | |
| 3312 | /* Create an empty dict. */ |
| 3313 | if (self->bin) { |
| 3314 | header[0] = EMPTY_DICT; |
| 3315 | len = 1; |
| 3316 | } |
| 3317 | else { |
| 3318 | header[0] = MARK; |
| 3319 | header[1] = DICT; |
| 3320 | len = 2; |
| 3321 | } |
| 3322 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3323 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3324 | goto error; |
| 3325 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3326 | if (memo_put(self, obj) < 0) |
| 3327 | goto error; |
| 3328 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 3329 | if (PyDict_GET_SIZE(obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3330 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3331 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 3332 | /* We can take certain shortcuts if we know this is a dict and |
| 3333 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3334 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 3335 | goto error; |
| 3336 | status = batch_dict_exact(self, obj); |
| 3337 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3338 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 3339 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 3340 | |
Jeroen Demeyer | 762f93f | 2019-07-08 10:19:25 +0200 | [diff] [blame] | 3341 | items = _PyObject_CallMethodIdNoArgs(obj, &PyId_items); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3342 | if (items == NULL) |
| 3343 | goto error; |
| 3344 | iter = PyObject_GetIter(items); |
| 3345 | Py_DECREF(items); |
| 3346 | if (iter == NULL) |
| 3347 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3348 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 3349 | Py_DECREF(iter); |
| 3350 | goto error; |
| 3351 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3352 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3353 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 3354 | Py_DECREF(iter); |
| 3355 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
| 3358 | if (0) { |
| 3359 | error: |
| 3360 | status = -1; |
| 3361 | } |
| 3362 | |
| 3363 | if (self->fast && !fast_save_leave(self, obj)) |
| 3364 | status = -1; |
| 3365 | |
| 3366 | return status; |
| 3367 | } |
| 3368 | |
| 3369 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3370 | save_set(PicklerObject *self, PyObject *obj) |
| 3371 | { |
| 3372 | PyObject *item; |
| 3373 | int i; |
| 3374 | Py_ssize_t set_size, ppos = 0; |
| 3375 | Py_hash_t hash; |
| 3376 | |
| 3377 | const char empty_set_op = EMPTY_SET; |
| 3378 | const char mark_op = MARK; |
| 3379 | const char additems_op = ADDITEMS; |
| 3380 | |
| 3381 | if (self->proto < 4) { |
| 3382 | PyObject *items; |
| 3383 | PyObject *reduce_value; |
| 3384 | int status; |
| 3385 | |
| 3386 | items = PySequence_List(obj); |
| 3387 | if (items == NULL) { |
| 3388 | return -1; |
| 3389 | } |
| 3390 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 3391 | Py_DECREF(items); |
| 3392 | if (reduce_value == NULL) { |
| 3393 | return -1; |
| 3394 | } |
| 3395 | /* save_reduce() will memoize the object automatically. */ |
| 3396 | status = save_reduce(self, reduce_value, obj); |
| 3397 | Py_DECREF(reduce_value); |
| 3398 | return status; |
| 3399 | } |
| 3400 | |
| 3401 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 3402 | return -1; |
| 3403 | |
| 3404 | if (memo_put(self, obj) < 0) |
| 3405 | return -1; |
| 3406 | |
| 3407 | set_size = PySet_GET_SIZE(obj); |
| 3408 | if (set_size == 0) |
| 3409 | return 0; /* nothing to do */ |
| 3410 | |
| 3411 | /* Write in batches of BATCHSIZE. */ |
| 3412 | do { |
| 3413 | i = 0; |
| 3414 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3415 | return -1; |
| 3416 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 3417 | if (save(self, item, 0) < 0) |
| 3418 | return -1; |
| 3419 | if (++i == BATCHSIZE) |
| 3420 | break; |
| 3421 | } |
| 3422 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 3423 | return -1; |
| 3424 | if (PySet_GET_SIZE(obj) != set_size) { |
| 3425 | PyErr_Format( |
| 3426 | PyExc_RuntimeError, |
| 3427 | "set changed size during iteration"); |
| 3428 | return -1; |
| 3429 | } |
| 3430 | } while (i == BATCHSIZE); |
| 3431 | |
| 3432 | return 0; |
| 3433 | } |
| 3434 | |
| 3435 | static int |
| 3436 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 3437 | { |
| 3438 | PyObject *iter; |
| 3439 | |
| 3440 | const char mark_op = MARK; |
| 3441 | const char frozenset_op = FROZENSET; |
| 3442 | |
| 3443 | if (self->fast && !fast_save_enter(self, obj)) |
| 3444 | return -1; |
| 3445 | |
| 3446 | if (self->proto < 4) { |
| 3447 | PyObject *items; |
| 3448 | PyObject *reduce_value; |
| 3449 | int status; |
| 3450 | |
| 3451 | items = PySequence_List(obj); |
| 3452 | if (items == NULL) { |
| 3453 | return -1; |
| 3454 | } |
| 3455 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3456 | items); |
| 3457 | Py_DECREF(items); |
| 3458 | if (reduce_value == NULL) { |
| 3459 | return -1; |
| 3460 | } |
| 3461 | /* save_reduce() will memoize the object automatically. */ |
| 3462 | status = save_reduce(self, reduce_value, obj); |
| 3463 | Py_DECREF(reduce_value); |
| 3464 | return status; |
| 3465 | } |
| 3466 | |
| 3467 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3468 | return -1; |
| 3469 | |
| 3470 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3471 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3472 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3473 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3474 | for (;;) { |
| 3475 | PyObject *item; |
| 3476 | |
| 3477 | item = PyIter_Next(iter); |
| 3478 | if (item == NULL) { |
| 3479 | if (PyErr_Occurred()) { |
| 3480 | Py_DECREF(iter); |
| 3481 | return -1; |
| 3482 | } |
| 3483 | break; |
| 3484 | } |
| 3485 | if (save(self, item, 0) < 0) { |
| 3486 | Py_DECREF(item); |
| 3487 | Py_DECREF(iter); |
| 3488 | return -1; |
| 3489 | } |
| 3490 | Py_DECREF(item); |
| 3491 | } |
| 3492 | Py_DECREF(iter); |
| 3493 | |
| 3494 | /* If the object is already in the memo, this means it is |
| 3495 | recursive. In this case, throw away everything we put on the |
| 3496 | stack, and fetch the object back from the memo. */ |
| 3497 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3498 | const char pop_mark_op = POP_MARK; |
| 3499 | |
| 3500 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3501 | return -1; |
| 3502 | if (memo_get(self, obj) < 0) |
| 3503 | return -1; |
| 3504 | return 0; |
| 3505 | } |
| 3506 | |
| 3507 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3508 | return -1; |
| 3509 | if (memo_put(self, obj) < 0) |
| 3510 | return -1; |
| 3511 | |
| 3512 | return 0; |
| 3513 | } |
| 3514 | |
| 3515 | static int |
| 3516 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3517 | { |
| 3518 | PyObject *key; |
| 3519 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3520 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3521 | |
| 3522 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3523 | if (key == NULL) |
| 3524 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3525 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3526 | Py_DECREF(key); |
| 3527 | if (item) { |
| 3528 | PyObject *fixed_module_name; |
| 3529 | PyObject *fixed_global_name; |
| 3530 | |
| 3531 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3532 | PyErr_Format(PyExc_RuntimeError, |
| 3533 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3534 | "should be 2-tuples, not %.200s", |
| 3535 | Py_TYPE(item)->tp_name); |
| 3536 | return -1; |
| 3537 | } |
| 3538 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3539 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3540 | if (!PyUnicode_Check(fixed_module_name) || |
| 3541 | !PyUnicode_Check(fixed_global_name)) { |
| 3542 | PyErr_Format(PyExc_RuntimeError, |
| 3543 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3544 | "should be pairs of str, not (%.200s, %.200s)", |
| 3545 | Py_TYPE(fixed_module_name)->tp_name, |
| 3546 | Py_TYPE(fixed_global_name)->tp_name); |
| 3547 | return -1; |
| 3548 | } |
| 3549 | |
| 3550 | Py_CLEAR(*module_name); |
| 3551 | Py_CLEAR(*global_name); |
| 3552 | Py_INCREF(fixed_module_name); |
| 3553 | Py_INCREF(fixed_global_name); |
| 3554 | *module_name = fixed_module_name; |
| 3555 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3556 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3557 | } |
| 3558 | else if (PyErr_Occurred()) { |
| 3559 | return -1; |
| 3560 | } |
| 3561 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3562 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3563 | if (item) { |
| 3564 | if (!PyUnicode_Check(item)) { |
| 3565 | PyErr_Format(PyExc_RuntimeError, |
| 3566 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3567 | "should be strings, not %.200s", |
| 3568 | Py_TYPE(item)->tp_name); |
| 3569 | return -1; |
| 3570 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3571 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3572 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3573 | } |
| 3574 | else if (PyErr_Occurred()) { |
| 3575 | return -1; |
| 3576 | } |
| 3577 | |
| 3578 | return 0; |
| 3579 | } |
| 3580 | |
| 3581 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3582 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3583 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3584 | PyObject *global_name = NULL; |
| 3585 | PyObject *module_name = NULL; |
| 3586 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3587 | PyObject *parent = NULL; |
| 3588 | PyObject *dotted_path = NULL; |
| 3589 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3590 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3591 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3592 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3593 | _Py_IDENTIFIER(__name__); |
| 3594 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3595 | |
| 3596 | const char global_op = GLOBAL; |
| 3597 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3598 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3599 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3600 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3601 | } |
| 3602 | else { |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3603 | if (_PyObject_LookupAttrId(obj, &PyId___qualname__, &global_name) < 0) |
| 3604 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3605 | if (global_name == NULL) { |
| 3606 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3607 | if (global_name == NULL) |
| 3608 | goto error; |
| 3609 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3610 | } |
| 3611 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3612 | dotted_path = get_dotted_path(module, global_name); |
| 3613 | if (dotted_path == NULL) |
| 3614 | goto error; |
| 3615 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3616 | if (module_name == NULL) |
| 3617 | goto error; |
| 3618 | |
| 3619 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3620 | relative imports. |
| 3621 | |
| 3622 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3623 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3624 | custom import functions (IMHO, this would be a nice security |
| 3625 | feature). The import C API would need to be extended to support the |
| 3626 | extra parameters of __import__ to fix that. */ |
| 3627 | module = PyImport_Import(module_name); |
| 3628 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3629 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3630 | "Can't pickle %R: import of module %R failed", |
| 3631 | obj, module_name); |
| 3632 | goto error; |
| 3633 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3634 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3635 | Py_INCREF(lastname); |
| 3636 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3637 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3638 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3639 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3640 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3641 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3642 | goto error; |
| 3643 | } |
| 3644 | if (cls != obj) { |
| 3645 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3646 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3647 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3648 | obj, module_name, global_name); |
| 3649 | goto error; |
| 3650 | } |
| 3651 | Py_DECREF(cls); |
| 3652 | |
| 3653 | if (self->proto >= 2) { |
| 3654 | /* See whether this is in the extension registry, and if |
| 3655 | * so generate an EXT opcode. |
| 3656 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3657 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3658 | PyObject *code_obj; /* extension code as Python object */ |
| 3659 | long code; /* extension code as C value */ |
| 3660 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3661 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3662 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3663 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3664 | if (extension_key == NULL) { |
| 3665 | goto error; |
| 3666 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3667 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3668 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3669 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3670 | /* The object is not registered in the extension registry. |
| 3671 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3672 | if (code_obj == NULL) { |
| 3673 | if (PyErr_Occurred()) { |
| 3674 | goto error; |
| 3675 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3676 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3677 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3678 | |
| 3679 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3680 | of the value returned by the extension_registry. It should for |
| 3681 | consistency. */ |
| 3682 | |
| 3683 | /* Verify code_obj has the right type and value. */ |
| 3684 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3685 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3686 | "Can't pickle %R: extension code %R isn't an integer", |
| 3687 | obj, code_obj); |
| 3688 | goto error; |
| 3689 | } |
| 3690 | code = PyLong_AS_LONG(code_obj); |
| 3691 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3692 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3693 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3694 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3695 | goto error; |
| 3696 | } |
| 3697 | |
| 3698 | /* Generate an EXT opcode. */ |
| 3699 | if (code <= 0xff) { |
| 3700 | pdata[0] = EXT1; |
| 3701 | pdata[1] = (unsigned char)code; |
| 3702 | n = 2; |
| 3703 | } |
| 3704 | else if (code <= 0xffff) { |
| 3705 | pdata[0] = EXT2; |
| 3706 | pdata[1] = (unsigned char)(code & 0xff); |
| 3707 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3708 | n = 3; |
| 3709 | } |
| 3710 | else { |
| 3711 | pdata[0] = EXT4; |
| 3712 | pdata[1] = (unsigned char)(code & 0xff); |
| 3713 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3714 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3715 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3716 | n = 5; |
| 3717 | } |
| 3718 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3719 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3720 | goto error; |
| 3721 | } |
| 3722 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3723 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3724 | if (parent == module) { |
| 3725 | Py_INCREF(lastname); |
| 3726 | Py_DECREF(global_name); |
| 3727 | global_name = lastname; |
| 3728 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3729 | if (self->proto >= 4) { |
| 3730 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3731 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3732 | if (save(self, module_name, 0) < 0) |
| 3733 | goto error; |
| 3734 | if (save(self, global_name, 0) < 0) |
| 3735 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3736 | |
| 3737 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3738 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3739 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3740 | else if (parent != module) { |
| 3741 | PickleState *st = _Pickle_GetGlobalState(); |
| 3742 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3743 | st->getattr, parent, lastname); |
Alexey Izbyshev | f8c06b0 | 2018-08-22 07:51:25 +0300 | [diff] [blame] | 3744 | if (reduce_value == NULL) |
| 3745 | goto error; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3746 | status = save_reduce(self, reduce_value, NULL); |
| 3747 | Py_DECREF(reduce_value); |
| 3748 | if (status < 0) |
| 3749 | goto error; |
| 3750 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3751 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3752 | /* Generate a normal global opcode if we are using a pickle |
| 3753 | protocol < 4, or if the object is not registered in the |
| 3754 | extension registry. */ |
| 3755 | PyObject *encoded; |
| 3756 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3757 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3758 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3759 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3760 | |
| 3761 | /* For protocol < 3 and if the user didn't request against doing |
| 3762 | so, we convert module names to the old 2.x module names. */ |
| 3763 | if (self->proto < 3 && self->fix_imports) { |
| 3764 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3765 | goto error; |
| 3766 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3767 | } |
| 3768 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3769 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3770 | both the module name and the global name using UTF-8. We do so |
| 3771 | only when we are using the pickle protocol newer than version |
| 3772 | 3. This is to ensure compatibility with older Unpickler running |
| 3773 | on Python 2.x. */ |
| 3774 | if (self->proto == 3) { |
| 3775 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3776 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3777 | else { |
| 3778 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3779 | } |
| 3780 | encoded = unicode_encoder(module_name); |
| 3781 | if (encoded == NULL) { |
| 3782 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3783 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3784 | "can't pickle module identifier '%S' using " |
| 3785 | "pickle protocol %i", |
| 3786 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3787 | goto error; |
| 3788 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3789 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3790 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3791 | Py_DECREF(encoded); |
| 3792 | goto error; |
| 3793 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3794 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3795 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3796 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3797 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3798 | /* Save the name of the module. */ |
| 3799 | encoded = unicode_encoder(global_name); |
| 3800 | if (encoded == NULL) { |
| 3801 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3802 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3803 | "can't pickle global identifier '%S' using " |
| 3804 | "pickle protocol %i", |
| 3805 | global_name, self->proto); |
| 3806 | goto error; |
| 3807 | } |
| 3808 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3809 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3810 | Py_DECREF(encoded); |
| 3811 | goto error; |
| 3812 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3813 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3814 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3815 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3816 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3817 | /* Memoize the object. */ |
| 3818 | if (memo_put(self, obj) < 0) |
| 3819 | goto error; |
| 3820 | } |
| 3821 | |
| 3822 | if (0) { |
| 3823 | error: |
| 3824 | status = -1; |
| 3825 | } |
| 3826 | Py_XDECREF(module_name); |
| 3827 | Py_XDECREF(global_name); |
| 3828 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3829 | Py_XDECREF(parent); |
| 3830 | Py_XDECREF(dotted_path); |
| 3831 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3832 | |
| 3833 | return status; |
| 3834 | } |
| 3835 | |
| 3836 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3837 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3838 | { |
| 3839 | PyObject *reduce_value; |
| 3840 | int status; |
| 3841 | |
| 3842 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3843 | if (reduce_value == NULL) { |
| 3844 | return -1; |
| 3845 | } |
| 3846 | status = save_reduce(self, reduce_value, obj); |
| 3847 | Py_DECREF(reduce_value); |
| 3848 | return status; |
| 3849 | } |
| 3850 | |
| 3851 | static int |
| 3852 | save_type(PicklerObject *self, PyObject *obj) |
| 3853 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3854 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3855 | return save_singleton_type(self, obj, Py_None); |
| 3856 | } |
| 3857 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3858 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3859 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3860 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3861 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3862 | } |
| 3863 | return save_global(self, obj, NULL); |
| 3864 | } |
| 3865 | |
| 3866 | static int |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3867 | save_pers(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3868 | { |
| 3869 | PyObject *pid = NULL; |
| 3870 | int status = 0; |
| 3871 | |
| 3872 | const char persid_op = PERSID; |
| 3873 | const char binpersid_op = BINPERSID; |
| 3874 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3875 | pid = call_method(self->pers_func, self->pers_func_self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3876 | if (pid == NULL) |
| 3877 | return -1; |
| 3878 | |
| 3879 | if (pid != Py_None) { |
| 3880 | if (self->bin) { |
| 3881 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3882 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3883 | goto error; |
| 3884 | } |
| 3885 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3886 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3887 | |
| 3888 | pid_str = PyObject_Str(pid); |
| 3889 | if (pid_str == NULL) |
| 3890 | goto error; |
| 3891 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3892 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3893 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3894 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3895 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3896 | "persistent IDs in protocol 0 must be " |
| 3897 | "ASCII strings"); |
| 3898 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3899 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3900 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3901 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3902 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3903 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3904 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3905 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3906 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3907 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3908 | } |
| 3909 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3910 | } |
| 3911 | status = 1; |
| 3912 | } |
| 3913 | |
| 3914 | if (0) { |
| 3915 | error: |
| 3916 | status = -1; |
| 3917 | } |
| 3918 | Py_XDECREF(pid); |
| 3919 | |
| 3920 | return status; |
| 3921 | } |
| 3922 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3923 | static PyObject * |
| 3924 | get_class(PyObject *obj) |
| 3925 | { |
| 3926 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3927 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3928 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 3929 | if (_PyObject_LookupAttrId(obj, &PyId___class__, &cls) == 0) { |
| 3930 | cls = (PyObject *) Py_TYPE(obj); |
| 3931 | Py_INCREF(cls); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3932 | } |
| 3933 | return cls; |
| 3934 | } |
| 3935 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3936 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3937 | * appropriate __reduce__ method for obj. |
| 3938 | */ |
| 3939 | static int |
| 3940 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3941 | { |
| 3942 | PyObject *callable; |
| 3943 | PyObject *argtup; |
| 3944 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3945 | PyObject *listitems = Py_None; |
| 3946 | PyObject *dictitems = Py_None; |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3947 | PyObject *state_setter = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3948 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3949 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3950 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3951 | |
| 3952 | const char reduce_op = REDUCE; |
| 3953 | const char build_op = BUILD; |
| 3954 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3955 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3956 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3957 | size = PyTuple_Size(args); |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3958 | if (size < 2 || size > 6) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3959 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3960 | "__reduce__ must contain 2 through 6 elements"); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3961 | return -1; |
| 3962 | } |
| 3963 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 3964 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6, |
| 3965 | &callable, &argtup, &state, &listitems, &dictitems, |
| 3966 | &state_setter)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3967 | return -1; |
| 3968 | |
| 3969 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3970 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3971 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3972 | return -1; |
| 3973 | } |
| 3974 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3975 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3976 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3977 | return -1; |
| 3978 | } |
| 3979 | |
| 3980 | if (state == Py_None) |
| 3981 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3982 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3983 | if (listitems == Py_None) |
| 3984 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3985 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3986 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3987 | "returned by __reduce__ must be an iterator, not %s", |
| 3988 | Py_TYPE(listitems)->tp_name); |
| 3989 | return -1; |
| 3990 | } |
| 3991 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3992 | if (dictitems == Py_None) |
| 3993 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3994 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3995 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3996 | "returned by __reduce__ must be an iterator, not %s", |
| 3997 | Py_TYPE(dictitems)->tp_name); |
| 3998 | return -1; |
| 3999 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4000 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 4001 | if (state_setter == Py_None) |
| 4002 | state_setter = NULL; |
| 4003 | else if (!PyCallable_Check(state_setter)) { |
| 4004 | PyErr_Format(st->PicklingError, "sixth element of the tuple " |
| 4005 | "returned by __reduce__ must be a function, not %s", |
| 4006 | Py_TYPE(state_setter)->tp_name); |
| 4007 | return -1; |
| 4008 | } |
| 4009 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4010 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4011 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4012 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4013 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4014 | if (_PyObject_LookupAttrId(callable, &PyId___name__, &name) < 0) { |
| 4015 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4016 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4017 | if (name != NULL && PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4018 | _Py_IDENTIFIER(__newobj_ex__); |
Serhiy Storchaka | f0f35a6 | 2017-01-09 10:09:43 +0200 | [diff] [blame] | 4019 | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
| 4020 | name, &PyId___newobj_ex__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4021 | if (!use_newobj_ex) { |
| 4022 | _Py_IDENTIFIER(__newobj__); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 4023 | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4024 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4025 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 4026 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4027 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4028 | |
| 4029 | if (use_newobj_ex) { |
| 4030 | PyObject *cls; |
| 4031 | PyObject *args; |
| 4032 | PyObject *kwargs; |
| 4033 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4034 | if (PyTuple_GET_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4035 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4036 | "length of the NEWOBJ_EX argument tuple must be " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4037 | "exactly 3, not %zd", PyTuple_GET_SIZE(argtup)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4038 | return -1; |
| 4039 | } |
| 4040 | |
| 4041 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 4042 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4043 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4044 | "first item from NEWOBJ_EX argument tuple must " |
| 4045 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 4046 | return -1; |
| 4047 | } |
| 4048 | args = PyTuple_GET_ITEM(argtup, 1); |
| 4049 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4050 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4051 | "second item from NEWOBJ_EX argument tuple must " |
| 4052 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 4053 | return -1; |
| 4054 | } |
| 4055 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 4056 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4057 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4058 | "third item from NEWOBJ_EX argument tuple must " |
| 4059 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 4060 | return -1; |
| 4061 | } |
| 4062 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4063 | if (self->proto >= 4) { |
| 4064 | if (save(self, cls, 0) < 0 || |
| 4065 | save(self, args, 0) < 0 || |
| 4066 | save(self, kwargs, 0) < 0 || |
| 4067 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 4068 | return -1; |
| 4069 | } |
| 4070 | } |
| 4071 | else { |
| 4072 | PyObject *newargs; |
| 4073 | PyObject *cls_new; |
| 4074 | Py_ssize_t i; |
| 4075 | _Py_IDENTIFIER(__new__); |
| 4076 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4077 | newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4078 | if (newargs == NULL) |
| 4079 | return -1; |
| 4080 | |
| 4081 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 4082 | if (cls_new == NULL) { |
| 4083 | Py_DECREF(newargs); |
| 4084 | return -1; |
| 4085 | } |
| 4086 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 4087 | Py_INCREF(cls); |
| 4088 | PyTuple_SET_ITEM(newargs, 1, cls); |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4089 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 4090 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 4091 | Py_INCREF(item); |
| 4092 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 4093 | } |
| 4094 | |
| 4095 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 4096 | Py_DECREF(newargs); |
| 4097 | if (callable == NULL) |
| 4098 | return -1; |
| 4099 | |
| 4100 | newargs = PyTuple_New(0); |
| 4101 | if (newargs == NULL) { |
| 4102 | Py_DECREF(callable); |
| 4103 | return -1; |
| 4104 | } |
| 4105 | |
| 4106 | if (save(self, callable, 0) < 0 || |
| 4107 | save(self, newargs, 0) < 0 || |
| 4108 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 4109 | Py_DECREF(newargs); |
| 4110 | Py_DECREF(callable); |
| 4111 | return -1; |
| 4112 | } |
| 4113 | Py_DECREF(newargs); |
| 4114 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4115 | } |
| 4116 | } |
| 4117 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4118 | PyObject *cls; |
| 4119 | PyObject *newargtup; |
| 4120 | PyObject *obj_class; |
| 4121 | int p; |
| 4122 | |
| 4123 | /* Sanity checks. */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4124 | if (PyTuple_GET_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4125 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4126 | return -1; |
| 4127 | } |
| 4128 | |
| 4129 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4130 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4131 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4132 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4133 | return -1; |
| 4134 | } |
| 4135 | |
| 4136 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 4137 | obj_class = get_class(obj); |
Zackery Spytz | 25d3897 | 2018-12-05 11:29:20 -0700 | [diff] [blame] | 4138 | if (obj_class == NULL) { |
| 4139 | return -1; |
| 4140 | } |
| 4141 | p = obj_class != cls; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4142 | Py_DECREF(obj_class); |
| 4143 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4144 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4145 | "__newobj__ args has the wrong class"); |
| 4146 | return -1; |
| 4147 | } |
| 4148 | } |
| 4149 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 4150 | what happen if the value returned by the __reduce__() method of |
| 4151 | some extension type contains another object of the same type. Ouch! |
| 4152 | |
| 4153 | Here is a quick example, that I ran into, to illustrate what I |
| 4154 | mean: |
| 4155 | |
| 4156 | >>> import pickle, copyreg |
| 4157 | >>> copyreg.dispatch_table.pop(complex) |
| 4158 | >>> pickle.dumps(1+2j) |
| 4159 | Traceback (most recent call last): |
| 4160 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 4161 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4162 | |
| 4163 | Removing the complex class from copyreg.dispatch_table made the |
| 4164 | __reduce_ex__() method emit another complex object: |
| 4165 | |
| 4166 | >>> (1+1j).__reduce_ex__(2) |
| 4167 | (<function __newobj__ at 0xb7b71c3c>, |
| 4168 | (<class 'complex'>, (1+1j)), None, None, None) |
| 4169 | |
| 4170 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 4171 | ensued. Of course, the bug was in the complex class which had a |
| 4172 | broken __getnewargs__() that emitted another complex object. But, |
| 4173 | the point, here, is it is quite easy to end up with a broken reduce |
| 4174 | function. */ |
| 4175 | |
| 4176 | /* Save the class and its __new__ arguments. */ |
| 4177 | if (save(self, cls, 0) < 0) |
| 4178 | return -1; |
| 4179 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4180 | newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4181 | if (newargtup == NULL) |
| 4182 | return -1; |
| 4183 | |
| 4184 | p = save(self, newargtup, 0); |
| 4185 | Py_DECREF(newargtup); |
| 4186 | if (p < 0) |
| 4187 | return -1; |
| 4188 | |
| 4189 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4190 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4191 | return -1; |
| 4192 | } |
| 4193 | else { /* Not using NEWOBJ. */ |
| 4194 | if (save(self, callable, 0) < 0 || |
| 4195 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4196 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4197 | return -1; |
| 4198 | } |
| 4199 | |
| 4200 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 4201 | the caller do not want to memoize the object. Not particularly useful, |
| 4202 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 4203 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4204 | if (obj != NULL) { |
| 4205 | /* If the object is already in the memo, this means it is |
| 4206 | recursive. In this case, throw away everything we put on the |
| 4207 | stack, and fetch the object back from the memo. */ |
| 4208 | if (PyMemoTable_Get(self->memo, obj)) { |
| 4209 | const char pop_op = POP; |
| 4210 | |
| 4211 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 4212 | return -1; |
| 4213 | if (memo_get(self, obj) < 0) |
| 4214 | return -1; |
| 4215 | |
| 4216 | return 0; |
| 4217 | } |
| 4218 | else if (memo_put(self, obj) < 0) |
| 4219 | return -1; |
| 4220 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4221 | |
| 4222 | if (listitems && batch_list(self, listitems) < 0) |
| 4223 | return -1; |
| 4224 | |
| 4225 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 4226 | return -1; |
| 4227 | |
| 4228 | if (state) { |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 4229 | if (state_setter == NULL) { |
| 4230 | if (save(self, state, 0) < 0 || |
| 4231 | _Pickler_Write(self, &build_op, 1) < 0) |
| 4232 | return -1; |
| 4233 | } |
| 4234 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4235 | |
Pierre Glaser | 65d98d0 | 2019-05-08 21:40:25 +0200 | [diff] [blame] | 4236 | /* If a state_setter is specified, call it instead of load_build to |
| 4237 | * update obj's with its previous state. |
| 4238 | * The first 4 save/write instructions push state_setter and its |
| 4239 | * tuple of expected arguments (obj, state) onto the stack. The |
| 4240 | * REDUCE opcode triggers the state_setter(obj, state) function |
| 4241 | * call. Finally, because state-updating routines only do in-place |
| 4242 | * modification, the whole operation has to be stack-transparent. |
| 4243 | * Thus, we finally pop the call's output from the stack.*/ |
| 4244 | |
| 4245 | const char tupletwo_op = TUPLE2; |
| 4246 | const char pop_op = POP; |
| 4247 | if (save(self, state_setter, 0) < 0 || |
| 4248 | save(self, obj, 0) < 0 || save(self, state, 0) < 0 || |
| 4249 | _Pickler_Write(self, &tupletwo_op, 1) < 0 || |
| 4250 | _Pickler_Write(self, &reduce_op, 1) < 0 || |
| 4251 | _Pickler_Write(self, &pop_op, 1) < 0) |
| 4252 | return -1; |
| 4253 | } |
| 4254 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4255 | return 0; |
| 4256 | } |
| 4257 | |
| 4258 | static int |
| 4259 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 4260 | { |
| 4261 | PyTypeObject *type; |
| 4262 | PyObject *reduce_func = NULL; |
| 4263 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4264 | int status = 0; |
| 4265 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4266 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 4267 | return -1; |
| 4268 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4269 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 4270 | on its returned object. */ |
| 4271 | if (!pers_save && self->pers_func) { |
| 4272 | /* save_pers() returns: |
| 4273 | -1 to signal an error; |
| 4274 | 0 if it did nothing successfully; |
| 4275 | 1 if a persistent id was saved. |
| 4276 | */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4277 | if ((status = save_pers(self, obj)) != 0) |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4278 | return status; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | type = Py_TYPE(obj); |
| 4282 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4283 | /* The old cPickle had an optimization that used switch-case statement |
| 4284 | dispatching on the first letter of the type name. This has was removed |
| 4285 | since benchmarks shown that this optimization was actually slowing |
| 4286 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4287 | |
| 4288 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 4289 | |
| 4290 | if (obj == Py_None) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4291 | return save_none(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4292 | } |
| 4293 | else if (obj == Py_False || obj == Py_True) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4294 | return save_bool(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4295 | } |
| 4296 | else if (type == &PyLong_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4297 | return save_long(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4298 | } |
| 4299 | else if (type == &PyFloat_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4300 | return save_float(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4301 | } |
| 4302 | |
| 4303 | /* Check the memo to see if it has the object. If so, generate |
| 4304 | a GET (or BINGET) opcode, instead of pickling the object |
| 4305 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4306 | if (PyMemoTable_Get(self->memo, obj)) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4307 | return memo_get(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4308 | } |
| 4309 | |
| 4310 | if (type == &PyBytes_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4311 | return save_bytes(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4312 | } |
| 4313 | else if (type == &PyUnicode_Type) { |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4314 | return save_unicode(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4315 | } |
Serhiy Storchaka | 5d4cb54 | 2018-07-18 10:10:49 +0300 | [diff] [blame] | 4316 | |
| 4317 | /* We're only calling Py_EnterRecursiveCall here so that atomic |
| 4318 | types above are pickled faster. */ |
| 4319 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 4320 | return -1; |
| 4321 | } |
| 4322 | |
| 4323 | if (type == &PyDict_Type) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4324 | status = save_dict(self, obj); |
| 4325 | goto done; |
| 4326 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4327 | else if (type == &PySet_Type) { |
| 4328 | status = save_set(self, obj); |
| 4329 | goto done; |
| 4330 | } |
| 4331 | else if (type == &PyFrozenSet_Type) { |
| 4332 | status = save_frozenset(self, obj); |
| 4333 | goto done; |
| 4334 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4335 | else if (type == &PyList_Type) { |
| 4336 | status = save_list(self, obj); |
| 4337 | goto done; |
| 4338 | } |
| 4339 | else if (type == &PyTuple_Type) { |
| 4340 | status = save_tuple(self, obj); |
| 4341 | goto done; |
| 4342 | } |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4343 | else if (type == &PyByteArray_Type) { |
| 4344 | status = save_bytearray(self, obj); |
| 4345 | goto done; |
| 4346 | } |
| 4347 | else if (type == &PyPickleBuffer_Type) { |
| 4348 | status = save_picklebuffer(self, obj); |
| 4349 | goto done; |
| 4350 | } |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4351 | |
| 4352 | /* Now, check reducer_override. If it returns NotImplemented, |
| 4353 | * fallback to save_type or save_global, and then perhaps to the |
| 4354 | * regular reduction mechanism. |
| 4355 | */ |
| 4356 | if (self->reducer_override != NULL) { |
Petr Viktorin | ffd9753 | 2020-02-11 17:46:57 +0100 | [diff] [blame] | 4357 | reduce_value = PyObject_CallOneArg(self->reducer_override, obj); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4358 | if (reduce_value == NULL) { |
| 4359 | goto error; |
| 4360 | } |
| 4361 | if (reduce_value != Py_NotImplemented) { |
| 4362 | goto reduce; |
| 4363 | } |
| 4364 | Py_DECREF(reduce_value); |
| 4365 | reduce_value = NULL; |
| 4366 | } |
| 4367 | |
| 4368 | if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 4369 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4370 | goto done; |
| 4371 | } |
| 4372 | else if (type == &PyFunction_Type) { |
| 4373 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 4374 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4375 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4376 | |
| 4377 | /* XXX: This part needs some unit tests. */ |
| 4378 | |
| 4379 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4380 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 4381 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4382 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4383 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4384 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4385 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 4386 | (PyObject *)type); |
| 4387 | if (reduce_func == NULL) { |
| 4388 | if (PyErr_Occurred()) { |
| 4389 | goto error; |
| 4390 | } |
| 4391 | } else { |
| 4392 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 4393 | Increase the reference count to be consistent with |
| 4394 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 4395 | Py_INCREF(reduce_func); |
| 4396 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4397 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 4398 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 4399 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4400 | if (reduce_func == NULL) { |
| 4401 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4402 | PyErr_Clear(); |
| 4403 | else |
| 4404 | goto error; |
| 4405 | } |
| 4406 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4407 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4408 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4409 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4410 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 4411 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 4412 | status = save_global(self, obj, NULL); |
| 4413 | goto done; |
| 4414 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4415 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4416 | _Py_IDENTIFIER(__reduce__); |
| 4417 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4418 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4419 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 4420 | automatically defined as __reduce__. While this is convenient, this |
| 4421 | make it impossible to know which method was actually called. Of |
| 4422 | course, this is not a big deal. But still, it would be nice to let |
| 4423 | the user know which method was called when something go |
| 4424 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 4425 | don't actually have to check for a __reduce__ method. */ |
| 4426 | |
| 4427 | /* Check for a __reduce_ex__ method. */ |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4428 | if (_PyObject_LookupAttrId(obj, &PyId___reduce_ex__, &reduce_func) < 0) { |
| 4429 | goto error; |
| 4430 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4431 | if (reduce_func != NULL) { |
| 4432 | PyObject *proto; |
| 4433 | proto = PyLong_FromLong(self->proto); |
| 4434 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4435 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4436 | } |
| 4437 | } |
| 4438 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4439 | /* Check for a __reduce__ method. */ |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 4440 | if (_PyObject_LookupAttrId(obj, &PyId___reduce__, &reduce_func) < 0) { |
| 4441 | goto error; |
| 4442 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4443 | if (reduce_func != NULL) { |
Victor Stinner | 2ff58a2 | 2019-06-17 14:27:23 +0200 | [diff] [blame] | 4444 | reduce_value = PyObject_CallNoArgs(reduce_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4445 | } |
| 4446 | else { |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 4447 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4448 | PyErr_Format(st->PicklingError, |
| 4449 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4450 | type->tp_name, obj); |
| 4451 | goto error; |
| 4452 | } |
| 4453 | } |
| 4454 | } |
| 4455 | |
| 4456 | if (reduce_value == NULL) |
| 4457 | goto error; |
| 4458 | |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4459 | reduce: |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4460 | if (PyUnicode_Check(reduce_value)) { |
| 4461 | status = save_global(self, obj, reduce_value); |
| 4462 | goto done; |
| 4463 | } |
| 4464 | |
| 4465 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4466 | PickleState *st = _Pickle_GetGlobalState(); |
| 4467 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4468 | "__reduce__ must return a string or tuple"); |
| 4469 | goto error; |
| 4470 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4471 | |
| 4472 | status = save_reduce(self, reduce_value, obj); |
| 4473 | |
| 4474 | if (0) { |
| 4475 | error: |
| 4476 | status = -1; |
| 4477 | } |
| 4478 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4479 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 4480 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4481 | Py_XDECREF(reduce_func); |
| 4482 | Py_XDECREF(reduce_value); |
| 4483 | |
| 4484 | return status; |
| 4485 | } |
| 4486 | |
| 4487 | static int |
| 4488 | dump(PicklerObject *self, PyObject *obj) |
| 4489 | { |
| 4490 | const char stop_op = STOP; |
Pierre Glaser | 0f2f35e | 2020-02-02 19:55:21 +0100 | [diff] [blame] | 4491 | int status = -1; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4492 | PyObject *tmp; |
| 4493 | _Py_IDENTIFIER(reducer_override); |
| 4494 | |
| 4495 | if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override, |
| 4496 | &tmp) < 0) { |
Pierre Glaser | 0f2f35e | 2020-02-02 19:55:21 +0100 | [diff] [blame] | 4497 | goto error; |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4498 | } |
| 4499 | /* Cache the reducer_override method, if it exists. */ |
| 4500 | if (tmp != NULL) { |
| 4501 | Py_XSETREF(self->reducer_override, tmp); |
| 4502 | } |
| 4503 | else { |
| 4504 | Py_CLEAR(self->reducer_override); |
| 4505 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4506 | |
| 4507 | if (self->proto >= 2) { |
| 4508 | char header[2]; |
| 4509 | |
| 4510 | header[0] = PROTO; |
| 4511 | assert(self->proto >= 0 && self->proto < 256); |
| 4512 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4513 | if (_Pickler_Write(self, header, 2) < 0) |
Pierre Glaser | 0f2f35e | 2020-02-02 19:55:21 +0100 | [diff] [blame] | 4514 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4515 | if (self->proto >= 4) |
| 4516 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4517 | } |
| 4518 | |
| 4519 | if (save(self, obj, 0) < 0 || |
Serhiy Storchaka | c869529 | 2018-04-04 00:11:27 +0300 | [diff] [blame] | 4520 | _Pickler_Write(self, &stop_op, 1) < 0 || |
| 4521 | _Pickler_CommitFrame(self) < 0) |
Pierre Glaser | 0f2f35e | 2020-02-02 19:55:21 +0100 | [diff] [blame] | 4522 | goto error; |
| 4523 | |
| 4524 | // Success |
| 4525 | status = 0; |
| 4526 | |
| 4527 | error: |
Serhiy Storchaka | c869529 | 2018-04-04 00:11:27 +0300 | [diff] [blame] | 4528 | self->framing = 0; |
Pierre Glaser | 0f2f35e | 2020-02-02 19:55:21 +0100 | [diff] [blame] | 4529 | |
| 4530 | /* Break the reference cycle we generated at the beginning this function |
| 4531 | * call when setting the reducer_override attribute of the Pickler instance |
| 4532 | * to a bound method of the same instance. This is important as the Pickler |
| 4533 | * instance holds a reference to each object it has pickled (through its |
| 4534 | * memo): thus, these objects wont be garbage-collected as long as the |
| 4535 | * Pickler itself is not collected. */ |
| 4536 | Py_CLEAR(self->reducer_override); |
| 4537 | return status; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4538 | } |
| 4539 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4540 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4541 | |
| 4542 | _pickle.Pickler.clear_memo |
| 4543 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4544 | Clears the pickler's "memo". |
| 4545 | |
| 4546 | The memo is the data structure that remembers which objects the |
| 4547 | pickler has already seen, so that shared or recursive objects are |
| 4548 | pickled by reference and not by value. This method is useful when |
| 4549 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4550 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4551 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4552 | static PyObject * |
| 4553 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4554 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4555 | { |
| 4556 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4557 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4558 | |
| 4559 | Py_RETURN_NONE; |
| 4560 | } |
| 4561 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4562 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4563 | |
| 4564 | _pickle.Pickler.dump |
| 4565 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4566 | obj: object |
| 4567 | / |
| 4568 | |
| 4569 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4570 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4571 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4572 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4573 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4574 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4575 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4576 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4577 | Developers often forget to call __init__() in their subclasses, which |
| 4578 | would trigger a segfault without this check. */ |
| 4579 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4580 | PickleState *st = _Pickle_GetGlobalState(); |
| 4581 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4582 | "Pickler.__init__() was not called by %s.__init__()", |
| 4583 | Py_TYPE(self)->tp_name); |
| 4584 | return NULL; |
| 4585 | } |
| 4586 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4587 | if (_Pickler_ClearBuffer(self) < 0) |
| 4588 | return NULL; |
| 4589 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4590 | if (dump(self, obj) < 0) |
| 4591 | return NULL; |
| 4592 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4593 | if (_Pickler_FlushToFile(self) < 0) |
| 4594 | return NULL; |
| 4595 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4596 | Py_RETURN_NONE; |
| 4597 | } |
| 4598 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4599 | /*[clinic input] |
| 4600 | |
| 4601 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4602 | |
| 4603 | Returns size in memory, in bytes. |
| 4604 | [clinic start generated code]*/ |
| 4605 | |
| 4606 | static Py_ssize_t |
| 4607 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4608 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4609 | { |
| 4610 | Py_ssize_t res, s; |
| 4611 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4612 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4613 | if (self->memo != NULL) { |
| 4614 | res += sizeof(PyMemoTable); |
| 4615 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4616 | } |
| 4617 | if (self->output_buffer != NULL) { |
| 4618 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4619 | if (s == -1) |
| 4620 | return -1; |
| 4621 | res += s; |
| 4622 | } |
| 4623 | return res; |
| 4624 | } |
| 4625 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4626 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4627 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4628 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4629 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4630 | {NULL, NULL} /* sentinel */ |
| 4631 | }; |
| 4632 | |
| 4633 | static void |
| 4634 | Pickler_dealloc(PicklerObject *self) |
| 4635 | { |
| 4636 | PyObject_GC_UnTrack(self); |
| 4637 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4638 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4639 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4640 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4641 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4642 | Py_XDECREF(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4643 | Py_XDECREF(self->reducer_override); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4644 | Py_XDECREF(self->buffer_callback); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4645 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4646 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4647 | |
| 4648 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4649 | } |
| 4650 | |
| 4651 | static int |
| 4652 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4653 | { |
| 4654 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4655 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4656 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4657 | Py_VISIT(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4658 | Py_VISIT(self->reducer_override); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4659 | Py_VISIT(self->buffer_callback); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4660 | return 0; |
| 4661 | } |
| 4662 | |
| 4663 | static int |
| 4664 | Pickler_clear(PicklerObject *self) |
| 4665 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4666 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4667 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4668 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4669 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4670 | Py_CLEAR(self->fast_memo); |
Pierre Glaser | 289f1f8 | 2019-05-08 23:08:25 +0200 | [diff] [blame] | 4671 | Py_CLEAR(self->reducer_override); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4672 | Py_CLEAR(self->buffer_callback); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4673 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4674 | if (self->memo != NULL) { |
| 4675 | PyMemoTable *memo = self->memo; |
| 4676 | self->memo = NULL; |
| 4677 | PyMemoTable_Del(memo); |
| 4678 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4679 | return 0; |
| 4680 | } |
| 4681 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4682 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4683 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4684 | |
| 4685 | _pickle.Pickler.__init__ |
| 4686 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4687 | file: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4688 | protocol: object = None |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4689 | fix_imports: bool = True |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 4690 | buffer_callback: object = None |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4691 | |
| 4692 | This takes a binary file for writing a pickle data stream. |
| 4693 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4694 | The optional *protocol* argument tells the pickler to use the given |
Mark Dickinson | e9652e8 | 2020-01-24 10:03:22 +0000 | [diff] [blame] | 4695 | protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default |
| 4696 | protocol is 4. It was introduced in Python 3.4, and is incompatible |
| 4697 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4698 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4699 | Specifying a negative protocol version selects the highest protocol |
| 4700 | version supported. The higher the protocol used, the more recent the |
| 4701 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4702 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4703 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4704 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4705 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4706 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4707 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4708 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4709 | to map the new Python 3 names to the old module names used in Python |
| 4710 | 2, so that the pickle data stream is readable with Python 2. |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4711 | |
| 4712 | If *buffer_callback* is None (the default), buffer views are |
| 4713 | serialized into *file* as part of the pickle stream. |
| 4714 | |
| 4715 | If *buffer_callback* is not None, then it can be called any number |
| 4716 | of times with a buffer view. If the callback returns a false value |
| 4717 | (such as None), the given buffer is out-of-band; otherwise the |
| 4718 | buffer is serialized in-band, i.e. inside the pickle stream. |
| 4719 | |
| 4720 | It is an error if *buffer_callback* is not None and *protocol* |
| 4721 | is None or smaller than 5. |
| 4722 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4723 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4724 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4725 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4726 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4727 | PyObject *protocol, int fix_imports, |
| 4728 | PyObject *buffer_callback) |
Mark Dickinson | e9652e8 | 2020-01-24 10:03:22 +0000 | [diff] [blame] | 4729 | /*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4730 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4731 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4732 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4733 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4734 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4735 | if (self->write != NULL) |
| 4736 | (void)Pickler_clear(self); |
| 4737 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4738 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4739 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4740 | |
| 4741 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4742 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4743 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 4744 | if (_Pickler_SetBufferCallback(self, buffer_callback) < 0) |
| 4745 | return -1; |
| 4746 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4747 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4748 | if (self->memo == NULL) { |
| 4749 | self->memo = PyMemoTable_New(); |
| 4750 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4751 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4752 | } |
| 4753 | self->output_len = 0; |
| 4754 | if (self->output_buffer == NULL) { |
| 4755 | self->max_output_len = WRITE_BUF_SIZE; |
| 4756 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4757 | self->max_output_len); |
| 4758 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4759 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4760 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4761 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4762 | self->fast = 0; |
| 4763 | self->fast_nesting = 0; |
| 4764 | self->fast_memo = NULL; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4765 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4766 | if (init_method_ref((PyObject *)self, &PyId_persistent_id, |
| 4767 | &self->pers_func, &self->pers_func_self) < 0) |
| 4768 | { |
| 4769 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4770 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4771 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 4772 | if (_PyObject_LookupAttrId((PyObject *)self, |
| 4773 | &PyId_dispatch_table, &self->dispatch_table) < 0) { |
| 4774 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4775 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4776 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4777 | return 0; |
| 4778 | } |
| 4779 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4780 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4781 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4782 | * avoid breaking code like: |
| 4783 | * pickler.memo.clear() |
| 4784 | * and |
| 4785 | * pickler.memo = saved_memo |
| 4786 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4787 | * it. Note that we don't implement the entire mapping API here. This is |
| 4788 | * intentional, as these should be treated as black-box implementation details. |
| 4789 | */ |
| 4790 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4791 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4792 | _pickle.PicklerMemoProxy.clear |
| 4793 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4794 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4795 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4796 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4797 | static PyObject * |
| 4798 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4799 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4800 | { |
| 4801 | if (self->pickler->memo) |
| 4802 | PyMemoTable_Clear(self->pickler->memo); |
| 4803 | Py_RETURN_NONE; |
| 4804 | } |
| 4805 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4806 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4807 | _pickle.PicklerMemoProxy.copy |
| 4808 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4809 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4810 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4811 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4812 | static PyObject * |
| 4813 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4814 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4815 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4816 | PyMemoTable *memo; |
| 4817 | PyObject *new_memo = PyDict_New(); |
| 4818 | if (new_memo == NULL) |
| 4819 | return NULL; |
| 4820 | |
| 4821 | memo = self->pickler->memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 4822 | for (size_t i = 0; i < memo->mt_allocated; ++i) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4823 | PyMemoEntry entry = memo->mt_table[i]; |
| 4824 | if (entry.me_key != NULL) { |
| 4825 | int status; |
| 4826 | PyObject *key, *value; |
| 4827 | |
| 4828 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4829 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4830 | |
| 4831 | if (key == NULL || value == NULL) { |
| 4832 | Py_XDECREF(key); |
| 4833 | Py_XDECREF(value); |
| 4834 | goto error; |
| 4835 | } |
| 4836 | status = PyDict_SetItem(new_memo, key, value); |
| 4837 | Py_DECREF(key); |
| 4838 | Py_DECREF(value); |
| 4839 | if (status < 0) |
| 4840 | goto error; |
| 4841 | } |
| 4842 | } |
| 4843 | return new_memo; |
| 4844 | |
| 4845 | error: |
| 4846 | Py_XDECREF(new_memo); |
| 4847 | return NULL; |
| 4848 | } |
| 4849 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4850 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4851 | _pickle.PicklerMemoProxy.__reduce__ |
| 4852 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4853 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4854 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4855 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4856 | static PyObject * |
| 4857 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4858 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4859 | { |
| 4860 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4861 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4862 | if (contents == NULL) |
| 4863 | return NULL; |
| 4864 | |
| 4865 | reduce_value = PyTuple_New(2); |
| 4866 | if (reduce_value == NULL) { |
| 4867 | Py_DECREF(contents); |
| 4868 | return NULL; |
| 4869 | } |
| 4870 | dict_args = PyTuple_New(1); |
| 4871 | if (dict_args == NULL) { |
| 4872 | Py_DECREF(contents); |
| 4873 | Py_DECREF(reduce_value); |
| 4874 | return NULL; |
| 4875 | } |
| 4876 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4877 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4878 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4879 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4880 | return reduce_value; |
| 4881 | } |
| 4882 | |
| 4883 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4884 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4885 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4886 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4887 | {NULL, NULL} /* sentinel */ |
| 4888 | }; |
| 4889 | |
| 4890 | static void |
| 4891 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4892 | { |
| 4893 | PyObject_GC_UnTrack(self); |
| 4894 | Py_XDECREF(self->pickler); |
| 4895 | PyObject_GC_Del((PyObject *)self); |
| 4896 | } |
| 4897 | |
| 4898 | static int |
| 4899 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4900 | visitproc visit, void *arg) |
| 4901 | { |
| 4902 | Py_VISIT(self->pickler); |
| 4903 | return 0; |
| 4904 | } |
| 4905 | |
| 4906 | static int |
| 4907 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4908 | { |
| 4909 | Py_CLEAR(self->pickler); |
| 4910 | return 0; |
| 4911 | } |
| 4912 | |
| 4913 | static PyTypeObject PicklerMemoProxyType = { |
| 4914 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4915 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4916 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4917 | 0, |
| 4918 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4919 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4920 | 0, /* tp_getattr */ |
| 4921 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 4922 | 0, /* tp_as_async */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4923 | 0, /* tp_repr */ |
| 4924 | 0, /* tp_as_number */ |
| 4925 | 0, /* tp_as_sequence */ |
| 4926 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4927 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4928 | 0, /* tp_call */ |
| 4929 | 0, /* tp_str */ |
| 4930 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4931 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4932 | 0, /* tp_as_buffer */ |
| 4933 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4934 | 0, /* tp_doc */ |
| 4935 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4936 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4937 | 0, /* tp_richcompare */ |
| 4938 | 0, /* tp_weaklistoffset */ |
| 4939 | 0, /* tp_iter */ |
| 4940 | 0, /* tp_iternext */ |
| 4941 | picklerproxy_methods, /* tp_methods */ |
| 4942 | }; |
| 4943 | |
| 4944 | static PyObject * |
| 4945 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4946 | { |
| 4947 | PicklerMemoProxyObject *self; |
| 4948 | |
| 4949 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4950 | if (self == NULL) |
| 4951 | return NULL; |
| 4952 | Py_INCREF(pickler); |
| 4953 | self->pickler = pickler; |
| 4954 | PyObject_GC_Track(self); |
| 4955 | return (PyObject *)self; |
| 4956 | } |
| 4957 | |
| 4958 | /*****************************************************************************/ |
| 4959 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4960 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4961 | Pickler_get_memo(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4962 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4963 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 4967 | Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4968 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4969 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4970 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4971 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4972 | PyErr_SetString(PyExc_TypeError, |
| 4973 | "attribute deletion is not supported"); |
| 4974 | return -1; |
| 4975 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4976 | |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 4977 | if (Py_IS_TYPE(obj, &PicklerMemoProxyType)) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4978 | PicklerObject *pickler = |
| 4979 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4980 | |
| 4981 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4982 | if (new_memo == NULL) |
| 4983 | return -1; |
| 4984 | } |
| 4985 | else if (PyDict_Check(obj)) { |
| 4986 | Py_ssize_t i = 0; |
| 4987 | PyObject *key, *value; |
| 4988 | |
| 4989 | new_memo = PyMemoTable_New(); |
| 4990 | if (new_memo == NULL) |
| 4991 | return -1; |
| 4992 | |
| 4993 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4994 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4995 | PyObject *memo_obj; |
| 4996 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4997 | if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4998 | PyErr_SetString(PyExc_TypeError, |
| 4999 | "'memo' values must be 2-item tuples"); |
| 5000 | goto error; |
| 5001 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5002 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5003 | if (memo_id == -1 && PyErr_Occurred()) |
| 5004 | goto error; |
| 5005 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 5006 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 5007 | goto error; |
| 5008 | } |
| 5009 | } |
| 5010 | else { |
| 5011 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 5012 | "'memo' attribute must be a PicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5013 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5014 | return -1; |
| 5015 | } |
| 5016 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5017 | PyMemoTable_Del(self->memo); |
| 5018 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5019 | |
| 5020 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5021 | |
| 5022 | error: |
| 5023 | if (new_memo) |
| 5024 | PyMemoTable_Del(new_memo); |
| 5025 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
| 5028 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 5029 | Pickler_get_persid(PicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5030 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5031 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5032 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5033 | return NULL; |
| 5034 | } |
| 5035 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5036 | } |
| 5037 | |
| 5038 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 5039 | Pickler_set_persid(PicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5040 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5041 | if (value == NULL) { |
| 5042 | PyErr_SetString(PyExc_TypeError, |
| 5043 | "attribute deletion is not supported"); |
| 5044 | return -1; |
| 5045 | } |
| 5046 | if (!PyCallable_Check(value)) { |
| 5047 | PyErr_SetString(PyExc_TypeError, |
| 5048 | "persistent_id must be a callable taking one argument"); |
| 5049 | return -1; |
| 5050 | } |
| 5051 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5052 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5053 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 5054 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5055 | |
| 5056 | return 0; |
| 5057 | } |
| 5058 | |
| 5059 | static PyMemberDef Pickler_members[] = { |
| 5060 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 5061 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 5062 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5063 | {NULL} |
| 5064 | }; |
| 5065 | |
| 5066 | static PyGetSetDef Pickler_getsets[] = { |
| 5067 | {"memo", (getter)Pickler_get_memo, |
| 5068 | (setter)Pickler_set_memo}, |
| 5069 | {"persistent_id", (getter)Pickler_get_persid, |
| 5070 | (setter)Pickler_set_persid}, |
| 5071 | {NULL} |
| 5072 | }; |
| 5073 | |
| 5074 | static PyTypeObject Pickler_Type = { |
| 5075 | PyVarObject_HEAD_INIT(NULL, 0) |
| 5076 | "_pickle.Pickler" , /*tp_name*/ |
| 5077 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 5078 | 0, /*tp_itemsize*/ |
| 5079 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5080 | 0, /*tp_vectorcall_offset*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5081 | 0, /*tp_getattr*/ |
| 5082 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 5083 | 0, /*tp_as_async*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5084 | 0, /*tp_repr*/ |
| 5085 | 0, /*tp_as_number*/ |
| 5086 | 0, /*tp_as_sequence*/ |
| 5087 | 0, /*tp_as_mapping*/ |
| 5088 | 0, /*tp_hash*/ |
| 5089 | 0, /*tp_call*/ |
| 5090 | 0, /*tp_str*/ |
| 5091 | 0, /*tp_getattro*/ |
| 5092 | 0, /*tp_setattro*/ |
| 5093 | 0, /*tp_as_buffer*/ |
| 5094 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 5095 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5096 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 5097 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 5098 | 0, /*tp_richcompare*/ |
| 5099 | 0, /*tp_weaklistoffset*/ |
| 5100 | 0, /*tp_iter*/ |
| 5101 | 0, /*tp_iternext*/ |
| 5102 | Pickler_methods, /*tp_methods*/ |
| 5103 | Pickler_members, /*tp_members*/ |
| 5104 | Pickler_getsets, /*tp_getset*/ |
| 5105 | 0, /*tp_base*/ |
| 5106 | 0, /*tp_dict*/ |
| 5107 | 0, /*tp_descr_get*/ |
| 5108 | 0, /*tp_descr_set*/ |
| 5109 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 5110 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5111 | PyType_GenericAlloc, /*tp_alloc*/ |
| 5112 | PyType_GenericNew, /*tp_new*/ |
| 5113 | PyObject_GC_Del, /*tp_free*/ |
| 5114 | 0, /*tp_is_gc*/ |
| 5115 | }; |
| 5116 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5117 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5118 | |
| 5119 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 5120 | using directly the C version of find_class(), when find_class() is not |
| 5121 | overridden by a subclass. Although, this could become rather hackish. A |
| 5122 | simpler optimization would be to call the C function when self is not a |
| 5123 | subclass instance. */ |
| 5124 | static PyObject * |
| 5125 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 5126 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5127 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5128 | |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 5129 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
| 5130 | module_name, global_name, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5131 | } |
| 5132 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5133 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5134 | marker(UnpicklerObject *self) |
| 5135 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5136 | Py_ssize_t mark; |
| 5137 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5138 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5139 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5140 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5141 | return -1; |
| 5142 | } |
| 5143 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5144 | mark = self->marks[--self->num_marks]; |
| 5145 | self->stack->mark_set = self->num_marks != 0; |
| 5146 | self->stack->fence = self->num_marks ? |
| 5147 | self->marks[self->num_marks - 1] : 0; |
| 5148 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5149 | } |
| 5150 | |
| 5151 | static int |
| 5152 | load_none(UnpicklerObject *self) |
| 5153 | { |
| 5154 | PDATA_APPEND(self->stack, Py_None, -1); |
| 5155 | return 0; |
| 5156 | } |
| 5157 | |
| 5158 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5159 | load_int(UnpicklerObject *self) |
| 5160 | { |
| 5161 | PyObject *value; |
| 5162 | char *endptr, *s; |
| 5163 | Py_ssize_t len; |
| 5164 | long x; |
| 5165 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5166 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5167 | return -1; |
| 5168 | if (len < 2) |
| 5169 | return bad_readline(); |
| 5170 | |
| 5171 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5172 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5173 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5174 | x = strtol(s, &endptr, 0); |
| 5175 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5176 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5177 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 5178 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5179 | errno = 0; |
| 5180 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5181 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5182 | if (value == NULL) { |
| 5183 | PyErr_SetString(PyExc_ValueError, |
| 5184 | "could not convert string to int"); |
| 5185 | return -1; |
| 5186 | } |
| 5187 | } |
| 5188 | else { |
| 5189 | if (len == 3 && (x == 0 || x == 1)) { |
| 5190 | if ((value = PyBool_FromLong(x)) == NULL) |
| 5191 | return -1; |
| 5192 | } |
| 5193 | else { |
| 5194 | if ((value = PyLong_FromLong(x)) == NULL) |
| 5195 | return -1; |
| 5196 | } |
| 5197 | } |
| 5198 | |
| 5199 | PDATA_PUSH(self->stack, value, -1); |
| 5200 | return 0; |
| 5201 | } |
| 5202 | |
| 5203 | static int |
| 5204 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 5205 | { |
| 5206 | assert(boolean == Py_True || boolean == Py_False); |
| 5207 | PDATA_APPEND(self->stack, boolean, -1); |
| 5208 | return 0; |
| 5209 | } |
| 5210 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5211 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 5212 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 5213 | */ |
| 5214 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5215 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5216 | { |
| 5217 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5218 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5219 | size_t x = 0; |
| 5220 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 5221 | if (nbytes > (int)sizeof(size_t)) { |
| 5222 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 5223 | * have 64-bit size that can't be represented on 32-bit platform. |
| 5224 | */ |
| 5225 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 5226 | if (s[i]) |
| 5227 | return -1; |
| 5228 | } |
| 5229 | nbytes = (int)sizeof(size_t); |
| 5230 | } |
| 5231 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5232 | x |= (size_t) s[i] << (8 * i); |
| 5233 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5234 | |
| 5235 | if (x > PY_SSIZE_T_MAX) |
| 5236 | return -1; |
| 5237 | else |
| 5238 | return (Py_ssize_t) x; |
| 5239 | } |
| 5240 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5241 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 5242 | * 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] | 5243 | * 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] | 5244 | * of x-platform bugs. |
| 5245 | */ |
| 5246 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5247 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5248 | { |
| 5249 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 5250 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5251 | long x = 0; |
| 5252 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5253 | for (i = 0; i < nbytes; i++) { |
| 5254 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5255 | } |
| 5256 | |
| 5257 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 5258 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 5259 | * to extend a BININT's sign bit to the full width. |
| 5260 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5261 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5262 | x |= -(x & (1L << 31)); |
| 5263 | } |
| 5264 | |
| 5265 | return x; |
| 5266 | } |
| 5267 | |
| 5268 | static int |
| 5269 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 5270 | { |
| 5271 | PyObject *value; |
| 5272 | long x; |
| 5273 | |
| 5274 | x = calc_binint(s, size); |
| 5275 | |
| 5276 | if ((value = PyLong_FromLong(x)) == NULL) |
| 5277 | return -1; |
| 5278 | |
| 5279 | PDATA_PUSH(self->stack, value, -1); |
| 5280 | return 0; |
| 5281 | } |
| 5282 | |
| 5283 | static int |
| 5284 | load_binint(UnpicklerObject *self) |
| 5285 | { |
| 5286 | char *s; |
| 5287 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5288 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5289 | return -1; |
| 5290 | |
| 5291 | return load_binintx(self, s, 4); |
| 5292 | } |
| 5293 | |
| 5294 | static int |
| 5295 | load_binint1(UnpicklerObject *self) |
| 5296 | { |
| 5297 | char *s; |
| 5298 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5299 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5300 | return -1; |
| 5301 | |
| 5302 | return load_binintx(self, s, 1); |
| 5303 | } |
| 5304 | |
| 5305 | static int |
| 5306 | load_binint2(UnpicklerObject *self) |
| 5307 | { |
| 5308 | char *s; |
| 5309 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5310 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5311 | return -1; |
| 5312 | |
| 5313 | return load_binintx(self, s, 2); |
| 5314 | } |
| 5315 | |
| 5316 | static int |
| 5317 | load_long(UnpicklerObject *self) |
| 5318 | { |
| 5319 | PyObject *value; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5320 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5321 | Py_ssize_t len; |
| 5322 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5323 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5324 | return -1; |
| 5325 | if (len < 2) |
| 5326 | return bad_readline(); |
| 5327 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 5328 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 5329 | the 'L' before calling PyLong_FromString. In order to maintain |
| 5330 | compatibility with Python 3.0.0, we don't actually *require* |
| 5331 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5332 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 5333 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 5334 | /* XXX: Should the base argument explicitly set to 10? */ |
| 5335 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 5336 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5337 | return -1; |
| 5338 | |
| 5339 | PDATA_PUSH(self->stack, value, -1); |
| 5340 | return 0; |
| 5341 | } |
| 5342 | |
| 5343 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 5344 | * data following. |
| 5345 | */ |
| 5346 | static int |
| 5347 | load_counted_long(UnpicklerObject *self, int size) |
| 5348 | { |
| 5349 | PyObject *value; |
| 5350 | char *nbytes; |
| 5351 | char *pdata; |
| 5352 | |
| 5353 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5354 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5355 | return -1; |
| 5356 | |
| 5357 | size = calc_binint(nbytes, size); |
| 5358 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5359 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5360 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5361 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5362 | "LONG pickle has negative byte count"); |
| 5363 | return -1; |
| 5364 | } |
| 5365 | |
| 5366 | if (size == 0) |
| 5367 | value = PyLong_FromLong(0L); |
| 5368 | else { |
| 5369 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5370 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5371 | return -1; |
| 5372 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 5373 | 1 /* little endian */ , 1 /* signed */ ); |
| 5374 | } |
| 5375 | if (value == NULL) |
| 5376 | return -1; |
| 5377 | PDATA_PUSH(self->stack, value, -1); |
| 5378 | return 0; |
| 5379 | } |
| 5380 | |
| 5381 | static int |
| 5382 | load_float(UnpicklerObject *self) |
| 5383 | { |
| 5384 | PyObject *value; |
| 5385 | char *endptr, *s; |
| 5386 | Py_ssize_t len; |
| 5387 | double d; |
| 5388 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5389 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5390 | return -1; |
| 5391 | if (len < 2) |
| 5392 | return bad_readline(); |
| 5393 | |
| 5394 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5395 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 5396 | if (d == -1.0 && PyErr_Occurred()) |
| 5397 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5398 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5399 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 5400 | return -1; |
| 5401 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 5402 | value = PyFloat_FromDouble(d); |
| 5403 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5404 | return -1; |
| 5405 | |
| 5406 | PDATA_PUSH(self->stack, value, -1); |
| 5407 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5408 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5409 | |
| 5410 | static int |
| 5411 | load_binfloat(UnpicklerObject *self) |
| 5412 | { |
| 5413 | PyObject *value; |
| 5414 | double x; |
| 5415 | char *s; |
| 5416 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5417 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5418 | return -1; |
| 5419 | |
| 5420 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 5421 | if (x == -1.0 && PyErr_Occurred()) |
| 5422 | return -1; |
| 5423 | |
| 5424 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 5425 | return -1; |
| 5426 | |
| 5427 | PDATA_PUSH(self->stack, value, -1); |
| 5428 | return 0; |
| 5429 | } |
| 5430 | |
| 5431 | static int |
| 5432 | load_string(UnpicklerObject *self) |
| 5433 | { |
| 5434 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5435 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5436 | Py_ssize_t len; |
| 5437 | char *s, *p; |
| 5438 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5439 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5440 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5441 | /* Strip the newline */ |
| 5442 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5443 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5444 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5445 | p = s + 1; |
| 5446 | len -= 2; |
| 5447 | } |
| 5448 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5449 | PickleState *st = _Pickle_GetGlobalState(); |
| 5450 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5451 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5452 | return -1; |
| 5453 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5454 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5455 | |
| 5456 | /* Use the PyBytes API to decode the string, since that is what is used |
| 5457 | to encode, and then coerce the result to Unicode. */ |
| 5458 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5459 | if (bytes == NULL) |
| 5460 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5461 | |
| 5462 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 5463 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5464 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5465 | obj = bytes; |
| 5466 | } |
| 5467 | else { |
| 5468 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 5469 | Py_DECREF(bytes); |
| 5470 | if (obj == NULL) { |
| 5471 | return -1; |
| 5472 | } |
| 5473 | } |
| 5474 | |
| 5475 | PDATA_PUSH(self->stack, obj, -1); |
| 5476 | return 0; |
| 5477 | } |
| 5478 | |
| 5479 | static int |
| 5480 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 5481 | { |
| 5482 | PyObject *obj; |
| 5483 | Py_ssize_t size; |
| 5484 | char *s; |
| 5485 | |
| 5486 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5487 | return -1; |
| 5488 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5489 | size = calc_binsize(s, nbytes); |
| 5490 | if (size < 0) { |
| 5491 | PickleState *st = _Pickle_GetGlobalState(); |
| 5492 | PyErr_Format(st->UnpicklingError, |
| 5493 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 5494 | PY_SSIZE_T_MAX); |
| 5495 | return -1; |
| 5496 | } |
| 5497 | |
| 5498 | if (_Unpickler_Read(self, &s, size) < 0) |
| 5499 | return -1; |
| 5500 | |
| 5501 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 5502 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5503 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5504 | obj = PyBytes_FromStringAndSize(s, size); |
| 5505 | } |
| 5506 | else { |
| 5507 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 5508 | } |
| 5509 | if (obj == NULL) { |
| 5510 | return -1; |
| 5511 | } |
| 5512 | |
| 5513 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5514 | return 0; |
| 5515 | } |
| 5516 | |
| 5517 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5518 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5519 | { |
| 5520 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5521 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5522 | char *s; |
| 5523 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5524 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5525 | return -1; |
| 5526 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5527 | size = calc_binsize(s, nbytes); |
| 5528 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5529 | PyErr_Format(PyExc_OverflowError, |
| 5530 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5531 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5532 | return -1; |
| 5533 | } |
| 5534 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 5535 | bytes = PyBytes_FromStringAndSize(NULL, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5536 | if (bytes == NULL) |
| 5537 | return -1; |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 5538 | if (_Unpickler_ReadInto(self, PyBytes_AS_STRING(bytes), size) < 0) { |
| 5539 | Py_DECREF(bytes); |
| 5540 | return -1; |
| 5541 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5542 | |
| 5543 | PDATA_PUSH(self->stack, bytes, -1); |
| 5544 | return 0; |
| 5545 | } |
| 5546 | |
| 5547 | static int |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 5548 | load_counted_bytearray(UnpicklerObject *self) |
| 5549 | { |
| 5550 | PyObject *bytearray; |
| 5551 | Py_ssize_t size; |
| 5552 | char *s; |
| 5553 | |
| 5554 | if (_Unpickler_Read(self, &s, 8) < 0) { |
| 5555 | return -1; |
| 5556 | } |
| 5557 | |
| 5558 | size = calc_binsize(s, 8); |
| 5559 | if (size < 0) { |
| 5560 | PyErr_Format(PyExc_OverflowError, |
| 5561 | "BYTEARRAY8 exceeds system's maximum size of %zd bytes", |
| 5562 | PY_SSIZE_T_MAX); |
| 5563 | return -1; |
| 5564 | } |
| 5565 | |
| 5566 | bytearray = PyByteArray_FromStringAndSize(NULL, size); |
| 5567 | if (bytearray == NULL) { |
| 5568 | return -1; |
| 5569 | } |
| 5570 | if (_Unpickler_ReadInto(self, PyByteArray_AS_STRING(bytearray), size) < 0) { |
| 5571 | Py_DECREF(bytearray); |
| 5572 | return -1; |
| 5573 | } |
| 5574 | |
| 5575 | PDATA_PUSH(self->stack, bytearray, -1); |
| 5576 | return 0; |
| 5577 | } |
| 5578 | |
| 5579 | static int |
| 5580 | load_next_buffer(UnpicklerObject *self) |
| 5581 | { |
| 5582 | if (self->buffers == NULL) { |
| 5583 | PickleState *st = _Pickle_GetGlobalState(); |
| 5584 | PyErr_SetString(st->UnpicklingError, |
| 5585 | "pickle stream refers to out-of-band data " |
| 5586 | "but no *buffers* argument was given"); |
| 5587 | return -1; |
| 5588 | } |
| 5589 | PyObject *buf = PyIter_Next(self->buffers); |
| 5590 | if (buf == NULL) { |
| 5591 | if (!PyErr_Occurred()) { |
| 5592 | PickleState *st = _Pickle_GetGlobalState(); |
| 5593 | PyErr_SetString(st->UnpicklingError, |
| 5594 | "not enough out-of-band buffers"); |
| 5595 | } |
| 5596 | return -1; |
| 5597 | } |
| 5598 | |
| 5599 | PDATA_PUSH(self->stack, buf, -1); |
| 5600 | return 0; |
| 5601 | } |
| 5602 | |
| 5603 | static int |
| 5604 | load_readonly_buffer(UnpicklerObject *self) |
| 5605 | { |
| 5606 | Py_ssize_t len = Py_SIZE(self->stack); |
| 5607 | if (len <= self->stack->fence) { |
| 5608 | return Pdata_stack_underflow(self->stack); |
| 5609 | } |
| 5610 | |
| 5611 | PyObject *obj = self->stack->data[len - 1]; |
| 5612 | PyObject *view = PyMemoryView_FromObject(obj); |
| 5613 | if (view == NULL) { |
| 5614 | return -1; |
| 5615 | } |
| 5616 | if (!PyMemoryView_GET_BUFFER(view)->readonly) { |
| 5617 | /* Original object is writable */ |
| 5618 | PyMemoryView_GET_BUFFER(view)->readonly = 1; |
| 5619 | self->stack->data[len - 1] = view; |
| 5620 | Py_DECREF(obj); |
| 5621 | } |
| 5622 | else { |
| 5623 | /* Original object is read-only, no need to replace it */ |
| 5624 | Py_DECREF(view); |
| 5625 | } |
| 5626 | return 0; |
| 5627 | } |
| 5628 | |
| 5629 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5630 | load_unicode(UnpicklerObject *self) |
| 5631 | { |
| 5632 | PyObject *str; |
| 5633 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5634 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5636 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5637 | return -1; |
| 5638 | if (len < 1) |
| 5639 | return bad_readline(); |
| 5640 | |
| 5641 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5642 | if (str == NULL) |
| 5643 | return -1; |
| 5644 | |
| 5645 | PDATA_PUSH(self->stack, str, -1); |
| 5646 | return 0; |
| 5647 | } |
| 5648 | |
| 5649 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5650 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5651 | { |
| 5652 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5653 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5654 | char *s; |
| 5655 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5656 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5657 | return -1; |
| 5658 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5659 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5660 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5661 | PyErr_Format(PyExc_OverflowError, |
| 5662 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5663 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5664 | return -1; |
| 5665 | } |
| 5666 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5667 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5668 | return -1; |
| 5669 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5670 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5671 | if (str == NULL) |
| 5672 | return -1; |
| 5673 | |
| 5674 | PDATA_PUSH(self->stack, str, -1); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
| 5678 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5679 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5680 | { |
| 5681 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5682 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5683 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5684 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5685 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5686 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5687 | if (tuple == NULL) |
| 5688 | return -1; |
| 5689 | PDATA_PUSH(self->stack, tuple, -1); |
| 5690 | return 0; |
| 5691 | } |
| 5692 | |
| 5693 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5694 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5695 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5696 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5697 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5698 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5699 | return -1; |
| 5700 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5701 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5702 | } |
| 5703 | |
| 5704 | static int |
| 5705 | load_empty_list(UnpicklerObject *self) |
| 5706 | { |
| 5707 | PyObject *list; |
| 5708 | |
| 5709 | if ((list = PyList_New(0)) == NULL) |
| 5710 | return -1; |
| 5711 | PDATA_PUSH(self->stack, list, -1); |
| 5712 | return 0; |
| 5713 | } |
| 5714 | |
| 5715 | static int |
| 5716 | load_empty_dict(UnpicklerObject *self) |
| 5717 | { |
| 5718 | PyObject *dict; |
| 5719 | |
| 5720 | if ((dict = PyDict_New()) == NULL) |
| 5721 | return -1; |
| 5722 | PDATA_PUSH(self->stack, dict, -1); |
| 5723 | return 0; |
| 5724 | } |
| 5725 | |
| 5726 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5727 | load_empty_set(UnpicklerObject *self) |
| 5728 | { |
| 5729 | PyObject *set; |
| 5730 | |
| 5731 | if ((set = PySet_New(NULL)) == NULL) |
| 5732 | return -1; |
| 5733 | PDATA_PUSH(self->stack, set, -1); |
| 5734 | return 0; |
| 5735 | } |
| 5736 | |
| 5737 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5738 | load_list(UnpicklerObject *self) |
| 5739 | { |
| 5740 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5741 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5742 | |
| 5743 | if ((i = marker(self)) < 0) |
| 5744 | return -1; |
| 5745 | |
| 5746 | list = Pdata_poplist(self->stack, i); |
| 5747 | if (list == NULL) |
| 5748 | return -1; |
| 5749 | PDATA_PUSH(self->stack, list, -1); |
| 5750 | return 0; |
| 5751 | } |
| 5752 | |
| 5753 | static int |
| 5754 | load_dict(UnpicklerObject *self) |
| 5755 | { |
| 5756 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5757 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5758 | |
| 5759 | if ((i = marker(self)) < 0) |
| 5760 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5761 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5762 | |
| 5763 | if ((dict = PyDict_New()) == NULL) |
| 5764 | return -1; |
| 5765 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5766 | if ((j - i) % 2 != 0) { |
| 5767 | PickleState *st = _Pickle_GetGlobalState(); |
| 5768 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5769 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5770 | return -1; |
| 5771 | } |
| 5772 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5773 | for (k = i + 1; k < j; k += 2) { |
| 5774 | key = self->stack->data[k - 1]; |
| 5775 | value = self->stack->data[k]; |
| 5776 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5777 | Py_DECREF(dict); |
| 5778 | return -1; |
| 5779 | } |
| 5780 | } |
| 5781 | Pdata_clear(self->stack, i); |
| 5782 | PDATA_PUSH(self->stack, dict, -1); |
| 5783 | return 0; |
| 5784 | } |
| 5785 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5786 | static int |
| 5787 | load_frozenset(UnpicklerObject *self) |
| 5788 | { |
| 5789 | PyObject *items; |
| 5790 | PyObject *frozenset; |
| 5791 | Py_ssize_t i; |
| 5792 | |
| 5793 | if ((i = marker(self)) < 0) |
| 5794 | return -1; |
| 5795 | |
| 5796 | items = Pdata_poptuple(self->stack, i); |
| 5797 | if (items == NULL) |
| 5798 | return -1; |
| 5799 | |
| 5800 | frozenset = PyFrozenSet_New(items); |
| 5801 | Py_DECREF(items); |
| 5802 | if (frozenset == NULL) |
| 5803 | return -1; |
| 5804 | |
| 5805 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5806 | return 0; |
| 5807 | } |
| 5808 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5809 | static PyObject * |
| 5810 | instantiate(PyObject *cls, PyObject *args) |
| 5811 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5812 | /* Caller must assure args are a tuple. Normally, args come from |
| 5813 | Pdata_poptuple which packs objects from the top of the stack |
| 5814 | into a newly created tuple. */ |
| 5815 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5816 | if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { |
| 5817 | _Py_IDENTIFIER(__getinitargs__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5818 | _Py_IDENTIFIER(__new__); |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 5819 | PyObject *func; |
| 5820 | if (_PyObject_LookupAttrId(cls, &PyId___getinitargs__, &func) < 0) { |
| 5821 | return NULL; |
| 5822 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5823 | if (func == NULL) { |
Jeroen Demeyer | 59ad110 | 2019-07-11 10:59:05 +0200 | [diff] [blame] | 5824 | return _PyObject_CallMethodIdOneArg(cls, &PyId___new__, cls); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5825 | } |
| 5826 | Py_DECREF(func); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5827 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5828 | return PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5829 | } |
| 5830 | |
| 5831 | static int |
| 5832 | load_obj(UnpicklerObject *self) |
| 5833 | { |
| 5834 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5835 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5836 | |
| 5837 | if ((i = marker(self)) < 0) |
| 5838 | return -1; |
| 5839 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5840 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5841 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5842 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5843 | args = Pdata_poptuple(self->stack, i + 1); |
| 5844 | if (args == NULL) |
| 5845 | return -1; |
| 5846 | |
| 5847 | PDATA_POP(self->stack, cls); |
| 5848 | if (cls) { |
| 5849 | obj = instantiate(cls, args); |
| 5850 | Py_DECREF(cls); |
| 5851 | } |
| 5852 | Py_DECREF(args); |
| 5853 | if (obj == NULL) |
| 5854 | return -1; |
| 5855 | |
| 5856 | PDATA_PUSH(self->stack, obj, -1); |
| 5857 | return 0; |
| 5858 | } |
| 5859 | |
| 5860 | static int |
| 5861 | load_inst(UnpicklerObject *self) |
| 5862 | { |
| 5863 | PyObject *cls = NULL; |
| 5864 | PyObject *args = NULL; |
| 5865 | PyObject *obj = NULL; |
| 5866 | PyObject *module_name; |
| 5867 | PyObject *class_name; |
| 5868 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5869 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5870 | char *s; |
| 5871 | |
| 5872 | if ((i = marker(self)) < 0) |
| 5873 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5874 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5875 | return -1; |
| 5876 | if (len < 2) |
| 5877 | return bad_readline(); |
| 5878 | |
| 5879 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5880 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5881 | supported by older protocols on Python 2.x. */ |
| 5882 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5883 | if (module_name == NULL) |
| 5884 | return -1; |
| 5885 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5886 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5887 | if (len < 2) { |
| 5888 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5889 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5890 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5891 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5892 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5893 | cls = find_class(self, module_name, class_name); |
| 5894 | Py_DECREF(class_name); |
| 5895 | } |
| 5896 | } |
| 5897 | Py_DECREF(module_name); |
| 5898 | |
| 5899 | if (cls == NULL) |
| 5900 | return -1; |
| 5901 | |
| 5902 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5903 | obj = instantiate(cls, args); |
| 5904 | Py_DECREF(args); |
| 5905 | } |
| 5906 | Py_DECREF(cls); |
| 5907 | |
| 5908 | if (obj == NULL) |
| 5909 | return -1; |
| 5910 | |
| 5911 | PDATA_PUSH(self->stack, obj, -1); |
| 5912 | return 0; |
| 5913 | } |
| 5914 | |
| 5915 | static int |
| 5916 | load_newobj(UnpicklerObject *self) |
| 5917 | { |
| 5918 | PyObject *args = NULL; |
| 5919 | PyObject *clsraw = NULL; |
| 5920 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5921 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5922 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5923 | |
| 5924 | /* Stack is ... cls argtuple, and we want to call |
| 5925 | * cls.__new__(cls, *argtuple). |
| 5926 | */ |
| 5927 | PDATA_POP(self->stack, args); |
| 5928 | if (args == NULL) |
| 5929 | goto error; |
| 5930 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5931 | PyErr_SetString(st->UnpicklingError, |
| 5932 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5933 | goto error; |
| 5934 | } |
| 5935 | |
| 5936 | PDATA_POP(self->stack, clsraw); |
| 5937 | cls = (PyTypeObject *)clsraw; |
| 5938 | if (cls == NULL) |
| 5939 | goto error; |
| 5940 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5941 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5942 | "isn't a type object"); |
| 5943 | goto error; |
| 5944 | } |
| 5945 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5946 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5947 | "has NULL tp_new"); |
| 5948 | goto error; |
| 5949 | } |
| 5950 | |
| 5951 | /* Call __new__. */ |
| 5952 | obj = cls->tp_new(cls, args, NULL); |
| 5953 | if (obj == NULL) |
| 5954 | goto error; |
| 5955 | |
| 5956 | Py_DECREF(args); |
| 5957 | Py_DECREF(clsraw); |
| 5958 | PDATA_PUSH(self->stack, obj, -1); |
| 5959 | return 0; |
| 5960 | |
| 5961 | error: |
| 5962 | Py_XDECREF(args); |
| 5963 | Py_XDECREF(clsraw); |
| 5964 | return -1; |
| 5965 | } |
| 5966 | |
| 5967 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5968 | load_newobj_ex(UnpicklerObject *self) |
| 5969 | { |
| 5970 | PyObject *cls, *args, *kwargs; |
| 5971 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5972 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5973 | |
| 5974 | PDATA_POP(self->stack, kwargs); |
| 5975 | if (kwargs == NULL) { |
| 5976 | return -1; |
| 5977 | } |
| 5978 | PDATA_POP(self->stack, args); |
| 5979 | if (args == NULL) { |
| 5980 | Py_DECREF(kwargs); |
| 5981 | return -1; |
| 5982 | } |
| 5983 | PDATA_POP(self->stack, cls); |
| 5984 | if (cls == NULL) { |
| 5985 | Py_DECREF(kwargs); |
| 5986 | Py_DECREF(args); |
| 5987 | return -1; |
| 5988 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5989 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5990 | if (!PyType_Check(cls)) { |
| 5991 | Py_DECREF(kwargs); |
| 5992 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5993 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5994 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5995 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5996 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5997 | return -1; |
| 5998 | } |
| 5999 | |
| 6000 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 6001 | Py_DECREF(kwargs); |
| 6002 | Py_DECREF(args); |
| 6003 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6004 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6005 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 6006 | return -1; |
| 6007 | } |
| 6008 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 6009 | Py_DECREF(kwargs); |
| 6010 | Py_DECREF(args); |
| 6011 | Py_DECREF(cls); |
| 6012 | if (obj == NULL) { |
| 6013 | return -1; |
| 6014 | } |
| 6015 | PDATA_PUSH(self->stack, obj, -1); |
| 6016 | return 0; |
| 6017 | } |
| 6018 | |
| 6019 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6020 | load_global(UnpicklerObject *self) |
| 6021 | { |
| 6022 | PyObject *global = NULL; |
| 6023 | PyObject *module_name; |
| 6024 | PyObject *global_name; |
| 6025 | Py_ssize_t len; |
| 6026 | char *s; |
| 6027 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6028 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6029 | return -1; |
| 6030 | if (len < 2) |
| 6031 | return bad_readline(); |
| 6032 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 6033 | if (!module_name) |
| 6034 | return -1; |
| 6035 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6036 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6037 | if (len < 2) { |
| 6038 | Py_DECREF(module_name); |
| 6039 | return bad_readline(); |
| 6040 | } |
| 6041 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 6042 | if (global_name) { |
| 6043 | global = find_class(self, module_name, global_name); |
| 6044 | Py_DECREF(global_name); |
| 6045 | } |
| 6046 | } |
| 6047 | Py_DECREF(module_name); |
| 6048 | |
| 6049 | if (global == NULL) |
| 6050 | return -1; |
| 6051 | PDATA_PUSH(self->stack, global, -1); |
| 6052 | return 0; |
| 6053 | } |
| 6054 | |
| 6055 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6056 | load_stack_global(UnpicklerObject *self) |
| 6057 | { |
| 6058 | PyObject *global; |
| 6059 | PyObject *module_name; |
| 6060 | PyObject *global_name; |
| 6061 | |
| 6062 | PDATA_POP(self->stack, global_name); |
| 6063 | PDATA_POP(self->stack, module_name); |
| 6064 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 6065 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6066 | PickleState *st = _Pickle_GetGlobalState(); |
| 6067 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6068 | Py_XDECREF(global_name); |
| 6069 | Py_XDECREF(module_name); |
| 6070 | return -1; |
| 6071 | } |
| 6072 | global = find_class(self, module_name, global_name); |
| 6073 | Py_DECREF(global_name); |
| 6074 | Py_DECREF(module_name); |
| 6075 | if (global == NULL) |
| 6076 | return -1; |
| 6077 | PDATA_PUSH(self->stack, global, -1); |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
| 6081 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6082 | load_persid(UnpicklerObject *self) |
| 6083 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6084 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6085 | Py_ssize_t len; |
| 6086 | char *s; |
| 6087 | |
| 6088 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6089 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6090 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 6091 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6092 | return bad_readline(); |
| 6093 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 6094 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 6095 | if (pid == NULL) { |
| 6096 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 6097 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 6098 | "persistent IDs in protocol 0 must be " |
| 6099 | "ASCII strings"); |
| 6100 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6101 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 6102 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6103 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6104 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 6105 | Py_DECREF(pid); |
| 6106 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6107 | return -1; |
| 6108 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6109 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6110 | return 0; |
| 6111 | } |
| 6112 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6113 | PickleState *st = _Pickle_GetGlobalState(); |
| 6114 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6115 | "A load persistent id instruction was encountered,\n" |
| 6116 | "but no persistent_load function was specified."); |
| 6117 | return -1; |
| 6118 | } |
| 6119 | } |
| 6120 | |
| 6121 | static int |
| 6122 | load_binpersid(UnpicklerObject *self) |
| 6123 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6124 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6125 | |
| 6126 | if (self->pers_func) { |
| 6127 | PDATA_POP(self->stack, pid); |
| 6128 | if (pid == NULL) |
| 6129 | return -1; |
| 6130 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6131 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 6132 | Py_DECREF(pid); |
| 6133 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6134 | return -1; |
| 6135 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6136 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6137 | return 0; |
| 6138 | } |
| 6139 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6140 | PickleState *st = _Pickle_GetGlobalState(); |
| 6141 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6142 | "A load persistent id instruction was encountered,\n" |
| 6143 | "but no persistent_load function was specified."); |
| 6144 | return -1; |
| 6145 | } |
| 6146 | } |
| 6147 | |
| 6148 | static int |
| 6149 | load_pop(UnpicklerObject *self) |
| 6150 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6151 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6152 | |
| 6153 | /* Note that we split the (pickle.py) stack into two stacks, |
| 6154 | * an object stack and a mark stack. We have to be clever and |
| 6155 | * 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] | 6156 | * mark stack first, and only signalling a stack underflow if |
| 6157 | * the object stack is empty and the mark stack doesn't match |
| 6158 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6159 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 6160 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6161 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6162 | self->stack->mark_set = self->num_marks != 0; |
| 6163 | self->stack->fence = self->num_marks ? |
| 6164 | self->marks[self->num_marks - 1] : 0; |
| 6165 | } else if (len <= self->stack->fence) |
| 6166 | return Pdata_stack_underflow(self->stack); |
| 6167 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6168 | len--; |
| 6169 | Py_DECREF(self->stack->data[len]); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 6170 | Py_SET_SIZE(self->stack, len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6171 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6172 | return 0; |
| 6173 | } |
| 6174 | |
| 6175 | static int |
| 6176 | load_pop_mark(UnpicklerObject *self) |
| 6177 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6178 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6179 | |
| 6180 | if ((i = marker(self)) < 0) |
| 6181 | return -1; |
| 6182 | |
| 6183 | Pdata_clear(self->stack, i); |
| 6184 | |
| 6185 | return 0; |
| 6186 | } |
| 6187 | |
| 6188 | static int |
| 6189 | load_dup(UnpicklerObject *self) |
| 6190 | { |
| 6191 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6192 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6193 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6194 | if (len <= self->stack->fence) |
| 6195 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6196 | last = self->stack->data[len - 1]; |
| 6197 | PDATA_APPEND(self->stack, last, -1); |
| 6198 | return 0; |
| 6199 | } |
| 6200 | |
| 6201 | static int |
| 6202 | load_get(UnpicklerObject *self) |
| 6203 | { |
| 6204 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6205 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6206 | Py_ssize_t len; |
| 6207 | char *s; |
| 6208 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6209 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6210 | return -1; |
| 6211 | if (len < 2) |
| 6212 | return bad_readline(); |
| 6213 | |
| 6214 | key = PyLong_FromString(s, NULL, 10); |
| 6215 | if (key == NULL) |
| 6216 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6217 | idx = PyLong_AsSsize_t(key); |
| 6218 | if (idx == -1 && PyErr_Occurred()) { |
| 6219 | Py_DECREF(key); |
| 6220 | return -1; |
| 6221 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6222 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6223 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6224 | if (value == NULL) { |
Claudiu Popa | 6f03b23 | 2019-11-24 20:15:08 +0100 | [diff] [blame] | 6225 | if (!PyErr_Occurred()) { |
| 6226 | PickleState *st = _Pickle_GetGlobalState(); |
| 6227 | PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx); |
| 6228 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6229 | Py_DECREF(key); |
| 6230 | return -1; |
| 6231 | } |
| 6232 | Py_DECREF(key); |
| 6233 | |
| 6234 | PDATA_APPEND(self->stack, value, -1); |
| 6235 | return 0; |
| 6236 | } |
| 6237 | |
| 6238 | static int |
| 6239 | load_binget(UnpicklerObject *self) |
| 6240 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6241 | PyObject *value; |
| 6242 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6243 | char *s; |
| 6244 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6245 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6246 | return -1; |
| 6247 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6248 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6249 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6250 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6251 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6252 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6253 | if (key != NULL) { |
Claudiu Popa | 6f03b23 | 2019-11-24 20:15:08 +0100 | [diff] [blame] | 6254 | PickleState *st = _Pickle_GetGlobalState(); |
| 6255 | PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6256 | Py_DECREF(key); |
| 6257 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6258 | return -1; |
| 6259 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6260 | |
| 6261 | PDATA_APPEND(self->stack, value, -1); |
| 6262 | return 0; |
| 6263 | } |
| 6264 | |
| 6265 | static int |
| 6266 | load_long_binget(UnpicklerObject *self) |
| 6267 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6268 | PyObject *value; |
| 6269 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6270 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6271 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6272 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6273 | return -1; |
| 6274 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6275 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6276 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6277 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6278 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6279 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6280 | if (key != NULL) { |
Claudiu Popa | 6f03b23 | 2019-11-24 20:15:08 +0100 | [diff] [blame] | 6281 | PickleState *st = _Pickle_GetGlobalState(); |
| 6282 | PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 6283 | Py_DECREF(key); |
| 6284 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6285 | return -1; |
| 6286 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6287 | |
| 6288 | PDATA_APPEND(self->stack, value, -1); |
| 6289 | return 0; |
| 6290 | } |
| 6291 | |
| 6292 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 6293 | * the number of bytes following the opcode, holding the index (code) value. |
| 6294 | */ |
| 6295 | static int |
| 6296 | load_extension(UnpicklerObject *self, int nbytes) |
| 6297 | { |
| 6298 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 6299 | long code; /* calc_binint returns long */ |
| 6300 | PyObject *py_code; /* code as a Python int */ |
| 6301 | PyObject *obj; /* the object to push */ |
| 6302 | PyObject *pair; /* (module_name, class_name) */ |
| 6303 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6304 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6305 | |
| 6306 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6307 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6308 | return -1; |
| 6309 | code = calc_binint(codebytes, nbytes); |
| 6310 | if (code <= 0) { /* note that 0 is forbidden */ |
| 6311 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6312 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6313 | return -1; |
| 6314 | } |
| 6315 | |
| 6316 | /* Look for the code in the cache. */ |
| 6317 | py_code = PyLong_FromLong(code); |
| 6318 | if (py_code == NULL) |
| 6319 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6320 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6321 | if (obj != NULL) { |
| 6322 | /* Bingo. */ |
| 6323 | Py_DECREF(py_code); |
| 6324 | PDATA_APPEND(self->stack, obj, -1); |
| 6325 | return 0; |
| 6326 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6327 | if (PyErr_Occurred()) { |
| 6328 | Py_DECREF(py_code); |
| 6329 | return -1; |
| 6330 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6331 | |
| 6332 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6333 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6334 | if (pair == NULL) { |
| 6335 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 6336 | if (!PyErr_Occurred()) { |
| 6337 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 6338 | "code %ld", code); |
| 6339 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6340 | return -1; |
| 6341 | } |
| 6342 | /* Since the extension registry is manipulable via Python code, |
| 6343 | * confirm that pair is really a 2-tuple of strings. |
| 6344 | */ |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6345 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) { |
| 6346 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6347 | } |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6348 | |
| 6349 | module_name = PyTuple_GET_ITEM(pair, 0); |
| 6350 | if (!PyUnicode_Check(module_name)) { |
| 6351 | goto error; |
| 6352 | } |
| 6353 | |
| 6354 | class_name = PyTuple_GET_ITEM(pair, 1); |
| 6355 | if (!PyUnicode_Check(class_name)) { |
| 6356 | goto error; |
| 6357 | } |
| 6358 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6359 | /* Load the object. */ |
| 6360 | obj = find_class(self, module_name, class_name); |
| 6361 | if (obj == NULL) { |
| 6362 | Py_DECREF(py_code); |
| 6363 | return -1; |
| 6364 | } |
| 6365 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6366 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6367 | Py_DECREF(py_code); |
| 6368 | if (code < 0) { |
| 6369 | Py_DECREF(obj); |
| 6370 | return -1; |
| 6371 | } |
| 6372 | PDATA_PUSH(self->stack, obj, -1); |
| 6373 | return 0; |
Victor Stinner | b37672d | 2018-11-22 03:37:50 +0100 | [diff] [blame] | 6374 | |
| 6375 | error: |
| 6376 | Py_DECREF(py_code); |
| 6377 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 6378 | "isn't a 2-tuple of strings", code); |
| 6379 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6380 | } |
| 6381 | |
| 6382 | static int |
| 6383 | load_put(UnpicklerObject *self) |
| 6384 | { |
| 6385 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6386 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6387 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 6388 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6389 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6390 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6391 | return -1; |
| 6392 | if (len < 2) |
| 6393 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6394 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6395 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6396 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6397 | |
| 6398 | key = PyLong_FromString(s, NULL, 10); |
| 6399 | if (key == NULL) |
| 6400 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6401 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6402 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6403 | if (idx < 0) { |
| 6404 | if (!PyErr_Occurred()) |
| 6405 | PyErr_SetString(PyExc_ValueError, |
| 6406 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6407 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6408 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6409 | |
| 6410 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6411 | } |
| 6412 | |
| 6413 | static int |
| 6414 | load_binput(UnpicklerObject *self) |
| 6415 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6416 | PyObject *value; |
| 6417 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6418 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6419 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6420 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6421 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6422 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6423 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6424 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6425 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6426 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6427 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6428 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6429 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
| 6432 | static int |
| 6433 | load_long_binput(UnpicklerObject *self) |
| 6434 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6435 | PyObject *value; |
| 6436 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6437 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6438 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6439 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6440 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6441 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6442 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6443 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6444 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6445 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6446 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 6447 | if (idx < 0) { |
| 6448 | PyErr_SetString(PyExc_ValueError, |
| 6449 | "negative LONG_BINPUT argument"); |
| 6450 | return -1; |
| 6451 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6452 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6453 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6454 | } |
| 6455 | |
| 6456 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6457 | load_memoize(UnpicklerObject *self) |
| 6458 | { |
| 6459 | PyObject *value; |
| 6460 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6461 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 6462 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6463 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 6464 | |
| 6465 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 6466 | } |
| 6467 | |
| 6468 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6469 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6470 | { |
| 6471 | PyObject *value; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6472 | PyObject *slice; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6473 | PyObject *list; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6474 | PyObject *result; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6475 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6476 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6477 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6478 | if (x > len || x <= self->stack->fence) |
| 6479 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6480 | if (len == x) /* nothing to do */ |
| 6481 | return 0; |
| 6482 | |
| 6483 | list = self->stack->data[x - 1]; |
| 6484 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6485 | if (PyList_CheckExact(list)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6486 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6487 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6488 | |
| 6489 | slice = Pdata_poplist(self->stack, x); |
| 6490 | if (!slice) |
| 6491 | return -1; |
| 6492 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6493 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6494 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6495 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6496 | } |
| 6497 | else { |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6498 | PyObject *extend_func; |
| 6499 | _Py_IDENTIFIER(extend); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6500 | |
Serhiy Storchaka | 41c57b3 | 2019-09-01 12:03:39 +0300 | [diff] [blame] | 6501 | if (_PyObject_LookupAttrId(list, &PyId_extend, &extend_func) < 0) { |
| 6502 | return -1; |
| 6503 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6504 | if (extend_func != NULL) { |
| 6505 | slice = Pdata_poplist(self->stack, x); |
| 6506 | if (!slice) { |
| 6507 | Py_DECREF(extend_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6508 | return -1; |
| 6509 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6510 | result = _Pickle_FastCall(extend_func, slice); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6511 | Py_DECREF(extend_func); |
| 6512 | if (result == NULL) |
| 6513 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6514 | Py_DECREF(result); |
| 6515 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6516 | else { |
| 6517 | PyObject *append_func; |
| 6518 | _Py_IDENTIFIER(append); |
| 6519 | |
| 6520 | /* Even if the PEP 307 requires extend() and append() methods, |
| 6521 | fall back on append() if the object has no extend() method |
| 6522 | for backward compatibility. */ |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6523 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
| 6524 | if (append_func == NULL) |
| 6525 | return -1; |
| 6526 | for (i = x; i < len; i++) { |
| 6527 | value = self->stack->data[i]; |
| 6528 | result = _Pickle_FastCall(append_func, value); |
| 6529 | if (result == NULL) { |
| 6530 | Pdata_clear(self->stack, i + 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 6531 | Py_SET_SIZE(self->stack, x); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6532 | Py_DECREF(append_func); |
| 6533 | return -1; |
| 6534 | } |
| 6535 | Py_DECREF(result); |
| 6536 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 6537 | Py_SET_SIZE(self->stack, x); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 6538 | Py_DECREF(append_func); |
| 6539 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6540 | } |
| 6541 | |
| 6542 | return 0; |
| 6543 | } |
| 6544 | |
| 6545 | static int |
| 6546 | load_append(UnpicklerObject *self) |
| 6547 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6548 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 6549 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6550 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6551 | } |
| 6552 | |
| 6553 | static int |
| 6554 | load_appends(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_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6560 | } |
| 6561 | |
| 6562 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6563 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6564 | { |
| 6565 | PyObject *value, *key; |
| 6566 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6567 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6568 | int status = 0; |
| 6569 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6570 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6571 | if (x > len || x <= self->stack->fence) |
| 6572 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6573 | if (len == x) /* nothing to do */ |
| 6574 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6575 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6576 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6577 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6578 | PyErr_SetString(st->UnpicklingError, |
| 6579 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6580 | return -1; |
| 6581 | } |
| 6582 | |
| 6583 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 6584 | that supports the __setitem__ attribute. */ |
| 6585 | dict = self->stack->data[x - 1]; |
| 6586 | |
| 6587 | for (i = x + 1; i < len; i += 2) { |
| 6588 | key = self->stack->data[i - 1]; |
| 6589 | value = self->stack->data[i]; |
| 6590 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 6591 | status = -1; |
| 6592 | break; |
| 6593 | } |
| 6594 | } |
| 6595 | |
| 6596 | Pdata_clear(self->stack, x); |
| 6597 | return status; |
| 6598 | } |
| 6599 | |
| 6600 | static int |
| 6601 | load_setitem(UnpicklerObject *self) |
| 6602 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6603 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6604 | } |
| 6605 | |
| 6606 | static int |
| 6607 | load_setitems(UnpicklerObject *self) |
| 6608 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6609 | Py_ssize_t i = marker(self); |
| 6610 | if (i < 0) |
| 6611 | return -1; |
| 6612 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
| 6615 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6616 | load_additems(UnpicklerObject *self) |
| 6617 | { |
| 6618 | PyObject *set; |
| 6619 | Py_ssize_t mark, len, i; |
| 6620 | |
| 6621 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6622 | if (mark < 0) |
| 6623 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6624 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6625 | if (mark > len || mark <= self->stack->fence) |
| 6626 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6627 | if (len == mark) /* nothing to do */ |
| 6628 | return 0; |
| 6629 | |
| 6630 | set = self->stack->data[mark - 1]; |
| 6631 | |
| 6632 | if (PySet_Check(set)) { |
| 6633 | PyObject *items; |
| 6634 | int status; |
| 6635 | |
| 6636 | items = Pdata_poptuple(self->stack, mark); |
| 6637 | if (items == NULL) |
| 6638 | return -1; |
| 6639 | |
| 6640 | status = _PySet_Update(set, items); |
| 6641 | Py_DECREF(items); |
| 6642 | return status; |
| 6643 | } |
| 6644 | else { |
| 6645 | PyObject *add_func; |
| 6646 | _Py_IDENTIFIER(add); |
| 6647 | |
| 6648 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6649 | if (add_func == NULL) |
| 6650 | return -1; |
| 6651 | for (i = mark; i < len; i++) { |
| 6652 | PyObject *result; |
| 6653 | PyObject *item; |
| 6654 | |
| 6655 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6656 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6657 | if (result == NULL) { |
| 6658 | Pdata_clear(self->stack, i + 1); |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 6659 | Py_SET_SIZE(self->stack, mark); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6660 | return -1; |
| 6661 | } |
| 6662 | Py_DECREF(result); |
| 6663 | } |
Victor Stinner | 60ac6ed | 2020-02-07 23:18:08 +0100 | [diff] [blame] | 6664 | Py_SET_SIZE(self->stack, mark); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | return 0; |
| 6668 | } |
| 6669 | |
| 6670 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6671 | load_build(UnpicklerObject *self) |
| 6672 | { |
| 6673 | PyObject *state, *inst, *slotstate; |
| 6674 | PyObject *setstate; |
| 6675 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6676 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6677 | |
| 6678 | /* Stack is ... instance, state. We want to leave instance at |
| 6679 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6680 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6681 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6682 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6683 | |
| 6684 | PDATA_POP(self->stack, state); |
| 6685 | if (state == NULL) |
| 6686 | return -1; |
| 6687 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6688 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6689 | |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6690 | if (_PyObject_LookupAttrId(inst, &PyId___setstate__, &setstate) < 0) { |
| 6691 | Py_DECREF(state); |
| 6692 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6693 | } |
Serhiy Storchaka | f320be7 | 2018-01-25 10:49:40 +0200 | [diff] [blame] | 6694 | if (setstate != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6695 | PyObject *result; |
| 6696 | |
| 6697 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6698 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6699 | Py_DECREF(setstate); |
| 6700 | if (result == NULL) |
| 6701 | return -1; |
| 6702 | Py_DECREF(result); |
| 6703 | return 0; |
| 6704 | } |
| 6705 | |
| 6706 | /* A default __setstate__. First see whether state embeds a |
| 6707 | * slot state dict too (a proto 2 addition). |
| 6708 | */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 6709 | if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6710 | PyObject *tmp = state; |
| 6711 | |
| 6712 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6713 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6714 | Py_INCREF(state); |
| 6715 | Py_INCREF(slotstate); |
| 6716 | Py_DECREF(tmp); |
| 6717 | } |
| 6718 | else |
| 6719 | slotstate = NULL; |
| 6720 | |
| 6721 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6722 | if (state != Py_None) { |
| 6723 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6724 | PyObject *d_key, *d_value; |
| 6725 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6726 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6727 | |
| 6728 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6729 | PickleState *st = _Pickle_GetGlobalState(); |
| 6730 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6731 | goto error; |
| 6732 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6733 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6734 | if (dict == NULL) |
| 6735 | goto error; |
| 6736 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6737 | i = 0; |
| 6738 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6739 | /* normally the keys for instance attributes are |
| 6740 | interned. we should try to do that here. */ |
| 6741 | Py_INCREF(d_key); |
| 6742 | if (PyUnicode_CheckExact(d_key)) |
| 6743 | PyUnicode_InternInPlace(&d_key); |
| 6744 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6745 | Py_DECREF(d_key); |
| 6746 | goto error; |
| 6747 | } |
| 6748 | Py_DECREF(d_key); |
| 6749 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6750 | Py_DECREF(dict); |
| 6751 | } |
| 6752 | |
| 6753 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6754 | if (slotstate != NULL) { |
| 6755 | PyObject *d_key, *d_value; |
| 6756 | Py_ssize_t i; |
| 6757 | |
| 6758 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6759 | PickleState *st = _Pickle_GetGlobalState(); |
| 6760 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6761 | "slot state is not a dictionary"); |
| 6762 | goto error; |
| 6763 | } |
| 6764 | i = 0; |
| 6765 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6766 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6767 | goto error; |
| 6768 | } |
| 6769 | } |
| 6770 | |
| 6771 | if (0) { |
| 6772 | error: |
| 6773 | status = -1; |
| 6774 | } |
| 6775 | |
| 6776 | Py_DECREF(state); |
| 6777 | Py_XDECREF(slotstate); |
| 6778 | return status; |
| 6779 | } |
| 6780 | |
| 6781 | static int |
| 6782 | load_mark(UnpicklerObject *self) |
| 6783 | { |
| 6784 | |
| 6785 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6786 | * object stack and a mark stack. Here we push a mark onto the |
| 6787 | * mark stack. |
| 6788 | */ |
| 6789 | |
Sergey Fedoseev | 86b8991 | 2018-08-25 12:54:40 +0500 | [diff] [blame] | 6790 | if (self->num_marks >= self->marks_size) { |
Sergey Fedoseev | 90555ec | 2018-08-25 15:41:58 +0500 | [diff] [blame] | 6791 | size_t alloc = ((size_t)self->num_marks << 1) + 20; |
| 6792 | Py_ssize_t *marks_new = self->marks; |
| 6793 | PyMem_RESIZE(marks_new, Py_ssize_t, alloc); |
| 6794 | if (marks_new == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6795 | PyErr_NoMemory(); |
| 6796 | return -1; |
| 6797 | } |
Sergey Fedoseev | 90555ec | 2018-08-25 15:41:58 +0500 | [diff] [blame] | 6798 | self->marks = marks_new; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6799 | self->marks_size = (Py_ssize_t)alloc; |
| 6800 | } |
| 6801 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6802 | self->stack->mark_set = 1; |
| 6803 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6804 | |
| 6805 | return 0; |
| 6806 | } |
| 6807 | |
| 6808 | static int |
| 6809 | load_reduce(UnpicklerObject *self) |
| 6810 | { |
| 6811 | PyObject *callable = NULL; |
| 6812 | PyObject *argtup = NULL; |
| 6813 | PyObject *obj = NULL; |
| 6814 | |
| 6815 | PDATA_POP(self->stack, argtup); |
| 6816 | if (argtup == NULL) |
| 6817 | return -1; |
| 6818 | PDATA_POP(self->stack, callable); |
| 6819 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6820 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6821 | Py_DECREF(callable); |
| 6822 | } |
| 6823 | Py_DECREF(argtup); |
| 6824 | |
| 6825 | if (obj == NULL) |
| 6826 | return -1; |
| 6827 | |
| 6828 | PDATA_PUSH(self->stack, obj, -1); |
| 6829 | return 0; |
| 6830 | } |
| 6831 | |
| 6832 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6833 | * is the first opcode for protocols >= 2. |
| 6834 | */ |
| 6835 | static int |
| 6836 | load_proto(UnpicklerObject *self) |
| 6837 | { |
| 6838 | char *s; |
| 6839 | int i; |
| 6840 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6841 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6842 | return -1; |
| 6843 | |
| 6844 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6845 | if (i <= HIGHEST_PROTOCOL) { |
| 6846 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6847 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6848 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6849 | |
| 6850 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6851 | return -1; |
| 6852 | } |
| 6853 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6854 | static int |
| 6855 | load_frame(UnpicklerObject *self) |
| 6856 | { |
| 6857 | char *s; |
| 6858 | Py_ssize_t frame_len; |
| 6859 | |
| 6860 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6861 | return -1; |
| 6862 | |
| 6863 | frame_len = calc_binsize(s, 8); |
| 6864 | if (frame_len < 0) { |
| 6865 | PyErr_Format(PyExc_OverflowError, |
| 6866 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6867 | PY_SSIZE_T_MAX); |
| 6868 | return -1; |
| 6869 | } |
| 6870 | |
| 6871 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6872 | return -1; |
| 6873 | |
| 6874 | /* Rewind to start of frame */ |
| 6875 | self->next_read_idx -= frame_len; |
| 6876 | return 0; |
| 6877 | } |
| 6878 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6879 | static PyObject * |
| 6880 | load(UnpicklerObject *self) |
| 6881 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6882 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6883 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6884 | |
| 6885 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6886 | self->stack->mark_set = 0; |
| 6887 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6888 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6889 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6890 | Pdata_clear(self->stack, 0); |
| 6891 | |
| 6892 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6893 | #define OP(opcode, load_func) \ |
| 6894 | case opcode: if (load_func(self) < 0) break; continue; |
| 6895 | |
| 6896 | #define OP_ARG(opcode, load_func, arg) \ |
| 6897 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6898 | |
| 6899 | while (1) { |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6900 | if (_Unpickler_Read(self, &s, 1) < 0) { |
| 6901 | PickleState *st = _Pickle_GetGlobalState(); |
| 6902 | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
| 6903 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 6904 | } |
| 6905 | return NULL; |
| 6906 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6907 | |
| 6908 | switch ((enum opcode)s[0]) { |
| 6909 | OP(NONE, load_none) |
| 6910 | OP(BININT, load_binint) |
| 6911 | OP(BININT1, load_binint1) |
| 6912 | OP(BININT2, load_binint2) |
| 6913 | OP(INT, load_int) |
| 6914 | OP(LONG, load_long) |
| 6915 | OP_ARG(LONG1, load_counted_long, 1) |
| 6916 | OP_ARG(LONG4, load_counted_long, 4) |
| 6917 | OP(FLOAT, load_float) |
| 6918 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6919 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6920 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6921 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 6922 | OP(BYTEARRAY8, load_counted_bytearray) |
| 6923 | OP(NEXT_BUFFER, load_next_buffer) |
| 6924 | OP(READONLY_BUFFER, load_readonly_buffer) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6925 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6926 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6927 | OP(STRING, load_string) |
| 6928 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6929 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6930 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6931 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6932 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6933 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6934 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6935 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6936 | OP(TUPLE, load_tuple) |
| 6937 | OP(EMPTY_LIST, load_empty_list) |
| 6938 | OP(LIST, load_list) |
| 6939 | OP(EMPTY_DICT, load_empty_dict) |
| 6940 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6941 | OP(EMPTY_SET, load_empty_set) |
| 6942 | OP(ADDITEMS, load_additems) |
| 6943 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6944 | OP(OBJ, load_obj) |
| 6945 | OP(INST, load_inst) |
| 6946 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6947 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6948 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6949 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6950 | OP(APPEND, load_append) |
| 6951 | OP(APPENDS, load_appends) |
| 6952 | OP(BUILD, load_build) |
| 6953 | OP(DUP, load_dup) |
| 6954 | OP(BINGET, load_binget) |
| 6955 | OP(LONG_BINGET, load_long_binget) |
| 6956 | OP(GET, load_get) |
| 6957 | OP(MARK, load_mark) |
| 6958 | OP(BINPUT, load_binput) |
| 6959 | OP(LONG_BINPUT, load_long_binput) |
| 6960 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6961 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6962 | OP(POP, load_pop) |
| 6963 | OP(POP_MARK, load_pop_mark) |
| 6964 | OP(SETITEM, load_setitem) |
| 6965 | OP(SETITEMS, load_setitems) |
| 6966 | OP(PERSID, load_persid) |
| 6967 | OP(BINPERSID, load_binpersid) |
| 6968 | OP(REDUCE, load_reduce) |
| 6969 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6970 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6971 | OP_ARG(EXT1, load_extension, 1) |
| 6972 | OP_ARG(EXT2, load_extension, 2) |
| 6973 | OP_ARG(EXT4, load_extension, 4) |
| 6974 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6975 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6976 | |
| 6977 | case STOP: |
| 6978 | break; |
| 6979 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6980 | default: |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6981 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6982 | PickleState *st = _Pickle_GetGlobalState(); |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6983 | unsigned char c = (unsigned char) *s; |
| 6984 | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
| 6985 | PyErr_Format(st->UnpicklingError, |
| 6986 | "invalid load key, '%c'.", c); |
| 6987 | } |
| 6988 | else { |
| 6989 | PyErr_Format(st->UnpicklingError, |
| 6990 | "invalid load key, '\\x%02x'.", c); |
| 6991 | } |
| 6992 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6993 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6994 | } |
| 6995 | |
| 6996 | break; /* and we are done! */ |
| 6997 | } |
| 6998 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6999 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7000 | return NULL; |
| 7001 | } |
| 7002 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 7003 | if (_Unpickler_SkipConsumed(self) < 0) |
| 7004 | return NULL; |
| 7005 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7006 | PDATA_POP(self->stack, value); |
| 7007 | return value; |
| 7008 | } |
| 7009 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7010 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7011 | |
| 7012 | _pickle.Unpickler.load |
| 7013 | |
| 7014 | Load a pickle. |
| 7015 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7016 | Read a pickled object representation from the open file object given |
| 7017 | in the constructor, and return the reconstituted object hierarchy |
| 7018 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7019 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7020 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7021 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 7022 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7023 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7024 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7025 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7026 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7027 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 7028 | segfaulting if a subclass overridden __init__ with a function that does |
| 7029 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 7030 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7031 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7032 | PickleState *st = _Pickle_GetGlobalState(); |
| 7033 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7034 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7035 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7036 | return NULL; |
| 7037 | } |
| 7038 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7039 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 7043 | function is used for loading any global (i.e., functions), not just |
| 7044 | classes. The name is kept only for backward compatibility. */ |
| 7045 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7046 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7047 | |
| 7048 | _pickle.Unpickler.find_class |
| 7049 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7050 | module_name: object |
| 7051 | global_name: object |
| 7052 | / |
| 7053 | |
| 7054 | Return an object from a specified module. |
| 7055 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7056 | If necessary, the module will be imported. Subclasses may override |
| 7057 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 7058 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7059 | |
| 7060 | This method is called whenever a class or a function object is |
| 7061 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7062 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7063 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7064 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7065 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 7066 | PyObject *module_name, |
| 7067 | PyObject *global_name) |
| 7068 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7069 | { |
| 7070 | PyObject *global; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7071 | PyObject *module; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7072 | |
Steve Dower | b82e17e | 2019-05-23 08:45:22 -0700 | [diff] [blame] | 7073 | if (PySys_Audit("pickle.find_class", "OO", |
| 7074 | module_name, global_name) < 0) { |
| 7075 | return NULL; |
| 7076 | } |
| 7077 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7078 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 7079 | Python 3.x. We do this only with old pickle protocols and when the |
| 7080 | user has not disabled the feature. */ |
| 7081 | if (self->proto < 3 && self->fix_imports) { |
| 7082 | PyObject *key; |
| 7083 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7084 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7085 | |
| 7086 | /* Check if the global (i.e., a function or a class) was renamed |
| 7087 | or moved to another module. */ |
| 7088 | key = PyTuple_Pack(2, module_name, global_name); |
| 7089 | if (key == NULL) |
| 7090 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7091 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7092 | Py_DECREF(key); |
| 7093 | if (item) { |
| 7094 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 7095 | PyErr_Format(PyExc_RuntimeError, |
| 7096 | "_compat_pickle.NAME_MAPPING values should be " |
| 7097 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 7098 | return NULL; |
| 7099 | } |
| 7100 | module_name = PyTuple_GET_ITEM(item, 0); |
| 7101 | global_name = PyTuple_GET_ITEM(item, 1); |
| 7102 | if (!PyUnicode_Check(module_name) || |
| 7103 | !PyUnicode_Check(global_name)) { |
| 7104 | PyErr_Format(PyExc_RuntimeError, |
| 7105 | "_compat_pickle.NAME_MAPPING values should be " |
| 7106 | "pairs of str, not (%.200s, %.200s)", |
| 7107 | Py_TYPE(module_name)->tp_name, |
| 7108 | Py_TYPE(global_name)->tp_name); |
| 7109 | return NULL; |
| 7110 | } |
| 7111 | } |
| 7112 | else if (PyErr_Occurred()) { |
| 7113 | return NULL; |
| 7114 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 7115 | else { |
| 7116 | /* Check if the module was renamed. */ |
| 7117 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 7118 | if (item) { |
| 7119 | if (!PyUnicode_Check(item)) { |
| 7120 | PyErr_Format(PyExc_RuntimeError, |
| 7121 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 7122 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 7123 | return NULL; |
| 7124 | } |
| 7125 | module_name = item; |
| 7126 | } |
| 7127 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7128 | return NULL; |
| 7129 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7130 | } |
| 7131 | } |
| 7132 | |
tjb900 | 4371c0a | 2019-02-18 23:30:51 +0800 | [diff] [blame] | 7133 | /* |
| 7134 | * we don't use PyImport_GetModule here, because it can return partially- |
| 7135 | * initialised modules, which then cause the getattribute to fail. |
| 7136 | */ |
| 7137 | module = PyImport_Import(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7138 | if (module == NULL) { |
tjb900 | 4371c0a | 2019-02-18 23:30:51 +0800 | [diff] [blame] | 7139 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7140 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 7141 | global = getattribute(module, global_name, self->proto >= 4); |
| 7142 | Py_DECREF(module); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7143 | return global; |
| 7144 | } |
| 7145 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 7146 | /*[clinic input] |
| 7147 | |
| 7148 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 7149 | |
| 7150 | Returns size in memory, in bytes. |
| 7151 | [clinic start generated code]*/ |
| 7152 | |
| 7153 | static Py_ssize_t |
| 7154 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 7155 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 7156 | { |
| 7157 | Py_ssize_t res; |
| 7158 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 7159 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 7160 | if (self->memo != NULL) |
| 7161 | res += self->memo_size * sizeof(PyObject *); |
| 7162 | if (self->marks != NULL) |
| 7163 | res += self->marks_size * sizeof(Py_ssize_t); |
| 7164 | if (self->input_line != NULL) |
| 7165 | res += strlen(self->input_line) + 1; |
| 7166 | if (self->encoding != NULL) |
| 7167 | res += strlen(self->encoding) + 1; |
| 7168 | if (self->errors != NULL) |
| 7169 | res += strlen(self->errors) + 1; |
| 7170 | return res; |
| 7171 | } |
| 7172 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7173 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7174 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 7175 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 7176 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7177 | {NULL, NULL} /* sentinel */ |
| 7178 | }; |
| 7179 | |
| 7180 | static void |
| 7181 | Unpickler_dealloc(UnpicklerObject *self) |
| 7182 | { |
| 7183 | PyObject_GC_UnTrack((PyObject *)self); |
| 7184 | Py_XDECREF(self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7185 | Py_XDECREF(self->readinto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7186 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 7187 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7188 | Py_XDECREF(self->stack); |
| 7189 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7190 | Py_XDECREF(self->buffers); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7191 | if (self->buffer.buf != NULL) { |
| 7192 | PyBuffer_Release(&self->buffer); |
| 7193 | self->buffer.buf = NULL; |
| 7194 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7195 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7196 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7197 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7198 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 7199 | PyMem_Free(self->encoding); |
| 7200 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7201 | |
| 7202 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 7203 | } |
| 7204 | |
| 7205 | static int |
| 7206 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 7207 | { |
| 7208 | Py_VISIT(self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7209 | Py_VISIT(self->readinto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7210 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 7211 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7212 | Py_VISIT(self->stack); |
| 7213 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7214 | Py_VISIT(self->buffers); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7215 | return 0; |
| 7216 | } |
| 7217 | |
| 7218 | static int |
| 7219 | Unpickler_clear(UnpicklerObject *self) |
| 7220 | { |
| 7221 | Py_CLEAR(self->readline); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7222 | Py_CLEAR(self->readinto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7223 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 7224 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7225 | Py_CLEAR(self->stack); |
| 7226 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7227 | Py_CLEAR(self->buffers); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7228 | if (self->buffer.buf != NULL) { |
| 7229 | PyBuffer_Release(&self->buffer); |
| 7230 | self->buffer.buf = NULL; |
| 7231 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7232 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7233 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7234 | PyMem_Free(self->marks); |
| 7235 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7236 | PyMem_Free(self->input_line); |
| 7237 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 7238 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7239 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 7240 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7241 | self->errors = NULL; |
| 7242 | |
| 7243 | return 0; |
| 7244 | } |
| 7245 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7246 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7247 | |
| 7248 | _pickle.Unpickler.__init__ |
| 7249 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7250 | file: object |
| 7251 | * |
| 7252 | fix_imports: bool = True |
| 7253 | encoding: str = 'ASCII' |
| 7254 | errors: str = 'strict' |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7255 | buffers: object(c_default="NULL") = () |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7256 | |
| 7257 | This takes a binary file for reading a pickle data stream. |
| 7258 | |
| 7259 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7260 | protocol argument is needed. Bytes past the pickled object's |
| 7261 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7262 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7263 | The argument *file* must have two methods, a read() method that takes |
| 7264 | an integer argument, and a readline() method that requires no |
| 7265 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7266 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7267 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7268 | |
| 7269 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7270 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7271 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7272 | 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] | 7273 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7274 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7275 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7276 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7277 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7278 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7279 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7280 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 7281 | int fix_imports, const char *encoding, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7282 | const char *errors, PyObject *buffers) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7283 | /*[clinic end generated code: output=09f0192649ea3f85 input=ca4c1faea9553121]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7284 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 7285 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7286 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7287 | /* In case of multiple __init__() calls, clear previous content. */ |
| 7288 | if (self->read != NULL) |
| 7289 | (void)Unpickler_clear(self); |
| 7290 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7291 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7292 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7293 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7294 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7295 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7296 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7297 | if (_Unpickler_SetBuffers(self, buffers) < 0) |
| 7298 | return -1; |
| 7299 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7300 | self->fix_imports = fix_imports; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7301 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7302 | if (init_method_ref((PyObject *)self, &PyId_persistent_load, |
| 7303 | &self->pers_func, &self->pers_func_self) < 0) |
| 7304 | { |
| 7305 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7306 | } |
| 7307 | |
| 7308 | self->stack = (Pdata *)Pdata_New(); |
| 7309 | if (self->stack == NULL) |
Zackery Spytz | 4b430e5 | 2018-09-28 23:48:46 -0600 | [diff] [blame] | 7310 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7311 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7312 | self->memo_size = 32; |
| 7313 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7314 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7315 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7316 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 7317 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 7318 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7319 | return 0; |
| 7320 | } |
| 7321 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7322 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7323 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 7324 | * avoid breaking code like: |
| 7325 | * unpickler.memo.clear() |
| 7326 | * and |
| 7327 | * unpickler.memo = saved_memo |
| 7328 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 7329 | * it. Note that we don't implement the entire mapping API here. This is |
| 7330 | * intentional, as these should be treated as black-box implementation details. |
| 7331 | * |
| 7332 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 7333 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7334 | */ |
| 7335 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7336 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7337 | _pickle.UnpicklerMemoProxy.clear |
| 7338 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7339 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7340 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7341 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7342 | static PyObject * |
| 7343 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7344 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7345 | { |
| 7346 | _Unpickler_MemoCleanup(self->unpickler); |
| 7347 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 7348 | if (self->unpickler->memo == NULL) |
| 7349 | return NULL; |
| 7350 | Py_RETURN_NONE; |
| 7351 | } |
| 7352 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7353 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7354 | _pickle.UnpicklerMemoProxy.copy |
| 7355 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7356 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7357 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7358 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7359 | static PyObject * |
| 7360 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7361 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7362 | { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7363 | size_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7364 | PyObject *new_memo = PyDict_New(); |
| 7365 | if (new_memo == NULL) |
| 7366 | return NULL; |
| 7367 | |
| 7368 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 7369 | int status; |
| 7370 | PyObject *key, *value; |
| 7371 | |
| 7372 | value = self->unpickler->memo[i]; |
| 7373 | if (value == NULL) |
| 7374 | continue; |
| 7375 | |
| 7376 | key = PyLong_FromSsize_t(i); |
| 7377 | if (key == NULL) |
| 7378 | goto error; |
| 7379 | status = PyDict_SetItem(new_memo, key, value); |
| 7380 | Py_DECREF(key); |
| 7381 | if (status < 0) |
| 7382 | goto error; |
| 7383 | } |
| 7384 | return new_memo; |
| 7385 | |
| 7386 | error: |
| 7387 | Py_DECREF(new_memo); |
| 7388 | return NULL; |
| 7389 | } |
| 7390 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7391 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7392 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 7393 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7394 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7395 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7396 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7397 | static PyObject * |
| 7398 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 7399 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7400 | { |
| 7401 | PyObject *reduce_value; |
| 7402 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 7403 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7404 | if (contents == NULL) |
| 7405 | return NULL; |
| 7406 | |
| 7407 | reduce_value = PyTuple_New(2); |
| 7408 | if (reduce_value == NULL) { |
| 7409 | Py_DECREF(contents); |
| 7410 | return NULL; |
| 7411 | } |
| 7412 | constructor_args = PyTuple_New(1); |
| 7413 | if (constructor_args == NULL) { |
| 7414 | Py_DECREF(contents); |
| 7415 | Py_DECREF(reduce_value); |
| 7416 | return NULL; |
| 7417 | } |
| 7418 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 7419 | Py_INCREF((PyObject *)&PyDict_Type); |
| 7420 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 7421 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 7422 | return reduce_value; |
| 7423 | } |
| 7424 | |
| 7425 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7426 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 7427 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 7428 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7429 | {NULL, NULL} /* sentinel */ |
| 7430 | }; |
| 7431 | |
| 7432 | static void |
| 7433 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 7434 | { |
| 7435 | PyObject_GC_UnTrack(self); |
| 7436 | Py_XDECREF(self->unpickler); |
| 7437 | PyObject_GC_Del((PyObject *)self); |
| 7438 | } |
| 7439 | |
| 7440 | static int |
| 7441 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 7442 | visitproc visit, void *arg) |
| 7443 | { |
| 7444 | Py_VISIT(self->unpickler); |
| 7445 | return 0; |
| 7446 | } |
| 7447 | |
| 7448 | static int |
| 7449 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 7450 | { |
| 7451 | Py_CLEAR(self->unpickler); |
| 7452 | return 0; |
| 7453 | } |
| 7454 | |
| 7455 | static PyTypeObject UnpicklerMemoProxyType = { |
| 7456 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7457 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 7458 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 7459 | 0, |
| 7460 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7461 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7462 | 0, /* tp_getattr */ |
| 7463 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7464 | 0, /* tp_as_async */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7465 | 0, /* tp_repr */ |
| 7466 | 0, /* tp_as_number */ |
| 7467 | 0, /* tp_as_sequence */ |
| 7468 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 7469 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7470 | 0, /* tp_call */ |
| 7471 | 0, /* tp_str */ |
| 7472 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 7473 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 7474 | 0, /* tp_as_buffer */ |
| 7475 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 7476 | 0, /* tp_doc */ |
| 7477 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 7478 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 7479 | 0, /* tp_richcompare */ |
| 7480 | 0, /* tp_weaklistoffset */ |
| 7481 | 0, /* tp_iter */ |
| 7482 | 0, /* tp_iternext */ |
| 7483 | unpicklerproxy_methods, /* tp_methods */ |
| 7484 | }; |
| 7485 | |
| 7486 | static PyObject * |
| 7487 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 7488 | { |
| 7489 | UnpicklerMemoProxyObject *self; |
| 7490 | |
| 7491 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 7492 | &UnpicklerMemoProxyType); |
| 7493 | if (self == NULL) |
| 7494 | return NULL; |
| 7495 | Py_INCREF(unpickler); |
| 7496 | self->unpickler = unpickler; |
| 7497 | PyObject_GC_Track(self); |
| 7498 | return (PyObject *)self; |
| 7499 | } |
| 7500 | |
| 7501 | /*****************************************************************************/ |
| 7502 | |
| 7503 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7504 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7505 | Unpickler_get_memo(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7506 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7507 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7508 | } |
| 7509 | |
| 7510 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7511 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7512 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7513 | PyObject **new_memo; |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7514 | size_t new_memo_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7515 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7516 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7517 | PyErr_SetString(PyExc_TypeError, |
| 7518 | "attribute deletion is not supported"); |
| 7519 | return -1; |
| 7520 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7521 | |
Andy Lester | dffe4c0 | 2020-03-04 07:15:20 -0600 | [diff] [blame] | 7522 | if (Py_IS_TYPE(obj, &UnpicklerMemoProxyType)) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7523 | UnpicklerObject *unpickler = |
| 7524 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 7525 | |
| 7526 | new_memo_size = unpickler->memo_size; |
| 7527 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7528 | if (new_memo == NULL) |
| 7529 | return -1; |
| 7530 | |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7531 | for (size_t i = 0; i < new_memo_size; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7532 | Py_XINCREF(unpickler->memo[i]); |
| 7533 | new_memo[i] = unpickler->memo[i]; |
| 7534 | } |
| 7535 | } |
| 7536 | else if (PyDict_Check(obj)) { |
| 7537 | Py_ssize_t i = 0; |
| 7538 | PyObject *key, *value; |
| 7539 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 7540 | new_memo_size = PyDict_GET_SIZE(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7541 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 7542 | if (new_memo == NULL) |
| 7543 | return -1; |
| 7544 | |
| 7545 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 7546 | Py_ssize_t idx; |
| 7547 | if (!PyLong_Check(key)) { |
| 7548 | PyErr_SetString(PyExc_TypeError, |
| 7549 | "memo key must be integers"); |
| 7550 | goto error; |
| 7551 | } |
| 7552 | idx = PyLong_AsSsize_t(key); |
| 7553 | if (idx == -1 && PyErr_Occurred()) |
| 7554 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7555 | if (idx < 0) { |
| 7556 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 7557 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7558 | goto error; |
| 7559 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7560 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 7561 | goto error; |
| 7562 | } |
| 7563 | } |
| 7564 | else { |
| 7565 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | 34fd4c2 | 2018-11-05 16:20:25 +0200 | [diff] [blame] | 7566 | "'memo' attribute must be an UnpicklerMemoProxy object " |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7567 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7568 | return -1; |
| 7569 | } |
| 7570 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7571 | _Unpickler_MemoCleanup(self); |
| 7572 | self->memo_size = new_memo_size; |
| 7573 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7574 | |
| 7575 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7576 | |
| 7577 | error: |
| 7578 | if (new_memo_size) { |
Benjamin Peterson | a4ae828 | 2018-09-20 18:36:40 -0700 | [diff] [blame] | 7579 | for (size_t i = new_memo_size - 1; i != SIZE_MAX; i--) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7580 | Py_XDECREF(new_memo[i]); |
| 7581 | } |
| 7582 | PyMem_FREE(new_memo); |
| 7583 | } |
| 7584 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
| 7587 | static PyObject * |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7588 | Unpickler_get_persload(UnpicklerObject *self, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7589 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7590 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7591 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7592 | return NULL; |
| 7593 | } |
| 7594 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7595 | } |
| 7596 | |
| 7597 | static int |
Serhiy Storchaka | d4f9cf5 | 2018-11-27 19:34:35 +0200 | [diff] [blame] | 7598 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value, void *Py_UNUSED(ignored)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7599 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7600 | if (value == NULL) { |
| 7601 | PyErr_SetString(PyExc_TypeError, |
| 7602 | "attribute deletion is not supported"); |
| 7603 | return -1; |
| 7604 | } |
| 7605 | if (!PyCallable_Check(value)) { |
| 7606 | PyErr_SetString(PyExc_TypeError, |
| 7607 | "persistent_load must be a callable taking " |
| 7608 | "one argument"); |
| 7609 | return -1; |
| 7610 | } |
| 7611 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7612 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7613 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 7614 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7615 | |
| 7616 | return 0; |
| 7617 | } |
| 7618 | |
| 7619 | static PyGetSetDef Unpickler_getsets[] = { |
| 7620 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 7621 | {"persistent_load", (getter)Unpickler_get_persload, |
| 7622 | (setter)Unpickler_set_persload}, |
| 7623 | {NULL} |
| 7624 | }; |
| 7625 | |
| 7626 | static PyTypeObject Unpickler_Type = { |
| 7627 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7628 | "_pickle.Unpickler", /*tp_name*/ |
| 7629 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 7630 | 0, /*tp_itemsize*/ |
| 7631 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7632 | 0, /*tp_vectorcall_offset*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7633 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7634 | 0, /*tp_setattr*/ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 7635 | 0, /*tp_as_async*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7636 | 0, /*tp_repr*/ |
| 7637 | 0, /*tp_as_number*/ |
| 7638 | 0, /*tp_as_sequence*/ |
| 7639 | 0, /*tp_as_mapping*/ |
| 7640 | 0, /*tp_hash*/ |
| 7641 | 0, /*tp_call*/ |
| 7642 | 0, /*tp_str*/ |
| 7643 | 0, /*tp_getattro*/ |
| 7644 | 0, /*tp_setattro*/ |
| 7645 | 0, /*tp_as_buffer*/ |
| 7646 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7647 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7648 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7649 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7650 | 0, /*tp_richcompare*/ |
| 7651 | 0, /*tp_weaklistoffset*/ |
| 7652 | 0, /*tp_iter*/ |
| 7653 | 0, /*tp_iternext*/ |
| 7654 | Unpickler_methods, /*tp_methods*/ |
| 7655 | 0, /*tp_members*/ |
| 7656 | Unpickler_getsets, /*tp_getset*/ |
| 7657 | 0, /*tp_base*/ |
| 7658 | 0, /*tp_dict*/ |
| 7659 | 0, /*tp_descr_get*/ |
| 7660 | 0, /*tp_descr_set*/ |
| 7661 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7662 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7663 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7664 | PyType_GenericNew, /*tp_new*/ |
| 7665 | PyObject_GC_Del, /*tp_free*/ |
| 7666 | 0, /*tp_is_gc*/ |
| 7667 | }; |
| 7668 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7669 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7670 | |
| 7671 | _pickle.dump |
| 7672 | |
| 7673 | obj: object |
| 7674 | file: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7675 | protocol: object = None |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7676 | * |
| 7677 | fix_imports: bool = True |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7678 | buffer_callback: object = None |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7679 | |
| 7680 | Write a pickled representation of obj to the open file object file. |
| 7681 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7682 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7683 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7684 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7685 | The optional *protocol* argument tells the pickler to use the given |
Mark Dickinson | e9652e8 | 2020-01-24 10:03:22 +0000 | [diff] [blame] | 7686 | protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default |
| 7687 | protocol is 4. It was introduced in Python 3.4, and is incompatible |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7688 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7689 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7690 | Specifying a negative protocol version selects the highest protocol |
| 7691 | version supported. The higher the protocol used, the more recent the |
| 7692 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7693 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7694 | The *file* argument must have a write() method that accepts a single |
| 7695 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7696 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7697 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7698 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7699 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7700 | to map the new Python 3 names to the old module names used in Python |
| 7701 | 2, so that the pickle data stream is readable with Python 2. |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7702 | |
| 7703 | If *buffer_callback* is None (the default), buffer views are serialized |
| 7704 | into *file* as part of the pickle stream. It is an error if |
| 7705 | *buffer_callback* is not None and *protocol* is None or smaller than 5. |
| 7706 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7707 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7708 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7709 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7710 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7711 | PyObject *protocol, int fix_imports, |
| 7712 | PyObject *buffer_callback) |
Mark Dickinson | e9652e8 | 2020-01-24 10:03:22 +0000 | [diff] [blame] | 7713 | /*[clinic end generated code: output=706186dba996490c input=5ed6653da99cd97c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7714 | { |
| 7715 | PicklerObject *pickler = _Pickler_New(); |
| 7716 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7717 | if (pickler == NULL) |
| 7718 | return NULL; |
| 7719 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7720 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7721 | goto error; |
| 7722 | |
| 7723 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7724 | goto error; |
| 7725 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7726 | if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0) |
| 7727 | goto error; |
| 7728 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7729 | if (dump(pickler, obj) < 0) |
| 7730 | goto error; |
| 7731 | |
| 7732 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7733 | goto error; |
| 7734 | |
| 7735 | Py_DECREF(pickler); |
| 7736 | Py_RETURN_NONE; |
| 7737 | |
| 7738 | error: |
| 7739 | Py_XDECREF(pickler); |
| 7740 | return NULL; |
| 7741 | } |
| 7742 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7743 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7744 | |
| 7745 | _pickle.dumps |
| 7746 | |
| 7747 | obj: object |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7748 | protocol: object = None |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7749 | * |
| 7750 | fix_imports: bool = True |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7751 | buffer_callback: object = None |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7752 | |
| 7753 | Return the pickled representation of the object as a bytes object. |
| 7754 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7755 | The optional *protocol* argument tells the pickler to use the given |
Mark Dickinson | e9652e8 | 2020-01-24 10:03:22 +0000 | [diff] [blame] | 7756 | protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default |
| 7757 | protocol is 4. It was introduced in Python 3.4, and is incompatible |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 7758 | with previous versions. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7759 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7760 | Specifying a negative protocol version selects the highest protocol |
| 7761 | version supported. The higher the protocol used, the more recent the |
| 7762 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7763 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7764 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7765 | try to map the new Python 3 names to the old module names used in |
| 7766 | 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] | 7767 | |
| 7768 | If *buffer_callback* is None (the default), buffer views are serialized |
| 7769 | into *file* as part of the pickle stream. It is an error if |
| 7770 | *buffer_callback* is not None and *protocol* is None or smaller than 5. |
| 7771 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7772 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7773 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7774 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7775 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7776 | int fix_imports, PyObject *buffer_callback) |
Mark Dickinson | e9652e8 | 2020-01-24 10:03:22 +0000 | [diff] [blame] | 7777 | /*[clinic end generated code: output=fbab0093a5580fdf input=e543272436c6f987]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7778 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7779 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7780 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7781 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7782 | if (pickler == NULL) |
| 7783 | return NULL; |
| 7784 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7785 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7786 | goto error; |
| 7787 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7788 | if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0) |
| 7789 | goto error; |
| 7790 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7791 | if (dump(pickler, obj) < 0) |
| 7792 | goto error; |
| 7793 | |
| 7794 | result = _Pickler_GetString(pickler); |
| 7795 | Py_DECREF(pickler); |
| 7796 | return result; |
| 7797 | |
| 7798 | error: |
| 7799 | Py_XDECREF(pickler); |
| 7800 | return NULL; |
| 7801 | } |
| 7802 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7803 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7804 | |
| 7805 | _pickle.load |
| 7806 | |
| 7807 | file: object |
| 7808 | * |
| 7809 | fix_imports: bool = True |
| 7810 | encoding: str = 'ASCII' |
| 7811 | errors: str = 'strict' |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7812 | buffers: object(c_default="NULL") = () |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7813 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7814 | 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] | 7815 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7816 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7817 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7818 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7819 | The protocol version of the pickle is detected automatically, so no |
| 7820 | protocol argument is needed. Bytes past the pickled object's |
| 7821 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7822 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7823 | The argument *file* must have two methods, a read() method that takes |
| 7824 | an integer argument, and a readline() method that requires no |
| 7825 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7826 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7827 | other custom object that meets this interface. |
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 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7830 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7831 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7832 | map the old Python 2 names to the new names used in Python 3. The |
| 7833 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7834 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7835 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7836 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7837 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7838 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7839 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7840 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7841 | const char *encoding, const char *errors, |
| 7842 | PyObject *buffers) |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7843 | /*[clinic end generated code: output=250452d141c23e76 input=46c7c31c92f4f371]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7844 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7845 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7846 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7847 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7848 | if (unpickler == NULL) |
| 7849 | return NULL; |
| 7850 | |
| 7851 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7852 | goto error; |
| 7853 | |
| 7854 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7855 | goto error; |
| 7856 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7857 | if (_Unpickler_SetBuffers(unpickler, buffers) < 0) |
| 7858 | goto error; |
| 7859 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7860 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7861 | |
| 7862 | result = load(unpickler); |
| 7863 | Py_DECREF(unpickler); |
| 7864 | return result; |
| 7865 | |
| 7866 | error: |
| 7867 | Py_XDECREF(unpickler); |
| 7868 | return NULL; |
| 7869 | } |
| 7870 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7871 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7872 | |
| 7873 | _pickle.loads |
| 7874 | |
| 7875 | data: object |
Serhiy Storchaka | 531d1e5 | 2020-05-02 09:38:01 +0300 | [diff] [blame] | 7876 | / |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7877 | * |
| 7878 | fix_imports: bool = True |
| 7879 | encoding: str = 'ASCII' |
| 7880 | errors: str = 'strict' |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 7881 | buffers: object(c_default="NULL") = () |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7882 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7883 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7884 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7885 | The protocol version of the pickle is detected automatically, so no |
| 7886 | protocol argument is needed. Bytes past the pickled object's |
| 7887 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7888 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7889 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7890 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7891 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7892 | map the old Python 2 names to the new names used in Python 3. The |
| 7893 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7894 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7895 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7896 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7897 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7898 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7899 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7900 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7901 | const char *encoding, const char *errors, |
| 7902 | PyObject *buffers) |
Serhiy Storchaka | 531d1e5 | 2020-05-02 09:38:01 +0300 | [diff] [blame] | 7903 | /*[clinic end generated code: output=82ac1e6b588e6d02 input=b3615540d0535087]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7904 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7905 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7906 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7907 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7908 | if (unpickler == NULL) |
| 7909 | return NULL; |
| 7910 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7911 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7912 | goto error; |
| 7913 | |
| 7914 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7915 | goto error; |
| 7916 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 7917 | if (_Unpickler_SetBuffers(unpickler, buffers) < 0) |
| 7918 | goto error; |
| 7919 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7920 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7921 | |
| 7922 | result = load(unpickler); |
| 7923 | Py_DECREF(unpickler); |
| 7924 | return result; |
| 7925 | |
| 7926 | error: |
| 7927 | Py_XDECREF(unpickler); |
| 7928 | return NULL; |
| 7929 | } |
| 7930 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7931 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7932 | _PICKLE_DUMP_METHODDEF |
| 7933 | _PICKLE_DUMPS_METHODDEF |
| 7934 | _PICKLE_LOAD_METHODDEF |
| 7935 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7936 | {NULL, NULL} /* sentinel */ |
| 7937 | }; |
| 7938 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7939 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7940 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7941 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7942 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7943 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7944 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7945 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7946 | static void |
| 7947 | pickle_free(PyObject *m) |
| 7948 | { |
| 7949 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7950 | } |
| 7951 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7952 | static int |
| 7953 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7954 | { |
| 7955 | PickleState *st = _Pickle_GetState(m); |
| 7956 | Py_VISIT(st->PickleError); |
| 7957 | Py_VISIT(st->PicklingError); |
| 7958 | Py_VISIT(st->UnpicklingError); |
| 7959 | Py_VISIT(st->dispatch_table); |
| 7960 | Py_VISIT(st->extension_registry); |
| 7961 | Py_VISIT(st->extension_cache); |
| 7962 | Py_VISIT(st->inverted_registry); |
| 7963 | Py_VISIT(st->name_mapping_2to3); |
| 7964 | Py_VISIT(st->import_mapping_2to3); |
| 7965 | Py_VISIT(st->name_mapping_3to2); |
| 7966 | Py_VISIT(st->import_mapping_3to2); |
| 7967 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7968 | Py_VISIT(st->getattr); |
Hai Shi | 1f577ce | 2020-03-02 14:28:44 +0800 | [diff] [blame] | 7969 | Py_VISIT(st->partial); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7970 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7971 | } |
| 7972 | |
| 7973 | static struct PyModuleDef _picklemodule = { |
| 7974 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7975 | "_pickle", /* m_name */ |
| 7976 | pickle_module_doc, /* m_doc */ |
| 7977 | sizeof(PickleState), /* m_size */ |
| 7978 | pickle_methods, /* m_methods */ |
| 7979 | NULL, /* m_reload */ |
| 7980 | pickle_traverse, /* m_traverse */ |
| 7981 | pickle_clear, /* m_clear */ |
| 7982 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7983 | }; |
| 7984 | |
| 7985 | PyMODINIT_FUNC |
| 7986 | PyInit__pickle(void) |
| 7987 | { |
| 7988 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7989 | PickleState *st; |
| 7990 | |
| 7991 | m = PyState_FindModule(&_picklemodule); |
| 7992 | if (m) { |
| 7993 | Py_INCREF(m); |
| 7994 | return m; |
| 7995 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7996 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7997 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7998 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7999 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 8000 | return NULL; |
| 8001 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 8002 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8003 | |
| 8004 | /* Create the module and add the functions. */ |
| 8005 | m = PyModule_Create(&_picklemodule); |
| 8006 | if (m == NULL) |
| 8007 | return NULL; |
| 8008 | |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 8009 | /* Add types */ |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 8010 | if (PyModule_AddType(m, &Pickler_Type) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8011 | return NULL; |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 8012 | } |
| 8013 | if (PyModule_AddType(m, &Unpickler_Type) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8014 | return NULL; |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 8015 | } |
| 8016 | if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) { |
Antoine Pitrou | 91f4380 | 2019-05-26 17:10:09 +0200 | [diff] [blame] | 8017 | return NULL; |
Dong-hee Na | 37fcbb6 | 2020-03-25 07:08:51 +0900 | [diff] [blame] | 8018 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8019 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8020 | st = _Pickle_GetState(m); |
| 8021 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8022 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8023 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 8024 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8025 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8026 | st->PicklingError = \ |
| 8027 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 8028 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8029 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8030 | st->UnpicklingError = \ |
| 8031 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 8032 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8033 | return NULL; |
| 8034 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8035 | Py_INCREF(st->PickleError); |
| 8036 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8037 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8038 | Py_INCREF(st->PicklingError); |
| 8039 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8040 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8041 | Py_INCREF(st->UnpicklingError); |
| 8042 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8043 | return NULL; |
| 8044 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 8045 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8046 | return NULL; |
| 8047 | |
| 8048 | return m; |
| 8049 | } |