Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 1 | |
| 2 | /* Core extension modules are built-in on some platforms (e.g. Windows). */ |
| 3 | #ifdef Py_BUILD_CORE |
Eric Snow | fc1bf87 | 2017-09-11 18:30:43 -0700 | [diff] [blame] | 4 | #define Py_BUILD_CORE_BUILTIN |
Eric Snow | 2ebc5ce | 2017-09-07 23:51:28 -0600 | [diff] [blame] | 5 | #undef Py_BUILD_CORE |
| 6 | #endif |
| 7 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 8 | #include "Python.h" |
| 9 | #include "structmember.h" |
| 10 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 11 | PyDoc_STRVAR(pickle_module_doc, |
| 12 | "Optimized C implementation for the Python pickle module."); |
| 13 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 14 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 15 | module _pickle |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 16 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 17 | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
| 18 | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
| 19 | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 20 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 21 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 22 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 23 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 24 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 25 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 26 | DEFAULT_PROTOCOL = 3 |
| 27 | }; |
| 28 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 29 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 30 | Extensive docs are in pickletools.py. */ |
| 31 | enum opcode { |
| 32 | MARK = '(', |
| 33 | STOP = '.', |
| 34 | POP = '0', |
| 35 | POP_MARK = '1', |
| 36 | DUP = '2', |
| 37 | FLOAT = 'F', |
| 38 | INT = 'I', |
| 39 | BININT = 'J', |
| 40 | BININT1 = 'K', |
| 41 | LONG = 'L', |
| 42 | BININT2 = 'M', |
| 43 | NONE = 'N', |
| 44 | PERSID = 'P', |
| 45 | BINPERSID = 'Q', |
| 46 | REDUCE = 'R', |
| 47 | STRING = 'S', |
| 48 | BINSTRING = 'T', |
| 49 | SHORT_BINSTRING = 'U', |
| 50 | UNICODE = 'V', |
| 51 | BINUNICODE = 'X', |
| 52 | APPEND = 'a', |
| 53 | BUILD = 'b', |
| 54 | GLOBAL = 'c', |
| 55 | DICT = 'd', |
| 56 | EMPTY_DICT = '}', |
| 57 | APPENDS = 'e', |
| 58 | GET = 'g', |
| 59 | BINGET = 'h', |
| 60 | INST = 'i', |
| 61 | LONG_BINGET = 'j', |
| 62 | LIST = 'l', |
| 63 | EMPTY_LIST = ']', |
| 64 | OBJ = 'o', |
| 65 | PUT = 'p', |
| 66 | BINPUT = 'q', |
| 67 | LONG_BINPUT = 'r', |
| 68 | SETITEM = 's', |
| 69 | TUPLE = 't', |
| 70 | EMPTY_TUPLE = ')', |
| 71 | SETITEMS = 'u', |
| 72 | BINFLOAT = 'G', |
| 73 | |
| 74 | /* Protocol 2. */ |
| 75 | PROTO = '\x80', |
| 76 | NEWOBJ = '\x81', |
| 77 | EXT1 = '\x82', |
| 78 | EXT2 = '\x83', |
| 79 | EXT4 = '\x84', |
| 80 | TUPLE1 = '\x85', |
| 81 | TUPLE2 = '\x86', |
| 82 | TUPLE3 = '\x87', |
| 83 | NEWTRUE = '\x88', |
| 84 | NEWFALSE = '\x89', |
| 85 | LONG1 = '\x8a', |
| 86 | LONG4 = '\x8b', |
| 87 | |
| 88 | /* Protocol 3 (Python 3.x) */ |
| 89 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 90 | SHORT_BINBYTES = 'C', |
| 91 | |
| 92 | /* Protocol 4 */ |
| 93 | SHORT_BINUNICODE = '\x8c', |
| 94 | BINUNICODE8 = '\x8d', |
| 95 | BINBYTES8 = '\x8e', |
| 96 | EMPTY_SET = '\x8f', |
| 97 | ADDITEMS = '\x90', |
| 98 | FROZENSET = '\x91', |
| 99 | NEWOBJ_EX = '\x92', |
| 100 | STACK_GLOBAL = '\x93', |
| 101 | MEMOIZE = '\x94', |
| 102 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 103 | }; |
| 104 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 105 | enum { |
| 106 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 107 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 108 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 109 | help anything either. */ |
| 110 | BATCHSIZE = 1000, |
| 111 | |
| 112 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 113 | checking for self-referential data-structures. */ |
| 114 | FAST_NESTING_LIMIT = 50, |
| 115 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 116 | /* Initial size of the write buffer of Pickler. */ |
| 117 | WRITE_BUF_SIZE = 4096, |
| 118 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 119 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 120 | PREFETCH = 8192 * 16, |
| 121 | |
| 122 | FRAME_SIZE_TARGET = 64 * 1024, |
| 123 | |
| 124 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 127 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 128 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 129 | /* State of the pickle module, per PEP 3121. */ |
| 130 | typedef struct { |
| 131 | /* Exception classes for pickle. */ |
| 132 | PyObject *PickleError; |
| 133 | PyObject *PicklingError; |
| 134 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 135 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 136 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 137 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 138 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 139 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 140 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 141 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 142 | PyObject *extension_registry; |
| 143 | /* copyreg._extension_cache, {code: object} */ |
| 144 | PyObject *extension_cache; |
| 145 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 146 | PyObject *inverted_registry; |
| 147 | |
| 148 | /* Import mappings for compatibility with Python 2.x */ |
| 149 | |
| 150 | /* _compat_pickle.NAME_MAPPING, |
| 151 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 152 | PyObject *name_mapping_2to3; |
| 153 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 154 | PyObject *import_mapping_2to3; |
| 155 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 156 | PyObject *name_mapping_3to2; |
| 157 | PyObject *import_mapping_3to2; |
| 158 | |
| 159 | /* codecs.encode, used for saving bytes in older protocols */ |
| 160 | PyObject *codecs_encode; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 161 | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
| 162 | PyObject *getattr; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 163 | /* functools.partial, used for implementing __newobj_ex__ with protocols |
| 164 | 2 and 3 */ |
| 165 | PyObject *partial; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 166 | } PickleState; |
| 167 | |
| 168 | /* Forward declaration of the _pickle module definition. */ |
| 169 | static struct PyModuleDef _picklemodule; |
| 170 | |
| 171 | /* Given a module object, get its per-module state. */ |
| 172 | static PickleState * |
| 173 | _Pickle_GetState(PyObject *module) |
| 174 | { |
| 175 | return (PickleState *)PyModule_GetState(module); |
| 176 | } |
| 177 | |
| 178 | /* Find the module instance imported in the currently running sub-interpreter |
| 179 | and get its state. */ |
| 180 | static PickleState * |
| 181 | _Pickle_GetGlobalState(void) |
| 182 | { |
| 183 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 184 | } |
| 185 | |
| 186 | /* Clear the given pickle module state. */ |
| 187 | static void |
| 188 | _Pickle_ClearState(PickleState *st) |
| 189 | { |
| 190 | Py_CLEAR(st->PickleError); |
| 191 | Py_CLEAR(st->PicklingError); |
| 192 | Py_CLEAR(st->UnpicklingError); |
| 193 | Py_CLEAR(st->dispatch_table); |
| 194 | Py_CLEAR(st->extension_registry); |
| 195 | Py_CLEAR(st->extension_cache); |
| 196 | Py_CLEAR(st->inverted_registry); |
| 197 | Py_CLEAR(st->name_mapping_2to3); |
| 198 | Py_CLEAR(st->import_mapping_2to3); |
| 199 | Py_CLEAR(st->name_mapping_3to2); |
| 200 | Py_CLEAR(st->import_mapping_3to2); |
| 201 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 202 | Py_CLEAR(st->getattr); |
Victor Stinner | 9ba97df | 2015-11-17 12:15:07 +0100 | [diff] [blame] | 203 | Py_CLEAR(st->partial); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* Initialize the given pickle module state. */ |
| 207 | static int |
| 208 | _Pickle_InitState(PickleState *st) |
| 209 | { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 210 | PyObject *builtins; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 211 | PyObject *copyreg = NULL; |
| 212 | PyObject *compat_pickle = NULL; |
| 213 | PyObject *codecs = NULL; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 214 | PyObject *functools = NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 215 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 216 | builtins = PyEval_GetBuiltins(); |
| 217 | if (builtins == NULL) |
| 218 | goto error; |
| 219 | st->getattr = PyDict_GetItemString(builtins, "getattr"); |
| 220 | if (st->getattr == NULL) |
| 221 | goto error; |
| 222 | Py_INCREF(st->getattr); |
| 223 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 224 | copyreg = PyImport_ImportModule("copyreg"); |
| 225 | if (!copyreg) |
| 226 | goto error; |
| 227 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 228 | if (!st->dispatch_table) |
| 229 | goto error; |
| 230 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 231 | PyErr_Format(PyExc_RuntimeError, |
| 232 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 233 | Py_TYPE(st->dispatch_table)->tp_name); |
| 234 | goto error; |
| 235 | } |
| 236 | st->extension_registry = \ |
| 237 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 238 | if (!st->extension_registry) |
| 239 | goto error; |
| 240 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 241 | PyErr_Format(PyExc_RuntimeError, |
| 242 | "copyreg._extension_registry should be a dict, " |
| 243 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 244 | goto error; |
| 245 | } |
| 246 | st->inverted_registry = \ |
| 247 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 248 | if (!st->inverted_registry) |
| 249 | goto error; |
| 250 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 251 | PyErr_Format(PyExc_RuntimeError, |
| 252 | "copyreg._inverted_registry should be a dict, " |
| 253 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 254 | goto error; |
| 255 | } |
| 256 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 257 | if (!st->extension_cache) |
| 258 | goto error; |
| 259 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 260 | PyErr_Format(PyExc_RuntimeError, |
| 261 | "copyreg._extension_cache should be a dict, " |
| 262 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 263 | goto error; |
| 264 | } |
| 265 | Py_CLEAR(copyreg); |
| 266 | |
| 267 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 268 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 269 | if (!compat_pickle) |
| 270 | goto error; |
| 271 | st->name_mapping_2to3 = \ |
| 272 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 273 | if (!st->name_mapping_2to3) |
| 274 | goto error; |
| 275 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 276 | PyErr_Format(PyExc_RuntimeError, |
| 277 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 278 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 279 | goto error; |
| 280 | } |
| 281 | st->import_mapping_2to3 = \ |
| 282 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 283 | if (!st->import_mapping_2to3) |
| 284 | goto error; |
| 285 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 286 | PyErr_Format(PyExc_RuntimeError, |
| 287 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 288 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 289 | goto error; |
| 290 | } |
| 291 | /* ... and the 3.x -> 2.x mapping tables */ |
| 292 | st->name_mapping_3to2 = \ |
| 293 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 294 | if (!st->name_mapping_3to2) |
| 295 | goto error; |
| 296 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 297 | PyErr_Format(PyExc_RuntimeError, |
| 298 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 299 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 300 | goto error; |
| 301 | } |
| 302 | st->import_mapping_3to2 = \ |
| 303 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 304 | if (!st->import_mapping_3to2) |
| 305 | goto error; |
| 306 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 307 | PyErr_Format(PyExc_RuntimeError, |
| 308 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 309 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 310 | goto error; |
| 311 | } |
| 312 | Py_CLEAR(compat_pickle); |
| 313 | |
| 314 | codecs = PyImport_ImportModule("codecs"); |
| 315 | if (codecs == NULL) |
| 316 | goto error; |
| 317 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 318 | if (st->codecs_encode == NULL) { |
| 319 | goto error; |
| 320 | } |
| 321 | if (!PyCallable_Check(st->codecs_encode)) { |
| 322 | PyErr_Format(PyExc_RuntimeError, |
| 323 | "codecs.encode should be a callable, not %.200s", |
| 324 | Py_TYPE(st->codecs_encode)->tp_name); |
| 325 | goto error; |
| 326 | } |
| 327 | Py_CLEAR(codecs); |
| 328 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 329 | functools = PyImport_ImportModule("functools"); |
| 330 | if (!functools) |
| 331 | goto error; |
| 332 | st->partial = PyObject_GetAttrString(functools, "partial"); |
| 333 | if (!st->partial) |
| 334 | goto error; |
| 335 | Py_CLEAR(functools); |
| 336 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 337 | return 0; |
| 338 | |
| 339 | error: |
| 340 | Py_CLEAR(copyreg); |
| 341 | Py_CLEAR(compat_pickle); |
| 342 | Py_CLEAR(codecs); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 343 | Py_CLEAR(functools); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 344 | _Pickle_ClearState(st); |
| 345 | return -1; |
| 346 | } |
| 347 | |
| 348 | /* Helper for calling a function with a single argument quickly. |
| 349 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 350 | This function steals the reference of the given argument. */ |
| 351 | static PyObject * |
| 352 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 353 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 354 | PyObject *result; |
| 355 | |
Victor Stinner | 7bfb42d | 2016-12-05 17:04:32 +0100 | [diff] [blame] | 356 | result = PyObject_CallFunctionObjArgs(func, obj, NULL); |
Victor Stinner | 7521069 | 2016-08-19 18:59:15 +0200 | [diff] [blame] | 357 | Py_DECREF(obj); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 358 | return result; |
| 359 | } |
| 360 | |
| 361 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 362 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 363 | /* Retrieve and deconstruct a method for avoiding a reference cycle |
| 364 | (pickler -> bound method of pickler -> pickler) */ |
| 365 | static int |
| 366 | init_method_ref(PyObject *self, _Py_Identifier *name, |
| 367 | PyObject **method_func, PyObject **method_self) |
| 368 | { |
| 369 | PyObject *func, *func2; |
| 370 | |
| 371 | /* *method_func and *method_self should be consistent. All refcount decrements |
| 372 | should be occurred after setting *method_self and *method_func. */ |
| 373 | func = _PyObject_GetAttrId(self, name); |
| 374 | if (func == NULL) { |
| 375 | *method_self = NULL; |
| 376 | Py_CLEAR(*method_func); |
| 377 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 378 | return -1; |
| 379 | } |
| 380 | PyErr_Clear(); |
| 381 | return 0; |
| 382 | } |
| 383 | |
| 384 | if (PyMethod_Check(func) && PyMethod_GET_SELF(func) == self) { |
| 385 | /* Deconstruct a bound Python method */ |
| 386 | func2 = PyMethod_GET_FUNCTION(func); |
| 387 | Py_INCREF(func2); |
| 388 | *method_self = self; /* borrowed */ |
| 389 | Py_XSETREF(*method_func, func2); |
| 390 | Py_DECREF(func); |
| 391 | return 0; |
| 392 | } |
| 393 | else { |
| 394 | *method_self = NULL; |
| 395 | Py_XSETREF(*method_func, func); |
| 396 | return 0; |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | /* Bind a method if it was deconstructed */ |
| 401 | static PyObject * |
| 402 | reconstruct_method(PyObject *func, PyObject *self) |
| 403 | { |
| 404 | if (self) { |
| 405 | return PyMethod_New(func, self); |
| 406 | } |
| 407 | else { |
| 408 | Py_INCREF(func); |
| 409 | return func; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | static PyObject * |
| 414 | call_method(PyObject *func, PyObject *self, PyObject *obj) |
| 415 | { |
| 416 | if (self) { |
| 417 | return PyObject_CallFunctionObjArgs(func, self, obj, NULL); |
| 418 | } |
| 419 | else { |
| 420 | return PyObject_CallFunctionObjArgs(func, obj, NULL); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | /*************************************************************************/ |
| 425 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 426 | /* Internal data type used as the unpickling stack. */ |
| 427 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 428 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 429 | PyObject **data; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 430 | int mark_set; /* is MARK set? */ |
| 431 | Py_ssize_t fence; /* position of top MARK or 0 */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 432 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 433 | } Pdata; |
| 434 | |
| 435 | static void |
| 436 | Pdata_dealloc(Pdata *self) |
| 437 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 438 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 439 | while (--i >= 0) { |
| 440 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 441 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 442 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 443 | PyObject_Del(self); |
| 444 | } |
| 445 | |
| 446 | static PyTypeObject Pdata_Type = { |
| 447 | PyVarObject_HEAD_INIT(NULL, 0) |
| 448 | "_pickle.Pdata", /*tp_name*/ |
| 449 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 450 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 451 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 452 | }; |
| 453 | |
| 454 | static PyObject * |
| 455 | Pdata_New(void) |
| 456 | { |
| 457 | Pdata *self; |
| 458 | |
| 459 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 460 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 461 | Py_SIZE(self) = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 462 | self->mark_set = 0; |
| 463 | self->fence = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 464 | self->allocated = 8; |
| 465 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 466 | if (self->data) |
| 467 | return (PyObject *)self; |
| 468 | Py_DECREF(self); |
| 469 | return PyErr_NoMemory(); |
| 470 | } |
| 471 | |
| 472 | |
| 473 | /* Retain only the initial clearto items. If clearto >= the current |
| 474 | * number of items, this is a (non-erroneous) NOP. |
| 475 | */ |
| 476 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 477 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 478 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 479 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 480 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 481 | assert(clearto >= self->fence); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 482 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 483 | return 0; |
| 484 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 485 | while (--i >= clearto) { |
| 486 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 487 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 488 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 489 | return 0; |
| 490 | } |
| 491 | |
| 492 | static int |
| 493 | Pdata_grow(Pdata *self) |
| 494 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 495 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 496 | size_t allocated = (size_t)self->allocated; |
| 497 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 498 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 499 | new_allocated = (allocated >> 3) + 6; |
| 500 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 501 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 502 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 503 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 504 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 505 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 506 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 507 | |
| 508 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 509 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 510 | return 0; |
| 511 | |
| 512 | nomemory: |
| 513 | PyErr_NoMemory(); |
| 514 | return -1; |
| 515 | } |
| 516 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 517 | static int |
| 518 | Pdata_stack_underflow(Pdata *self) |
| 519 | { |
| 520 | PickleState *st = _Pickle_GetGlobalState(); |
| 521 | PyErr_SetString(st->UnpicklingError, |
| 522 | self->mark_set ? |
| 523 | "unexpected MARK found" : |
| 524 | "unpickling stack underflow"); |
| 525 | return -1; |
| 526 | } |
| 527 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 528 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 529 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 530 | * is raised and V is set to NULL. |
| 531 | */ |
| 532 | static PyObject * |
| 533 | Pdata_pop(Pdata *self) |
| 534 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 535 | if (Py_SIZE(self) <= self->fence) { |
| 536 | Pdata_stack_underflow(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 537 | return NULL; |
| 538 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 539 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 540 | } |
| 541 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 542 | |
| 543 | static int |
| 544 | Pdata_push(Pdata *self, PyObject *obj) |
| 545 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 546 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 547 | return -1; |
| 548 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 549 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 550 | return 0; |
| 551 | } |
| 552 | |
| 553 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 554 | #define PDATA_PUSH(D, O, ER) do { \ |
| 555 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 556 | |
| 557 | /* Push an object on stack, adding a new reference to the object. */ |
| 558 | #define PDATA_APPEND(D, O, ER) do { \ |
| 559 | Py_INCREF((O)); \ |
| 560 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 561 | |
| 562 | static PyObject * |
| 563 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 564 | { |
| 565 | PyObject *tuple; |
| 566 | Py_ssize_t len, i, j; |
| 567 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 568 | if (start < self->fence) { |
| 569 | Pdata_stack_underflow(self); |
| 570 | return NULL; |
| 571 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 572 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 573 | tuple = PyTuple_New(len); |
| 574 | if (tuple == NULL) |
| 575 | return NULL; |
| 576 | for (i = start, j = 0; j < len; i++, j++) |
| 577 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 578 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 579 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 580 | return tuple; |
| 581 | } |
| 582 | |
| 583 | static PyObject * |
| 584 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 585 | { |
| 586 | PyObject *list; |
| 587 | Py_ssize_t len, i, j; |
| 588 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 589 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 590 | list = PyList_New(len); |
| 591 | if (list == NULL) |
| 592 | return NULL; |
| 593 | for (i = start, j = 0; j < len; i++, j++) |
| 594 | PyList_SET_ITEM(list, j, self->data[i]); |
| 595 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 596 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 597 | return list; |
| 598 | } |
| 599 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 600 | typedef struct { |
| 601 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 602 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 603 | } PyMemoEntry; |
| 604 | |
| 605 | typedef struct { |
| 606 | Py_ssize_t mt_mask; |
| 607 | Py_ssize_t mt_used; |
| 608 | Py_ssize_t mt_allocated; |
| 609 | PyMemoEntry *mt_table; |
| 610 | } PyMemoTable; |
| 611 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 612 | typedef struct PicklerObject { |
| 613 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 614 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 615 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 616 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 617 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 618 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 619 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 620 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 621 | |
| 622 | PyObject *write; /* write() method of the output stream. */ |
| 623 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 624 | flushing to the stream. */ |
| 625 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 626 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 627 | int proto; /* Pickle protocol number, >= 0 */ |
| 628 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 629 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 630 | Py_ssize_t frame_start; /* Position in output_buffer where the |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 631 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 632 | is no frame currently open. */ |
| 633 | |
| 634 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 635 | int fast; /* Enable fast mode if set to a true value. |
| 636 | The fast mode disable the usage of memo, |
| 637 | therefore speeding the pickling process by |
| 638 | not generating superfluous PUT opcodes. It |
| 639 | should not be used if with self-referential |
| 640 | objects. */ |
| 641 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 642 | int fix_imports; /* Indicate whether Pickler should fix |
| 643 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 644 | PyObject *fast_memo; |
| 645 | } PicklerObject; |
| 646 | |
| 647 | typedef struct UnpicklerObject { |
| 648 | PyObject_HEAD |
| 649 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 650 | |
| 651 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 652 | is unnecessary, since the keys are contiguous ints. */ |
| 653 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 654 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 655 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 656 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 657 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 658 | PyObject *pers_func_self; /* borrowed reference to self if pers_func |
| 659 | is an unbound method, NULL otherwise */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 660 | |
| 661 | Py_buffer buffer; |
| 662 | char *input_buffer; |
| 663 | char *input_line; |
| 664 | Py_ssize_t input_len; |
| 665 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 666 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 667 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 668 | PyObject *read; /* read() method of the input stream. */ |
| 669 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 670 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 671 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 672 | char *encoding; /* Name of the encoding to be used for |
| 673 | decoding strings pickled using Python |
| 674 | 2.x. The default value is "ASCII" */ |
| 675 | char *errors; /* Name of errors handling scheme to used when |
| 676 | decoding strings. The default value is |
| 677 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 678 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 679 | objects. */ |
| 680 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 681 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 682 | int proto; /* Protocol of the pickle loaded. */ |
| 683 | int fix_imports; /* Indicate whether Unpickler should fix |
| 684 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 685 | } UnpicklerObject; |
| 686 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 687 | typedef struct { |
| 688 | PyObject_HEAD |
| 689 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 690 | } PicklerMemoProxyObject; |
| 691 | |
| 692 | typedef struct { |
| 693 | PyObject_HEAD |
| 694 | UnpicklerObject *unpickler; |
| 695 | } UnpicklerMemoProxyObject; |
| 696 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 697 | /* Forward declarations */ |
| 698 | static int save(PicklerObject *, PyObject *, int); |
| 699 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 700 | static PyTypeObject Pickler_Type; |
| 701 | static PyTypeObject Unpickler_Type; |
| 702 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 703 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 704 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 705 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 706 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 707 | 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] | 708 | a bunch of unnecessary object creation. This makes a huge performance |
| 709 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 710 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 711 | #define MT_MINSIZE 8 |
| 712 | #define PERTURB_SHIFT 5 |
| 713 | |
| 714 | |
| 715 | static PyMemoTable * |
| 716 | PyMemoTable_New(void) |
| 717 | { |
| 718 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 719 | if (memo == NULL) { |
| 720 | PyErr_NoMemory(); |
| 721 | return NULL; |
| 722 | } |
| 723 | |
| 724 | memo->mt_used = 0; |
| 725 | memo->mt_allocated = MT_MINSIZE; |
| 726 | memo->mt_mask = MT_MINSIZE - 1; |
| 727 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 728 | if (memo->mt_table == NULL) { |
| 729 | PyMem_FREE(memo); |
| 730 | PyErr_NoMemory(); |
| 731 | return NULL; |
| 732 | } |
| 733 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 734 | |
| 735 | return memo; |
| 736 | } |
| 737 | |
| 738 | static PyMemoTable * |
| 739 | PyMemoTable_Copy(PyMemoTable *self) |
| 740 | { |
| 741 | Py_ssize_t i; |
| 742 | PyMemoTable *new = PyMemoTable_New(); |
| 743 | if (new == NULL) |
| 744 | return NULL; |
| 745 | |
| 746 | new->mt_used = self->mt_used; |
| 747 | new->mt_allocated = self->mt_allocated; |
| 748 | new->mt_mask = self->mt_mask; |
| 749 | /* The table we get from _New() is probably smaller than we wanted. |
| 750 | Free it and allocate one that's the right size. */ |
| 751 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 752 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 753 | if (new->mt_table == NULL) { |
| 754 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 755 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 756 | return NULL; |
| 757 | } |
| 758 | for (i = 0; i < self->mt_allocated; i++) { |
| 759 | Py_XINCREF(self->mt_table[i].me_key); |
| 760 | } |
| 761 | memcpy(new->mt_table, self->mt_table, |
| 762 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 763 | |
| 764 | return new; |
| 765 | } |
| 766 | |
| 767 | static Py_ssize_t |
| 768 | PyMemoTable_Size(PyMemoTable *self) |
| 769 | { |
| 770 | return self->mt_used; |
| 771 | } |
| 772 | |
| 773 | static int |
| 774 | PyMemoTable_Clear(PyMemoTable *self) |
| 775 | { |
| 776 | Py_ssize_t i = self->mt_allocated; |
| 777 | |
| 778 | while (--i >= 0) { |
| 779 | Py_XDECREF(self->mt_table[i].me_key); |
| 780 | } |
| 781 | self->mt_used = 0; |
| 782 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 783 | return 0; |
| 784 | } |
| 785 | |
| 786 | static void |
| 787 | PyMemoTable_Del(PyMemoTable *self) |
| 788 | { |
| 789 | if (self == NULL) |
| 790 | return; |
| 791 | PyMemoTable_Clear(self); |
| 792 | |
| 793 | PyMem_FREE(self->mt_table); |
| 794 | PyMem_FREE(self); |
| 795 | } |
| 796 | |
| 797 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 798 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 799 | static PyMemoEntry * |
| 800 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 801 | { |
| 802 | size_t i; |
| 803 | size_t perturb; |
| 804 | size_t mask = (size_t)self->mt_mask; |
| 805 | PyMemoEntry *table = self->mt_table; |
| 806 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 807 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 808 | |
| 809 | i = hash & mask; |
| 810 | entry = &table[i]; |
| 811 | if (entry->me_key == NULL || entry->me_key == key) |
| 812 | return entry; |
| 813 | |
| 814 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 815 | i = (i << 2) + i + perturb + 1; |
| 816 | entry = &table[i & mask]; |
| 817 | if (entry->me_key == NULL || entry->me_key == key) |
| 818 | return entry; |
| 819 | } |
Barry Warsaw | b2e5794 | 2017-09-14 18:13:16 -0700 | [diff] [blame] | 820 | Py_UNREACHABLE(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 821 | } |
| 822 | |
| 823 | /* Returns -1 on failure, 0 on success. */ |
| 824 | static int |
| 825 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 826 | { |
| 827 | PyMemoEntry *oldtable = NULL; |
| 828 | PyMemoEntry *oldentry, *newentry; |
| 829 | Py_ssize_t new_size = MT_MINSIZE; |
| 830 | Py_ssize_t to_process; |
| 831 | |
| 832 | assert(min_size > 0); |
| 833 | |
| 834 | /* Find the smallest valid table size >= min_size. */ |
| 835 | while (new_size < min_size && new_size > 0) |
| 836 | new_size <<= 1; |
| 837 | if (new_size <= 0) { |
| 838 | PyErr_NoMemory(); |
| 839 | return -1; |
| 840 | } |
| 841 | /* new_size needs to be a power of two. */ |
| 842 | assert((new_size & (new_size - 1)) == 0); |
| 843 | |
| 844 | /* Allocate new table. */ |
| 845 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 846 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 847 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 848 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 849 | PyErr_NoMemory(); |
| 850 | return -1; |
| 851 | } |
| 852 | self->mt_allocated = new_size; |
| 853 | self->mt_mask = new_size - 1; |
| 854 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 855 | |
| 856 | /* Copy entries from the old table. */ |
| 857 | to_process = self->mt_used; |
| 858 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 859 | if (oldentry->me_key != NULL) { |
| 860 | to_process--; |
| 861 | /* newentry is a pointer to a chunk of the new |
| 862 | mt_table, so we're setting the key:value pair |
| 863 | in-place. */ |
| 864 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 865 | newentry->me_key = oldentry->me_key; |
| 866 | newentry->me_value = oldentry->me_value; |
| 867 | } |
| 868 | } |
| 869 | |
| 870 | /* Deallocate the old table. */ |
| 871 | PyMem_FREE(oldtable); |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 876 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 877 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 878 | { |
| 879 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 880 | if (entry->me_key == NULL) |
| 881 | return NULL; |
| 882 | return &entry->me_value; |
| 883 | } |
| 884 | |
| 885 | /* Returns -1 on failure, 0 on success. */ |
| 886 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 887 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 888 | { |
| 889 | PyMemoEntry *entry; |
| 890 | |
| 891 | assert(key != NULL); |
| 892 | |
| 893 | entry = _PyMemoTable_Lookup(self, key); |
| 894 | if (entry->me_key != NULL) { |
| 895 | entry->me_value = value; |
| 896 | return 0; |
| 897 | } |
| 898 | Py_INCREF(key); |
| 899 | entry->me_key = key; |
| 900 | entry->me_value = value; |
| 901 | self->mt_used++; |
| 902 | |
| 903 | /* If we added a key, we can safely resize. Otherwise just return! |
| 904 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 905 | * |
| 906 | * Quadrupling the size improves average table sparseness |
| 907 | * (reducing collisions) at the cost of some memory. It also halves |
| 908 | * the number of expensive resize operations in a growing memo table. |
| 909 | * |
| 910 | * Very large memo tables (over 50K items) use doubling instead. |
| 911 | * This may help applications with severe memory constraints. |
| 912 | */ |
| 913 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 914 | return 0; |
| 915 | return _PyMemoTable_ResizeTable(self, |
| 916 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 917 | } |
| 918 | |
| 919 | #undef MT_MINSIZE |
| 920 | #undef PERTURB_SHIFT |
| 921 | |
| 922 | /*************************************************************************/ |
| 923 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 925 | static int |
| 926 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 927 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 928 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 929 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 930 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 931 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 932 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 933 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | return 0; |
| 935 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 936 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 937 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 938 | _write_size64(char *out, size_t value) |
| 939 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 940 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 941 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 942 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 943 | |
| 944 | for (i = 0; i < sizeof(size_t); i++) { |
| 945 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 946 | } |
| 947 | for (i = sizeof(size_t); i < 8; i++) { |
| 948 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 949 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 950 | } |
| 951 | |
| 952 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 953 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 954 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 955 | qdata[0] = FRAME; |
| 956 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 957 | } |
| 958 | |
| 959 | static int |
| 960 | _Pickler_CommitFrame(PicklerObject *self) |
| 961 | { |
| 962 | size_t frame_len; |
| 963 | char *qdata; |
| 964 | |
| 965 | if (!self->framing || self->frame_start == -1) |
| 966 | return 0; |
| 967 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 968 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 969 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 970 | self->frame_start = -1; |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static int |
| 975 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 976 | { |
| 977 | Py_ssize_t frame_len; |
| 978 | |
| 979 | if (!self->framing || self->frame_start == -1) |
| 980 | return 0; |
| 981 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 982 | if (frame_len >= FRAME_SIZE_TARGET) |
| 983 | return _Pickler_CommitFrame(self); |
| 984 | else |
| 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 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1022 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1023 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1024 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1025 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1026 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1027 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1028 | |
| 1029 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1030 | need_new_frame = (self->framing && self->frame_start == -1); |
| 1031 | |
| 1032 | if (need_new_frame) |
| 1033 | n = data_len + FRAME_HEADER_SIZE; |
| 1034 | else |
| 1035 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1036 | |
| 1037 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1038 | if (required > self->max_output_len) { |
| 1039 | /* Make place in buffer for the pickle chunk */ |
| 1040 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 1041 | PyErr_NoMemory(); |
| 1042 | return -1; |
| 1043 | } |
| 1044 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 1045 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 1046 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1047 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1048 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1049 | if (need_new_frame) { |
| 1050 | /* Setup new frame */ |
| 1051 | Py_ssize_t frame_start = self->output_len; |
| 1052 | self->frame_start = frame_start; |
| 1053 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 1054 | /* Write an invalid value, for debugging */ |
| 1055 | buffer[frame_start + i] = 0xFE; |
| 1056 | } |
| 1057 | self->output_len += FRAME_HEADER_SIZE; |
| 1058 | } |
| 1059 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1060 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1061 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1062 | buffer[self->output_len + i] = s[i]; |
| 1063 | } |
| 1064 | } |
| 1065 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1066 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1067 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1068 | self->output_len += data_len; |
| 1069 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1070 | } |
| 1071 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1072 | static PicklerObject * |
| 1073 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1074 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1075 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1076 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1077 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1078 | if (self == NULL) |
| 1079 | return NULL; |
| 1080 | |
| 1081 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1082 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1083 | self->write = NULL; |
| 1084 | self->proto = 0; |
| 1085 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1086 | self->framing = 0; |
| 1087 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1088 | self->fast = 0; |
| 1089 | self->fast_nesting = 0; |
| 1090 | self->fix_imports = 0; |
| 1091 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1092 | self->max_output_len = WRITE_BUF_SIZE; |
| 1093 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1094 | |
| 1095 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1096 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1097 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1098 | |
| 1099 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1100 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1101 | return NULL; |
| 1102 | } |
| 1103 | return self; |
| 1104 | } |
| 1105 | |
| 1106 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1107 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1108 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1109 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1110 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1111 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1112 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1113 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1114 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1115 | proto = PyLong_AsLong(protocol); |
| 1116 | if (proto < 0) { |
| 1117 | if (proto == -1 && PyErr_Occurred()) |
| 1118 | return -1; |
| 1119 | proto = HIGHEST_PROTOCOL; |
| 1120 | } |
| 1121 | else if (proto > HIGHEST_PROTOCOL) { |
| 1122 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1123 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1124 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1125 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1126 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1127 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1128 | self->bin = proto > 0; |
| 1129 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1130 | return 0; |
| 1131 | } |
| 1132 | |
| 1133 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1134 | be called once on a freshly created Pickler. */ |
| 1135 | static int |
| 1136 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1137 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1138 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1139 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1140 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1141 | if (self->write == NULL) { |
| 1142 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1143 | PyErr_SetString(PyExc_TypeError, |
| 1144 | "file must have a 'write' attribute"); |
| 1145 | return -1; |
| 1146 | } |
| 1147 | |
| 1148 | return 0; |
| 1149 | } |
| 1150 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1151 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1152 | own reference to `input`. */ |
| 1153 | static Py_ssize_t |
| 1154 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1155 | { |
| 1156 | if (self->buffer.buf != NULL) |
| 1157 | PyBuffer_Release(&self->buffer); |
| 1158 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1159 | return -1; |
| 1160 | self->input_buffer = self->buffer.buf; |
| 1161 | self->input_len = self->buffer.len; |
| 1162 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1163 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1164 | return self->input_len; |
| 1165 | } |
| 1166 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1167 | static int |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1168 | bad_readline(void) |
| 1169 | { |
| 1170 | PickleState *st = _Pickle_GetGlobalState(); |
| 1171 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
| 1172 | return -1; |
| 1173 | } |
| 1174 | |
| 1175 | static int |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1176 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1177 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1178 | Py_ssize_t consumed; |
| 1179 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1180 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1181 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1182 | if (consumed <= 0) |
| 1183 | return 0; |
| 1184 | |
| 1185 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1186 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1187 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1188 | if (r == NULL) |
| 1189 | return -1; |
| 1190 | Py_DECREF(r); |
| 1191 | |
| 1192 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1193 | return 0; |
| 1194 | } |
| 1195 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1196 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1197 | |
| 1198 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1199 | may be multiple pickle objects arranged contiguously in the same input |
| 1200 | buffer. |
| 1201 | |
| 1202 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1203 | bytes from the input stream/buffer. |
| 1204 | |
| 1205 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1206 | failure; on success, returns the number of bytes read from the file. |
| 1207 | |
| 1208 | On success, self->input_len will be 0; this is intentional so that when |
| 1209 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1210 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1211 | size to tell you how much data you can process. */ |
| 1212 | static Py_ssize_t |
| 1213 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1214 | { |
| 1215 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1216 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1217 | |
| 1218 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1219 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1220 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1221 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1222 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1223 | if (n == READ_WHOLE_LINE) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 1224 | data = _PyObject_CallNoArg(self->readline); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1225 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1226 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1227 | PyObject *len; |
| 1228 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1229 | if (self->peek && n < PREFETCH) { |
| 1230 | len = PyLong_FromSsize_t(PREFETCH); |
| 1231 | if (len == NULL) |
| 1232 | return -1; |
| 1233 | data = _Pickle_FastCall(self->peek, len); |
| 1234 | if (data == NULL) { |
| 1235 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1236 | return -1; |
| 1237 | /* peek() is probably not supported by the given file object */ |
| 1238 | PyErr_Clear(); |
| 1239 | Py_CLEAR(self->peek); |
| 1240 | } |
| 1241 | else { |
| 1242 | read_size = _Unpickler_SetStringInput(self, data); |
| 1243 | Py_DECREF(data); |
| 1244 | self->prefetched_idx = 0; |
| 1245 | if (n <= read_size) |
| 1246 | return n; |
| 1247 | } |
| 1248 | } |
| 1249 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1250 | if (len == NULL) |
| 1251 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1252 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1253 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1254 | if (data == NULL) |
| 1255 | return -1; |
| 1256 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1257 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1258 | Py_DECREF(data); |
| 1259 | return read_size; |
| 1260 | } |
| 1261 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1262 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1263 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1264 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1265 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1266 | Py_ssize_t num_read; |
| 1267 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1268 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1269 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1270 | PickleState *st = _Pickle_GetGlobalState(); |
| 1271 | PyErr_SetString(st->UnpicklingError, |
| 1272 | "read would overflow (invalid bytecode)"); |
| 1273 | return -1; |
| 1274 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1275 | |
| 1276 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1277 | assert(self->next_read_idx + n > self->input_len); |
| 1278 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1279 | if (!self->read) |
| 1280 | return bad_readline(); |
| 1281 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1282 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1283 | if (num_read < 0) |
| 1284 | return -1; |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1285 | if (num_read < n) |
| 1286 | return bad_readline(); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1287 | *s = self->input_buffer; |
| 1288 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1289 | return n; |
| 1290 | } |
| 1291 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1292 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1293 | |
| 1294 | This should be used for all data reads, rather than accessing the unpickler's |
| 1295 | input buffer directly. This method deals correctly with reading from input |
| 1296 | streams, which the input buffer doesn't deal with. |
| 1297 | |
| 1298 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1299 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1300 | should use this function's return value to know how many bytes you can |
| 1301 | consume. |
| 1302 | |
| 1303 | Returns -1 (with an exception set) on failure. On success, return the |
| 1304 | number of chars read. */ |
| 1305 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1306 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1307 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1308 | (self)->next_read_idx += (n), \ |
| 1309 | (n)) \ |
| 1310 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1311 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1312 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1313 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1314 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1315 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1316 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1317 | if (input_line == NULL) { |
| 1318 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1319 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1320 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1321 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1322 | memcpy(input_line, line, len); |
| 1323 | input_line[len] = '\0'; |
| 1324 | self->input_line = input_line; |
| 1325 | *result = self->input_line; |
| 1326 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1327 | } |
| 1328 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1329 | /* 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] | 1330 | before hitting \n, raise an error. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1331 | |
| 1332 | Returns the number of chars read, or -1 on failure. */ |
| 1333 | static Py_ssize_t |
| 1334 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1335 | { |
| 1336 | Py_ssize_t i, num_read; |
| 1337 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1338 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1339 | if (self->input_buffer[i] == '\n') { |
| 1340 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1341 | num_read = i - self->next_read_idx + 1; |
| 1342 | self->next_read_idx = i + 1; |
| 1343 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1344 | } |
| 1345 | } |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1346 | if (!self->read) |
| 1347 | return bad_readline(); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1348 | |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 1349 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1350 | if (num_read < 0) |
| 1351 | return -1; |
| 1352 | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
| 1353 | return bad_readline(); |
| 1354 | self->next_read_idx = num_read; |
| 1355 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1356 | } |
| 1357 | |
| 1358 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1359 | will be modified in place. */ |
| 1360 | static int |
| 1361 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1362 | { |
| 1363 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1364 | |
| 1365 | assert(new_size > self->memo_size); |
| 1366 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1367 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1368 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1369 | PyErr_NoMemory(); |
| 1370 | return -1; |
| 1371 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1372 | for (i = self->memo_size; i < new_size; i++) |
| 1373 | self->memo[i] = NULL; |
| 1374 | self->memo_size = new_size; |
| 1375 | return 0; |
| 1376 | } |
| 1377 | |
| 1378 | /* Returns NULL if idx is out of bounds. */ |
| 1379 | static PyObject * |
| 1380 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1381 | { |
| 1382 | if (idx < 0 || idx >= self->memo_size) |
| 1383 | return NULL; |
| 1384 | |
| 1385 | return self->memo[idx]; |
| 1386 | } |
| 1387 | |
| 1388 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1389 | This takes its own reference to `value`. */ |
| 1390 | static int |
| 1391 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1392 | { |
| 1393 | PyObject *old_item; |
| 1394 | |
| 1395 | if (idx >= self->memo_size) { |
| 1396 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1397 | return -1; |
| 1398 | assert(idx < self->memo_size); |
| 1399 | } |
| 1400 | Py_INCREF(value); |
| 1401 | old_item = self->memo[idx]; |
| 1402 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1403 | if (old_item != NULL) { |
| 1404 | Py_DECREF(old_item); |
| 1405 | } |
| 1406 | else { |
| 1407 | self->memo_len++; |
| 1408 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static PyObject ** |
| 1413 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1414 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1415 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1416 | if (memo == NULL) { |
| 1417 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1418 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1419 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1420 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1421 | return memo; |
| 1422 | } |
| 1423 | |
| 1424 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1425 | static void |
| 1426 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1427 | { |
| 1428 | Py_ssize_t i; |
| 1429 | PyObject **memo = self->memo; |
| 1430 | |
| 1431 | if (self->memo == NULL) |
| 1432 | return; |
| 1433 | self->memo = NULL; |
| 1434 | i = self->memo_size; |
| 1435 | while (--i >= 0) { |
| 1436 | Py_XDECREF(memo[i]); |
| 1437 | } |
| 1438 | PyMem_FREE(memo); |
| 1439 | } |
| 1440 | |
| 1441 | static UnpicklerObject * |
| 1442 | _Unpickler_New(void) |
| 1443 | { |
| 1444 | UnpicklerObject *self; |
| 1445 | |
| 1446 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1447 | if (self == NULL) |
| 1448 | return NULL; |
| 1449 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1450 | self->pers_func = NULL; |
| 1451 | self->input_buffer = NULL; |
| 1452 | self->input_line = NULL; |
| 1453 | self->input_len = 0; |
| 1454 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1455 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1456 | self->read = NULL; |
| 1457 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1458 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1459 | self->encoding = NULL; |
| 1460 | self->errors = NULL; |
| 1461 | self->marks = NULL; |
| 1462 | self->num_marks = 0; |
| 1463 | self->marks_size = 0; |
| 1464 | self->proto = 0; |
| 1465 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1466 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1467 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1468 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1469 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1470 | self->stack = (Pdata *)Pdata_New(); |
| 1471 | |
| 1472 | if (self->memo == NULL || self->stack == NULL) { |
| 1473 | Py_DECREF(self); |
| 1474 | return NULL; |
| 1475 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1476 | |
| 1477 | return self; |
| 1478 | } |
| 1479 | |
| 1480 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1481 | be called once on a freshly created Pickler. */ |
| 1482 | static int |
| 1483 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1484 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1485 | _Py_IDENTIFIER(peek); |
| 1486 | _Py_IDENTIFIER(read); |
| 1487 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1488 | |
| 1489 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1490 | if (self->peek == NULL) { |
| 1491 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1492 | PyErr_Clear(); |
| 1493 | else |
| 1494 | return -1; |
| 1495 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1496 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1497 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1498 | if (self->readline == NULL || self->read == NULL) { |
| 1499 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1500 | PyErr_SetString(PyExc_TypeError, |
| 1501 | "file must have 'read' and 'readline' attributes"); |
| 1502 | Py_CLEAR(self->read); |
| 1503 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1504 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1505 | return -1; |
| 1506 | } |
| 1507 | return 0; |
| 1508 | } |
| 1509 | |
| 1510 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1511 | be called once on a freshly created Pickler. */ |
| 1512 | static int |
| 1513 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1514 | const char *encoding, |
| 1515 | const char *errors) |
| 1516 | { |
| 1517 | if (encoding == NULL) |
| 1518 | encoding = "ASCII"; |
| 1519 | if (errors == NULL) |
| 1520 | errors = "strict"; |
| 1521 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1522 | self->encoding = _PyMem_Strdup(encoding); |
| 1523 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1524 | if (self->encoding == NULL || self->errors == NULL) { |
| 1525 | PyErr_NoMemory(); |
| 1526 | return -1; |
| 1527 | } |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1532 | static int |
| 1533 | memo_get(PicklerObject *self, PyObject *key) |
| 1534 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1535 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1536 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1537 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1538 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1539 | value = PyMemoTable_Get(self->memo, key); |
| 1540 | if (value == NULL) { |
| 1541 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1542 | return -1; |
| 1543 | } |
| 1544 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1545 | if (!self->bin) { |
| 1546 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1547 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1548 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1549 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1550 | } |
| 1551 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1552 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1553 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1554 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1555 | len = 2; |
| 1556 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1557 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1558 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1559 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1560 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1561 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1562 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1563 | len = 5; |
| 1564 | } |
| 1565 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1566 | PickleState *st = _Pickle_GetGlobalState(); |
| 1567 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1568 | "memo id too large for LONG_BINGET"); |
| 1569 | return -1; |
| 1570 | } |
| 1571 | } |
| 1572 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1573 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1574 | return -1; |
| 1575 | |
| 1576 | return 0; |
| 1577 | } |
| 1578 | |
| 1579 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1580 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1581 | static int |
| 1582 | memo_put(PicklerObject *self, PyObject *obj) |
| 1583 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1584 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1585 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1586 | Py_ssize_t idx; |
| 1587 | |
| 1588 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1589 | |
| 1590 | if (self->fast) |
| 1591 | return 0; |
| 1592 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1593 | idx = PyMemoTable_Size(self->memo); |
| 1594 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1595 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1596 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1597 | if (self->proto >= 4) { |
| 1598 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1599 | return -1; |
| 1600 | return 0; |
| 1601 | } |
| 1602 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1603 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1604 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1605 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1606 | len = strlen(pdata); |
| 1607 | } |
| 1608 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1609 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1610 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1611 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1612 | len = 2; |
| 1613 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1614 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1615 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1616 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1617 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1618 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1619 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1620 | len = 5; |
| 1621 | } |
| 1622 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1623 | PickleState *st = _Pickle_GetGlobalState(); |
| 1624 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1625 | "memo id too large for LONG_BINPUT"); |
| 1626 | return -1; |
| 1627 | } |
| 1628 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1629 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1630 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1631 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1632 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1633 | } |
| 1634 | |
| 1635 | static PyObject * |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1636 | get_dotted_path(PyObject *obj, PyObject *name) |
| 1637 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1638 | _Py_static_string(PyId_dot, "."); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1639 | PyObject *dotted_path; |
| 1640 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1641 | |
| 1642 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1643 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1644 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1645 | n = PyList_GET_SIZE(dotted_path); |
| 1646 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1647 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1648 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 1649 | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1650 | if (obj == NULL) |
| 1651 | PyErr_Format(PyExc_AttributeError, |
| 1652 | "Can't pickle local object %R", name); |
| 1653 | else |
| 1654 | PyErr_Format(PyExc_AttributeError, |
| 1655 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1656 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1657 | return NULL; |
| 1658 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1659 | } |
| 1660 | return dotted_path; |
| 1661 | } |
| 1662 | |
| 1663 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1664 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1665 | { |
| 1666 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1667 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1668 | |
| 1669 | assert(PyList_CheckExact(names)); |
| 1670 | Py_INCREF(obj); |
| 1671 | n = PyList_GET_SIZE(names); |
| 1672 | for (i = 0; i < n; i++) { |
| 1673 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1674 | Py_XDECREF(parent); |
| 1675 | parent = obj; |
| 1676 | obj = PyObject_GetAttr(parent, name); |
| 1677 | if (obj == NULL) { |
| 1678 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1679 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1680 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1681 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1682 | if (pparent != NULL) |
| 1683 | *pparent = parent; |
| 1684 | else |
| 1685 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1686 | return obj; |
| 1687 | } |
| 1688 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1689 | static void |
| 1690 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1691 | { |
| 1692 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1693 | PyErr_Clear(); |
| 1694 | PyErr_Format(PyExc_AttributeError, |
| 1695 | "Can't get attribute %R on %R", name, obj); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | |
| 1700 | static PyObject * |
| 1701 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1702 | { |
| 1703 | PyObject *dotted_path, *attr; |
| 1704 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1705 | if (allow_qualname) { |
| 1706 | dotted_path = get_dotted_path(obj, name); |
| 1707 | if (dotted_path == NULL) |
| 1708 | return NULL; |
| 1709 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1710 | Py_DECREF(dotted_path); |
| 1711 | } |
| 1712 | else |
| 1713 | attr = PyObject_GetAttr(obj, name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1714 | if (attr == NULL) |
| 1715 | reformat_attribute_error(obj, name); |
| 1716 | return attr; |
| 1717 | } |
| 1718 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1719 | static int |
| 1720 | _checkmodule(PyObject *module_name, PyObject *module, |
| 1721 | PyObject *global, PyObject *dotted_path) |
| 1722 | { |
| 1723 | if (module == Py_None) { |
| 1724 | return -1; |
| 1725 | } |
| 1726 | if (PyUnicode_Check(module_name) && |
| 1727 | _PyUnicode_EqualToASCIIString(module_name, "__main__")) { |
| 1728 | return -1; |
| 1729 | } |
| 1730 | |
| 1731 | PyObject *candidate = get_deep_attribute(module, dotted_path, NULL); |
| 1732 | if (candidate == NULL) { |
| 1733 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1734 | PyErr_Clear(); |
| 1735 | } |
| 1736 | return -1; |
| 1737 | } |
| 1738 | if (candidate != global) { |
| 1739 | Py_DECREF(candidate); |
| 1740 | return -1; |
| 1741 | } |
| 1742 | Py_DECREF(candidate); |
| 1743 | return 0; |
| 1744 | } |
| 1745 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1746 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1747 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1748 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1749 | PyObject *module_name; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1750 | PyObject *module = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1751 | Py_ssize_t i; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1752 | PyObject *modules; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1753 | _Py_IDENTIFIER(__module__); |
| 1754 | _Py_IDENTIFIER(modules); |
| 1755 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1756 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1757 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1758 | |
| 1759 | if (module_name == NULL) { |
| 1760 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1761 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1762 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1763 | } |
| 1764 | else { |
| 1765 | /* In some rare cases (e.g., bound methods of extension types), |
| 1766 | __module__ can be None. If it is so, then search sys.modules for |
| 1767 | the module of global. */ |
| 1768 | if (module_name != Py_None) |
| 1769 | return module_name; |
| 1770 | Py_CLEAR(module_name); |
| 1771 | } |
| 1772 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1773 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1774 | /* Fallback on walking sys.modules */ |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1775 | modules = _PySys_GetObjectId(&PyId_modules); |
| 1776 | if (modules == NULL) { |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1777 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1778 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1779 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1780 | if (PyDict_CheckExact(modules)) { |
| 1781 | i = 0; |
| 1782 | while (PyDict_Next(modules, &i, &module_name, &module)) { |
| 1783 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1784 | Py_INCREF(module_name); |
| 1785 | return module_name; |
| 1786 | } |
| 1787 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1788 | return NULL; |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1789 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1790 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1791 | } |
| 1792 | else { |
| 1793 | PyObject *iterator = PyObject_GetIter(modules); |
| 1794 | if (iterator == NULL) { |
| 1795 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1796 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 1797 | while ((module_name = PyIter_Next(iterator))) { |
| 1798 | module = PyObject_GetItem(modules, module_name); |
| 1799 | if (module == NULL) { |
| 1800 | Py_DECREF(module_name); |
| 1801 | Py_DECREF(iterator); |
| 1802 | return NULL; |
| 1803 | } |
| 1804 | if (_checkmodule(module_name, module, global, dotted_path) == 0) { |
| 1805 | Py_DECREF(module); |
| 1806 | Py_DECREF(iterator); |
| 1807 | return module_name; |
| 1808 | } |
| 1809 | Py_DECREF(module); |
| 1810 | Py_DECREF(module_name); |
| 1811 | if (PyErr_Occurred()) { |
| 1812 | Py_DECREF(iterator); |
| 1813 | return NULL; |
| 1814 | } |
| 1815 | } |
| 1816 | Py_DECREF(iterator); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1820 | module_name = _PyUnicode_FromId(&PyId___main__); |
Victor Stinner | af46eb8 | 2017-09-05 23:30:16 +0200 | [diff] [blame] | 1821 | Py_XINCREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1822 | return module_name; |
| 1823 | } |
| 1824 | |
| 1825 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1826 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1827 | memoization disabled). If the nesting of a list or dict object exceed |
| 1828 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1829 | reference to the seen list or dict objects and check whether these objects |
| 1830 | are recursive. These are not strictly necessary, since save() has a |
| 1831 | hard-coded recursion limit, but they give a nicer error message than the |
| 1832 | typical RuntimeError. */ |
| 1833 | static int |
| 1834 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1835 | { |
| 1836 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1837 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1838 | PyObject *key = NULL; |
| 1839 | if (self->fast_memo == NULL) { |
| 1840 | self->fast_memo = PyDict_New(); |
| 1841 | if (self->fast_memo == NULL) { |
| 1842 | self->fast_nesting = -1; |
| 1843 | return 0; |
| 1844 | } |
| 1845 | } |
| 1846 | key = PyLong_FromVoidPtr(obj); |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1847 | if (key == NULL) { |
| 1848 | self->fast_nesting = -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1849 | return 0; |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1850 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1851 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1852 | Py_DECREF(key); |
| 1853 | PyErr_Format(PyExc_ValueError, |
| 1854 | "fast mode: can't pickle cyclic objects " |
| 1855 | "including object type %.200s at %p", |
| 1856 | obj->ob_type->tp_name, obj); |
| 1857 | self->fast_nesting = -1; |
| 1858 | return 0; |
| 1859 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1860 | if (PyErr_Occurred()) { |
Mat M | f76231f | 2017-11-13 02:50:16 -0500 | [diff] [blame] | 1861 | Py_DECREF(key); |
| 1862 | self->fast_nesting = -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1863 | return 0; |
| 1864 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1865 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1866 | Py_DECREF(key); |
| 1867 | self->fast_nesting = -1; |
| 1868 | return 0; |
| 1869 | } |
| 1870 | Py_DECREF(key); |
| 1871 | } |
| 1872 | return 1; |
| 1873 | } |
| 1874 | |
| 1875 | static int |
| 1876 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1877 | { |
| 1878 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1879 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1880 | if (key == NULL) |
| 1881 | return 0; |
| 1882 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1883 | Py_DECREF(key); |
| 1884 | return 0; |
| 1885 | } |
| 1886 | Py_DECREF(key); |
| 1887 | } |
| 1888 | return 1; |
| 1889 | } |
| 1890 | |
| 1891 | static int |
| 1892 | save_none(PicklerObject *self, PyObject *obj) |
| 1893 | { |
| 1894 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1895 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1896 | return -1; |
| 1897 | |
| 1898 | return 0; |
| 1899 | } |
| 1900 | |
| 1901 | static int |
| 1902 | save_bool(PicklerObject *self, PyObject *obj) |
| 1903 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1904 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1905 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1906 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1907 | return -1; |
| 1908 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1909 | else { |
| 1910 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1911 | * so that unpicklers written before bools were introduced unpickle them |
| 1912 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1913 | * Note that protocol 2 added direct ways to pickle bools. |
| 1914 | */ |
| 1915 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1916 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1917 | return -1; |
| 1918 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1919 | return 0; |
| 1920 | } |
| 1921 | |
| 1922 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1923 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1924 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1925 | PyObject *repr = NULL; |
| 1926 | Py_ssize_t size; |
| 1927 | long val; |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1928 | int overflow; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1929 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1930 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1931 | val= PyLong_AsLongAndOverflow(obj, &overflow); |
| 1932 | if (!overflow && (sizeof(long) <= 4 || |
| 1933 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) |
| 1934 | { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1935 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1936 | |
| 1937 | Note: we can't use -0x80000000L in the above condition because some |
| 1938 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1939 | before applying the unary minus when sizeof(long) <= 4. The |
| 1940 | resulting value stays unsigned which is commonly not what we want, |
| 1941 | so MSVC happily warns us about it. However, that result would have |
| 1942 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1943 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1944 | char pdata[32]; |
| 1945 | Py_ssize_t len = 0; |
| 1946 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1947 | if (self->bin) { |
| 1948 | pdata[1] = (unsigned char)(val & 0xff); |
| 1949 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1950 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1951 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1952 | |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1953 | if ((pdata[4] != 0) || (pdata[3] != 0)) { |
| 1954 | pdata[0] = BININT; |
| 1955 | len = 5; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1956 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1957 | else if (pdata[2] != 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1958 | pdata[0] = BININT2; |
| 1959 | len = 3; |
| 1960 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1961 | else { |
| 1962 | pdata[0] = BININT1; |
| 1963 | len = 2; |
| 1964 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1965 | } |
| 1966 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1967 | sprintf(pdata, "%c%ld\n", INT, val); |
| 1968 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1969 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1970 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1971 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1972 | |
| 1973 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1974 | } |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 1975 | assert(!PyErr_Occurred()); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1976 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1977 | if (self->proto >= 2) { |
| 1978 | /* Linear-time pickling. */ |
| 1979 | size_t nbits; |
| 1980 | size_t nbytes; |
| 1981 | unsigned char *pdata; |
| 1982 | char header[5]; |
| 1983 | int i; |
| 1984 | int sign = _PyLong_Sign(obj); |
| 1985 | |
| 1986 | if (sign == 0) { |
| 1987 | header[0] = LONG1; |
| 1988 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1989 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1990 | goto error; |
| 1991 | return 0; |
| 1992 | } |
| 1993 | nbits = _PyLong_NumBits(obj); |
| 1994 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1995 | goto error; |
| 1996 | /* How many bytes do we need? There are nbits >> 3 full |
| 1997 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1998 | * are any leftover bits, then we clearly need another |
| 1999 | * byte. Wnat's not so obvious is that we *probably* |
| 2000 | * need another byte even if there aren't any leftovers: |
| 2001 | * the most-significant bit of the most-significant byte |
| 2002 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2003 | * opposite of the one we need. The exception is ints |
| 2004 | * 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] | 2005 | * its own 256's-complement, so has the right sign bit |
| 2006 | * even without the extra byte. That's a pain to check |
| 2007 | * for in advance, though, so we always grab an extra |
| 2008 | * byte at the start, and cut it back later if possible. |
| 2009 | */ |
| 2010 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 2011 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2012 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2013 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2014 | goto error; |
| 2015 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2016 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2017 | if (repr == NULL) |
| 2018 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 2019 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2020 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 2021 | pdata, nbytes, |
| 2022 | 1 /* little endian */ , 1 /* signed */ ); |
| 2023 | if (i < 0) |
| 2024 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 2025 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2026 | * needed. This is so iff the MSB is all redundant sign |
| 2027 | * bits. |
| 2028 | */ |
| 2029 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2030 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2031 | pdata[nbytes - 1] == 0xff && |
| 2032 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 2033 | nbytes--; |
| 2034 | } |
| 2035 | |
| 2036 | if (nbytes < 256) { |
| 2037 | header[0] = LONG1; |
| 2038 | header[1] = (unsigned char)nbytes; |
| 2039 | size = 2; |
| 2040 | } |
| 2041 | else { |
| 2042 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2043 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2044 | for (i = 1; i < 5; i++) { |
| 2045 | header[i] = (unsigned char)(size & 0xff); |
| 2046 | size >>= 8; |
| 2047 | } |
| 2048 | size = 5; |
| 2049 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2050 | if (_Pickler_Write(self, header, size) < 0 || |
| 2051 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2052 | goto error; |
| 2053 | } |
| 2054 | else { |
Serhiy Storchaka | 3daaafb | 2017-11-16 09:44:43 +0200 | [diff] [blame] | 2055 | const char long_op = LONG; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2056 | const char *string; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2057 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 2058 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 2059 | the number of digits), in both directions. We add a trailing 'L' |
| 2060 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2061 | |
| 2062 | repr = PyObject_Repr(obj); |
| 2063 | if (repr == NULL) |
| 2064 | goto error; |
| 2065 | |
Serhiy Storchaka | 0651583 | 2016-11-20 09:13:07 +0200 | [diff] [blame] | 2066 | string = PyUnicode_AsUTF8AndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2067 | if (string == NULL) |
| 2068 | goto error; |
| 2069 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2070 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 2071 | _Pickler_Write(self, string, size) < 0 || |
| 2072 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2073 | goto error; |
| 2074 | } |
| 2075 | |
| 2076 | if (0) { |
| 2077 | error: |
| 2078 | status = -1; |
| 2079 | } |
| 2080 | Py_XDECREF(repr); |
| 2081 | |
| 2082 | return status; |
| 2083 | } |
| 2084 | |
| 2085 | static int |
| 2086 | save_float(PicklerObject *self, PyObject *obj) |
| 2087 | { |
| 2088 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 2089 | |
| 2090 | if (self->bin) { |
| 2091 | char pdata[9]; |
| 2092 | pdata[0] = BINFLOAT; |
| 2093 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2094 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2095 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2096 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2097 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2098 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2099 | int result = -1; |
| 2100 | char *buf = NULL; |
| 2101 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2102 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2103 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2104 | goto done; |
| 2105 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2106 | 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] | 2107 | if (!buf) { |
| 2108 | PyErr_NoMemory(); |
| 2109 | goto done; |
| 2110 | } |
| 2111 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2112 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2113 | goto done; |
| 2114 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2115 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2116 | goto done; |
| 2117 | |
| 2118 | result = 0; |
| 2119 | done: |
| 2120 | PyMem_Free(buf); |
| 2121 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
| 2124 | return 0; |
| 2125 | } |
| 2126 | |
| 2127 | static int |
| 2128 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2129 | { |
| 2130 | if (self->proto < 3) { |
| 2131 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2132 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2133 | the __reduce__ method) to permit bytes object unpickling. |
| 2134 | |
| 2135 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2136 | 2 'bytes' is just an alias for 'str' (which has different |
| 2137 | parameters than the actual bytes object), we use codecs.encode |
| 2138 | to create the appropriate 'str' object when unpickled using |
| 2139 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2140 | using Python 3. Again this is a hack and we don't need to do this |
| 2141 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2142 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2143 | int status; |
| 2144 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2145 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2146 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2147 | } |
| 2148 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2149 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2150 | PyObject *unicode_str = |
| 2151 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2152 | PyBytes_GET_SIZE(obj), |
| 2153 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2154 | _Py_IDENTIFIER(latin1); |
| 2155 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2156 | if (unicode_str == NULL) |
| 2157 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2158 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2159 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2160 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2161 | Py_DECREF(unicode_str); |
| 2162 | } |
| 2163 | |
| 2164 | if (reduce_value == NULL) |
| 2165 | return -1; |
| 2166 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2167 | /* save_reduce() will memoize the object automatically. */ |
| 2168 | status = save_reduce(self, reduce_value, obj); |
| 2169 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2170 | return status; |
| 2171 | } |
| 2172 | else { |
| 2173 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2174 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2175 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2176 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2177 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2178 | if (size < 0) |
| 2179 | return -1; |
| 2180 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2181 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2182 | header[0] = SHORT_BINBYTES; |
| 2183 | header[1] = (unsigned char)size; |
| 2184 | len = 2; |
| 2185 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2186 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2187 | header[0] = BINBYTES; |
| 2188 | header[1] = (unsigned char)(size & 0xff); |
| 2189 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2190 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2191 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2192 | len = 5; |
| 2193 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2194 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2195 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2196 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2197 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2198 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2199 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2200 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2201 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2202 | return -1; /* string too large */ |
| 2203 | } |
| 2204 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2205 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2206 | return -1; |
| 2207 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2208 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2209 | return -1; |
| 2210 | |
| 2211 | if (memo_put(self, obj) < 0) |
| 2212 | return -1; |
| 2213 | |
| 2214 | return 0; |
| 2215 | } |
| 2216 | } |
| 2217 | |
| 2218 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2219 | backslash and newline characters to \uXXXX escapes. */ |
| 2220 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2221 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2222 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2223 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2224 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2225 | void *data; |
| 2226 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2227 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2228 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2229 | if (PyUnicode_READY(obj)) |
| 2230 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2231 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2232 | _PyBytesWriter_Init(&writer); |
| 2233 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2234 | size = PyUnicode_GET_LENGTH(obj); |
| 2235 | data = PyUnicode_DATA(obj); |
| 2236 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2237 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2238 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2239 | if (p == NULL) |
| 2240 | goto error; |
| 2241 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2242 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2243 | for (i=0; i < size; i++) { |
| 2244 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2245 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2246 | if (ch >= 0x10000) { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2247 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2248 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2249 | if (p == NULL) |
| 2250 | goto error; |
| 2251 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2252 | *p++ = '\\'; |
| 2253 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2254 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2255 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2256 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2257 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2258 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2259 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2260 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2261 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2262 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2263 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2264 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Raymond Hettinger | 15f44ab | 2016-08-30 10:47:49 -0700 | [diff] [blame] | 2265 | /* -1: subtract 1 preallocated byte */ |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2266 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2267 | if (p == NULL) |
| 2268 | goto error; |
| 2269 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2270 | *p++ = '\\'; |
| 2271 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2272 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2273 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2274 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2275 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2276 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2277 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2278 | else |
| 2279 | *p++ = (char) ch; |
| 2280 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2281 | |
| 2282 | return _PyBytesWriter_Finish(&writer, p); |
| 2283 | |
| 2284 | error: |
| 2285 | _PyBytesWriter_Dealloc(&writer); |
| 2286 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2287 | } |
| 2288 | |
| 2289 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2290 | write_utf8(PicklerObject *self, const char *data, Py_ssize_t size) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2291 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2292 | char header[9]; |
| 2293 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2294 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2295 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2296 | if (size <= 0xff && self->proto >= 4) { |
| 2297 | header[0] = SHORT_BINUNICODE; |
| 2298 | header[1] = (unsigned char)(size & 0xff); |
| 2299 | len = 2; |
| 2300 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2301 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2302 | header[0] = BINUNICODE; |
| 2303 | header[1] = (unsigned char)(size & 0xff); |
| 2304 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2305 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2306 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2307 | len = 5; |
| 2308 | } |
| 2309 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2310 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2311 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2312 | len = 9; |
| 2313 | } |
| 2314 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2315 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2316 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2317 | return -1; |
| 2318 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2319 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2320 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2321 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2322 | if (_Pickler_Write(self, data, size) < 0) |
| 2323 | return -1; |
| 2324 | |
| 2325 | return 0; |
| 2326 | } |
| 2327 | |
| 2328 | static int |
| 2329 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2330 | { |
| 2331 | PyObject *encoded = NULL; |
| 2332 | Py_ssize_t size; |
Serhiy Storchaka | 85b0f5b | 2016-11-20 10:16:47 +0200 | [diff] [blame] | 2333 | const char *data; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2334 | int r; |
| 2335 | |
| 2336 | if (PyUnicode_READY(obj)) |
| 2337 | return -1; |
| 2338 | |
| 2339 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2340 | if (data != NULL) |
| 2341 | return write_utf8(self, data, size); |
| 2342 | |
| 2343 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2344 | "surrogatepass" error handler. */ |
| 2345 | PyErr_Clear(); |
| 2346 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2347 | if (encoded == NULL) |
| 2348 | return -1; |
| 2349 | |
| 2350 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2351 | PyBytes_GET_SIZE(encoded)); |
| 2352 | Py_DECREF(encoded); |
| 2353 | return r; |
| 2354 | } |
| 2355 | |
| 2356 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2357 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2358 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2359 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2360 | if (write_unicode_binary(self, obj) < 0) |
| 2361 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2362 | } |
| 2363 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2364 | PyObject *encoded; |
| 2365 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2366 | const char unicode_op = UNICODE; |
| 2367 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2368 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2369 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2370 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2371 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2372 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2373 | Py_DECREF(encoded); |
| 2374 | return -1; |
| 2375 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2376 | |
| 2377 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2378 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2379 | Py_DECREF(encoded); |
| 2380 | return -1; |
| 2381 | } |
| 2382 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2383 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2384 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2385 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2386 | } |
| 2387 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2388 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2389 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2390 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2391 | } |
| 2392 | |
| 2393 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2394 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2395 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2396 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2397 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2398 | |
| 2399 | assert(PyTuple_Size(t) == len); |
| 2400 | |
| 2401 | for (i = 0; i < len; i++) { |
| 2402 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2403 | |
| 2404 | if (element == NULL) |
| 2405 | return -1; |
| 2406 | if (save(self, element, 0) < 0) |
| 2407 | return -1; |
| 2408 | } |
| 2409 | |
| 2410 | return 0; |
| 2411 | } |
| 2412 | |
| 2413 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2414 | * used across protocols to minimize the space needed to pickle them. |
| 2415 | * Tuples are also the only builtin immutable type that can be recursive |
| 2416 | * (a tuple can be reached from itself), and that requires some subtle |
| 2417 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2418 | */ |
| 2419 | static int |
| 2420 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2421 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2422 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2423 | |
| 2424 | const char mark_op = MARK; |
| 2425 | const char tuple_op = TUPLE; |
| 2426 | const char pop_op = POP; |
| 2427 | const char pop_mark_op = POP_MARK; |
| 2428 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2429 | |
| 2430 | if ((len = PyTuple_Size(obj)) < 0) |
| 2431 | return -1; |
| 2432 | |
| 2433 | if (len == 0) { |
| 2434 | char pdata[2]; |
| 2435 | |
| 2436 | if (self->proto) { |
| 2437 | pdata[0] = EMPTY_TUPLE; |
| 2438 | len = 1; |
| 2439 | } |
| 2440 | else { |
| 2441 | pdata[0] = MARK; |
| 2442 | pdata[1] = TUPLE; |
| 2443 | len = 2; |
| 2444 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2445 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2446 | return -1; |
| 2447 | return 0; |
| 2448 | } |
| 2449 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2450 | /* 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] | 2451 | * saving the tuple elements, the tuple must be recursive, in |
| 2452 | * which case we'll pop everything we put on the stack, and fetch |
| 2453 | * its value from the memo. |
| 2454 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2455 | if (len <= 3 && self->proto >= 2) { |
| 2456 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2457 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2458 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2459 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2460 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2461 | /* pop the len elements */ |
| 2462 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2463 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2464 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2465 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2466 | if (memo_get(self, obj) < 0) |
| 2467 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2468 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2469 | return 0; |
| 2470 | } |
| 2471 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2472 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2473 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2474 | } |
| 2475 | goto memoize; |
| 2476 | } |
| 2477 | |
| 2478 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2479 | * Generate MARK e1 e2 ... TUPLE |
| 2480 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2481 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2482 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2483 | |
| 2484 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2485 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2486 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2487 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2488 | /* pop the stack stuff we pushed */ |
| 2489 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2490 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2491 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2492 | } |
| 2493 | else { |
| 2494 | /* Note that we pop one more than len, to remove |
| 2495 | * the MARK too. |
| 2496 | */ |
| 2497 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2498 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2499 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2500 | } |
| 2501 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2502 | if (memo_get(self, obj) < 0) |
| 2503 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2504 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2505 | return 0; |
| 2506 | } |
| 2507 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2508 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2509 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2510 | } |
| 2511 | |
| 2512 | memoize: |
| 2513 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2514 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2515 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2516 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
| 2519 | /* iter is an iterator giving items, and we batch up chunks of |
| 2520 | * MARK item item ... item APPENDS |
| 2521 | * opcode sequences. Calling code should have arranged to first create an |
| 2522 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2523 | * Returns 0 on success, <0 on error. |
| 2524 | */ |
| 2525 | static int |
| 2526 | batch_list(PicklerObject *self, PyObject *iter) |
| 2527 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2528 | PyObject *obj = NULL; |
| 2529 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2530 | int i, n; |
| 2531 | |
| 2532 | const char mark_op = MARK; |
| 2533 | const char append_op = APPEND; |
| 2534 | const char appends_op = APPENDS; |
| 2535 | |
| 2536 | assert(iter != NULL); |
| 2537 | |
| 2538 | /* XXX: I think this function could be made faster by avoiding the |
| 2539 | iterator interface and fetching objects directly from list using |
| 2540 | PyList_GET_ITEM. |
| 2541 | */ |
| 2542 | |
| 2543 | if (self->proto == 0) { |
| 2544 | /* APPENDS isn't available; do one at a time. */ |
| 2545 | for (;;) { |
| 2546 | obj = PyIter_Next(iter); |
| 2547 | if (obj == NULL) { |
| 2548 | if (PyErr_Occurred()) |
| 2549 | return -1; |
| 2550 | break; |
| 2551 | } |
| 2552 | i = save(self, obj, 0); |
| 2553 | Py_DECREF(obj); |
| 2554 | if (i < 0) |
| 2555 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2556 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2557 | return -1; |
| 2558 | } |
| 2559 | return 0; |
| 2560 | } |
| 2561 | |
| 2562 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2563 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2564 | /* Get first item */ |
| 2565 | firstitem = PyIter_Next(iter); |
| 2566 | if (firstitem == NULL) { |
| 2567 | if (PyErr_Occurred()) |
| 2568 | goto error; |
| 2569 | |
| 2570 | /* nothing more to add */ |
| 2571 | break; |
| 2572 | } |
| 2573 | |
| 2574 | /* Try to get a second item */ |
| 2575 | obj = PyIter_Next(iter); |
| 2576 | if (obj == NULL) { |
| 2577 | if (PyErr_Occurred()) |
| 2578 | goto error; |
| 2579 | |
| 2580 | /* Only one item to write */ |
| 2581 | if (save(self, firstitem, 0) < 0) |
| 2582 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2583 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2584 | goto error; |
| 2585 | Py_CLEAR(firstitem); |
| 2586 | break; |
| 2587 | } |
| 2588 | |
| 2589 | /* More than one item to write */ |
| 2590 | |
| 2591 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2592 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2593 | goto error; |
| 2594 | |
| 2595 | if (save(self, firstitem, 0) < 0) |
| 2596 | goto error; |
| 2597 | Py_CLEAR(firstitem); |
| 2598 | n = 1; |
| 2599 | |
| 2600 | /* Fetch and save up to BATCHSIZE items */ |
| 2601 | while (obj) { |
| 2602 | if (save(self, obj, 0) < 0) |
| 2603 | goto error; |
| 2604 | Py_CLEAR(obj); |
| 2605 | n += 1; |
| 2606 | |
| 2607 | if (n == BATCHSIZE) |
| 2608 | break; |
| 2609 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2610 | obj = PyIter_Next(iter); |
| 2611 | if (obj == NULL) { |
| 2612 | if (PyErr_Occurred()) |
| 2613 | goto error; |
| 2614 | break; |
| 2615 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2616 | } |
| 2617 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2618 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2619 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2620 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2621 | } while (n == BATCHSIZE); |
| 2622 | return 0; |
| 2623 | |
| 2624 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2625 | Py_XDECREF(firstitem); |
| 2626 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2627 | return -1; |
| 2628 | } |
| 2629 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2630 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2631 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2632 | * MARK item item ... item APPENDS |
| 2633 | * opcode sequences. Calling code should have arranged to first create an |
| 2634 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2635 | * Returns 0 on success, -1 on error. |
| 2636 | * |
| 2637 | * This version is considerably faster than batch_list(), if less general. |
| 2638 | * |
| 2639 | * Note that this only works for protocols > 0. |
| 2640 | */ |
| 2641 | static int |
| 2642 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2643 | { |
| 2644 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2645 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2646 | |
| 2647 | const char append_op = APPEND; |
| 2648 | const char appends_op = APPENDS; |
| 2649 | const char mark_op = MARK; |
| 2650 | |
| 2651 | assert(obj != NULL); |
| 2652 | assert(self->proto > 0); |
| 2653 | assert(PyList_CheckExact(obj)); |
| 2654 | |
| 2655 | if (PyList_GET_SIZE(obj) == 1) { |
| 2656 | item = PyList_GET_ITEM(obj, 0); |
| 2657 | if (save(self, item, 0) < 0) |
| 2658 | return -1; |
| 2659 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2660 | return -1; |
| 2661 | return 0; |
| 2662 | } |
| 2663 | |
| 2664 | /* Write in batches of BATCHSIZE. */ |
| 2665 | total = 0; |
| 2666 | do { |
| 2667 | this_batch = 0; |
| 2668 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2669 | return -1; |
| 2670 | while (total < PyList_GET_SIZE(obj)) { |
| 2671 | item = PyList_GET_ITEM(obj, total); |
| 2672 | if (save(self, item, 0) < 0) |
| 2673 | return -1; |
| 2674 | total++; |
| 2675 | if (++this_batch == BATCHSIZE) |
| 2676 | break; |
| 2677 | } |
| 2678 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2679 | return -1; |
| 2680 | |
| 2681 | } while (total < PyList_GET_SIZE(obj)); |
| 2682 | |
| 2683 | return 0; |
| 2684 | } |
| 2685 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2686 | static int |
| 2687 | save_list(PicklerObject *self, PyObject *obj) |
| 2688 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2689 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2690 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2691 | int status = 0; |
| 2692 | |
| 2693 | if (self->fast && !fast_save_enter(self, obj)) |
| 2694 | goto error; |
| 2695 | |
| 2696 | /* Create an empty list. */ |
| 2697 | if (self->bin) { |
| 2698 | header[0] = EMPTY_LIST; |
| 2699 | len = 1; |
| 2700 | } |
| 2701 | else { |
| 2702 | header[0] = MARK; |
| 2703 | header[1] = LIST; |
| 2704 | len = 2; |
| 2705 | } |
| 2706 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2707 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2708 | goto error; |
| 2709 | |
| 2710 | /* Get list length, and bow out early if empty. */ |
| 2711 | if ((len = PyList_Size(obj)) < 0) |
| 2712 | goto error; |
| 2713 | |
| 2714 | if (memo_put(self, obj) < 0) |
| 2715 | goto error; |
| 2716 | |
| 2717 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2718 | /* Materialize the list elements. */ |
| 2719 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2720 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2721 | goto error; |
| 2722 | status = batch_list_exact(self, obj); |
| 2723 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2724 | } else { |
| 2725 | PyObject *iter = PyObject_GetIter(obj); |
| 2726 | if (iter == NULL) |
| 2727 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2728 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2729 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2730 | Py_DECREF(iter); |
| 2731 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2732 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2733 | status = batch_list(self, iter); |
| 2734 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2735 | Py_DECREF(iter); |
| 2736 | } |
| 2737 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2738 | if (0) { |
| 2739 | error: |
| 2740 | status = -1; |
| 2741 | } |
| 2742 | |
| 2743 | if (self->fast && !fast_save_leave(self, obj)) |
| 2744 | status = -1; |
| 2745 | |
| 2746 | return status; |
| 2747 | } |
| 2748 | |
| 2749 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2750 | * MARK key value ... key value SETITEMS |
| 2751 | * opcode sequences. Calling code should have arranged to first create an |
| 2752 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2753 | * Returns 0 on success, <0 on error. |
| 2754 | * |
| 2755 | * This is very much like batch_list(). The difference between saving |
| 2756 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2757 | * the C level, though, that attempts to combine these routines were too |
| 2758 | * ugly to bear. |
| 2759 | */ |
| 2760 | static int |
| 2761 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2762 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2763 | PyObject *obj = NULL; |
| 2764 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2765 | int i, n; |
| 2766 | |
| 2767 | const char mark_op = MARK; |
| 2768 | const char setitem_op = SETITEM; |
| 2769 | const char setitems_op = SETITEMS; |
| 2770 | |
| 2771 | assert(iter != NULL); |
| 2772 | |
| 2773 | if (self->proto == 0) { |
| 2774 | /* SETITEMS isn't available; do one at a time. */ |
| 2775 | for (;;) { |
| 2776 | obj = PyIter_Next(iter); |
| 2777 | if (obj == NULL) { |
| 2778 | if (PyErr_Occurred()) |
| 2779 | return -1; |
| 2780 | break; |
| 2781 | } |
| 2782 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2783 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2784 | "iterator must return 2-tuples"); |
| 2785 | return -1; |
| 2786 | } |
| 2787 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2788 | if (i >= 0) |
| 2789 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2790 | Py_DECREF(obj); |
| 2791 | if (i < 0) |
| 2792 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2793 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2794 | return -1; |
| 2795 | } |
| 2796 | return 0; |
| 2797 | } |
| 2798 | |
| 2799 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2800 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2801 | /* Get first item */ |
| 2802 | firstitem = PyIter_Next(iter); |
| 2803 | if (firstitem == NULL) { |
| 2804 | if (PyErr_Occurred()) |
| 2805 | goto error; |
| 2806 | |
| 2807 | /* nothing more to add */ |
| 2808 | break; |
| 2809 | } |
| 2810 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2811 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2812 | "iterator must return 2-tuples"); |
| 2813 | goto error; |
| 2814 | } |
| 2815 | |
| 2816 | /* Try to get a second item */ |
| 2817 | obj = PyIter_Next(iter); |
| 2818 | if (obj == NULL) { |
| 2819 | if (PyErr_Occurred()) |
| 2820 | goto error; |
| 2821 | |
| 2822 | /* Only one item to write */ |
| 2823 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2824 | goto error; |
| 2825 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2826 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2827 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2828 | goto error; |
| 2829 | Py_CLEAR(firstitem); |
| 2830 | break; |
| 2831 | } |
| 2832 | |
| 2833 | /* More than one item to write */ |
| 2834 | |
| 2835 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2836 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2837 | goto error; |
| 2838 | |
| 2839 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2840 | goto error; |
| 2841 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2842 | goto error; |
| 2843 | Py_CLEAR(firstitem); |
| 2844 | n = 1; |
| 2845 | |
| 2846 | /* Fetch and save up to BATCHSIZE items */ |
| 2847 | while (obj) { |
| 2848 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2849 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2850 | "iterator must return 2-tuples"); |
| 2851 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2852 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2853 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2854 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2855 | goto error; |
| 2856 | Py_CLEAR(obj); |
| 2857 | n += 1; |
| 2858 | |
| 2859 | if (n == BATCHSIZE) |
| 2860 | break; |
| 2861 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2862 | obj = PyIter_Next(iter); |
| 2863 | if (obj == NULL) { |
| 2864 | if (PyErr_Occurred()) |
| 2865 | goto error; |
| 2866 | break; |
| 2867 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2868 | } |
| 2869 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2870 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2871 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2872 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2873 | } while (n == BATCHSIZE); |
| 2874 | return 0; |
| 2875 | |
| 2876 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2877 | Py_XDECREF(firstitem); |
| 2878 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2879 | return -1; |
| 2880 | } |
| 2881 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2882 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2883 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2884 | * MARK key value ... key value SETITEMS |
| 2885 | * opcode sequences. Calling code should have arranged to first create an |
| 2886 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2887 | * Returns 0 on success, -1 on error. |
| 2888 | * |
| 2889 | * Note that this currently doesn't work for protocol 0. |
| 2890 | */ |
| 2891 | static int |
| 2892 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2893 | { |
| 2894 | PyObject *key = NULL, *value = NULL; |
| 2895 | int i; |
| 2896 | Py_ssize_t dict_size, ppos = 0; |
| 2897 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2898 | const char mark_op = MARK; |
| 2899 | const char setitem_op = SETITEM; |
| 2900 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2901 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2902 | assert(obj != NULL && PyDict_CheckExact(obj)); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2903 | assert(self->proto > 0); |
| 2904 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2905 | dict_size = PyDict_GET_SIZE(obj); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2906 | |
| 2907 | /* Special-case len(d) == 1 to save space. */ |
| 2908 | if (dict_size == 1) { |
| 2909 | PyDict_Next(obj, &ppos, &key, &value); |
| 2910 | if (save(self, key, 0) < 0) |
| 2911 | return -1; |
| 2912 | if (save(self, value, 0) < 0) |
| 2913 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2914 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2915 | return -1; |
| 2916 | return 0; |
| 2917 | } |
| 2918 | |
| 2919 | /* Write in batches of BATCHSIZE. */ |
| 2920 | do { |
| 2921 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2922 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2923 | return -1; |
| 2924 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2925 | if (save(self, key, 0) < 0) |
| 2926 | return -1; |
| 2927 | if (save(self, value, 0) < 0) |
| 2928 | return -1; |
| 2929 | if (++i == BATCHSIZE) |
| 2930 | break; |
| 2931 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2932 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2933 | return -1; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2934 | if (PyDict_GET_SIZE(obj) != dict_size) { |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2935 | PyErr_Format( |
| 2936 | PyExc_RuntimeError, |
| 2937 | "dictionary changed size during iteration"); |
| 2938 | return -1; |
| 2939 | } |
| 2940 | |
| 2941 | } while (i == BATCHSIZE); |
| 2942 | return 0; |
| 2943 | } |
| 2944 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2945 | static int |
| 2946 | save_dict(PicklerObject *self, PyObject *obj) |
| 2947 | { |
| 2948 | PyObject *items, *iter; |
| 2949 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2950 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2951 | int status = 0; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2952 | assert(PyDict_Check(obj)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2953 | |
| 2954 | if (self->fast && !fast_save_enter(self, obj)) |
| 2955 | goto error; |
| 2956 | |
| 2957 | /* Create an empty dict. */ |
| 2958 | if (self->bin) { |
| 2959 | header[0] = EMPTY_DICT; |
| 2960 | len = 1; |
| 2961 | } |
| 2962 | else { |
| 2963 | header[0] = MARK; |
| 2964 | header[1] = DICT; |
| 2965 | len = 2; |
| 2966 | } |
| 2967 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2968 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2969 | goto error; |
| 2970 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2971 | if (memo_put(self, obj) < 0) |
| 2972 | goto error; |
| 2973 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2974 | if (PyDict_GET_SIZE(obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2975 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2976 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2977 | /* We can take certain shortcuts if we know this is a dict and |
| 2978 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2979 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2980 | goto error; |
| 2981 | status = batch_dict_exact(self, obj); |
| 2982 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2983 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2984 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2985 | |
Victor Stinner | ad8c83a | 2016-09-05 17:53:15 -0700 | [diff] [blame] | 2986 | items = _PyObject_CallMethodId(obj, &PyId_items, NULL); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2987 | if (items == NULL) |
| 2988 | goto error; |
| 2989 | iter = PyObject_GetIter(items); |
| 2990 | Py_DECREF(items); |
| 2991 | if (iter == NULL) |
| 2992 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2993 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2994 | Py_DECREF(iter); |
| 2995 | goto error; |
| 2996 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2997 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2998 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2999 | Py_DECREF(iter); |
| 3000 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | if (0) { |
| 3004 | error: |
| 3005 | status = -1; |
| 3006 | } |
| 3007 | |
| 3008 | if (self->fast && !fast_save_leave(self, obj)) |
| 3009 | status = -1; |
| 3010 | |
| 3011 | return status; |
| 3012 | } |
| 3013 | |
| 3014 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3015 | save_set(PicklerObject *self, PyObject *obj) |
| 3016 | { |
| 3017 | PyObject *item; |
| 3018 | int i; |
| 3019 | Py_ssize_t set_size, ppos = 0; |
| 3020 | Py_hash_t hash; |
| 3021 | |
| 3022 | const char empty_set_op = EMPTY_SET; |
| 3023 | const char mark_op = MARK; |
| 3024 | const char additems_op = ADDITEMS; |
| 3025 | |
| 3026 | if (self->proto < 4) { |
| 3027 | PyObject *items; |
| 3028 | PyObject *reduce_value; |
| 3029 | int status; |
| 3030 | |
| 3031 | items = PySequence_List(obj); |
| 3032 | if (items == NULL) { |
| 3033 | return -1; |
| 3034 | } |
| 3035 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 3036 | Py_DECREF(items); |
| 3037 | if (reduce_value == NULL) { |
| 3038 | return -1; |
| 3039 | } |
| 3040 | /* save_reduce() will memoize the object automatically. */ |
| 3041 | status = save_reduce(self, reduce_value, obj); |
| 3042 | Py_DECREF(reduce_value); |
| 3043 | return status; |
| 3044 | } |
| 3045 | |
| 3046 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 3047 | return -1; |
| 3048 | |
| 3049 | if (memo_put(self, obj) < 0) |
| 3050 | return -1; |
| 3051 | |
| 3052 | set_size = PySet_GET_SIZE(obj); |
| 3053 | if (set_size == 0) |
| 3054 | return 0; /* nothing to do */ |
| 3055 | |
| 3056 | /* Write in batches of BATCHSIZE. */ |
| 3057 | do { |
| 3058 | i = 0; |
| 3059 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3060 | return -1; |
| 3061 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 3062 | if (save(self, item, 0) < 0) |
| 3063 | return -1; |
| 3064 | if (++i == BATCHSIZE) |
| 3065 | break; |
| 3066 | } |
| 3067 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 3068 | return -1; |
| 3069 | if (PySet_GET_SIZE(obj) != set_size) { |
| 3070 | PyErr_Format( |
| 3071 | PyExc_RuntimeError, |
| 3072 | "set changed size during iteration"); |
| 3073 | return -1; |
| 3074 | } |
| 3075 | } while (i == BATCHSIZE); |
| 3076 | |
| 3077 | return 0; |
| 3078 | } |
| 3079 | |
| 3080 | static int |
| 3081 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 3082 | { |
| 3083 | PyObject *iter; |
| 3084 | |
| 3085 | const char mark_op = MARK; |
| 3086 | const char frozenset_op = FROZENSET; |
| 3087 | |
| 3088 | if (self->fast && !fast_save_enter(self, obj)) |
| 3089 | return -1; |
| 3090 | |
| 3091 | if (self->proto < 4) { |
| 3092 | PyObject *items; |
| 3093 | PyObject *reduce_value; |
| 3094 | int status; |
| 3095 | |
| 3096 | items = PySequence_List(obj); |
| 3097 | if (items == NULL) { |
| 3098 | return -1; |
| 3099 | } |
| 3100 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3101 | items); |
| 3102 | Py_DECREF(items); |
| 3103 | if (reduce_value == NULL) { |
| 3104 | return -1; |
| 3105 | } |
| 3106 | /* save_reduce() will memoize the object automatically. */ |
| 3107 | status = save_reduce(self, reduce_value, obj); |
| 3108 | Py_DECREF(reduce_value); |
| 3109 | return status; |
| 3110 | } |
| 3111 | |
| 3112 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3113 | return -1; |
| 3114 | |
| 3115 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3116 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3117 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3118 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3119 | for (;;) { |
| 3120 | PyObject *item; |
| 3121 | |
| 3122 | item = PyIter_Next(iter); |
| 3123 | if (item == NULL) { |
| 3124 | if (PyErr_Occurred()) { |
| 3125 | Py_DECREF(iter); |
| 3126 | return -1; |
| 3127 | } |
| 3128 | break; |
| 3129 | } |
| 3130 | if (save(self, item, 0) < 0) { |
| 3131 | Py_DECREF(item); |
| 3132 | Py_DECREF(iter); |
| 3133 | return -1; |
| 3134 | } |
| 3135 | Py_DECREF(item); |
| 3136 | } |
| 3137 | Py_DECREF(iter); |
| 3138 | |
| 3139 | /* If the object is already in the memo, this means it is |
| 3140 | recursive. In this case, throw away everything we put on the |
| 3141 | stack, and fetch the object back from the memo. */ |
| 3142 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3143 | const char pop_mark_op = POP_MARK; |
| 3144 | |
| 3145 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3146 | return -1; |
| 3147 | if (memo_get(self, obj) < 0) |
| 3148 | return -1; |
| 3149 | return 0; |
| 3150 | } |
| 3151 | |
| 3152 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3153 | return -1; |
| 3154 | if (memo_put(self, obj) < 0) |
| 3155 | return -1; |
| 3156 | |
| 3157 | return 0; |
| 3158 | } |
| 3159 | |
| 3160 | static int |
| 3161 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3162 | { |
| 3163 | PyObject *key; |
| 3164 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3165 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3166 | |
| 3167 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3168 | if (key == NULL) |
| 3169 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3170 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3171 | Py_DECREF(key); |
| 3172 | if (item) { |
| 3173 | PyObject *fixed_module_name; |
| 3174 | PyObject *fixed_global_name; |
| 3175 | |
| 3176 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3177 | PyErr_Format(PyExc_RuntimeError, |
| 3178 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3179 | "should be 2-tuples, not %.200s", |
| 3180 | Py_TYPE(item)->tp_name); |
| 3181 | return -1; |
| 3182 | } |
| 3183 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3184 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3185 | if (!PyUnicode_Check(fixed_module_name) || |
| 3186 | !PyUnicode_Check(fixed_global_name)) { |
| 3187 | PyErr_Format(PyExc_RuntimeError, |
| 3188 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3189 | "should be pairs of str, not (%.200s, %.200s)", |
| 3190 | Py_TYPE(fixed_module_name)->tp_name, |
| 3191 | Py_TYPE(fixed_global_name)->tp_name); |
| 3192 | return -1; |
| 3193 | } |
| 3194 | |
| 3195 | Py_CLEAR(*module_name); |
| 3196 | Py_CLEAR(*global_name); |
| 3197 | Py_INCREF(fixed_module_name); |
| 3198 | Py_INCREF(fixed_global_name); |
| 3199 | *module_name = fixed_module_name; |
| 3200 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3201 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3202 | } |
| 3203 | else if (PyErr_Occurred()) { |
| 3204 | return -1; |
| 3205 | } |
| 3206 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3207 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3208 | if (item) { |
| 3209 | if (!PyUnicode_Check(item)) { |
| 3210 | PyErr_Format(PyExc_RuntimeError, |
| 3211 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3212 | "should be strings, not %.200s", |
| 3213 | Py_TYPE(item)->tp_name); |
| 3214 | return -1; |
| 3215 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3216 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3217 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3218 | } |
| 3219 | else if (PyErr_Occurred()) { |
| 3220 | return -1; |
| 3221 | } |
| 3222 | |
| 3223 | return 0; |
| 3224 | } |
| 3225 | |
| 3226 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3227 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3228 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3229 | PyObject *global_name = NULL; |
| 3230 | PyObject *module_name = NULL; |
| 3231 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3232 | PyObject *parent = NULL; |
| 3233 | PyObject *dotted_path = NULL; |
| 3234 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3235 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3236 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3237 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3238 | _Py_IDENTIFIER(__name__); |
| 3239 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3240 | |
| 3241 | const char global_op = GLOBAL; |
| 3242 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3243 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3244 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3245 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3246 | } |
| 3247 | else { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3248 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3249 | if (global_name == NULL) { |
| 3250 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3251 | goto error; |
| 3252 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3253 | } |
| 3254 | if (global_name == NULL) { |
| 3255 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3256 | if (global_name == NULL) |
| 3257 | goto error; |
| 3258 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3261 | dotted_path = get_dotted_path(module, global_name); |
| 3262 | if (dotted_path == NULL) |
| 3263 | goto error; |
| 3264 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3265 | if (module_name == NULL) |
| 3266 | goto error; |
| 3267 | |
| 3268 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3269 | relative imports. |
| 3270 | |
| 3271 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3272 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3273 | custom import functions (IMHO, this would be a nice security |
| 3274 | feature). The import C API would need to be extended to support the |
| 3275 | extra parameters of __import__ to fix that. */ |
| 3276 | module = PyImport_Import(module_name); |
| 3277 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3278 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3279 | "Can't pickle %R: import of module %R failed", |
| 3280 | obj, module_name); |
| 3281 | goto error; |
| 3282 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3283 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3284 | Py_INCREF(lastname); |
| 3285 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3286 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3287 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3288 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3289 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3290 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3291 | goto error; |
| 3292 | } |
| 3293 | if (cls != obj) { |
| 3294 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3295 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3296 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3297 | obj, module_name, global_name); |
| 3298 | goto error; |
| 3299 | } |
| 3300 | Py_DECREF(cls); |
| 3301 | |
| 3302 | if (self->proto >= 2) { |
| 3303 | /* See whether this is in the extension registry, and if |
| 3304 | * so generate an EXT opcode. |
| 3305 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3306 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3307 | PyObject *code_obj; /* extension code as Python object */ |
| 3308 | long code; /* extension code as C value */ |
| 3309 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3310 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3311 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3312 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3313 | if (extension_key == NULL) { |
| 3314 | goto error; |
| 3315 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3316 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3317 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3318 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3319 | /* The object is not registered in the extension registry. |
| 3320 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3321 | if (code_obj == NULL) { |
| 3322 | if (PyErr_Occurred()) { |
| 3323 | goto error; |
| 3324 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3325 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3326 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3327 | |
| 3328 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3329 | of the value returned by the extension_registry. It should for |
| 3330 | consistency. */ |
| 3331 | |
| 3332 | /* Verify code_obj has the right type and value. */ |
| 3333 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3334 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3335 | "Can't pickle %R: extension code %R isn't an integer", |
| 3336 | obj, code_obj); |
| 3337 | goto error; |
| 3338 | } |
| 3339 | code = PyLong_AS_LONG(code_obj); |
| 3340 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3341 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3342 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3343 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3344 | goto error; |
| 3345 | } |
| 3346 | |
| 3347 | /* Generate an EXT opcode. */ |
| 3348 | if (code <= 0xff) { |
| 3349 | pdata[0] = EXT1; |
| 3350 | pdata[1] = (unsigned char)code; |
| 3351 | n = 2; |
| 3352 | } |
| 3353 | else if (code <= 0xffff) { |
| 3354 | pdata[0] = EXT2; |
| 3355 | pdata[1] = (unsigned char)(code & 0xff); |
| 3356 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3357 | n = 3; |
| 3358 | } |
| 3359 | else { |
| 3360 | pdata[0] = EXT4; |
| 3361 | pdata[1] = (unsigned char)(code & 0xff); |
| 3362 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3363 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3364 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3365 | n = 5; |
| 3366 | } |
| 3367 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3368 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3369 | goto error; |
| 3370 | } |
| 3371 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3372 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3373 | if (parent == module) { |
| 3374 | Py_INCREF(lastname); |
| 3375 | Py_DECREF(global_name); |
| 3376 | global_name = lastname; |
| 3377 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3378 | if (self->proto >= 4) { |
| 3379 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3380 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3381 | if (save(self, module_name, 0) < 0) |
| 3382 | goto error; |
| 3383 | if (save(self, global_name, 0) < 0) |
| 3384 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3385 | |
| 3386 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3387 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3388 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3389 | else if (parent != module) { |
| 3390 | PickleState *st = _Pickle_GetGlobalState(); |
| 3391 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3392 | st->getattr, parent, lastname); |
| 3393 | status = save_reduce(self, reduce_value, NULL); |
| 3394 | Py_DECREF(reduce_value); |
| 3395 | if (status < 0) |
| 3396 | goto error; |
| 3397 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3398 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3399 | /* Generate a normal global opcode if we are using a pickle |
| 3400 | protocol < 4, or if the object is not registered in the |
| 3401 | extension registry. */ |
| 3402 | PyObject *encoded; |
| 3403 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3404 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3405 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3406 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3407 | |
| 3408 | /* For protocol < 3 and if the user didn't request against doing |
| 3409 | so, we convert module names to the old 2.x module names. */ |
| 3410 | if (self->proto < 3 && self->fix_imports) { |
| 3411 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3412 | goto error; |
| 3413 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3414 | } |
| 3415 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3416 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3417 | both the module name and the global name using UTF-8. We do so |
| 3418 | only when we are using the pickle protocol newer than version |
| 3419 | 3. This is to ensure compatibility with older Unpickler running |
| 3420 | on Python 2.x. */ |
| 3421 | if (self->proto == 3) { |
| 3422 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3423 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3424 | else { |
| 3425 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3426 | } |
| 3427 | encoded = unicode_encoder(module_name); |
| 3428 | if (encoded == NULL) { |
| 3429 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3430 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3431 | "can't pickle module identifier '%S' using " |
| 3432 | "pickle protocol %i", |
| 3433 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3434 | goto error; |
| 3435 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3436 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3437 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3438 | Py_DECREF(encoded); |
| 3439 | goto error; |
| 3440 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3441 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3442 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3443 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3444 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3445 | /* Save the name of the module. */ |
| 3446 | encoded = unicode_encoder(global_name); |
| 3447 | if (encoded == NULL) { |
| 3448 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3449 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3450 | "can't pickle global identifier '%S' using " |
| 3451 | "pickle protocol %i", |
| 3452 | global_name, self->proto); |
| 3453 | goto error; |
| 3454 | } |
| 3455 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3456 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3457 | Py_DECREF(encoded); |
| 3458 | goto error; |
| 3459 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3460 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3461 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3462 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3463 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3464 | /* Memoize the object. */ |
| 3465 | if (memo_put(self, obj) < 0) |
| 3466 | goto error; |
| 3467 | } |
| 3468 | |
| 3469 | if (0) { |
| 3470 | error: |
| 3471 | status = -1; |
| 3472 | } |
| 3473 | Py_XDECREF(module_name); |
| 3474 | Py_XDECREF(global_name); |
| 3475 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3476 | Py_XDECREF(parent); |
| 3477 | Py_XDECREF(dotted_path); |
| 3478 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3479 | |
| 3480 | return status; |
| 3481 | } |
| 3482 | |
| 3483 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3484 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3485 | { |
| 3486 | PyObject *reduce_value; |
| 3487 | int status; |
| 3488 | |
| 3489 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3490 | if (reduce_value == NULL) { |
| 3491 | return -1; |
| 3492 | } |
| 3493 | status = save_reduce(self, reduce_value, obj); |
| 3494 | Py_DECREF(reduce_value); |
| 3495 | return status; |
| 3496 | } |
| 3497 | |
| 3498 | static int |
| 3499 | save_type(PicklerObject *self, PyObject *obj) |
| 3500 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3501 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3502 | return save_singleton_type(self, obj, Py_None); |
| 3503 | } |
| 3504 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3505 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3506 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3507 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3508 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3509 | } |
| 3510 | return save_global(self, obj, NULL); |
| 3511 | } |
| 3512 | |
| 3513 | static int |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3514 | save_pers(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3515 | { |
| 3516 | PyObject *pid = NULL; |
| 3517 | int status = 0; |
| 3518 | |
| 3519 | const char persid_op = PERSID; |
| 3520 | const char binpersid_op = BINPERSID; |
| 3521 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3522 | pid = call_method(self->pers_func, self->pers_func_self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3523 | if (pid == NULL) |
| 3524 | return -1; |
| 3525 | |
| 3526 | if (pid != Py_None) { |
| 3527 | if (self->bin) { |
| 3528 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3529 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3530 | goto error; |
| 3531 | } |
| 3532 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3533 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3534 | |
| 3535 | pid_str = PyObject_Str(pid); |
| 3536 | if (pid_str == NULL) |
| 3537 | goto error; |
| 3538 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3539 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3540 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3541 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3542 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3543 | "persistent IDs in protocol 0 must be " |
| 3544 | "ASCII strings"); |
| 3545 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3546 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3547 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3548 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3549 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3550 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3551 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3552 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3553 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3554 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3555 | } |
| 3556 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3557 | } |
| 3558 | status = 1; |
| 3559 | } |
| 3560 | |
| 3561 | if (0) { |
| 3562 | error: |
| 3563 | status = -1; |
| 3564 | } |
| 3565 | Py_XDECREF(pid); |
| 3566 | |
| 3567 | return status; |
| 3568 | } |
| 3569 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3570 | static PyObject * |
| 3571 | get_class(PyObject *obj) |
| 3572 | { |
| 3573 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3574 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3575 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3576 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3577 | if (cls == NULL) { |
| 3578 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3579 | PyErr_Clear(); |
| 3580 | cls = (PyObject *) Py_TYPE(obj); |
| 3581 | Py_INCREF(cls); |
| 3582 | } |
| 3583 | } |
| 3584 | return cls; |
| 3585 | } |
| 3586 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3587 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3588 | * appropriate __reduce__ method for obj. |
| 3589 | */ |
| 3590 | static int |
| 3591 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3592 | { |
| 3593 | PyObject *callable; |
| 3594 | PyObject *argtup; |
| 3595 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3596 | PyObject *listitems = Py_None; |
| 3597 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3598 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3599 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3600 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3601 | |
| 3602 | const char reduce_op = REDUCE; |
| 3603 | const char build_op = BUILD; |
| 3604 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3605 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3606 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3607 | size = PyTuple_Size(args); |
| 3608 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3609 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3610 | "__reduce__ must contain 2 through 5 elements"); |
| 3611 | return -1; |
| 3612 | } |
| 3613 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3614 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3615 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3616 | return -1; |
| 3617 | |
| 3618 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3619 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3620 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3621 | return -1; |
| 3622 | } |
| 3623 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3624 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3625 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3626 | return -1; |
| 3627 | } |
| 3628 | |
| 3629 | if (state == Py_None) |
| 3630 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3631 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3632 | if (listitems == Py_None) |
| 3633 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3634 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3635 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3636 | "returned by __reduce__ must be an iterator, not %s", |
| 3637 | Py_TYPE(listitems)->tp_name); |
| 3638 | return -1; |
| 3639 | } |
| 3640 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3641 | if (dictitems == Py_None) |
| 3642 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3643 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3644 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3645 | "returned by __reduce__ must be an iterator, not %s", |
| 3646 | Py_TYPE(dictitems)->tp_name); |
| 3647 | return -1; |
| 3648 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3649 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3650 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3651 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3652 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3653 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3654 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3655 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3656 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3657 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3658 | } |
| 3659 | PyErr_Clear(); |
| 3660 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3661 | else if (PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3662 | _Py_IDENTIFIER(__newobj_ex__); |
Serhiy Storchaka | f0f35a6 | 2017-01-09 10:09:43 +0200 | [diff] [blame] | 3663 | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
| 3664 | name, &PyId___newobj_ex__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3665 | if (!use_newobj_ex) { |
| 3666 | _Py_IDENTIFIER(__newobj__); |
Serhiy Storchaka | 9937d90 | 2017-01-09 10:04:34 +0200 | [diff] [blame] | 3667 | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3668 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3669 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3670 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3671 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3672 | |
| 3673 | if (use_newobj_ex) { |
| 3674 | PyObject *cls; |
| 3675 | PyObject *args; |
| 3676 | PyObject *kwargs; |
| 3677 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3678 | if (PyTuple_GET_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3679 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3680 | "length of the NEWOBJ_EX argument tuple must be " |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3681 | "exactly 3, not %zd", PyTuple_GET_SIZE(argtup)); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3682 | return -1; |
| 3683 | } |
| 3684 | |
| 3685 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3686 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3687 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3688 | "first item from NEWOBJ_EX argument tuple must " |
| 3689 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3690 | return -1; |
| 3691 | } |
| 3692 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3693 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3694 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3695 | "second item from NEWOBJ_EX argument tuple must " |
| 3696 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3697 | return -1; |
| 3698 | } |
| 3699 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3700 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3701 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3702 | "third item from NEWOBJ_EX argument tuple must " |
| 3703 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3704 | return -1; |
| 3705 | } |
| 3706 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3707 | if (self->proto >= 4) { |
| 3708 | if (save(self, cls, 0) < 0 || |
| 3709 | save(self, args, 0) < 0 || |
| 3710 | save(self, kwargs, 0) < 0 || |
| 3711 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3712 | return -1; |
| 3713 | } |
| 3714 | } |
| 3715 | else { |
| 3716 | PyObject *newargs; |
| 3717 | PyObject *cls_new; |
| 3718 | Py_ssize_t i; |
| 3719 | _Py_IDENTIFIER(__new__); |
| 3720 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3721 | newargs = PyTuple_New(PyTuple_GET_SIZE(args) + 2); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3722 | if (newargs == NULL) |
| 3723 | return -1; |
| 3724 | |
| 3725 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 3726 | if (cls_new == NULL) { |
| 3727 | Py_DECREF(newargs); |
| 3728 | return -1; |
| 3729 | } |
| 3730 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 3731 | Py_INCREF(cls); |
| 3732 | PyTuple_SET_ITEM(newargs, 1, cls); |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3733 | for (i = 0; i < PyTuple_GET_SIZE(args); i++) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3734 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 3735 | Py_INCREF(item); |
| 3736 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 3737 | } |
| 3738 | |
| 3739 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 3740 | Py_DECREF(newargs); |
| 3741 | if (callable == NULL) |
| 3742 | return -1; |
| 3743 | |
| 3744 | newargs = PyTuple_New(0); |
| 3745 | if (newargs == NULL) { |
| 3746 | Py_DECREF(callable); |
| 3747 | return -1; |
| 3748 | } |
| 3749 | |
| 3750 | if (save(self, callable, 0) < 0 || |
| 3751 | save(self, newargs, 0) < 0 || |
| 3752 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 3753 | Py_DECREF(newargs); |
| 3754 | Py_DECREF(callable); |
| 3755 | return -1; |
| 3756 | } |
| 3757 | Py_DECREF(newargs); |
| 3758 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3759 | } |
| 3760 | } |
| 3761 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3762 | PyObject *cls; |
| 3763 | PyObject *newargtup; |
| 3764 | PyObject *obj_class; |
| 3765 | int p; |
| 3766 | |
| 3767 | /* Sanity checks. */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3768 | if (PyTuple_GET_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3769 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3770 | return -1; |
| 3771 | } |
| 3772 | |
| 3773 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3774 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3775 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3776 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3777 | return -1; |
| 3778 | } |
| 3779 | |
| 3780 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3781 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3782 | p = obj_class != cls; /* true iff a problem */ |
| 3783 | Py_DECREF(obj_class); |
| 3784 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3785 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3786 | "__newobj__ args has the wrong class"); |
| 3787 | return -1; |
| 3788 | } |
| 3789 | } |
| 3790 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3791 | what happen if the value returned by the __reduce__() method of |
| 3792 | some extension type contains another object of the same type. Ouch! |
| 3793 | |
| 3794 | Here is a quick example, that I ran into, to illustrate what I |
| 3795 | mean: |
| 3796 | |
| 3797 | >>> import pickle, copyreg |
| 3798 | >>> copyreg.dispatch_table.pop(complex) |
| 3799 | >>> pickle.dumps(1+2j) |
| 3800 | Traceback (most recent call last): |
| 3801 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3802 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3803 | |
| 3804 | Removing the complex class from copyreg.dispatch_table made the |
| 3805 | __reduce_ex__() method emit another complex object: |
| 3806 | |
| 3807 | >>> (1+1j).__reduce_ex__(2) |
| 3808 | (<function __newobj__ at 0xb7b71c3c>, |
| 3809 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3810 | |
| 3811 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3812 | ensued. Of course, the bug was in the complex class which had a |
| 3813 | broken __getnewargs__() that emitted another complex object. But, |
| 3814 | the point, here, is it is quite easy to end up with a broken reduce |
| 3815 | function. */ |
| 3816 | |
| 3817 | /* Save the class and its __new__ arguments. */ |
| 3818 | if (save(self, cls, 0) < 0) |
| 3819 | return -1; |
| 3820 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 3821 | newargtup = PyTuple_GetSlice(argtup, 1, PyTuple_GET_SIZE(argtup)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3822 | if (newargtup == NULL) |
| 3823 | return -1; |
| 3824 | |
| 3825 | p = save(self, newargtup, 0); |
| 3826 | Py_DECREF(newargtup); |
| 3827 | if (p < 0) |
| 3828 | return -1; |
| 3829 | |
| 3830 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3831 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3832 | return -1; |
| 3833 | } |
| 3834 | else { /* Not using NEWOBJ. */ |
| 3835 | if (save(self, callable, 0) < 0 || |
| 3836 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3837 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3838 | return -1; |
| 3839 | } |
| 3840 | |
| 3841 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3842 | the caller do not want to memoize the object. Not particularly useful, |
| 3843 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3844 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3845 | if (obj != NULL) { |
| 3846 | /* If the object is already in the memo, this means it is |
| 3847 | recursive. In this case, throw away everything we put on the |
| 3848 | stack, and fetch the object back from the memo. */ |
| 3849 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3850 | const char pop_op = POP; |
| 3851 | |
| 3852 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3853 | return -1; |
| 3854 | if (memo_get(self, obj) < 0) |
| 3855 | return -1; |
| 3856 | |
| 3857 | return 0; |
| 3858 | } |
| 3859 | else if (memo_put(self, obj) < 0) |
| 3860 | return -1; |
| 3861 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3862 | |
| 3863 | if (listitems && batch_list(self, listitems) < 0) |
| 3864 | return -1; |
| 3865 | |
| 3866 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3867 | return -1; |
| 3868 | |
| 3869 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3870 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3871 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3872 | return -1; |
| 3873 | } |
| 3874 | |
| 3875 | return 0; |
| 3876 | } |
| 3877 | |
| 3878 | static int |
| 3879 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3880 | { |
| 3881 | PyTypeObject *type; |
| 3882 | PyObject *reduce_func = NULL; |
| 3883 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3884 | int status = 0; |
| 3885 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3886 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3887 | return -1; |
| 3888 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3889 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3890 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3891 | |
| 3892 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3893 | on its returned object. */ |
| 3894 | if (!pers_save && self->pers_func) { |
| 3895 | /* save_pers() returns: |
| 3896 | -1 to signal an error; |
| 3897 | 0 if it did nothing successfully; |
| 3898 | 1 if a persistent id was saved. |
| 3899 | */ |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 3900 | if ((status = save_pers(self, obj)) != 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3901 | goto done; |
| 3902 | } |
| 3903 | |
| 3904 | type = Py_TYPE(obj); |
| 3905 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3906 | /* The old cPickle had an optimization that used switch-case statement |
| 3907 | dispatching on the first letter of the type name. This has was removed |
| 3908 | since benchmarks shown that this optimization was actually slowing |
| 3909 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3910 | |
| 3911 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3912 | |
| 3913 | if (obj == Py_None) { |
| 3914 | status = save_none(self, obj); |
| 3915 | goto done; |
| 3916 | } |
| 3917 | else if (obj == Py_False || obj == Py_True) { |
| 3918 | status = save_bool(self, obj); |
| 3919 | goto done; |
| 3920 | } |
| 3921 | else if (type == &PyLong_Type) { |
| 3922 | status = save_long(self, obj); |
| 3923 | goto done; |
| 3924 | } |
| 3925 | else if (type == &PyFloat_Type) { |
| 3926 | status = save_float(self, obj); |
| 3927 | goto done; |
| 3928 | } |
| 3929 | |
| 3930 | /* Check the memo to see if it has the object. If so, generate |
| 3931 | a GET (or BINGET) opcode, instead of pickling the object |
| 3932 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3933 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3934 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3935 | goto error; |
| 3936 | goto done; |
| 3937 | } |
| 3938 | |
| 3939 | if (type == &PyBytes_Type) { |
| 3940 | status = save_bytes(self, obj); |
| 3941 | goto done; |
| 3942 | } |
| 3943 | else if (type == &PyUnicode_Type) { |
| 3944 | status = save_unicode(self, obj); |
| 3945 | goto done; |
| 3946 | } |
| 3947 | else if (type == &PyDict_Type) { |
| 3948 | status = save_dict(self, obj); |
| 3949 | goto done; |
| 3950 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3951 | else if (type == &PySet_Type) { |
| 3952 | status = save_set(self, obj); |
| 3953 | goto done; |
| 3954 | } |
| 3955 | else if (type == &PyFrozenSet_Type) { |
| 3956 | status = save_frozenset(self, obj); |
| 3957 | goto done; |
| 3958 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3959 | else if (type == &PyList_Type) { |
| 3960 | status = save_list(self, obj); |
| 3961 | goto done; |
| 3962 | } |
| 3963 | else if (type == &PyTuple_Type) { |
| 3964 | status = save_tuple(self, obj); |
| 3965 | goto done; |
| 3966 | } |
| 3967 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3968 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3969 | goto done; |
| 3970 | } |
| 3971 | else if (type == &PyFunction_Type) { |
| 3972 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3973 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3974 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3975 | |
| 3976 | /* XXX: This part needs some unit tests. */ |
| 3977 | |
| 3978 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3979 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3980 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3981 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3982 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3983 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3984 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3985 | (PyObject *)type); |
| 3986 | if (reduce_func == NULL) { |
| 3987 | if (PyErr_Occurred()) { |
| 3988 | goto error; |
| 3989 | } |
| 3990 | } else { |
| 3991 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3992 | Increase the reference count to be consistent with |
| 3993 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3994 | Py_INCREF(reduce_func); |
| 3995 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3996 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3997 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3998 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3999 | if (reduce_func == NULL) { |
| 4000 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 4001 | PyErr_Clear(); |
| 4002 | else |
| 4003 | goto error; |
| 4004 | } |
| 4005 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4006 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4007 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4008 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4009 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 4010 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 4011 | status = save_global(self, obj, NULL); |
| 4012 | goto done; |
| 4013 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4014 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4015 | _Py_IDENTIFIER(__reduce__); |
| 4016 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4017 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4018 | |
| 4019 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 4020 | automatically defined as __reduce__. While this is convenient, this |
| 4021 | make it impossible to know which method was actually called. Of |
| 4022 | course, this is not a big deal. But still, it would be nice to let |
| 4023 | the user know which method was called when something go |
| 4024 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 4025 | don't actually have to check for a __reduce__ method. */ |
| 4026 | |
| 4027 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4028 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4029 | if (reduce_func != NULL) { |
| 4030 | PyObject *proto; |
| 4031 | proto = PyLong_FromLong(self->proto); |
| 4032 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 4033 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4034 | } |
| 4035 | } |
| 4036 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4037 | PickleState *st = _Pickle_GetGlobalState(); |
| 4038 | |
| 4039 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4040 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4041 | } |
| 4042 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4043 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4044 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4045 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4046 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4047 | if (reduce_func != NULL) { |
Victor Stinner | 559bb6a | 2016-08-22 22:48:54 +0200 | [diff] [blame] | 4048 | reduce_value = _PyObject_CallNoArg(reduce_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4049 | } |
| 4050 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4051 | PyErr_Format(st->PicklingError, |
| 4052 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4053 | type->tp_name, obj); |
| 4054 | goto error; |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | |
| 4059 | if (reduce_value == NULL) |
| 4060 | goto error; |
| 4061 | |
| 4062 | if (PyUnicode_Check(reduce_value)) { |
| 4063 | status = save_global(self, obj, reduce_value); |
| 4064 | goto done; |
| 4065 | } |
| 4066 | |
| 4067 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4068 | PickleState *st = _Pickle_GetGlobalState(); |
| 4069 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4070 | "__reduce__ must return a string or tuple"); |
| 4071 | goto error; |
| 4072 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4073 | |
| 4074 | status = save_reduce(self, reduce_value, obj); |
| 4075 | |
| 4076 | if (0) { |
| 4077 | error: |
| 4078 | status = -1; |
| 4079 | } |
| 4080 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 4081 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 4082 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4083 | Py_XDECREF(reduce_func); |
| 4084 | Py_XDECREF(reduce_value); |
| 4085 | |
| 4086 | return status; |
| 4087 | } |
| 4088 | |
| 4089 | static int |
| 4090 | dump(PicklerObject *self, PyObject *obj) |
| 4091 | { |
| 4092 | const char stop_op = STOP; |
| 4093 | |
| 4094 | if (self->proto >= 2) { |
| 4095 | char header[2]; |
| 4096 | |
| 4097 | header[0] = PROTO; |
| 4098 | assert(self->proto >= 0 && self->proto < 256); |
| 4099 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4100 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4101 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4102 | if (self->proto >= 4) |
| 4103 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4104 | } |
| 4105 | |
| 4106 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4107 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4108 | return -1; |
| 4109 | |
| 4110 | return 0; |
| 4111 | } |
| 4112 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4113 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4114 | |
| 4115 | _pickle.Pickler.clear_memo |
| 4116 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4117 | Clears the pickler's "memo". |
| 4118 | |
| 4119 | The memo is the data structure that remembers which objects the |
| 4120 | pickler has already seen, so that shared or recursive objects are |
| 4121 | pickled by reference and not by value. This method is useful when |
| 4122 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4123 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4124 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4125 | static PyObject * |
| 4126 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4127 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4128 | { |
| 4129 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4130 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4131 | |
| 4132 | Py_RETURN_NONE; |
| 4133 | } |
| 4134 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4135 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4136 | |
| 4137 | _pickle.Pickler.dump |
| 4138 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4139 | obj: object |
| 4140 | / |
| 4141 | |
| 4142 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4143 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4144 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4145 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4146 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4147 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4148 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4149 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4150 | Developers often forget to call __init__() in their subclasses, which |
| 4151 | would trigger a segfault without this check. */ |
| 4152 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4153 | PickleState *st = _Pickle_GetGlobalState(); |
| 4154 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4155 | "Pickler.__init__() was not called by %s.__init__()", |
| 4156 | Py_TYPE(self)->tp_name); |
| 4157 | return NULL; |
| 4158 | } |
| 4159 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4160 | if (_Pickler_ClearBuffer(self) < 0) |
| 4161 | return NULL; |
| 4162 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4163 | if (dump(self, obj) < 0) |
| 4164 | return NULL; |
| 4165 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4166 | if (_Pickler_FlushToFile(self) < 0) |
| 4167 | return NULL; |
| 4168 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4169 | Py_RETURN_NONE; |
| 4170 | } |
| 4171 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4172 | /*[clinic input] |
| 4173 | |
| 4174 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4175 | |
| 4176 | Returns size in memory, in bytes. |
| 4177 | [clinic start generated code]*/ |
| 4178 | |
| 4179 | static Py_ssize_t |
| 4180 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4181 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4182 | { |
| 4183 | Py_ssize_t res, s; |
| 4184 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4185 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4186 | if (self->memo != NULL) { |
| 4187 | res += sizeof(PyMemoTable); |
| 4188 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4189 | } |
| 4190 | if (self->output_buffer != NULL) { |
| 4191 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4192 | if (s == -1) |
| 4193 | return -1; |
| 4194 | res += s; |
| 4195 | } |
| 4196 | return res; |
| 4197 | } |
| 4198 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4199 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4200 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4201 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4202 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4203 | {NULL, NULL} /* sentinel */ |
| 4204 | }; |
| 4205 | |
| 4206 | static void |
| 4207 | Pickler_dealloc(PicklerObject *self) |
| 4208 | { |
| 4209 | PyObject_GC_UnTrack(self); |
| 4210 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4211 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4212 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4213 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4214 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4215 | Py_XDECREF(self->fast_memo); |
| 4216 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4217 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4218 | |
| 4219 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4220 | } |
| 4221 | |
| 4222 | static int |
| 4223 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4224 | { |
| 4225 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4226 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4227 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4228 | Py_VISIT(self->fast_memo); |
| 4229 | return 0; |
| 4230 | } |
| 4231 | |
| 4232 | static int |
| 4233 | Pickler_clear(PicklerObject *self) |
| 4234 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4235 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4236 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4237 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4238 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4239 | Py_CLEAR(self->fast_memo); |
| 4240 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4241 | if (self->memo != NULL) { |
| 4242 | PyMemoTable *memo = self->memo; |
| 4243 | self->memo = NULL; |
| 4244 | PyMemoTable_Del(memo); |
| 4245 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4246 | return 0; |
| 4247 | } |
| 4248 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4249 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4250 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4251 | |
| 4252 | _pickle.Pickler.__init__ |
| 4253 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4254 | file: object |
| 4255 | protocol: object = NULL |
| 4256 | fix_imports: bool = True |
| 4257 | |
| 4258 | This takes a binary file for writing a pickle data stream. |
| 4259 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4260 | The optional *protocol* argument tells the pickler to use the given |
| 4261 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4262 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4263 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4264 | Specifying a negative protocol version selects the highest protocol |
| 4265 | version supported. The higher the protocol used, the more recent the |
| 4266 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4267 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4268 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4269 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4270 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4271 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4272 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4273 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4274 | to map the new Python 3 names to the old module names used in Python |
| 4275 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4276 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4277 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4278 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4279 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4280 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4281 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4282 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4283 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4284 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4285 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4286 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4287 | if (self->write != NULL) |
| 4288 | (void)Pickler_clear(self); |
| 4289 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4290 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4291 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4292 | |
| 4293 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4294 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4295 | |
| 4296 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4297 | if (self->memo == NULL) { |
| 4298 | self->memo = PyMemoTable_New(); |
| 4299 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4300 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4301 | } |
| 4302 | self->output_len = 0; |
| 4303 | if (self->output_buffer == NULL) { |
| 4304 | self->max_output_len = WRITE_BUF_SIZE; |
| 4305 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4306 | self->max_output_len); |
| 4307 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4308 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4309 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4310 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4311 | self->fast = 0; |
| 4312 | self->fast_nesting = 0; |
| 4313 | self->fast_memo = NULL; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4314 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4315 | if (init_method_ref((PyObject *)self, &PyId_persistent_id, |
| 4316 | &self->pers_func, &self->pers_func_self) < 0) |
| 4317 | { |
| 4318 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4319 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4320 | |
| 4321 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4322 | &PyId_dispatch_table); |
| 4323 | if (self->dispatch_table == NULL) { |
| 4324 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4325 | return -1; |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 4326 | } |
| 4327 | PyErr_Clear(); |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4328 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4329 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4330 | return 0; |
| 4331 | } |
| 4332 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4333 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4334 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4335 | * avoid breaking code like: |
| 4336 | * pickler.memo.clear() |
| 4337 | * and |
| 4338 | * pickler.memo = saved_memo |
| 4339 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4340 | * it. Note that we don't implement the entire mapping API here. This is |
| 4341 | * intentional, as these should be treated as black-box implementation details. |
| 4342 | */ |
| 4343 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4344 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4345 | _pickle.PicklerMemoProxy.clear |
| 4346 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4347 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4348 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4349 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4350 | static PyObject * |
| 4351 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4352 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4353 | { |
| 4354 | if (self->pickler->memo) |
| 4355 | PyMemoTable_Clear(self->pickler->memo); |
| 4356 | Py_RETURN_NONE; |
| 4357 | } |
| 4358 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4359 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4360 | _pickle.PicklerMemoProxy.copy |
| 4361 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4362 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4363 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4364 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4365 | static PyObject * |
| 4366 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4367 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4368 | { |
| 4369 | Py_ssize_t i; |
| 4370 | PyMemoTable *memo; |
| 4371 | PyObject *new_memo = PyDict_New(); |
| 4372 | if (new_memo == NULL) |
| 4373 | return NULL; |
| 4374 | |
| 4375 | memo = self->pickler->memo; |
| 4376 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4377 | PyMemoEntry entry = memo->mt_table[i]; |
| 4378 | if (entry.me_key != NULL) { |
| 4379 | int status; |
| 4380 | PyObject *key, *value; |
| 4381 | |
| 4382 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4383 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4384 | |
| 4385 | if (key == NULL || value == NULL) { |
| 4386 | Py_XDECREF(key); |
| 4387 | Py_XDECREF(value); |
| 4388 | goto error; |
| 4389 | } |
| 4390 | status = PyDict_SetItem(new_memo, key, value); |
| 4391 | Py_DECREF(key); |
| 4392 | Py_DECREF(value); |
| 4393 | if (status < 0) |
| 4394 | goto error; |
| 4395 | } |
| 4396 | } |
| 4397 | return new_memo; |
| 4398 | |
| 4399 | error: |
| 4400 | Py_XDECREF(new_memo); |
| 4401 | return NULL; |
| 4402 | } |
| 4403 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4404 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4405 | _pickle.PicklerMemoProxy.__reduce__ |
| 4406 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4407 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4408 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4409 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4410 | static PyObject * |
| 4411 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4412 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4413 | { |
| 4414 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4415 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4416 | if (contents == NULL) |
| 4417 | return NULL; |
| 4418 | |
| 4419 | reduce_value = PyTuple_New(2); |
| 4420 | if (reduce_value == NULL) { |
| 4421 | Py_DECREF(contents); |
| 4422 | return NULL; |
| 4423 | } |
| 4424 | dict_args = PyTuple_New(1); |
| 4425 | if (dict_args == NULL) { |
| 4426 | Py_DECREF(contents); |
| 4427 | Py_DECREF(reduce_value); |
| 4428 | return NULL; |
| 4429 | } |
| 4430 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4431 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4432 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4433 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4434 | return reduce_value; |
| 4435 | } |
| 4436 | |
| 4437 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4438 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4439 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4440 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4441 | {NULL, NULL} /* sentinel */ |
| 4442 | }; |
| 4443 | |
| 4444 | static void |
| 4445 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4446 | { |
| 4447 | PyObject_GC_UnTrack(self); |
| 4448 | Py_XDECREF(self->pickler); |
| 4449 | PyObject_GC_Del((PyObject *)self); |
| 4450 | } |
| 4451 | |
| 4452 | static int |
| 4453 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4454 | visitproc visit, void *arg) |
| 4455 | { |
| 4456 | Py_VISIT(self->pickler); |
| 4457 | return 0; |
| 4458 | } |
| 4459 | |
| 4460 | static int |
| 4461 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4462 | { |
| 4463 | Py_CLEAR(self->pickler); |
| 4464 | return 0; |
| 4465 | } |
| 4466 | |
| 4467 | static PyTypeObject PicklerMemoProxyType = { |
| 4468 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4469 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4470 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4471 | 0, |
| 4472 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4473 | 0, /* tp_print */ |
| 4474 | 0, /* tp_getattr */ |
| 4475 | 0, /* tp_setattr */ |
| 4476 | 0, /* tp_compare */ |
| 4477 | 0, /* tp_repr */ |
| 4478 | 0, /* tp_as_number */ |
| 4479 | 0, /* tp_as_sequence */ |
| 4480 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4481 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4482 | 0, /* tp_call */ |
| 4483 | 0, /* tp_str */ |
| 4484 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4485 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4486 | 0, /* tp_as_buffer */ |
| 4487 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4488 | 0, /* tp_doc */ |
| 4489 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4490 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4491 | 0, /* tp_richcompare */ |
| 4492 | 0, /* tp_weaklistoffset */ |
| 4493 | 0, /* tp_iter */ |
| 4494 | 0, /* tp_iternext */ |
| 4495 | picklerproxy_methods, /* tp_methods */ |
| 4496 | }; |
| 4497 | |
| 4498 | static PyObject * |
| 4499 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4500 | { |
| 4501 | PicklerMemoProxyObject *self; |
| 4502 | |
| 4503 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4504 | if (self == NULL) |
| 4505 | return NULL; |
| 4506 | Py_INCREF(pickler); |
| 4507 | self->pickler = pickler; |
| 4508 | PyObject_GC_Track(self); |
| 4509 | return (PyObject *)self; |
| 4510 | } |
| 4511 | |
| 4512 | /*****************************************************************************/ |
| 4513 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4514 | static PyObject * |
| 4515 | Pickler_get_memo(PicklerObject *self) |
| 4516 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4517 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4518 | } |
| 4519 | |
| 4520 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4521 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4522 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4523 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4524 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4525 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4526 | PyErr_SetString(PyExc_TypeError, |
| 4527 | "attribute deletion is not supported"); |
| 4528 | return -1; |
| 4529 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4530 | |
| 4531 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4532 | PicklerObject *pickler = |
| 4533 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4534 | |
| 4535 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4536 | if (new_memo == NULL) |
| 4537 | return -1; |
| 4538 | } |
| 4539 | else if (PyDict_Check(obj)) { |
| 4540 | Py_ssize_t i = 0; |
| 4541 | PyObject *key, *value; |
| 4542 | |
| 4543 | new_memo = PyMemoTable_New(); |
| 4544 | if (new_memo == NULL) |
| 4545 | return -1; |
| 4546 | |
| 4547 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4548 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4549 | PyObject *memo_obj; |
| 4550 | |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 4551 | if (!PyTuple_Check(value) || PyTuple_GET_SIZE(value) != 2) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4552 | PyErr_SetString(PyExc_TypeError, |
| 4553 | "'memo' values must be 2-item tuples"); |
| 4554 | goto error; |
| 4555 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4556 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4557 | if (memo_id == -1 && PyErr_Occurred()) |
| 4558 | goto error; |
| 4559 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4560 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4561 | goto error; |
| 4562 | } |
| 4563 | } |
| 4564 | else { |
| 4565 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b6a9c97 | 2016-04-17 09:39:28 +0300 | [diff] [blame] | 4566 | "'memo' attribute must be a PicklerMemoProxy object" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4567 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4568 | return -1; |
| 4569 | } |
| 4570 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4571 | PyMemoTable_Del(self->memo); |
| 4572 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4573 | |
| 4574 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4575 | |
| 4576 | error: |
| 4577 | if (new_memo) |
| 4578 | PyMemoTable_Del(new_memo); |
| 4579 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4580 | } |
| 4581 | |
| 4582 | static PyObject * |
| 4583 | Pickler_get_persid(PicklerObject *self) |
| 4584 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4585 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4586 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4587 | return NULL; |
| 4588 | } |
| 4589 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | static int |
| 4593 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4594 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4595 | if (value == NULL) { |
| 4596 | PyErr_SetString(PyExc_TypeError, |
| 4597 | "attribute deletion is not supported"); |
| 4598 | return -1; |
| 4599 | } |
| 4600 | if (!PyCallable_Check(value)) { |
| 4601 | PyErr_SetString(PyExc_TypeError, |
| 4602 | "persistent_id must be a callable taking one argument"); |
| 4603 | return -1; |
| 4604 | } |
| 4605 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 4606 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4607 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 4608 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4609 | |
| 4610 | return 0; |
| 4611 | } |
| 4612 | |
| 4613 | static PyMemberDef Pickler_members[] = { |
| 4614 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4615 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4616 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4617 | {NULL} |
| 4618 | }; |
| 4619 | |
| 4620 | static PyGetSetDef Pickler_getsets[] = { |
| 4621 | {"memo", (getter)Pickler_get_memo, |
| 4622 | (setter)Pickler_set_memo}, |
| 4623 | {"persistent_id", (getter)Pickler_get_persid, |
| 4624 | (setter)Pickler_set_persid}, |
| 4625 | {NULL} |
| 4626 | }; |
| 4627 | |
| 4628 | static PyTypeObject Pickler_Type = { |
| 4629 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4630 | "_pickle.Pickler" , /*tp_name*/ |
| 4631 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4632 | 0, /*tp_itemsize*/ |
| 4633 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4634 | 0, /*tp_print*/ |
| 4635 | 0, /*tp_getattr*/ |
| 4636 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4637 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4638 | 0, /*tp_repr*/ |
| 4639 | 0, /*tp_as_number*/ |
| 4640 | 0, /*tp_as_sequence*/ |
| 4641 | 0, /*tp_as_mapping*/ |
| 4642 | 0, /*tp_hash*/ |
| 4643 | 0, /*tp_call*/ |
| 4644 | 0, /*tp_str*/ |
| 4645 | 0, /*tp_getattro*/ |
| 4646 | 0, /*tp_setattro*/ |
| 4647 | 0, /*tp_as_buffer*/ |
| 4648 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4649 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4650 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4651 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4652 | 0, /*tp_richcompare*/ |
| 4653 | 0, /*tp_weaklistoffset*/ |
| 4654 | 0, /*tp_iter*/ |
| 4655 | 0, /*tp_iternext*/ |
| 4656 | Pickler_methods, /*tp_methods*/ |
| 4657 | Pickler_members, /*tp_members*/ |
| 4658 | Pickler_getsets, /*tp_getset*/ |
| 4659 | 0, /*tp_base*/ |
| 4660 | 0, /*tp_dict*/ |
| 4661 | 0, /*tp_descr_get*/ |
| 4662 | 0, /*tp_descr_set*/ |
| 4663 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4664 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4665 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4666 | PyType_GenericNew, /*tp_new*/ |
| 4667 | PyObject_GC_Del, /*tp_free*/ |
| 4668 | 0, /*tp_is_gc*/ |
| 4669 | }; |
| 4670 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4671 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4672 | |
| 4673 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4674 | using directly the C version of find_class(), when find_class() is not |
| 4675 | overridden by a subclass. Although, this could become rather hackish. A |
| 4676 | simpler optimization would be to call the C function when self is not a |
| 4677 | subclass instance. */ |
| 4678 | static PyObject * |
| 4679 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4680 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4681 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4682 | |
Victor Stinner | 55ba38a | 2016-12-09 16:09:30 +0100 | [diff] [blame] | 4683 | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
| 4684 | module_name, global_name, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4685 | } |
| 4686 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4687 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4688 | marker(UnpicklerObject *self) |
| 4689 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4690 | Py_ssize_t mark; |
| 4691 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4692 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4693 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4694 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4695 | return -1; |
| 4696 | } |
| 4697 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4698 | mark = self->marks[--self->num_marks]; |
| 4699 | self->stack->mark_set = self->num_marks != 0; |
| 4700 | self->stack->fence = self->num_marks ? |
| 4701 | self->marks[self->num_marks - 1] : 0; |
| 4702 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | static int |
| 4706 | load_none(UnpicklerObject *self) |
| 4707 | { |
| 4708 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4709 | return 0; |
| 4710 | } |
| 4711 | |
| 4712 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4713 | load_int(UnpicklerObject *self) |
| 4714 | { |
| 4715 | PyObject *value; |
| 4716 | char *endptr, *s; |
| 4717 | Py_ssize_t len; |
| 4718 | long x; |
| 4719 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4720 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4721 | return -1; |
| 4722 | if (len < 2) |
| 4723 | return bad_readline(); |
| 4724 | |
| 4725 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4726 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4727 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4728 | x = strtol(s, &endptr, 0); |
| 4729 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4730 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4731 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4732 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4733 | errno = 0; |
| 4734 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4735 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4736 | if (value == NULL) { |
| 4737 | PyErr_SetString(PyExc_ValueError, |
| 4738 | "could not convert string to int"); |
| 4739 | return -1; |
| 4740 | } |
| 4741 | } |
| 4742 | else { |
| 4743 | if (len == 3 && (x == 0 || x == 1)) { |
| 4744 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4745 | return -1; |
| 4746 | } |
| 4747 | else { |
| 4748 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4749 | return -1; |
| 4750 | } |
| 4751 | } |
| 4752 | |
| 4753 | PDATA_PUSH(self->stack, value, -1); |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
| 4757 | static int |
| 4758 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4759 | { |
| 4760 | assert(boolean == Py_True || boolean == Py_False); |
| 4761 | PDATA_APPEND(self->stack, boolean, -1); |
| 4762 | return 0; |
| 4763 | } |
| 4764 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4765 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4766 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4767 | */ |
| 4768 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4769 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4770 | { |
| 4771 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4772 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4773 | size_t x = 0; |
| 4774 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4775 | if (nbytes > (int)sizeof(size_t)) { |
| 4776 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4777 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4778 | */ |
| 4779 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4780 | if (s[i]) |
| 4781 | return -1; |
| 4782 | } |
| 4783 | nbytes = (int)sizeof(size_t); |
| 4784 | } |
| 4785 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4786 | x |= (size_t) s[i] << (8 * i); |
| 4787 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4788 | |
| 4789 | if (x > PY_SSIZE_T_MAX) |
| 4790 | return -1; |
| 4791 | else |
| 4792 | return (Py_ssize_t) x; |
| 4793 | } |
| 4794 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4795 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4796 | * 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] | 4797 | * 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] | 4798 | * of x-platform bugs. |
| 4799 | */ |
| 4800 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4801 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4802 | { |
| 4803 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4804 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4805 | long x = 0; |
| 4806 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4807 | for (i = 0; i < nbytes; i++) { |
| 4808 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4809 | } |
| 4810 | |
| 4811 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4812 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4813 | * to extend a BININT's sign bit to the full width. |
| 4814 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4815 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4816 | x |= -(x & (1L << 31)); |
| 4817 | } |
| 4818 | |
| 4819 | return x; |
| 4820 | } |
| 4821 | |
| 4822 | static int |
| 4823 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4824 | { |
| 4825 | PyObject *value; |
| 4826 | long x; |
| 4827 | |
| 4828 | x = calc_binint(s, size); |
| 4829 | |
| 4830 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4831 | return -1; |
| 4832 | |
| 4833 | PDATA_PUSH(self->stack, value, -1); |
| 4834 | return 0; |
| 4835 | } |
| 4836 | |
| 4837 | static int |
| 4838 | load_binint(UnpicklerObject *self) |
| 4839 | { |
| 4840 | char *s; |
| 4841 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4842 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4843 | return -1; |
| 4844 | |
| 4845 | return load_binintx(self, s, 4); |
| 4846 | } |
| 4847 | |
| 4848 | static int |
| 4849 | load_binint1(UnpicklerObject *self) |
| 4850 | { |
| 4851 | char *s; |
| 4852 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4853 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4854 | return -1; |
| 4855 | |
| 4856 | return load_binintx(self, s, 1); |
| 4857 | } |
| 4858 | |
| 4859 | static int |
| 4860 | load_binint2(UnpicklerObject *self) |
| 4861 | { |
| 4862 | char *s; |
| 4863 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4864 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4865 | return -1; |
| 4866 | |
| 4867 | return load_binintx(self, s, 2); |
| 4868 | } |
| 4869 | |
| 4870 | static int |
| 4871 | load_long(UnpicklerObject *self) |
| 4872 | { |
| 4873 | PyObject *value; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 4874 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4875 | Py_ssize_t len; |
| 4876 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4877 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4878 | return -1; |
| 4879 | if (len < 2) |
| 4880 | return bad_readline(); |
| 4881 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4882 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4883 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4884 | compatibility with Python 3.0.0, we don't actually *require* |
| 4885 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4886 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4887 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4888 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4889 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4890 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4891 | return -1; |
| 4892 | |
| 4893 | PDATA_PUSH(self->stack, value, -1); |
| 4894 | return 0; |
| 4895 | } |
| 4896 | |
| 4897 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4898 | * data following. |
| 4899 | */ |
| 4900 | static int |
| 4901 | load_counted_long(UnpicklerObject *self, int size) |
| 4902 | { |
| 4903 | PyObject *value; |
| 4904 | char *nbytes; |
| 4905 | char *pdata; |
| 4906 | |
| 4907 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4908 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4909 | return -1; |
| 4910 | |
| 4911 | size = calc_binint(nbytes, size); |
| 4912 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4913 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4914 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4915 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4916 | "LONG pickle has negative byte count"); |
| 4917 | return -1; |
| 4918 | } |
| 4919 | |
| 4920 | if (size == 0) |
| 4921 | value = PyLong_FromLong(0L); |
| 4922 | else { |
| 4923 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4924 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4925 | return -1; |
| 4926 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4927 | 1 /* little endian */ , 1 /* signed */ ); |
| 4928 | } |
| 4929 | if (value == NULL) |
| 4930 | return -1; |
| 4931 | PDATA_PUSH(self->stack, value, -1); |
| 4932 | return 0; |
| 4933 | } |
| 4934 | |
| 4935 | static int |
| 4936 | load_float(UnpicklerObject *self) |
| 4937 | { |
| 4938 | PyObject *value; |
| 4939 | char *endptr, *s; |
| 4940 | Py_ssize_t len; |
| 4941 | double d; |
| 4942 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4943 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4944 | return -1; |
| 4945 | if (len < 2) |
| 4946 | return bad_readline(); |
| 4947 | |
| 4948 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4949 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4950 | if (d == -1.0 && PyErr_Occurred()) |
| 4951 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4952 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4953 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4954 | return -1; |
| 4955 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4956 | value = PyFloat_FromDouble(d); |
| 4957 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4958 | return -1; |
| 4959 | |
| 4960 | PDATA_PUSH(self->stack, value, -1); |
| 4961 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4962 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4963 | |
| 4964 | static int |
| 4965 | load_binfloat(UnpicklerObject *self) |
| 4966 | { |
| 4967 | PyObject *value; |
| 4968 | double x; |
| 4969 | char *s; |
| 4970 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4971 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4972 | return -1; |
| 4973 | |
| 4974 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4975 | if (x == -1.0 && PyErr_Occurred()) |
| 4976 | return -1; |
| 4977 | |
| 4978 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4979 | return -1; |
| 4980 | |
| 4981 | PDATA_PUSH(self->stack, value, -1); |
| 4982 | return 0; |
| 4983 | } |
| 4984 | |
| 4985 | static int |
| 4986 | load_string(UnpicklerObject *self) |
| 4987 | { |
| 4988 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4989 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4990 | Py_ssize_t len; |
| 4991 | char *s, *p; |
| 4992 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4993 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4994 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4995 | /* Strip the newline */ |
| 4996 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4997 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4998 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4999 | p = s + 1; |
| 5000 | len -= 2; |
| 5001 | } |
| 5002 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5003 | PickleState *st = _Pickle_GetGlobalState(); |
| 5004 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5005 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5006 | return -1; |
| 5007 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 5008 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5009 | |
| 5010 | /* Use the PyBytes API to decode the string, since that is what is used |
| 5011 | to encode, and then coerce the result to Unicode. */ |
| 5012 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5013 | if (bytes == NULL) |
| 5014 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5015 | |
| 5016 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 5017 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5018 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5019 | obj = bytes; |
| 5020 | } |
| 5021 | else { |
| 5022 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 5023 | Py_DECREF(bytes); |
| 5024 | if (obj == NULL) { |
| 5025 | return -1; |
| 5026 | } |
| 5027 | } |
| 5028 | |
| 5029 | PDATA_PUSH(self->stack, obj, -1); |
| 5030 | return 0; |
| 5031 | } |
| 5032 | |
| 5033 | static int |
| 5034 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 5035 | { |
| 5036 | PyObject *obj; |
| 5037 | Py_ssize_t size; |
| 5038 | char *s; |
| 5039 | |
| 5040 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5041 | return -1; |
| 5042 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 5043 | size = calc_binsize(s, nbytes); |
| 5044 | if (size < 0) { |
| 5045 | PickleState *st = _Pickle_GetGlobalState(); |
| 5046 | PyErr_Format(st->UnpicklingError, |
| 5047 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 5048 | PY_SSIZE_T_MAX); |
| 5049 | return -1; |
| 5050 | } |
| 5051 | |
| 5052 | if (_Unpickler_Read(self, &s, size) < 0) |
| 5053 | return -1; |
| 5054 | |
| 5055 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 5056 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 5057 | if (strcmp(self->encoding, "bytes") == 0) { |
| 5058 | obj = PyBytes_FromStringAndSize(s, size); |
| 5059 | } |
| 5060 | else { |
| 5061 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 5062 | } |
| 5063 | if (obj == NULL) { |
| 5064 | return -1; |
| 5065 | } |
| 5066 | |
| 5067 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5068 | return 0; |
| 5069 | } |
| 5070 | |
| 5071 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5072 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5073 | { |
| 5074 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5075 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5076 | char *s; |
| 5077 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5078 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5079 | return -1; |
| 5080 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5081 | size = calc_binsize(s, nbytes); |
| 5082 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5083 | PyErr_Format(PyExc_OverflowError, |
| 5084 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5085 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5086 | return -1; |
| 5087 | } |
| 5088 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5089 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5090 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5091 | |
| 5092 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5093 | if (bytes == NULL) |
| 5094 | return -1; |
| 5095 | |
| 5096 | PDATA_PUSH(self->stack, bytes, -1); |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
| 5100 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5101 | load_unicode(UnpicklerObject *self) |
| 5102 | { |
| 5103 | PyObject *str; |
| 5104 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5105 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5106 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5107 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5108 | return -1; |
| 5109 | if (len < 1) |
| 5110 | return bad_readline(); |
| 5111 | |
| 5112 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5113 | if (str == NULL) |
| 5114 | return -1; |
| 5115 | |
| 5116 | PDATA_PUSH(self->stack, str, -1); |
| 5117 | return 0; |
| 5118 | } |
| 5119 | |
| 5120 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5121 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5122 | { |
| 5123 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5124 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5125 | char *s; |
| 5126 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5127 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5128 | return -1; |
| 5129 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5130 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5131 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5132 | PyErr_Format(PyExc_OverflowError, |
| 5133 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5134 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5135 | return -1; |
| 5136 | } |
| 5137 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5138 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5139 | return -1; |
| 5140 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5141 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5142 | if (str == NULL) |
| 5143 | return -1; |
| 5144 | |
| 5145 | PDATA_PUSH(self->stack, str, -1); |
| 5146 | return 0; |
| 5147 | } |
| 5148 | |
| 5149 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5150 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5151 | { |
| 5152 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5153 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5154 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5155 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5156 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5157 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5158 | if (tuple == NULL) |
| 5159 | return -1; |
| 5160 | PDATA_PUSH(self->stack, tuple, -1); |
| 5161 | return 0; |
| 5162 | } |
| 5163 | |
| 5164 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5165 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5166 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5167 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5168 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5169 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5170 | return -1; |
| 5171 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5172 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5173 | } |
| 5174 | |
| 5175 | static int |
| 5176 | load_empty_list(UnpicklerObject *self) |
| 5177 | { |
| 5178 | PyObject *list; |
| 5179 | |
| 5180 | if ((list = PyList_New(0)) == NULL) |
| 5181 | return -1; |
| 5182 | PDATA_PUSH(self->stack, list, -1); |
| 5183 | return 0; |
| 5184 | } |
| 5185 | |
| 5186 | static int |
| 5187 | load_empty_dict(UnpicklerObject *self) |
| 5188 | { |
| 5189 | PyObject *dict; |
| 5190 | |
| 5191 | if ((dict = PyDict_New()) == NULL) |
| 5192 | return -1; |
| 5193 | PDATA_PUSH(self->stack, dict, -1); |
| 5194 | return 0; |
| 5195 | } |
| 5196 | |
| 5197 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5198 | load_empty_set(UnpicklerObject *self) |
| 5199 | { |
| 5200 | PyObject *set; |
| 5201 | |
| 5202 | if ((set = PySet_New(NULL)) == NULL) |
| 5203 | return -1; |
| 5204 | PDATA_PUSH(self->stack, set, -1); |
| 5205 | return 0; |
| 5206 | } |
| 5207 | |
| 5208 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5209 | load_list(UnpicklerObject *self) |
| 5210 | { |
| 5211 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5212 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5213 | |
| 5214 | if ((i = marker(self)) < 0) |
| 5215 | return -1; |
| 5216 | |
| 5217 | list = Pdata_poplist(self->stack, i); |
| 5218 | if (list == NULL) |
| 5219 | return -1; |
| 5220 | PDATA_PUSH(self->stack, list, -1); |
| 5221 | return 0; |
| 5222 | } |
| 5223 | |
| 5224 | static int |
| 5225 | load_dict(UnpicklerObject *self) |
| 5226 | { |
| 5227 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5228 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5229 | |
| 5230 | if ((i = marker(self)) < 0) |
| 5231 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5232 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5233 | |
| 5234 | if ((dict = PyDict_New()) == NULL) |
| 5235 | return -1; |
| 5236 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5237 | if ((j - i) % 2 != 0) { |
| 5238 | PickleState *st = _Pickle_GetGlobalState(); |
| 5239 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5240 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5241 | return -1; |
| 5242 | } |
| 5243 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5244 | for (k = i + 1; k < j; k += 2) { |
| 5245 | key = self->stack->data[k - 1]; |
| 5246 | value = self->stack->data[k]; |
| 5247 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5248 | Py_DECREF(dict); |
| 5249 | return -1; |
| 5250 | } |
| 5251 | } |
| 5252 | Pdata_clear(self->stack, i); |
| 5253 | PDATA_PUSH(self->stack, dict, -1); |
| 5254 | return 0; |
| 5255 | } |
| 5256 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5257 | static int |
| 5258 | load_frozenset(UnpicklerObject *self) |
| 5259 | { |
| 5260 | PyObject *items; |
| 5261 | PyObject *frozenset; |
| 5262 | Py_ssize_t i; |
| 5263 | |
| 5264 | if ((i = marker(self)) < 0) |
| 5265 | return -1; |
| 5266 | |
| 5267 | items = Pdata_poptuple(self->stack, i); |
| 5268 | if (items == NULL) |
| 5269 | return -1; |
| 5270 | |
| 5271 | frozenset = PyFrozenSet_New(items); |
| 5272 | Py_DECREF(items); |
| 5273 | if (frozenset == NULL) |
| 5274 | return -1; |
| 5275 | |
| 5276 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5277 | return 0; |
| 5278 | } |
| 5279 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5280 | static PyObject * |
| 5281 | instantiate(PyObject *cls, PyObject *args) |
| 5282 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5283 | /* Caller must assure args are a tuple. Normally, args come from |
| 5284 | Pdata_poptuple which packs objects from the top of the stack |
| 5285 | into a newly created tuple. */ |
| 5286 | assert(PyTuple_Check(args)); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5287 | if (!PyTuple_GET_SIZE(args) && PyType_Check(cls)) { |
| 5288 | _Py_IDENTIFIER(__getinitargs__); |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5289 | _Py_IDENTIFIER(__new__); |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5290 | PyObject *func = _PyObject_GetAttrId(cls, &PyId___getinitargs__); |
| 5291 | if (func == NULL) { |
| 5292 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 5293 | return NULL; |
| 5294 | } |
| 5295 | PyErr_Clear(); |
| 5296 | return _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL); |
| 5297 | } |
| 5298 | Py_DECREF(func); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5299 | } |
Serhiy Storchaka | 04e36af | 2017-10-22 21:31:34 +0300 | [diff] [blame] | 5300 | return PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
| 5303 | static int |
| 5304 | load_obj(UnpicklerObject *self) |
| 5305 | { |
| 5306 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5307 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5308 | |
| 5309 | if ((i = marker(self)) < 0) |
| 5310 | return -1; |
| 5311 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5312 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5313 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5314 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5315 | args = Pdata_poptuple(self->stack, i + 1); |
| 5316 | if (args == NULL) |
| 5317 | return -1; |
| 5318 | |
| 5319 | PDATA_POP(self->stack, cls); |
| 5320 | if (cls) { |
| 5321 | obj = instantiate(cls, args); |
| 5322 | Py_DECREF(cls); |
| 5323 | } |
| 5324 | Py_DECREF(args); |
| 5325 | if (obj == NULL) |
| 5326 | return -1; |
| 5327 | |
| 5328 | PDATA_PUSH(self->stack, obj, -1); |
| 5329 | return 0; |
| 5330 | } |
| 5331 | |
| 5332 | static int |
| 5333 | load_inst(UnpicklerObject *self) |
| 5334 | { |
| 5335 | PyObject *cls = NULL; |
| 5336 | PyObject *args = NULL; |
| 5337 | PyObject *obj = NULL; |
| 5338 | PyObject *module_name; |
| 5339 | PyObject *class_name; |
| 5340 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5341 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5342 | char *s; |
| 5343 | |
| 5344 | if ((i = marker(self)) < 0) |
| 5345 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5346 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5347 | return -1; |
| 5348 | if (len < 2) |
| 5349 | return bad_readline(); |
| 5350 | |
| 5351 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5352 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5353 | supported by older protocols on Python 2.x. */ |
| 5354 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5355 | if (module_name == NULL) |
| 5356 | return -1; |
| 5357 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5358 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5359 | if (len < 2) { |
| 5360 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5361 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5362 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5363 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5364 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5365 | cls = find_class(self, module_name, class_name); |
| 5366 | Py_DECREF(class_name); |
| 5367 | } |
| 5368 | } |
| 5369 | Py_DECREF(module_name); |
| 5370 | |
| 5371 | if (cls == NULL) |
| 5372 | return -1; |
| 5373 | |
| 5374 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5375 | obj = instantiate(cls, args); |
| 5376 | Py_DECREF(args); |
| 5377 | } |
| 5378 | Py_DECREF(cls); |
| 5379 | |
| 5380 | if (obj == NULL) |
| 5381 | return -1; |
| 5382 | |
| 5383 | PDATA_PUSH(self->stack, obj, -1); |
| 5384 | return 0; |
| 5385 | } |
| 5386 | |
| 5387 | static int |
| 5388 | load_newobj(UnpicklerObject *self) |
| 5389 | { |
| 5390 | PyObject *args = NULL; |
| 5391 | PyObject *clsraw = NULL; |
| 5392 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5393 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5394 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5395 | |
| 5396 | /* Stack is ... cls argtuple, and we want to call |
| 5397 | * cls.__new__(cls, *argtuple). |
| 5398 | */ |
| 5399 | PDATA_POP(self->stack, args); |
| 5400 | if (args == NULL) |
| 5401 | goto error; |
| 5402 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5403 | PyErr_SetString(st->UnpicklingError, |
| 5404 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5405 | goto error; |
| 5406 | } |
| 5407 | |
| 5408 | PDATA_POP(self->stack, clsraw); |
| 5409 | cls = (PyTypeObject *)clsraw; |
| 5410 | if (cls == NULL) |
| 5411 | goto error; |
| 5412 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5413 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5414 | "isn't a type object"); |
| 5415 | goto error; |
| 5416 | } |
| 5417 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5418 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5419 | "has NULL tp_new"); |
| 5420 | goto error; |
| 5421 | } |
| 5422 | |
| 5423 | /* Call __new__. */ |
| 5424 | obj = cls->tp_new(cls, args, NULL); |
| 5425 | if (obj == NULL) |
| 5426 | goto error; |
| 5427 | |
| 5428 | Py_DECREF(args); |
| 5429 | Py_DECREF(clsraw); |
| 5430 | PDATA_PUSH(self->stack, obj, -1); |
| 5431 | return 0; |
| 5432 | |
| 5433 | error: |
| 5434 | Py_XDECREF(args); |
| 5435 | Py_XDECREF(clsraw); |
| 5436 | return -1; |
| 5437 | } |
| 5438 | |
| 5439 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5440 | load_newobj_ex(UnpicklerObject *self) |
| 5441 | { |
| 5442 | PyObject *cls, *args, *kwargs; |
| 5443 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5444 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5445 | |
| 5446 | PDATA_POP(self->stack, kwargs); |
| 5447 | if (kwargs == NULL) { |
| 5448 | return -1; |
| 5449 | } |
| 5450 | PDATA_POP(self->stack, args); |
| 5451 | if (args == NULL) { |
| 5452 | Py_DECREF(kwargs); |
| 5453 | return -1; |
| 5454 | } |
| 5455 | PDATA_POP(self->stack, cls); |
| 5456 | if (cls == NULL) { |
| 5457 | Py_DECREF(kwargs); |
| 5458 | Py_DECREF(args); |
| 5459 | return -1; |
| 5460 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5461 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5462 | if (!PyType_Check(cls)) { |
| 5463 | Py_DECREF(kwargs); |
| 5464 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5465 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5466 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5467 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5468 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5469 | return -1; |
| 5470 | } |
| 5471 | |
| 5472 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5473 | Py_DECREF(kwargs); |
| 5474 | Py_DECREF(args); |
| 5475 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5476 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5477 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5478 | return -1; |
| 5479 | } |
| 5480 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5481 | Py_DECREF(kwargs); |
| 5482 | Py_DECREF(args); |
| 5483 | Py_DECREF(cls); |
| 5484 | if (obj == NULL) { |
| 5485 | return -1; |
| 5486 | } |
| 5487 | PDATA_PUSH(self->stack, obj, -1); |
| 5488 | return 0; |
| 5489 | } |
| 5490 | |
| 5491 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5492 | load_global(UnpicklerObject *self) |
| 5493 | { |
| 5494 | PyObject *global = NULL; |
| 5495 | PyObject *module_name; |
| 5496 | PyObject *global_name; |
| 5497 | Py_ssize_t len; |
| 5498 | char *s; |
| 5499 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5500 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5501 | return -1; |
| 5502 | if (len < 2) |
| 5503 | return bad_readline(); |
| 5504 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5505 | if (!module_name) |
| 5506 | return -1; |
| 5507 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5508 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5509 | if (len < 2) { |
| 5510 | Py_DECREF(module_name); |
| 5511 | return bad_readline(); |
| 5512 | } |
| 5513 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5514 | if (global_name) { |
| 5515 | global = find_class(self, module_name, global_name); |
| 5516 | Py_DECREF(global_name); |
| 5517 | } |
| 5518 | } |
| 5519 | Py_DECREF(module_name); |
| 5520 | |
| 5521 | if (global == NULL) |
| 5522 | return -1; |
| 5523 | PDATA_PUSH(self->stack, global, -1); |
| 5524 | return 0; |
| 5525 | } |
| 5526 | |
| 5527 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5528 | load_stack_global(UnpicklerObject *self) |
| 5529 | { |
| 5530 | PyObject *global; |
| 5531 | PyObject *module_name; |
| 5532 | PyObject *global_name; |
| 5533 | |
| 5534 | PDATA_POP(self->stack, global_name); |
| 5535 | PDATA_POP(self->stack, module_name); |
| 5536 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5537 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5538 | PickleState *st = _Pickle_GetGlobalState(); |
| 5539 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5540 | Py_XDECREF(global_name); |
| 5541 | Py_XDECREF(module_name); |
| 5542 | return -1; |
| 5543 | } |
| 5544 | global = find_class(self, module_name, global_name); |
| 5545 | Py_DECREF(global_name); |
| 5546 | Py_DECREF(module_name); |
| 5547 | if (global == NULL) |
| 5548 | return -1; |
| 5549 | PDATA_PUSH(self->stack, global, -1); |
| 5550 | return 0; |
| 5551 | } |
| 5552 | |
| 5553 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5554 | load_persid(UnpicklerObject *self) |
| 5555 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5556 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5557 | Py_ssize_t len; |
| 5558 | char *s; |
| 5559 | |
| 5560 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5561 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5562 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5563 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5564 | return bad_readline(); |
| 5565 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5566 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5567 | if (pid == NULL) { |
| 5568 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 5569 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 5570 | "persistent IDs in protocol 0 must be " |
| 5571 | "ASCII strings"); |
| 5572 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5573 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5574 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5575 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5576 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5577 | Py_DECREF(pid); |
| 5578 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5579 | return -1; |
| 5580 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5581 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5582 | return 0; |
| 5583 | } |
| 5584 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5585 | PickleState *st = _Pickle_GetGlobalState(); |
| 5586 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5587 | "A load persistent id instruction was encountered,\n" |
| 5588 | "but no persistent_load function was specified."); |
| 5589 | return -1; |
| 5590 | } |
| 5591 | } |
| 5592 | |
| 5593 | static int |
| 5594 | load_binpersid(UnpicklerObject *self) |
| 5595 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5596 | PyObject *pid, *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5597 | |
| 5598 | if (self->pers_func) { |
| 5599 | PDATA_POP(self->stack, pid); |
| 5600 | if (pid == NULL) |
| 5601 | return -1; |
| 5602 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5603 | obj = call_method(self->pers_func, self->pers_func_self, pid); |
| 5604 | Py_DECREF(pid); |
| 5605 | if (obj == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5606 | return -1; |
| 5607 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 5608 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5609 | return 0; |
| 5610 | } |
| 5611 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5612 | PickleState *st = _Pickle_GetGlobalState(); |
| 5613 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5614 | "A load persistent id instruction was encountered,\n" |
| 5615 | "but no persistent_load function was specified."); |
| 5616 | return -1; |
| 5617 | } |
| 5618 | } |
| 5619 | |
| 5620 | static int |
| 5621 | load_pop(UnpicklerObject *self) |
| 5622 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5623 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5624 | |
| 5625 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5626 | * an object stack and a mark stack. We have to be clever and |
| 5627 | * 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] | 5628 | * mark stack first, and only signalling a stack underflow if |
| 5629 | * the object stack is empty and the mark stack doesn't match |
| 5630 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5631 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5632 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5633 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5634 | self->stack->mark_set = self->num_marks != 0; |
| 5635 | self->stack->fence = self->num_marks ? |
| 5636 | self->marks[self->num_marks - 1] : 0; |
| 5637 | } else if (len <= self->stack->fence) |
| 5638 | return Pdata_stack_underflow(self->stack); |
| 5639 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5640 | len--; |
| 5641 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5642 | Py_SIZE(self->stack) = len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5643 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5644 | return 0; |
| 5645 | } |
| 5646 | |
| 5647 | static int |
| 5648 | load_pop_mark(UnpicklerObject *self) |
| 5649 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5650 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5651 | |
| 5652 | if ((i = marker(self)) < 0) |
| 5653 | return -1; |
| 5654 | |
| 5655 | Pdata_clear(self->stack, i); |
| 5656 | |
| 5657 | return 0; |
| 5658 | } |
| 5659 | |
| 5660 | static int |
| 5661 | load_dup(UnpicklerObject *self) |
| 5662 | { |
| 5663 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5664 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5665 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5666 | if (len <= self->stack->fence) |
| 5667 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5668 | last = self->stack->data[len - 1]; |
| 5669 | PDATA_APPEND(self->stack, last, -1); |
| 5670 | return 0; |
| 5671 | } |
| 5672 | |
| 5673 | static int |
| 5674 | load_get(UnpicklerObject *self) |
| 5675 | { |
| 5676 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5677 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5678 | Py_ssize_t len; |
| 5679 | char *s; |
| 5680 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5681 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5682 | return -1; |
| 5683 | if (len < 2) |
| 5684 | return bad_readline(); |
| 5685 | |
| 5686 | key = PyLong_FromString(s, NULL, 10); |
| 5687 | if (key == NULL) |
| 5688 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5689 | idx = PyLong_AsSsize_t(key); |
| 5690 | if (idx == -1 && PyErr_Occurred()) { |
| 5691 | Py_DECREF(key); |
| 5692 | return -1; |
| 5693 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5695 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5696 | if (value == NULL) { |
| 5697 | if (!PyErr_Occurred()) |
| 5698 | PyErr_SetObject(PyExc_KeyError, key); |
| 5699 | Py_DECREF(key); |
| 5700 | return -1; |
| 5701 | } |
| 5702 | Py_DECREF(key); |
| 5703 | |
| 5704 | PDATA_APPEND(self->stack, value, -1); |
| 5705 | return 0; |
| 5706 | } |
| 5707 | |
| 5708 | static int |
| 5709 | load_binget(UnpicklerObject *self) |
| 5710 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5711 | PyObject *value; |
| 5712 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5713 | char *s; |
| 5714 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5715 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5716 | return -1; |
| 5717 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5718 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5719 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5720 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5721 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5722 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5723 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5724 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5725 | Py_DECREF(key); |
| 5726 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5727 | return -1; |
| 5728 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5729 | |
| 5730 | PDATA_APPEND(self->stack, value, -1); |
| 5731 | return 0; |
| 5732 | } |
| 5733 | |
| 5734 | static int |
| 5735 | load_long_binget(UnpicklerObject *self) |
| 5736 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5737 | PyObject *value; |
| 5738 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5739 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5740 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5741 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5742 | return -1; |
| 5743 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5744 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5745 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5746 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5747 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5748 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5749 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5750 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5751 | Py_DECREF(key); |
| 5752 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5753 | return -1; |
| 5754 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5755 | |
| 5756 | PDATA_APPEND(self->stack, value, -1); |
| 5757 | return 0; |
| 5758 | } |
| 5759 | |
| 5760 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5761 | * the number of bytes following the opcode, holding the index (code) value. |
| 5762 | */ |
| 5763 | static int |
| 5764 | load_extension(UnpicklerObject *self, int nbytes) |
| 5765 | { |
| 5766 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5767 | long code; /* calc_binint returns long */ |
| 5768 | PyObject *py_code; /* code as a Python int */ |
| 5769 | PyObject *obj; /* the object to push */ |
| 5770 | PyObject *pair; /* (module_name, class_name) */ |
| 5771 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5772 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5773 | |
| 5774 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5775 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5776 | return -1; |
| 5777 | code = calc_binint(codebytes, nbytes); |
| 5778 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5779 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5780 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5781 | return -1; |
| 5782 | } |
| 5783 | |
| 5784 | /* Look for the code in the cache. */ |
| 5785 | py_code = PyLong_FromLong(code); |
| 5786 | if (py_code == NULL) |
| 5787 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5788 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5789 | if (obj != NULL) { |
| 5790 | /* Bingo. */ |
| 5791 | Py_DECREF(py_code); |
| 5792 | PDATA_APPEND(self->stack, obj, -1); |
| 5793 | return 0; |
| 5794 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5795 | if (PyErr_Occurred()) { |
| 5796 | Py_DECREF(py_code); |
| 5797 | return -1; |
| 5798 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5799 | |
| 5800 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5801 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5802 | if (pair == NULL) { |
| 5803 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5804 | if (!PyErr_Occurred()) { |
| 5805 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5806 | "code %ld", code); |
| 5807 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5808 | return -1; |
| 5809 | } |
| 5810 | /* Since the extension registry is manipulable via Python code, |
| 5811 | * confirm that pair is really a 2-tuple of strings. |
| 5812 | */ |
| 5813 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5814 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5815 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5816 | Py_DECREF(py_code); |
| 5817 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5818 | "isn't a 2-tuple of strings", code); |
| 5819 | return -1; |
| 5820 | } |
| 5821 | /* Load the object. */ |
| 5822 | obj = find_class(self, module_name, class_name); |
| 5823 | if (obj == NULL) { |
| 5824 | Py_DECREF(py_code); |
| 5825 | return -1; |
| 5826 | } |
| 5827 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5828 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5829 | Py_DECREF(py_code); |
| 5830 | if (code < 0) { |
| 5831 | Py_DECREF(obj); |
| 5832 | return -1; |
| 5833 | } |
| 5834 | PDATA_PUSH(self->stack, obj, -1); |
| 5835 | return 0; |
| 5836 | } |
| 5837 | |
| 5838 | static int |
| 5839 | load_put(UnpicklerObject *self) |
| 5840 | { |
| 5841 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5842 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5843 | Py_ssize_t len; |
Victor Stinner | b110dad | 2016-12-09 17:06:43 +0100 | [diff] [blame] | 5844 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5845 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5846 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5847 | return -1; |
| 5848 | if (len < 2) |
| 5849 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5850 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5851 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5852 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5853 | |
| 5854 | key = PyLong_FromString(s, NULL, 10); |
| 5855 | if (key == NULL) |
| 5856 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5857 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5858 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5859 | if (idx < 0) { |
| 5860 | if (!PyErr_Occurred()) |
| 5861 | PyErr_SetString(PyExc_ValueError, |
| 5862 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5863 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5864 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5865 | |
| 5866 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5867 | } |
| 5868 | |
| 5869 | static int |
| 5870 | load_binput(UnpicklerObject *self) |
| 5871 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5872 | PyObject *value; |
| 5873 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5874 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5875 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5876 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5877 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5878 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5879 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5880 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5881 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5882 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5883 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5884 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5885 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5886 | } |
| 5887 | |
| 5888 | static int |
| 5889 | load_long_binput(UnpicklerObject *self) |
| 5890 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5891 | PyObject *value; |
| 5892 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5893 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5894 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5895 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5896 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5897 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5898 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5899 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5900 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5901 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5902 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5903 | if (idx < 0) { |
| 5904 | PyErr_SetString(PyExc_ValueError, |
| 5905 | "negative LONG_BINPUT argument"); |
| 5906 | return -1; |
| 5907 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5908 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5909 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5910 | } |
| 5911 | |
| 5912 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5913 | load_memoize(UnpicklerObject *self) |
| 5914 | { |
| 5915 | PyObject *value; |
| 5916 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5917 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5918 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5919 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5920 | |
| 5921 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5922 | } |
| 5923 | |
| 5924 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5925 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5926 | { |
| 5927 | PyObject *value; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5928 | PyObject *slice; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5929 | PyObject *list; |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5930 | PyObject *result; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5931 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5932 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5933 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5934 | if (x > len || x <= self->stack->fence) |
| 5935 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5936 | if (len == x) /* nothing to do */ |
| 5937 | return 0; |
| 5938 | |
| 5939 | list = self->stack->data[x - 1]; |
| 5940 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5941 | if (PyList_CheckExact(list)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5942 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5943 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5944 | |
| 5945 | slice = Pdata_poplist(self->stack, x); |
| 5946 | if (!slice) |
| 5947 | return -1; |
| 5948 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5949 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5950 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5951 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5952 | } |
| 5953 | else { |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5954 | PyObject *extend_func; |
| 5955 | _Py_IDENTIFIER(extend); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5956 | |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5957 | extend_func = _PyObject_GetAttrId(list, &PyId_extend); |
| 5958 | if (extend_func != NULL) { |
| 5959 | slice = Pdata_poplist(self->stack, x); |
| 5960 | if (!slice) { |
| 5961 | Py_DECREF(extend_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5962 | return -1; |
| 5963 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5964 | result = _Pickle_FastCall(extend_func, slice); |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5965 | Py_DECREF(extend_func); |
| 5966 | if (result == NULL) |
| 5967 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5968 | Py_DECREF(result); |
| 5969 | } |
Serhiy Storchaka | bee09ae | 2017-02-02 11:12:47 +0200 | [diff] [blame] | 5970 | else { |
| 5971 | PyObject *append_func; |
| 5972 | _Py_IDENTIFIER(append); |
| 5973 | |
| 5974 | /* Even if the PEP 307 requires extend() and append() methods, |
| 5975 | fall back on append() if the object has no extend() method |
| 5976 | for backward compatibility. */ |
| 5977 | PyErr_Clear(); |
| 5978 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
| 5979 | if (append_func == NULL) |
| 5980 | return -1; |
| 5981 | for (i = x; i < len; i++) { |
| 5982 | value = self->stack->data[i]; |
| 5983 | result = _Pickle_FastCall(append_func, value); |
| 5984 | if (result == NULL) { |
| 5985 | Pdata_clear(self->stack, i + 1); |
| 5986 | Py_SIZE(self->stack) = x; |
| 5987 | Py_DECREF(append_func); |
| 5988 | return -1; |
| 5989 | } |
| 5990 | Py_DECREF(result); |
| 5991 | } |
| 5992 | Py_SIZE(self->stack) = x; |
| 5993 | Py_DECREF(append_func); |
| 5994 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5995 | } |
| 5996 | |
| 5997 | return 0; |
| 5998 | } |
| 5999 | |
| 6000 | static int |
| 6001 | load_append(UnpicklerObject *self) |
| 6002 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6003 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 6004 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6005 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6006 | } |
| 6007 | |
| 6008 | static int |
| 6009 | load_appends(UnpicklerObject *self) |
| 6010 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6011 | Py_ssize_t i = marker(self); |
| 6012 | if (i < 0) |
| 6013 | return -1; |
| 6014 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6015 | } |
| 6016 | |
| 6017 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6018 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6019 | { |
| 6020 | PyObject *value, *key; |
| 6021 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6022 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6023 | int status = 0; |
| 6024 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6025 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6026 | if (x > len || x <= self->stack->fence) |
| 6027 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6028 | if (len == x) /* nothing to do */ |
| 6029 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6030 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6031 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6032 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6033 | PyErr_SetString(st->UnpicklingError, |
| 6034 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6035 | return -1; |
| 6036 | } |
| 6037 | |
| 6038 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 6039 | that supports the __setitem__ attribute. */ |
| 6040 | dict = self->stack->data[x - 1]; |
| 6041 | |
| 6042 | for (i = x + 1; i < len; i += 2) { |
| 6043 | key = self->stack->data[i - 1]; |
| 6044 | value = self->stack->data[i]; |
| 6045 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 6046 | status = -1; |
| 6047 | break; |
| 6048 | } |
| 6049 | } |
| 6050 | |
| 6051 | Pdata_clear(self->stack, x); |
| 6052 | return status; |
| 6053 | } |
| 6054 | |
| 6055 | static int |
| 6056 | load_setitem(UnpicklerObject *self) |
| 6057 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6058 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6059 | } |
| 6060 | |
| 6061 | static int |
| 6062 | load_setitems(UnpicklerObject *self) |
| 6063 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6064 | Py_ssize_t i = marker(self); |
| 6065 | if (i < 0) |
| 6066 | return -1; |
| 6067 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6068 | } |
| 6069 | |
| 6070 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6071 | load_additems(UnpicklerObject *self) |
| 6072 | { |
| 6073 | PyObject *set; |
| 6074 | Py_ssize_t mark, len, i; |
| 6075 | |
| 6076 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 6077 | if (mark < 0) |
| 6078 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6079 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6080 | if (mark > len || mark <= self->stack->fence) |
| 6081 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6082 | if (len == mark) /* nothing to do */ |
| 6083 | return 0; |
| 6084 | |
| 6085 | set = self->stack->data[mark - 1]; |
| 6086 | |
| 6087 | if (PySet_Check(set)) { |
| 6088 | PyObject *items; |
| 6089 | int status; |
| 6090 | |
| 6091 | items = Pdata_poptuple(self->stack, mark); |
| 6092 | if (items == NULL) |
| 6093 | return -1; |
| 6094 | |
| 6095 | status = _PySet_Update(set, items); |
| 6096 | Py_DECREF(items); |
| 6097 | return status; |
| 6098 | } |
| 6099 | else { |
| 6100 | PyObject *add_func; |
| 6101 | _Py_IDENTIFIER(add); |
| 6102 | |
| 6103 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6104 | if (add_func == NULL) |
| 6105 | return -1; |
| 6106 | for (i = mark; i < len; i++) { |
| 6107 | PyObject *result; |
| 6108 | PyObject *item; |
| 6109 | |
| 6110 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6111 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6112 | if (result == NULL) { |
| 6113 | Pdata_clear(self->stack, i + 1); |
| 6114 | Py_SIZE(self->stack) = mark; |
| 6115 | return -1; |
| 6116 | } |
| 6117 | Py_DECREF(result); |
| 6118 | } |
| 6119 | Py_SIZE(self->stack) = mark; |
| 6120 | } |
| 6121 | |
| 6122 | return 0; |
| 6123 | } |
| 6124 | |
| 6125 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6126 | load_build(UnpicklerObject *self) |
| 6127 | { |
| 6128 | PyObject *state, *inst, *slotstate; |
| 6129 | PyObject *setstate; |
| 6130 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6131 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6132 | |
| 6133 | /* Stack is ... instance, state. We want to leave instance at |
| 6134 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6135 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6136 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6137 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6138 | |
| 6139 | PDATA_POP(self->stack, state); |
| 6140 | if (state == NULL) |
| 6141 | return -1; |
| 6142 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6143 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6144 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6145 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 6146 | if (setstate == NULL) { |
| 6147 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 6148 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 6149 | else { |
| 6150 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 6151 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 6152 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6153 | } |
| 6154 | else { |
| 6155 | PyObject *result; |
| 6156 | |
| 6157 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6158 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6159 | Py_DECREF(setstate); |
| 6160 | if (result == NULL) |
| 6161 | return -1; |
| 6162 | Py_DECREF(result); |
| 6163 | return 0; |
| 6164 | } |
| 6165 | |
| 6166 | /* A default __setstate__. First see whether state embeds a |
| 6167 | * slot state dict too (a proto 2 addition). |
| 6168 | */ |
Serhiy Storchaka | fff9a31 | 2017-03-21 08:53:25 +0200 | [diff] [blame] | 6169 | if (PyTuple_Check(state) && PyTuple_GET_SIZE(state) == 2) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6170 | PyObject *tmp = state; |
| 6171 | |
| 6172 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6173 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6174 | Py_INCREF(state); |
| 6175 | Py_INCREF(slotstate); |
| 6176 | Py_DECREF(tmp); |
| 6177 | } |
| 6178 | else |
| 6179 | slotstate = NULL; |
| 6180 | |
| 6181 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6182 | if (state != Py_None) { |
| 6183 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6184 | PyObject *d_key, *d_value; |
| 6185 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6186 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6187 | |
| 6188 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6189 | PickleState *st = _Pickle_GetGlobalState(); |
| 6190 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6191 | goto error; |
| 6192 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6193 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6194 | if (dict == NULL) |
| 6195 | goto error; |
| 6196 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6197 | i = 0; |
| 6198 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6199 | /* normally the keys for instance attributes are |
| 6200 | interned. we should try to do that here. */ |
| 6201 | Py_INCREF(d_key); |
| 6202 | if (PyUnicode_CheckExact(d_key)) |
| 6203 | PyUnicode_InternInPlace(&d_key); |
| 6204 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6205 | Py_DECREF(d_key); |
| 6206 | goto error; |
| 6207 | } |
| 6208 | Py_DECREF(d_key); |
| 6209 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6210 | Py_DECREF(dict); |
| 6211 | } |
| 6212 | |
| 6213 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6214 | if (slotstate != NULL) { |
| 6215 | PyObject *d_key, *d_value; |
| 6216 | Py_ssize_t i; |
| 6217 | |
| 6218 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6219 | PickleState *st = _Pickle_GetGlobalState(); |
| 6220 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6221 | "slot state is not a dictionary"); |
| 6222 | goto error; |
| 6223 | } |
| 6224 | i = 0; |
| 6225 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6226 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6227 | goto error; |
| 6228 | } |
| 6229 | } |
| 6230 | |
| 6231 | if (0) { |
| 6232 | error: |
| 6233 | status = -1; |
| 6234 | } |
| 6235 | |
| 6236 | Py_DECREF(state); |
| 6237 | Py_XDECREF(slotstate); |
| 6238 | return status; |
| 6239 | } |
| 6240 | |
| 6241 | static int |
| 6242 | load_mark(UnpicklerObject *self) |
| 6243 | { |
| 6244 | |
| 6245 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6246 | * object stack and a mark stack. Here we push a mark onto the |
| 6247 | * mark stack. |
| 6248 | */ |
| 6249 | |
| 6250 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6251 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6252 | |
| 6253 | /* Use the size_t type to check for overflow. */ |
| 6254 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6255 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6256 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6257 | PyErr_NoMemory(); |
| 6258 | return -1; |
| 6259 | } |
| 6260 | |
| 6261 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6262 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6263 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6264 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6265 | if (self->marks == NULL) { |
| 6266 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6267 | PyErr_NoMemory(); |
| 6268 | return -1; |
| 6269 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6270 | self->marks_size = (Py_ssize_t)alloc; |
| 6271 | } |
| 6272 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6273 | self->stack->mark_set = 1; |
| 6274 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6275 | |
| 6276 | return 0; |
| 6277 | } |
| 6278 | |
| 6279 | static int |
| 6280 | load_reduce(UnpicklerObject *self) |
| 6281 | { |
| 6282 | PyObject *callable = NULL; |
| 6283 | PyObject *argtup = NULL; |
| 6284 | PyObject *obj = NULL; |
| 6285 | |
| 6286 | PDATA_POP(self->stack, argtup); |
| 6287 | if (argtup == NULL) |
| 6288 | return -1; |
| 6289 | PDATA_POP(self->stack, callable); |
| 6290 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6291 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6292 | Py_DECREF(callable); |
| 6293 | } |
| 6294 | Py_DECREF(argtup); |
| 6295 | |
| 6296 | if (obj == NULL) |
| 6297 | return -1; |
| 6298 | |
| 6299 | PDATA_PUSH(self->stack, obj, -1); |
| 6300 | return 0; |
| 6301 | } |
| 6302 | |
| 6303 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6304 | * is the first opcode for protocols >= 2. |
| 6305 | */ |
| 6306 | static int |
| 6307 | load_proto(UnpicklerObject *self) |
| 6308 | { |
| 6309 | char *s; |
| 6310 | int i; |
| 6311 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6312 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6313 | return -1; |
| 6314 | |
| 6315 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6316 | if (i <= HIGHEST_PROTOCOL) { |
| 6317 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6318 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6319 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6320 | |
| 6321 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6322 | return -1; |
| 6323 | } |
| 6324 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6325 | static int |
| 6326 | load_frame(UnpicklerObject *self) |
| 6327 | { |
| 6328 | char *s; |
| 6329 | Py_ssize_t frame_len; |
| 6330 | |
| 6331 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6332 | return -1; |
| 6333 | |
| 6334 | frame_len = calc_binsize(s, 8); |
| 6335 | if (frame_len < 0) { |
| 6336 | PyErr_Format(PyExc_OverflowError, |
| 6337 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6338 | PY_SSIZE_T_MAX); |
| 6339 | return -1; |
| 6340 | } |
| 6341 | |
| 6342 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6343 | return -1; |
| 6344 | |
| 6345 | /* Rewind to start of frame */ |
| 6346 | self->next_read_idx -= frame_len; |
| 6347 | return 0; |
| 6348 | } |
| 6349 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6350 | static PyObject * |
| 6351 | load(UnpicklerObject *self) |
| 6352 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6353 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6354 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6355 | |
| 6356 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6357 | self->stack->mark_set = 0; |
| 6358 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6359 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6360 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6361 | Pdata_clear(self->stack, 0); |
| 6362 | |
| 6363 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6364 | #define OP(opcode, load_func) \ |
| 6365 | case opcode: if (load_func(self) < 0) break; continue; |
| 6366 | |
| 6367 | #define OP_ARG(opcode, load_func, arg) \ |
| 6368 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6369 | |
| 6370 | while (1) { |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6371 | if (_Unpickler_Read(self, &s, 1) < 0) { |
| 6372 | PickleState *st = _Pickle_GetGlobalState(); |
| 6373 | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
| 6374 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 6375 | } |
| 6376 | return NULL; |
| 6377 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6378 | |
| 6379 | switch ((enum opcode)s[0]) { |
| 6380 | OP(NONE, load_none) |
| 6381 | OP(BININT, load_binint) |
| 6382 | OP(BININT1, load_binint1) |
| 6383 | OP(BININT2, load_binint2) |
| 6384 | OP(INT, load_int) |
| 6385 | OP(LONG, load_long) |
| 6386 | OP_ARG(LONG1, load_counted_long, 1) |
| 6387 | OP_ARG(LONG4, load_counted_long, 4) |
| 6388 | OP(FLOAT, load_float) |
| 6389 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6390 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6391 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6392 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6393 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6394 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6395 | OP(STRING, load_string) |
| 6396 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6397 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6398 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6399 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6400 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6401 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6402 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6403 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6404 | OP(TUPLE, load_tuple) |
| 6405 | OP(EMPTY_LIST, load_empty_list) |
| 6406 | OP(LIST, load_list) |
| 6407 | OP(EMPTY_DICT, load_empty_dict) |
| 6408 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6409 | OP(EMPTY_SET, load_empty_set) |
| 6410 | OP(ADDITEMS, load_additems) |
| 6411 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6412 | OP(OBJ, load_obj) |
| 6413 | OP(INST, load_inst) |
| 6414 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6415 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6416 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6417 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6418 | OP(APPEND, load_append) |
| 6419 | OP(APPENDS, load_appends) |
| 6420 | OP(BUILD, load_build) |
| 6421 | OP(DUP, load_dup) |
| 6422 | OP(BINGET, load_binget) |
| 6423 | OP(LONG_BINGET, load_long_binget) |
| 6424 | OP(GET, load_get) |
| 6425 | OP(MARK, load_mark) |
| 6426 | OP(BINPUT, load_binput) |
| 6427 | OP(LONG_BINPUT, load_long_binput) |
| 6428 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6429 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6430 | OP(POP, load_pop) |
| 6431 | OP(POP_MARK, load_pop_mark) |
| 6432 | OP(SETITEM, load_setitem) |
| 6433 | OP(SETITEMS, load_setitems) |
| 6434 | OP(PERSID, load_persid) |
| 6435 | OP(BINPERSID, load_binpersid) |
| 6436 | OP(REDUCE, load_reduce) |
| 6437 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6438 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6439 | OP_ARG(EXT1, load_extension, 1) |
| 6440 | OP_ARG(EXT2, load_extension, 2) |
| 6441 | OP_ARG(EXT4, load_extension, 4) |
| 6442 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6443 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6444 | |
| 6445 | case STOP: |
| 6446 | break; |
| 6447 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6448 | default: |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6449 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6450 | PickleState *st = _Pickle_GetGlobalState(); |
Serhiy Storchaka | 90493ab | 2016-09-06 23:55:11 +0300 | [diff] [blame] | 6451 | unsigned char c = (unsigned char) *s; |
| 6452 | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
| 6453 | PyErr_Format(st->UnpicklingError, |
| 6454 | "invalid load key, '%c'.", c); |
| 6455 | } |
| 6456 | else { |
| 6457 | PyErr_Format(st->UnpicklingError, |
| 6458 | "invalid load key, '\\x%02x'.", c); |
| 6459 | } |
| 6460 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6461 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6462 | } |
| 6463 | |
| 6464 | break; /* and we are done! */ |
| 6465 | } |
| 6466 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6467 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6468 | return NULL; |
| 6469 | } |
| 6470 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6471 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6472 | return NULL; |
| 6473 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6474 | PDATA_POP(self->stack, value); |
| 6475 | return value; |
| 6476 | } |
| 6477 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6478 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6479 | |
| 6480 | _pickle.Unpickler.load |
| 6481 | |
| 6482 | Load a pickle. |
| 6483 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6484 | Read a pickled object representation from the open file object given |
| 6485 | in the constructor, and return the reconstituted object hierarchy |
| 6486 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6487 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6488 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6489 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6490 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6491 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6492 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6493 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6494 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6495 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6496 | segfaulting if a subclass overridden __init__ with a function that does |
| 6497 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6498 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6499 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6500 | PickleState *st = _Pickle_GetGlobalState(); |
| 6501 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6502 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6503 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6504 | return NULL; |
| 6505 | } |
| 6506 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6507 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6508 | } |
| 6509 | |
| 6510 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6511 | function is used for loading any global (i.e., functions), not just |
| 6512 | classes. The name is kept only for backward compatibility. */ |
| 6513 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6514 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6515 | |
| 6516 | _pickle.Unpickler.find_class |
| 6517 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6518 | module_name: object |
| 6519 | global_name: object |
| 6520 | / |
| 6521 | |
| 6522 | Return an object from a specified module. |
| 6523 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6524 | If necessary, the module will be imported. Subclasses may override |
| 6525 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6526 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6527 | |
| 6528 | This method is called whenever a class or a function object is |
| 6529 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6530 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6531 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6532 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6533 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6534 | PyObject *module_name, |
| 6535 | PyObject *global_name) |
| 6536 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6537 | { |
| 6538 | PyObject *global; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6539 | PyObject *module; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6540 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6541 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6542 | Python 3.x. We do this only with old pickle protocols and when the |
| 6543 | user has not disabled the feature. */ |
| 6544 | if (self->proto < 3 && self->fix_imports) { |
| 6545 | PyObject *key; |
| 6546 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6547 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6548 | |
| 6549 | /* Check if the global (i.e., a function or a class) was renamed |
| 6550 | or moved to another module. */ |
| 6551 | key = PyTuple_Pack(2, module_name, global_name); |
| 6552 | if (key == NULL) |
| 6553 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6554 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6555 | Py_DECREF(key); |
| 6556 | if (item) { |
| 6557 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6558 | PyErr_Format(PyExc_RuntimeError, |
| 6559 | "_compat_pickle.NAME_MAPPING values should be " |
| 6560 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6561 | return NULL; |
| 6562 | } |
| 6563 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6564 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6565 | if (!PyUnicode_Check(module_name) || |
| 6566 | !PyUnicode_Check(global_name)) { |
| 6567 | PyErr_Format(PyExc_RuntimeError, |
| 6568 | "_compat_pickle.NAME_MAPPING values should be " |
| 6569 | "pairs of str, not (%.200s, %.200s)", |
| 6570 | Py_TYPE(module_name)->tp_name, |
| 6571 | Py_TYPE(global_name)->tp_name); |
| 6572 | return NULL; |
| 6573 | } |
| 6574 | } |
| 6575 | else if (PyErr_Occurred()) { |
| 6576 | return NULL; |
| 6577 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6578 | else { |
| 6579 | /* Check if the module was renamed. */ |
| 6580 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6581 | if (item) { |
| 6582 | if (!PyUnicode_Check(item)) { |
| 6583 | PyErr_Format(PyExc_RuntimeError, |
| 6584 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6585 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6586 | return NULL; |
| 6587 | } |
| 6588 | module_name = item; |
| 6589 | } |
| 6590 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6591 | return NULL; |
| 6592 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6593 | } |
| 6594 | } |
| 6595 | |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6596 | module = PyImport_GetModule(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6597 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6598 | if (PyErr_Occurred()) |
| 6599 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6600 | module = PyImport_Import(module_name); |
| 6601 | if (module == NULL) |
| 6602 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6603 | } |
Eric Snow | 3f9eee6 | 2017-09-15 16:35:20 -0600 | [diff] [blame] | 6604 | global = getattribute(module, global_name, self->proto >= 4); |
| 6605 | Py_DECREF(module); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6606 | return global; |
| 6607 | } |
| 6608 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6609 | /*[clinic input] |
| 6610 | |
| 6611 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6612 | |
| 6613 | Returns size in memory, in bytes. |
| 6614 | [clinic start generated code]*/ |
| 6615 | |
| 6616 | static Py_ssize_t |
| 6617 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6618 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6619 | { |
| 6620 | Py_ssize_t res; |
| 6621 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 6622 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6623 | if (self->memo != NULL) |
| 6624 | res += self->memo_size * sizeof(PyObject *); |
| 6625 | if (self->marks != NULL) |
| 6626 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6627 | if (self->input_line != NULL) |
| 6628 | res += strlen(self->input_line) + 1; |
| 6629 | if (self->encoding != NULL) |
| 6630 | res += strlen(self->encoding) + 1; |
| 6631 | if (self->errors != NULL) |
| 6632 | res += strlen(self->errors) + 1; |
| 6633 | return res; |
| 6634 | } |
| 6635 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6636 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6637 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6638 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6639 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6640 | {NULL, NULL} /* sentinel */ |
| 6641 | }; |
| 6642 | |
| 6643 | static void |
| 6644 | Unpickler_dealloc(UnpicklerObject *self) |
| 6645 | { |
| 6646 | PyObject_GC_UnTrack((PyObject *)self); |
| 6647 | Py_XDECREF(self->readline); |
| 6648 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6649 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6650 | Py_XDECREF(self->stack); |
| 6651 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6652 | if (self->buffer.buf != NULL) { |
| 6653 | PyBuffer_Release(&self->buffer); |
| 6654 | self->buffer.buf = NULL; |
| 6655 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6656 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6657 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6658 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6659 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6660 | PyMem_Free(self->encoding); |
| 6661 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6662 | |
| 6663 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6664 | } |
| 6665 | |
| 6666 | static int |
| 6667 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6668 | { |
| 6669 | Py_VISIT(self->readline); |
| 6670 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6671 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6672 | Py_VISIT(self->stack); |
| 6673 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6674 | return 0; |
| 6675 | } |
| 6676 | |
| 6677 | static int |
| 6678 | Unpickler_clear(UnpicklerObject *self) |
| 6679 | { |
| 6680 | Py_CLEAR(self->readline); |
| 6681 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6682 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6683 | Py_CLEAR(self->stack); |
| 6684 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6685 | if (self->buffer.buf != NULL) { |
| 6686 | PyBuffer_Release(&self->buffer); |
| 6687 | self->buffer.buf = NULL; |
| 6688 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6689 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6690 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6691 | PyMem_Free(self->marks); |
| 6692 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6693 | PyMem_Free(self->input_line); |
| 6694 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6695 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6696 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6697 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6698 | self->errors = NULL; |
| 6699 | |
| 6700 | return 0; |
| 6701 | } |
| 6702 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6703 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6704 | |
| 6705 | _pickle.Unpickler.__init__ |
| 6706 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6707 | file: object |
| 6708 | * |
| 6709 | fix_imports: bool = True |
| 6710 | encoding: str = 'ASCII' |
| 6711 | errors: str = 'strict' |
| 6712 | |
| 6713 | This takes a binary file for reading a pickle data stream. |
| 6714 | |
| 6715 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6716 | protocol argument is needed. Bytes past the pickled object's |
| 6717 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6718 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6719 | The argument *file* must have two methods, a read() method that takes |
| 6720 | an integer argument, and a readline() method that requires no |
| 6721 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6722 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6723 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6724 | |
| 6725 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6726 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6727 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6728 | 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] | 6729 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6730 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6731 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6732 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6733 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6734 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6735 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6736 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6737 | int fix_imports, const char *encoding, |
| 6738 | const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6739 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6740 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6741 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6742 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6743 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6744 | if (self->read != NULL) |
| 6745 | (void)Unpickler_clear(self); |
| 6746 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6747 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6748 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6749 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6750 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6751 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6752 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6753 | self->fix_imports = fix_imports; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6754 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 6755 | if (init_method_ref((PyObject *)self, &PyId_persistent_load, |
| 6756 | &self->pers_func, &self->pers_func_self) < 0) |
| 6757 | { |
| 6758 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
| 6761 | self->stack = (Pdata *)Pdata_New(); |
| 6762 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6763 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6764 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6765 | self->memo_size = 32; |
| 6766 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6767 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6768 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6769 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6770 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6771 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6772 | return 0; |
| 6773 | } |
| 6774 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6775 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6776 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6777 | * avoid breaking code like: |
| 6778 | * unpickler.memo.clear() |
| 6779 | * and |
| 6780 | * unpickler.memo = saved_memo |
| 6781 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6782 | * it. Note that we don't implement the entire mapping API here. This is |
| 6783 | * intentional, as these should be treated as black-box implementation details. |
| 6784 | * |
| 6785 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6786 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6787 | */ |
| 6788 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6789 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6790 | _pickle.UnpicklerMemoProxy.clear |
| 6791 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6792 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6793 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6794 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6795 | static PyObject * |
| 6796 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6797 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6798 | { |
| 6799 | _Unpickler_MemoCleanup(self->unpickler); |
| 6800 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6801 | if (self->unpickler->memo == NULL) |
| 6802 | return NULL; |
| 6803 | Py_RETURN_NONE; |
| 6804 | } |
| 6805 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6806 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6807 | _pickle.UnpicklerMemoProxy.copy |
| 6808 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6809 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6810 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6811 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6812 | static PyObject * |
| 6813 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6814 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6815 | { |
| 6816 | Py_ssize_t i; |
| 6817 | PyObject *new_memo = PyDict_New(); |
| 6818 | if (new_memo == NULL) |
| 6819 | return NULL; |
| 6820 | |
| 6821 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6822 | int status; |
| 6823 | PyObject *key, *value; |
| 6824 | |
| 6825 | value = self->unpickler->memo[i]; |
| 6826 | if (value == NULL) |
| 6827 | continue; |
| 6828 | |
| 6829 | key = PyLong_FromSsize_t(i); |
| 6830 | if (key == NULL) |
| 6831 | goto error; |
| 6832 | status = PyDict_SetItem(new_memo, key, value); |
| 6833 | Py_DECREF(key); |
| 6834 | if (status < 0) |
| 6835 | goto error; |
| 6836 | } |
| 6837 | return new_memo; |
| 6838 | |
| 6839 | error: |
| 6840 | Py_DECREF(new_memo); |
| 6841 | return NULL; |
| 6842 | } |
| 6843 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6844 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6845 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6846 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6847 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6848 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6849 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6850 | static PyObject * |
| 6851 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6852 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6853 | { |
| 6854 | PyObject *reduce_value; |
| 6855 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6856 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6857 | if (contents == NULL) |
| 6858 | return NULL; |
| 6859 | |
| 6860 | reduce_value = PyTuple_New(2); |
| 6861 | if (reduce_value == NULL) { |
| 6862 | Py_DECREF(contents); |
| 6863 | return NULL; |
| 6864 | } |
| 6865 | constructor_args = PyTuple_New(1); |
| 6866 | if (constructor_args == NULL) { |
| 6867 | Py_DECREF(contents); |
| 6868 | Py_DECREF(reduce_value); |
| 6869 | return NULL; |
| 6870 | } |
| 6871 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6872 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6873 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6874 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6875 | return reduce_value; |
| 6876 | } |
| 6877 | |
| 6878 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6879 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6880 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6881 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6882 | {NULL, NULL} /* sentinel */ |
| 6883 | }; |
| 6884 | |
| 6885 | static void |
| 6886 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6887 | { |
| 6888 | PyObject_GC_UnTrack(self); |
| 6889 | Py_XDECREF(self->unpickler); |
| 6890 | PyObject_GC_Del((PyObject *)self); |
| 6891 | } |
| 6892 | |
| 6893 | static int |
| 6894 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6895 | visitproc visit, void *arg) |
| 6896 | { |
| 6897 | Py_VISIT(self->unpickler); |
| 6898 | return 0; |
| 6899 | } |
| 6900 | |
| 6901 | static int |
| 6902 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6903 | { |
| 6904 | Py_CLEAR(self->unpickler); |
| 6905 | return 0; |
| 6906 | } |
| 6907 | |
| 6908 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6909 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6910 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6911 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6912 | 0, |
| 6913 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6914 | 0, /* tp_print */ |
| 6915 | 0, /* tp_getattr */ |
| 6916 | 0, /* tp_setattr */ |
| 6917 | 0, /* tp_compare */ |
| 6918 | 0, /* tp_repr */ |
| 6919 | 0, /* tp_as_number */ |
| 6920 | 0, /* tp_as_sequence */ |
| 6921 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6922 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6923 | 0, /* tp_call */ |
| 6924 | 0, /* tp_str */ |
| 6925 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6926 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6927 | 0, /* tp_as_buffer */ |
| 6928 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6929 | 0, /* tp_doc */ |
| 6930 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6931 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6932 | 0, /* tp_richcompare */ |
| 6933 | 0, /* tp_weaklistoffset */ |
| 6934 | 0, /* tp_iter */ |
| 6935 | 0, /* tp_iternext */ |
| 6936 | unpicklerproxy_methods, /* tp_methods */ |
| 6937 | }; |
| 6938 | |
| 6939 | static PyObject * |
| 6940 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6941 | { |
| 6942 | UnpicklerMemoProxyObject *self; |
| 6943 | |
| 6944 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6945 | &UnpicklerMemoProxyType); |
| 6946 | if (self == NULL) |
| 6947 | return NULL; |
| 6948 | Py_INCREF(unpickler); |
| 6949 | self->unpickler = unpickler; |
| 6950 | PyObject_GC_Track(self); |
| 6951 | return (PyObject *)self; |
| 6952 | } |
| 6953 | |
| 6954 | /*****************************************************************************/ |
| 6955 | |
| 6956 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6957 | static PyObject * |
| 6958 | Unpickler_get_memo(UnpicklerObject *self) |
| 6959 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6960 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6961 | } |
| 6962 | |
| 6963 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6964 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6965 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6966 | PyObject **new_memo; |
| 6967 | Py_ssize_t new_memo_size = 0; |
| 6968 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6969 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6970 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6971 | PyErr_SetString(PyExc_TypeError, |
| 6972 | "attribute deletion is not supported"); |
| 6973 | return -1; |
| 6974 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6975 | |
| 6976 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6977 | UnpicklerObject *unpickler = |
| 6978 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6979 | |
| 6980 | new_memo_size = unpickler->memo_size; |
| 6981 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6982 | if (new_memo == NULL) |
| 6983 | return -1; |
| 6984 | |
| 6985 | for (i = 0; i < new_memo_size; i++) { |
| 6986 | Py_XINCREF(unpickler->memo[i]); |
| 6987 | new_memo[i] = unpickler->memo[i]; |
| 6988 | } |
| 6989 | } |
| 6990 | else if (PyDict_Check(obj)) { |
| 6991 | Py_ssize_t i = 0; |
| 6992 | PyObject *key, *value; |
| 6993 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 6994 | new_memo_size = PyDict_GET_SIZE(obj); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6995 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6996 | if (new_memo == NULL) |
| 6997 | return -1; |
| 6998 | |
| 6999 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 7000 | Py_ssize_t idx; |
| 7001 | if (!PyLong_Check(key)) { |
| 7002 | PyErr_SetString(PyExc_TypeError, |
| 7003 | "memo key must be integers"); |
| 7004 | goto error; |
| 7005 | } |
| 7006 | idx = PyLong_AsSsize_t(key); |
| 7007 | if (idx == -1 && PyErr_Occurred()) |
| 7008 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7009 | if (idx < 0) { |
| 7010 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 7011 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 7012 | goto error; |
| 7013 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7014 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 7015 | goto error; |
| 7016 | } |
| 7017 | } |
| 7018 | else { |
| 7019 | PyErr_Format(PyExc_TypeError, |
| 7020 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 7021 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7022 | return -1; |
| 7023 | } |
| 7024 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7025 | _Unpickler_MemoCleanup(self); |
| 7026 | self->memo_size = new_memo_size; |
| 7027 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7028 | |
| 7029 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7030 | |
| 7031 | error: |
| 7032 | if (new_memo_size) { |
| 7033 | i = new_memo_size; |
| 7034 | while (--i >= 0) { |
| 7035 | Py_XDECREF(new_memo[i]); |
| 7036 | } |
| 7037 | PyMem_FREE(new_memo); |
| 7038 | } |
| 7039 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7040 | } |
| 7041 | |
| 7042 | static PyObject * |
| 7043 | Unpickler_get_persload(UnpicklerObject *self) |
| 7044 | { |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7045 | if (self->pers_func == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7046 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7047 | return NULL; |
| 7048 | } |
| 7049 | return reconstruct_method(self->pers_func, self->pers_func_self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7050 | } |
| 7051 | |
| 7052 | static int |
| 7053 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 7054 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7055 | if (value == NULL) { |
| 7056 | PyErr_SetString(PyExc_TypeError, |
| 7057 | "attribute deletion is not supported"); |
| 7058 | return -1; |
| 7059 | } |
| 7060 | if (!PyCallable_Check(value)) { |
| 7061 | PyErr_SetString(PyExc_TypeError, |
| 7062 | "persistent_load must be a callable taking " |
| 7063 | "one argument"); |
| 7064 | return -1; |
| 7065 | } |
| 7066 | |
Serhiy Storchaka | 986375e | 2017-11-30 22:48:31 +0200 | [diff] [blame] | 7067 | self->pers_func_self = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7068 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 7069 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7070 | |
| 7071 | return 0; |
| 7072 | } |
| 7073 | |
| 7074 | static PyGetSetDef Unpickler_getsets[] = { |
| 7075 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 7076 | {"persistent_load", (getter)Unpickler_get_persload, |
| 7077 | (setter)Unpickler_set_persload}, |
| 7078 | {NULL} |
| 7079 | }; |
| 7080 | |
| 7081 | static PyTypeObject Unpickler_Type = { |
| 7082 | PyVarObject_HEAD_INIT(NULL, 0) |
| 7083 | "_pickle.Unpickler", /*tp_name*/ |
| 7084 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 7085 | 0, /*tp_itemsize*/ |
| 7086 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 7087 | 0, /*tp_print*/ |
| 7088 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7089 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 7090 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7091 | 0, /*tp_repr*/ |
| 7092 | 0, /*tp_as_number*/ |
| 7093 | 0, /*tp_as_sequence*/ |
| 7094 | 0, /*tp_as_mapping*/ |
| 7095 | 0, /*tp_hash*/ |
| 7096 | 0, /*tp_call*/ |
| 7097 | 0, /*tp_str*/ |
| 7098 | 0, /*tp_getattro*/ |
| 7099 | 0, /*tp_setattro*/ |
| 7100 | 0, /*tp_as_buffer*/ |
| 7101 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7102 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7103 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7104 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7105 | 0, /*tp_richcompare*/ |
| 7106 | 0, /*tp_weaklistoffset*/ |
| 7107 | 0, /*tp_iter*/ |
| 7108 | 0, /*tp_iternext*/ |
| 7109 | Unpickler_methods, /*tp_methods*/ |
| 7110 | 0, /*tp_members*/ |
| 7111 | Unpickler_getsets, /*tp_getset*/ |
| 7112 | 0, /*tp_base*/ |
| 7113 | 0, /*tp_dict*/ |
| 7114 | 0, /*tp_descr_get*/ |
| 7115 | 0, /*tp_descr_set*/ |
| 7116 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7117 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7118 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7119 | PyType_GenericNew, /*tp_new*/ |
| 7120 | PyObject_GC_Del, /*tp_free*/ |
| 7121 | 0, /*tp_is_gc*/ |
| 7122 | }; |
| 7123 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7124 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7125 | |
| 7126 | _pickle.dump |
| 7127 | |
| 7128 | obj: object |
| 7129 | file: object |
| 7130 | protocol: object = NULL |
| 7131 | * |
| 7132 | fix_imports: bool = True |
| 7133 | |
| 7134 | Write a pickled representation of obj to the open file object file. |
| 7135 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7136 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7137 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7138 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7139 | The optional *protocol* argument tells the pickler to use the given |
| 7140 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 7141 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7142 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7143 | Specifying a negative protocol version selects the highest protocol |
| 7144 | version supported. The higher the protocol used, the more recent the |
| 7145 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7146 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7147 | The *file* argument must have a write() method that accepts a single |
| 7148 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7149 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7150 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7151 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7152 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7153 | to map the new Python 3 names to the old module names used in Python |
| 7154 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7155 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7156 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7157 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7158 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7159 | PyObject *protocol, int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7160 | /*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7161 | { |
| 7162 | PicklerObject *pickler = _Pickler_New(); |
| 7163 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7164 | if (pickler == NULL) |
| 7165 | return NULL; |
| 7166 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7167 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7168 | goto error; |
| 7169 | |
| 7170 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7171 | goto error; |
| 7172 | |
| 7173 | if (dump(pickler, obj) < 0) |
| 7174 | goto error; |
| 7175 | |
| 7176 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7177 | goto error; |
| 7178 | |
| 7179 | Py_DECREF(pickler); |
| 7180 | Py_RETURN_NONE; |
| 7181 | |
| 7182 | error: |
| 7183 | Py_XDECREF(pickler); |
| 7184 | return NULL; |
| 7185 | } |
| 7186 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7187 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7188 | |
| 7189 | _pickle.dumps |
| 7190 | |
| 7191 | obj: object |
| 7192 | protocol: object = NULL |
| 7193 | * |
| 7194 | fix_imports: bool = True |
| 7195 | |
| 7196 | Return the pickled representation of the object as a bytes object. |
| 7197 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7198 | The optional *protocol* argument tells the pickler to use the given |
| 7199 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7200 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7201 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7202 | Specifying a negative protocol version selects the highest protocol |
| 7203 | version supported. The higher the protocol used, the more recent the |
| 7204 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7205 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7206 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7207 | try to map the new Python 3 names to the old module names used in |
| 7208 | Python 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7209 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7210 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7211 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7212 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7213 | int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7214 | /*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7215 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7216 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7217 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7218 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7219 | if (pickler == NULL) |
| 7220 | return NULL; |
| 7221 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7222 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7223 | goto error; |
| 7224 | |
| 7225 | if (dump(pickler, obj) < 0) |
| 7226 | goto error; |
| 7227 | |
| 7228 | result = _Pickler_GetString(pickler); |
| 7229 | Py_DECREF(pickler); |
| 7230 | return result; |
| 7231 | |
| 7232 | error: |
| 7233 | Py_XDECREF(pickler); |
| 7234 | return NULL; |
| 7235 | } |
| 7236 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7237 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7238 | |
| 7239 | _pickle.load |
| 7240 | |
| 7241 | file: object |
| 7242 | * |
| 7243 | fix_imports: bool = True |
| 7244 | encoding: str = 'ASCII' |
| 7245 | errors: str = 'strict' |
| 7246 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7247 | 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] | 7248 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7249 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7250 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7251 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7252 | The protocol version of the pickle is detected automatically, so no |
| 7253 | protocol argument is needed. Bytes past the pickled object's |
| 7254 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7255 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7256 | The argument *file* must have two methods, a read() method that takes |
| 7257 | an integer argument, and a readline() method that requires no |
| 7258 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7259 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7260 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7261 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7262 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7263 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7264 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7265 | map the old Python 2 names to the new names used in Python 3. The |
| 7266 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7267 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7268 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7269 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7270 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7271 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7272 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7273 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7274 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7275 | /*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7276 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7277 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7278 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7279 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7280 | if (unpickler == NULL) |
| 7281 | return NULL; |
| 7282 | |
| 7283 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7284 | goto error; |
| 7285 | |
| 7286 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7287 | goto error; |
| 7288 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7289 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7290 | |
| 7291 | result = load(unpickler); |
| 7292 | Py_DECREF(unpickler); |
| 7293 | return result; |
| 7294 | |
| 7295 | error: |
| 7296 | Py_XDECREF(unpickler); |
| 7297 | return NULL; |
| 7298 | } |
| 7299 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7300 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7301 | |
| 7302 | _pickle.loads |
| 7303 | |
| 7304 | data: object |
| 7305 | * |
| 7306 | fix_imports: bool = True |
| 7307 | encoding: str = 'ASCII' |
| 7308 | errors: str = 'strict' |
| 7309 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7310 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7311 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7312 | The protocol version of the pickle is detected automatically, so no |
| 7313 | protocol argument is needed. Bytes past the pickled object's |
| 7314 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7315 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7316 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7317 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7318 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7319 | map the old Python 2 names to the new names used in Python 3. The |
| 7320 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7321 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7322 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7323 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7324 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7325 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7326 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7327 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7328 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7329 | /*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7330 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7331 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7332 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7333 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7334 | if (unpickler == NULL) |
| 7335 | return NULL; |
| 7336 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7337 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7338 | goto error; |
| 7339 | |
| 7340 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7341 | goto error; |
| 7342 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7343 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7344 | |
| 7345 | result = load(unpickler); |
| 7346 | Py_DECREF(unpickler); |
| 7347 | return result; |
| 7348 | |
| 7349 | error: |
| 7350 | Py_XDECREF(unpickler); |
| 7351 | return NULL; |
| 7352 | } |
| 7353 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7354 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7355 | _PICKLE_DUMP_METHODDEF |
| 7356 | _PICKLE_DUMPS_METHODDEF |
| 7357 | _PICKLE_LOAD_METHODDEF |
| 7358 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7359 | {NULL, NULL} /* sentinel */ |
| 7360 | }; |
| 7361 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7362 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7363 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7364 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7365 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7366 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7367 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7368 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7369 | static void |
| 7370 | pickle_free(PyObject *m) |
| 7371 | { |
| 7372 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7373 | } |
| 7374 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7375 | static int |
| 7376 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7377 | { |
| 7378 | PickleState *st = _Pickle_GetState(m); |
| 7379 | Py_VISIT(st->PickleError); |
| 7380 | Py_VISIT(st->PicklingError); |
| 7381 | Py_VISIT(st->UnpicklingError); |
| 7382 | Py_VISIT(st->dispatch_table); |
| 7383 | Py_VISIT(st->extension_registry); |
| 7384 | Py_VISIT(st->extension_cache); |
| 7385 | Py_VISIT(st->inverted_registry); |
| 7386 | Py_VISIT(st->name_mapping_2to3); |
| 7387 | Py_VISIT(st->import_mapping_2to3); |
| 7388 | Py_VISIT(st->name_mapping_3to2); |
| 7389 | Py_VISIT(st->import_mapping_3to2); |
| 7390 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7391 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7392 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7393 | } |
| 7394 | |
| 7395 | static struct PyModuleDef _picklemodule = { |
| 7396 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7397 | "_pickle", /* m_name */ |
| 7398 | pickle_module_doc, /* m_doc */ |
| 7399 | sizeof(PickleState), /* m_size */ |
| 7400 | pickle_methods, /* m_methods */ |
| 7401 | NULL, /* m_reload */ |
| 7402 | pickle_traverse, /* m_traverse */ |
| 7403 | pickle_clear, /* m_clear */ |
| 7404 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7405 | }; |
| 7406 | |
| 7407 | PyMODINIT_FUNC |
| 7408 | PyInit__pickle(void) |
| 7409 | { |
| 7410 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7411 | PickleState *st; |
| 7412 | |
| 7413 | m = PyState_FindModule(&_picklemodule); |
| 7414 | if (m) { |
| 7415 | Py_INCREF(m); |
| 7416 | return m; |
| 7417 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7418 | |
| 7419 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7420 | return NULL; |
| 7421 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7422 | return NULL; |
| 7423 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7424 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7425 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7426 | return NULL; |
| 7427 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7428 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7429 | |
| 7430 | /* Create the module and add the functions. */ |
| 7431 | m = PyModule_Create(&_picklemodule); |
| 7432 | if (m == NULL) |
| 7433 | return NULL; |
| 7434 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7435 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7436 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7437 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7438 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7439 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7440 | return NULL; |
| 7441 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7442 | st = _Pickle_GetState(m); |
| 7443 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7444 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7445 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7446 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7447 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7448 | st->PicklingError = \ |
| 7449 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7450 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7451 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7452 | st->UnpicklingError = \ |
| 7453 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7454 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7455 | return NULL; |
| 7456 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7457 | Py_INCREF(st->PickleError); |
| 7458 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7459 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7460 | Py_INCREF(st->PicklingError); |
| 7461 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7462 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7463 | Py_INCREF(st->UnpicklingError); |
| 7464 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7465 | return NULL; |
| 7466 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7467 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7468 | return NULL; |
| 7469 | |
| 7470 | return m; |
| 7471 | } |