Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1 | #include "Python.h" |
| 2 | #include "structmember.h" |
| 3 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4 | PyDoc_STRVAR(pickle_module_doc, |
| 5 | "Optimized C implementation for the Python pickle module."); |
| 6 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 8 | module _pickle |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 9 | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
| 10 | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
| 11 | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
| 12 | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 13 | [clinic start generated code]*/ |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 14 | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 15 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 16 | /* Bump this when new opcodes are added to the pickle protocol. */ |
| 17 | enum { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 18 | HIGHEST_PROTOCOL = 4, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 19 | DEFAULT_PROTOCOL = 3 |
| 20 | }; |
| 21 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 22 | /* Pickle opcodes. These must be kept updated with pickle.py. |
| 23 | Extensive docs are in pickletools.py. */ |
| 24 | enum opcode { |
| 25 | MARK = '(', |
| 26 | STOP = '.', |
| 27 | POP = '0', |
| 28 | POP_MARK = '1', |
| 29 | DUP = '2', |
| 30 | FLOAT = 'F', |
| 31 | INT = 'I', |
| 32 | BININT = 'J', |
| 33 | BININT1 = 'K', |
| 34 | LONG = 'L', |
| 35 | BININT2 = 'M', |
| 36 | NONE = 'N', |
| 37 | PERSID = 'P', |
| 38 | BINPERSID = 'Q', |
| 39 | REDUCE = 'R', |
| 40 | STRING = 'S', |
| 41 | BINSTRING = 'T', |
| 42 | SHORT_BINSTRING = 'U', |
| 43 | UNICODE = 'V', |
| 44 | BINUNICODE = 'X', |
| 45 | APPEND = 'a', |
| 46 | BUILD = 'b', |
| 47 | GLOBAL = 'c', |
| 48 | DICT = 'd', |
| 49 | EMPTY_DICT = '}', |
| 50 | APPENDS = 'e', |
| 51 | GET = 'g', |
| 52 | BINGET = 'h', |
| 53 | INST = 'i', |
| 54 | LONG_BINGET = 'j', |
| 55 | LIST = 'l', |
| 56 | EMPTY_LIST = ']', |
| 57 | OBJ = 'o', |
| 58 | PUT = 'p', |
| 59 | BINPUT = 'q', |
| 60 | LONG_BINPUT = 'r', |
| 61 | SETITEM = 's', |
| 62 | TUPLE = 't', |
| 63 | EMPTY_TUPLE = ')', |
| 64 | SETITEMS = 'u', |
| 65 | BINFLOAT = 'G', |
| 66 | |
| 67 | /* Protocol 2. */ |
| 68 | PROTO = '\x80', |
| 69 | NEWOBJ = '\x81', |
| 70 | EXT1 = '\x82', |
| 71 | EXT2 = '\x83', |
| 72 | EXT4 = '\x84', |
| 73 | TUPLE1 = '\x85', |
| 74 | TUPLE2 = '\x86', |
| 75 | TUPLE3 = '\x87', |
| 76 | NEWTRUE = '\x88', |
| 77 | NEWFALSE = '\x89', |
| 78 | LONG1 = '\x8a', |
| 79 | LONG4 = '\x8b', |
| 80 | |
| 81 | /* Protocol 3 (Python 3.x) */ |
| 82 | BINBYTES = 'B', |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 83 | SHORT_BINBYTES = 'C', |
| 84 | |
| 85 | /* Protocol 4 */ |
| 86 | SHORT_BINUNICODE = '\x8c', |
| 87 | BINUNICODE8 = '\x8d', |
| 88 | BINBYTES8 = '\x8e', |
| 89 | EMPTY_SET = '\x8f', |
| 90 | ADDITEMS = '\x90', |
| 91 | FROZENSET = '\x91', |
| 92 | NEWOBJ_EX = '\x92', |
| 93 | STACK_GLOBAL = '\x93', |
| 94 | MEMOIZE = '\x94', |
| 95 | FRAME = '\x95' |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 96 | }; |
| 97 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 98 | enum { |
| 99 | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
| 100 | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
| 101 | break if this gets out of synch with pickle.py, but it's unclear that would |
| 102 | help anything either. */ |
| 103 | BATCHSIZE = 1000, |
| 104 | |
| 105 | /* Nesting limit until Pickler, when running in "fast mode", starts |
| 106 | checking for self-referential data-structures. */ |
| 107 | FAST_NESTING_LIMIT = 50, |
| 108 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 109 | /* Initial size of the write buffer of Pickler. */ |
| 110 | WRITE_BUF_SIZE = 4096, |
| 111 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 112 | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 113 | PREFETCH = 8192 * 16, |
| 114 | |
| 115 | FRAME_SIZE_TARGET = 64 * 1024, |
| 116 | |
| 117 | FRAME_HEADER_SIZE = 9 |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 118 | }; |
| 119 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 120 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 121 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 122 | /* State of the pickle module, per PEP 3121. */ |
| 123 | typedef struct { |
| 124 | /* Exception classes for pickle. */ |
| 125 | PyObject *PickleError; |
| 126 | PyObject *PicklingError; |
| 127 | PyObject *UnpicklingError; |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 128 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 129 | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
| 130 | PyObject *dispatch_table; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 131 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 132 | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 133 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 134 | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
| 135 | PyObject *extension_registry; |
| 136 | /* copyreg._extension_cache, {code: object} */ |
| 137 | PyObject *extension_cache; |
| 138 | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
| 139 | PyObject *inverted_registry; |
| 140 | |
| 141 | /* Import mappings for compatibility with Python 2.x */ |
| 142 | |
| 143 | /* _compat_pickle.NAME_MAPPING, |
| 144 | {(oldmodule, oldname): (newmodule, newname)} */ |
| 145 | PyObject *name_mapping_2to3; |
| 146 | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
| 147 | PyObject *import_mapping_2to3; |
| 148 | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
| 149 | PyObject *name_mapping_3to2; |
| 150 | PyObject *import_mapping_3to2; |
| 151 | |
| 152 | /* codecs.encode, used for saving bytes in older protocols */ |
| 153 | PyObject *codecs_encode; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 154 | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
| 155 | PyObject *getattr; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 156 | /* functools.partial, used for implementing __newobj_ex__ with protocols |
| 157 | 2 and 3 */ |
| 158 | PyObject *partial; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 159 | } PickleState; |
| 160 | |
| 161 | /* Forward declaration of the _pickle module definition. */ |
| 162 | static struct PyModuleDef _picklemodule; |
| 163 | |
| 164 | /* Given a module object, get its per-module state. */ |
| 165 | static PickleState * |
| 166 | _Pickle_GetState(PyObject *module) |
| 167 | { |
| 168 | return (PickleState *)PyModule_GetState(module); |
| 169 | } |
| 170 | |
| 171 | /* Find the module instance imported in the currently running sub-interpreter |
| 172 | and get its state. */ |
| 173 | static PickleState * |
| 174 | _Pickle_GetGlobalState(void) |
| 175 | { |
| 176 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 177 | } |
| 178 | |
| 179 | /* Clear the given pickle module state. */ |
| 180 | static void |
| 181 | _Pickle_ClearState(PickleState *st) |
| 182 | { |
| 183 | Py_CLEAR(st->PickleError); |
| 184 | Py_CLEAR(st->PicklingError); |
| 185 | Py_CLEAR(st->UnpicklingError); |
| 186 | Py_CLEAR(st->dispatch_table); |
| 187 | Py_CLEAR(st->extension_registry); |
| 188 | Py_CLEAR(st->extension_cache); |
| 189 | Py_CLEAR(st->inverted_registry); |
| 190 | Py_CLEAR(st->name_mapping_2to3); |
| 191 | Py_CLEAR(st->import_mapping_2to3); |
| 192 | Py_CLEAR(st->name_mapping_3to2); |
| 193 | Py_CLEAR(st->import_mapping_3to2); |
| 194 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 195 | Py_CLEAR(st->getattr); |
Victor Stinner | 9ba97df | 2015-11-17 12:15:07 +0100 | [diff] [blame] | 196 | Py_CLEAR(st->partial); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* Initialize the given pickle module state. */ |
| 200 | static int |
| 201 | _Pickle_InitState(PickleState *st) |
| 202 | { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 203 | PyObject *builtins; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 204 | PyObject *copyreg = NULL; |
| 205 | PyObject *compat_pickle = NULL; |
| 206 | PyObject *codecs = NULL; |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 207 | PyObject *functools = NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 208 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 209 | builtins = PyEval_GetBuiltins(); |
| 210 | if (builtins == NULL) |
| 211 | goto error; |
| 212 | st->getattr = PyDict_GetItemString(builtins, "getattr"); |
| 213 | if (st->getattr == NULL) |
| 214 | goto error; |
| 215 | Py_INCREF(st->getattr); |
| 216 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 217 | copyreg = PyImport_ImportModule("copyreg"); |
| 218 | if (!copyreg) |
| 219 | goto error; |
| 220 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 221 | if (!st->dispatch_table) |
| 222 | goto error; |
| 223 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 224 | PyErr_Format(PyExc_RuntimeError, |
| 225 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 226 | Py_TYPE(st->dispatch_table)->tp_name); |
| 227 | goto error; |
| 228 | } |
| 229 | st->extension_registry = \ |
| 230 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 231 | if (!st->extension_registry) |
| 232 | goto error; |
| 233 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 234 | PyErr_Format(PyExc_RuntimeError, |
| 235 | "copyreg._extension_registry should be a dict, " |
| 236 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 237 | goto error; |
| 238 | } |
| 239 | st->inverted_registry = \ |
| 240 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 241 | if (!st->inverted_registry) |
| 242 | goto error; |
| 243 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 244 | PyErr_Format(PyExc_RuntimeError, |
| 245 | "copyreg._inverted_registry should be a dict, " |
| 246 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 247 | goto error; |
| 248 | } |
| 249 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 250 | if (!st->extension_cache) |
| 251 | goto error; |
| 252 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 253 | PyErr_Format(PyExc_RuntimeError, |
| 254 | "copyreg._extension_cache should be a dict, " |
| 255 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 256 | goto error; |
| 257 | } |
| 258 | Py_CLEAR(copyreg); |
| 259 | |
| 260 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 261 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 262 | if (!compat_pickle) |
| 263 | goto error; |
| 264 | st->name_mapping_2to3 = \ |
| 265 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 266 | if (!st->name_mapping_2to3) |
| 267 | goto error; |
| 268 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 269 | PyErr_Format(PyExc_RuntimeError, |
| 270 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 271 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 272 | goto error; |
| 273 | } |
| 274 | st->import_mapping_2to3 = \ |
| 275 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 276 | if (!st->import_mapping_2to3) |
| 277 | goto error; |
| 278 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 279 | PyErr_Format(PyExc_RuntimeError, |
| 280 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 281 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 282 | goto error; |
| 283 | } |
| 284 | /* ... and the 3.x -> 2.x mapping tables */ |
| 285 | st->name_mapping_3to2 = \ |
| 286 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 287 | if (!st->name_mapping_3to2) |
| 288 | goto error; |
| 289 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 290 | PyErr_Format(PyExc_RuntimeError, |
| 291 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 292 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 293 | goto error; |
| 294 | } |
| 295 | st->import_mapping_3to2 = \ |
| 296 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 297 | if (!st->import_mapping_3to2) |
| 298 | goto error; |
| 299 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 300 | PyErr_Format(PyExc_RuntimeError, |
| 301 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 302 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 303 | goto error; |
| 304 | } |
| 305 | Py_CLEAR(compat_pickle); |
| 306 | |
| 307 | codecs = PyImport_ImportModule("codecs"); |
| 308 | if (codecs == NULL) |
| 309 | goto error; |
| 310 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 311 | if (st->codecs_encode == NULL) { |
| 312 | goto error; |
| 313 | } |
| 314 | if (!PyCallable_Check(st->codecs_encode)) { |
| 315 | PyErr_Format(PyExc_RuntimeError, |
| 316 | "codecs.encode should be a callable, not %.200s", |
| 317 | Py_TYPE(st->codecs_encode)->tp_name); |
| 318 | goto error; |
| 319 | } |
| 320 | Py_CLEAR(codecs); |
| 321 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 322 | functools = PyImport_ImportModule("functools"); |
| 323 | if (!functools) |
| 324 | goto error; |
| 325 | st->partial = PyObject_GetAttrString(functools, "partial"); |
| 326 | if (!st->partial) |
| 327 | goto error; |
| 328 | Py_CLEAR(functools); |
| 329 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 330 | return 0; |
| 331 | |
| 332 | error: |
| 333 | Py_CLEAR(copyreg); |
| 334 | Py_CLEAR(compat_pickle); |
| 335 | Py_CLEAR(codecs); |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 336 | Py_CLEAR(functools); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 337 | _Pickle_ClearState(st); |
| 338 | return -1; |
| 339 | } |
| 340 | |
| 341 | /* Helper for calling a function with a single argument quickly. |
| 342 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 343 | This function steals the reference of the given argument. */ |
| 344 | static PyObject * |
| 345 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 346 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 347 | PyObject *result; |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 348 | PyObject *arg_tuple = PyTuple_New(1); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 349 | |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 350 | /* Note: this function used to reuse the argument tuple. This used to give |
| 351 | a slight performance boost with older pickle implementations where many |
| 352 | unbuffered reads occurred (thus needing many function calls). |
| 353 | |
| 354 | However, this optimization was removed because it was too complicated |
| 355 | to get right. It abused the C API for tuples to mutate them which led |
| 356 | to subtle reference counting and concurrency bugs. Furthermore, the |
| 357 | introduction of protocol 4 and the prefetching optimization via peek() |
| 358 | significantly reduced the number of function calls we do. Thus, the |
| 359 | benefits became marginal at best. */ |
| 360 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 361 | if (arg_tuple == NULL) { |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 362 | Py_DECREF(obj); |
| 363 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 364 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 365 | PyTuple_SET_ITEM(arg_tuple, 0, obj); |
| 366 | result = PyObject_Call(func, arg_tuple, NULL); |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 367 | Py_CLEAR(arg_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 368 | return result; |
| 369 | } |
| 370 | |
| 371 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 372 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 373 | /* Internal data type used as the unpickling stack. */ |
| 374 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 375 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 376 | PyObject **data; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 377 | int mark_set; /* is MARK set? */ |
| 378 | Py_ssize_t fence; /* position of top MARK or 0 */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 379 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 380 | } Pdata; |
| 381 | |
| 382 | static void |
| 383 | Pdata_dealloc(Pdata *self) |
| 384 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 385 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 386 | while (--i >= 0) { |
| 387 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 388 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 389 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 390 | PyObject_Del(self); |
| 391 | } |
| 392 | |
| 393 | static PyTypeObject Pdata_Type = { |
| 394 | PyVarObject_HEAD_INIT(NULL, 0) |
| 395 | "_pickle.Pdata", /*tp_name*/ |
| 396 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 397 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 398 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 399 | }; |
| 400 | |
| 401 | static PyObject * |
| 402 | Pdata_New(void) |
| 403 | { |
| 404 | Pdata *self; |
| 405 | |
| 406 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 407 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 408 | Py_SIZE(self) = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 409 | self->mark_set = 0; |
| 410 | self->fence = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 411 | self->allocated = 8; |
| 412 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 413 | if (self->data) |
| 414 | return (PyObject *)self; |
| 415 | Py_DECREF(self); |
| 416 | return PyErr_NoMemory(); |
| 417 | } |
| 418 | |
| 419 | |
| 420 | /* Retain only the initial clearto items. If clearto >= the current |
| 421 | * number of items, this is a (non-erroneous) NOP. |
| 422 | */ |
| 423 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 424 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 425 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 426 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 427 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 428 | assert(clearto >= self->fence); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 429 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 430 | return 0; |
| 431 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 432 | while (--i >= clearto) { |
| 433 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 434 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 435 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 436 | return 0; |
| 437 | } |
| 438 | |
| 439 | static int |
| 440 | Pdata_grow(Pdata *self) |
| 441 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 442 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 443 | size_t allocated = (size_t)self->allocated; |
| 444 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 445 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 446 | new_allocated = (allocated >> 3) + 6; |
| 447 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 448 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 449 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 450 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 451 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 452 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 453 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 454 | |
| 455 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 456 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 457 | return 0; |
| 458 | |
| 459 | nomemory: |
| 460 | PyErr_NoMemory(); |
| 461 | return -1; |
| 462 | } |
| 463 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 464 | static int |
| 465 | Pdata_stack_underflow(Pdata *self) |
| 466 | { |
| 467 | PickleState *st = _Pickle_GetGlobalState(); |
| 468 | PyErr_SetString(st->UnpicklingError, |
| 469 | self->mark_set ? |
| 470 | "unexpected MARK found" : |
| 471 | "unpickling stack underflow"); |
| 472 | return -1; |
| 473 | } |
| 474 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 475 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 476 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 477 | * is raised and V is set to NULL. |
| 478 | */ |
| 479 | static PyObject * |
| 480 | Pdata_pop(Pdata *self) |
| 481 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 482 | if (Py_SIZE(self) <= self->fence) { |
| 483 | Pdata_stack_underflow(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 484 | return NULL; |
| 485 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 486 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 487 | } |
| 488 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 489 | |
| 490 | static int |
| 491 | Pdata_push(Pdata *self, PyObject *obj) |
| 492 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 493 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 494 | return -1; |
| 495 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 496 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 501 | #define PDATA_PUSH(D, O, ER) do { \ |
| 502 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 503 | |
| 504 | /* Push an object on stack, adding a new reference to the object. */ |
| 505 | #define PDATA_APPEND(D, O, ER) do { \ |
| 506 | Py_INCREF((O)); \ |
| 507 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 508 | |
| 509 | static PyObject * |
| 510 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 511 | { |
| 512 | PyObject *tuple; |
| 513 | Py_ssize_t len, i, j; |
| 514 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 515 | if (start < self->fence) { |
| 516 | Pdata_stack_underflow(self); |
| 517 | return NULL; |
| 518 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 519 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 520 | tuple = PyTuple_New(len); |
| 521 | if (tuple == NULL) |
| 522 | return NULL; |
| 523 | for (i = start, j = 0; j < len; i++, j++) |
| 524 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 525 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 526 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 527 | return tuple; |
| 528 | } |
| 529 | |
| 530 | static PyObject * |
| 531 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 532 | { |
| 533 | PyObject *list; |
| 534 | Py_ssize_t len, i, j; |
| 535 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 536 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 537 | list = PyList_New(len); |
| 538 | if (list == NULL) |
| 539 | return NULL; |
| 540 | for (i = start, j = 0; j < len; i++, j++) |
| 541 | PyList_SET_ITEM(list, j, self->data[i]); |
| 542 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 543 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 544 | return list; |
| 545 | } |
| 546 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 547 | typedef struct { |
| 548 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 549 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 550 | } PyMemoEntry; |
| 551 | |
| 552 | typedef struct { |
| 553 | Py_ssize_t mt_mask; |
| 554 | Py_ssize_t mt_used; |
| 555 | Py_ssize_t mt_allocated; |
| 556 | PyMemoEntry *mt_table; |
| 557 | } PyMemoTable; |
| 558 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 559 | typedef struct PicklerObject { |
| 560 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 561 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 562 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 563 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 564 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 565 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 566 | |
| 567 | PyObject *write; /* write() method of the output stream. */ |
| 568 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 569 | flushing to the stream. */ |
| 570 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 571 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 572 | int proto; /* Pickle protocol number, >= 0 */ |
| 573 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 574 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 575 | Py_ssize_t frame_start; /* Position in output_buffer where the |
Martin Panter | a90a4a9 | 2016-05-30 04:04:50 +0000 | [diff] [blame] | 576 | current frame begins. -1 if there |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 577 | is no frame currently open. */ |
| 578 | |
| 579 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 580 | int fast; /* Enable fast mode if set to a true value. |
| 581 | The fast mode disable the usage of memo, |
| 582 | therefore speeding the pickling process by |
| 583 | not generating superfluous PUT opcodes. It |
| 584 | should not be used if with self-referential |
| 585 | objects. */ |
| 586 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 587 | int fix_imports; /* Indicate whether Pickler should fix |
| 588 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 589 | PyObject *fast_memo; |
| 590 | } PicklerObject; |
| 591 | |
| 592 | typedef struct UnpicklerObject { |
| 593 | PyObject_HEAD |
| 594 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 595 | |
| 596 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 597 | is unnecessary, since the keys are contiguous ints. */ |
| 598 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 599 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 600 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 601 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 602 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 603 | |
| 604 | Py_buffer buffer; |
| 605 | char *input_buffer; |
| 606 | char *input_line; |
| 607 | Py_ssize_t input_len; |
| 608 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 609 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 610 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 611 | PyObject *read; /* read() method of the input stream. */ |
| 612 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 613 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 614 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 615 | char *encoding; /* Name of the encoding to be used for |
| 616 | decoding strings pickled using Python |
| 617 | 2.x. The default value is "ASCII" */ |
| 618 | char *errors; /* Name of errors handling scheme to used when |
| 619 | decoding strings. The default value is |
| 620 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 621 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 622 | objects. */ |
| 623 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 624 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 625 | int proto; /* Protocol of the pickle loaded. */ |
| 626 | int fix_imports; /* Indicate whether Unpickler should fix |
| 627 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 628 | } UnpicklerObject; |
| 629 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 630 | typedef struct { |
| 631 | PyObject_HEAD |
| 632 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 633 | } PicklerMemoProxyObject; |
| 634 | |
| 635 | typedef struct { |
| 636 | PyObject_HEAD |
| 637 | UnpicklerObject *unpickler; |
| 638 | } UnpicklerMemoProxyObject; |
| 639 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 640 | /* Forward declarations */ |
| 641 | static int save(PicklerObject *, PyObject *, int); |
| 642 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 643 | static PyTypeObject Pickler_Type; |
| 644 | static PyTypeObject Unpickler_Type; |
| 645 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 646 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 647 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 648 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 649 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 650 | 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] | 651 | a bunch of unnecessary object creation. This makes a huge performance |
| 652 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 653 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 654 | #define MT_MINSIZE 8 |
| 655 | #define PERTURB_SHIFT 5 |
| 656 | |
| 657 | |
| 658 | static PyMemoTable * |
| 659 | PyMemoTable_New(void) |
| 660 | { |
| 661 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 662 | if (memo == NULL) { |
| 663 | PyErr_NoMemory(); |
| 664 | return NULL; |
| 665 | } |
| 666 | |
| 667 | memo->mt_used = 0; |
| 668 | memo->mt_allocated = MT_MINSIZE; |
| 669 | memo->mt_mask = MT_MINSIZE - 1; |
| 670 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 671 | if (memo->mt_table == NULL) { |
| 672 | PyMem_FREE(memo); |
| 673 | PyErr_NoMemory(); |
| 674 | return NULL; |
| 675 | } |
| 676 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 677 | |
| 678 | return memo; |
| 679 | } |
| 680 | |
| 681 | static PyMemoTable * |
| 682 | PyMemoTable_Copy(PyMemoTable *self) |
| 683 | { |
| 684 | Py_ssize_t i; |
| 685 | PyMemoTable *new = PyMemoTable_New(); |
| 686 | if (new == NULL) |
| 687 | return NULL; |
| 688 | |
| 689 | new->mt_used = self->mt_used; |
| 690 | new->mt_allocated = self->mt_allocated; |
| 691 | new->mt_mask = self->mt_mask; |
| 692 | /* The table we get from _New() is probably smaller than we wanted. |
| 693 | Free it and allocate one that's the right size. */ |
| 694 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 695 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 696 | if (new->mt_table == NULL) { |
| 697 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 698 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 699 | return NULL; |
| 700 | } |
| 701 | for (i = 0; i < self->mt_allocated; i++) { |
| 702 | Py_XINCREF(self->mt_table[i].me_key); |
| 703 | } |
| 704 | memcpy(new->mt_table, self->mt_table, |
| 705 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 706 | |
| 707 | return new; |
| 708 | } |
| 709 | |
| 710 | static Py_ssize_t |
| 711 | PyMemoTable_Size(PyMemoTable *self) |
| 712 | { |
| 713 | return self->mt_used; |
| 714 | } |
| 715 | |
| 716 | static int |
| 717 | PyMemoTable_Clear(PyMemoTable *self) |
| 718 | { |
| 719 | Py_ssize_t i = self->mt_allocated; |
| 720 | |
| 721 | while (--i >= 0) { |
| 722 | Py_XDECREF(self->mt_table[i].me_key); |
| 723 | } |
| 724 | self->mt_used = 0; |
| 725 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 726 | return 0; |
| 727 | } |
| 728 | |
| 729 | static void |
| 730 | PyMemoTable_Del(PyMemoTable *self) |
| 731 | { |
| 732 | if (self == NULL) |
| 733 | return; |
| 734 | PyMemoTable_Clear(self); |
| 735 | |
| 736 | PyMem_FREE(self->mt_table); |
| 737 | PyMem_FREE(self); |
| 738 | } |
| 739 | |
| 740 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 741 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 742 | static PyMemoEntry * |
| 743 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 744 | { |
| 745 | size_t i; |
| 746 | size_t perturb; |
| 747 | size_t mask = (size_t)self->mt_mask; |
| 748 | PyMemoEntry *table = self->mt_table; |
| 749 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 750 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 751 | |
| 752 | i = hash & mask; |
| 753 | entry = &table[i]; |
| 754 | if (entry->me_key == NULL || entry->me_key == key) |
| 755 | return entry; |
| 756 | |
| 757 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 758 | i = (i << 2) + i + perturb + 1; |
| 759 | entry = &table[i & mask]; |
| 760 | if (entry->me_key == NULL || entry->me_key == key) |
| 761 | return entry; |
| 762 | } |
| 763 | assert(0); /* Never reached */ |
| 764 | return NULL; |
| 765 | } |
| 766 | |
| 767 | /* Returns -1 on failure, 0 on success. */ |
| 768 | static int |
| 769 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 770 | { |
| 771 | PyMemoEntry *oldtable = NULL; |
| 772 | PyMemoEntry *oldentry, *newentry; |
| 773 | Py_ssize_t new_size = MT_MINSIZE; |
| 774 | Py_ssize_t to_process; |
| 775 | |
| 776 | assert(min_size > 0); |
| 777 | |
| 778 | /* Find the smallest valid table size >= min_size. */ |
| 779 | while (new_size < min_size && new_size > 0) |
| 780 | new_size <<= 1; |
| 781 | if (new_size <= 0) { |
| 782 | PyErr_NoMemory(); |
| 783 | return -1; |
| 784 | } |
| 785 | /* new_size needs to be a power of two. */ |
| 786 | assert((new_size & (new_size - 1)) == 0); |
| 787 | |
| 788 | /* Allocate new table. */ |
| 789 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 790 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 791 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 792 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 793 | PyErr_NoMemory(); |
| 794 | return -1; |
| 795 | } |
| 796 | self->mt_allocated = new_size; |
| 797 | self->mt_mask = new_size - 1; |
| 798 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 799 | |
| 800 | /* Copy entries from the old table. */ |
| 801 | to_process = self->mt_used; |
| 802 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 803 | if (oldentry->me_key != NULL) { |
| 804 | to_process--; |
| 805 | /* newentry is a pointer to a chunk of the new |
| 806 | mt_table, so we're setting the key:value pair |
| 807 | in-place. */ |
| 808 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 809 | newentry->me_key = oldentry->me_key; |
| 810 | newentry->me_value = oldentry->me_value; |
| 811 | } |
| 812 | } |
| 813 | |
| 814 | /* Deallocate the old table. */ |
| 815 | PyMem_FREE(oldtable); |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 820 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 821 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 822 | { |
| 823 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 824 | if (entry->me_key == NULL) |
| 825 | return NULL; |
| 826 | return &entry->me_value; |
| 827 | } |
| 828 | |
| 829 | /* Returns -1 on failure, 0 on success. */ |
| 830 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 831 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 832 | { |
| 833 | PyMemoEntry *entry; |
| 834 | |
| 835 | assert(key != NULL); |
| 836 | |
| 837 | entry = _PyMemoTable_Lookup(self, key); |
| 838 | if (entry->me_key != NULL) { |
| 839 | entry->me_value = value; |
| 840 | return 0; |
| 841 | } |
| 842 | Py_INCREF(key); |
| 843 | entry->me_key = key; |
| 844 | entry->me_value = value; |
| 845 | self->mt_used++; |
| 846 | |
| 847 | /* If we added a key, we can safely resize. Otherwise just return! |
| 848 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 849 | * |
| 850 | * Quadrupling the size improves average table sparseness |
| 851 | * (reducing collisions) at the cost of some memory. It also halves |
| 852 | * the number of expensive resize operations in a growing memo table. |
| 853 | * |
| 854 | * Very large memo tables (over 50K items) use doubling instead. |
| 855 | * This may help applications with severe memory constraints. |
| 856 | */ |
| 857 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 858 | return 0; |
| 859 | return _PyMemoTable_ResizeTable(self, |
| 860 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 861 | } |
| 862 | |
| 863 | #undef MT_MINSIZE |
| 864 | #undef PERTURB_SHIFT |
| 865 | |
| 866 | /*************************************************************************/ |
| 867 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 868 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 869 | static int |
| 870 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 871 | { |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 872 | Py_XSETREF(self->output_buffer, |
Serhiy Storchaka | 4a1e70f | 2015-12-27 12:36:18 +0200 | [diff] [blame] | 873 | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 874 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 875 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 876 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 877 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 878 | return 0; |
| 879 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 880 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 881 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 882 | _write_size64(char *out, size_t value) |
| 883 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 884 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 885 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 886 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 887 | |
| 888 | for (i = 0; i < sizeof(size_t); i++) { |
| 889 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 890 | } |
| 891 | for (i = sizeof(size_t); i < 8; i++) { |
| 892 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 893 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 894 | } |
| 895 | |
| 896 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 897 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 898 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 899 | qdata[0] = FRAME; |
| 900 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | static int |
| 904 | _Pickler_CommitFrame(PicklerObject *self) |
| 905 | { |
| 906 | size_t frame_len; |
| 907 | char *qdata; |
| 908 | |
| 909 | if (!self->framing || self->frame_start == -1) |
| 910 | return 0; |
| 911 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 912 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 913 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 914 | self->frame_start = -1; |
| 915 | return 0; |
| 916 | } |
| 917 | |
| 918 | static int |
| 919 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 920 | { |
| 921 | Py_ssize_t frame_len; |
| 922 | |
| 923 | if (!self->framing || self->frame_start == -1) |
| 924 | return 0; |
| 925 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 926 | if (frame_len >= FRAME_SIZE_TARGET) |
| 927 | return _Pickler_CommitFrame(self); |
| 928 | else |
| 929 | return 0; |
| 930 | } |
| 931 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 932 | static PyObject * |
| 933 | _Pickler_GetString(PicklerObject *self) |
| 934 | { |
| 935 | PyObject *output_buffer = self->output_buffer; |
| 936 | |
| 937 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 938 | |
| 939 | if (_Pickler_CommitFrame(self)) |
| 940 | return NULL; |
| 941 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 942 | self->output_buffer = NULL; |
| 943 | /* Resize down to exact size */ |
| 944 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 945 | return NULL; |
| 946 | return output_buffer; |
| 947 | } |
| 948 | |
| 949 | static int |
| 950 | _Pickler_FlushToFile(PicklerObject *self) |
| 951 | { |
| 952 | PyObject *output, *result; |
| 953 | |
| 954 | assert(self->write != NULL); |
| 955 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 956 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 957 | output = _Pickler_GetString(self); |
| 958 | if (output == NULL) |
| 959 | return -1; |
| 960 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 961 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 962 | Py_XDECREF(result); |
| 963 | return (result == NULL) ? -1 : 0; |
| 964 | } |
| 965 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 966 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 967 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 968 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 969 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 970 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 971 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 972 | |
| 973 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 974 | need_new_frame = (self->framing && self->frame_start == -1); |
| 975 | |
| 976 | if (need_new_frame) |
| 977 | n = data_len + FRAME_HEADER_SIZE; |
| 978 | else |
| 979 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 980 | |
| 981 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 982 | if (required > self->max_output_len) { |
| 983 | /* Make place in buffer for the pickle chunk */ |
| 984 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 985 | PyErr_NoMemory(); |
| 986 | return -1; |
| 987 | } |
| 988 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 989 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 990 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 991 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 992 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 993 | if (need_new_frame) { |
| 994 | /* Setup new frame */ |
| 995 | Py_ssize_t frame_start = self->output_len; |
| 996 | self->frame_start = frame_start; |
| 997 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 998 | /* Write an invalid value, for debugging */ |
| 999 | buffer[frame_start + i] = 0xFE; |
| 1000 | } |
| 1001 | self->output_len += FRAME_HEADER_SIZE; |
| 1002 | } |
| 1003 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1004 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1005 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1006 | buffer[self->output_len + i] = s[i]; |
| 1007 | } |
| 1008 | } |
| 1009 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1010 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1011 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1012 | self->output_len += data_len; |
| 1013 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1016 | static PicklerObject * |
| 1017 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1018 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1019 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1020 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1021 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1022 | if (self == NULL) |
| 1023 | return NULL; |
| 1024 | |
| 1025 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1026 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1027 | self->write = NULL; |
| 1028 | self->proto = 0; |
| 1029 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1030 | self->framing = 0; |
| 1031 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1032 | self->fast = 0; |
| 1033 | self->fast_nesting = 0; |
| 1034 | self->fix_imports = 0; |
| 1035 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1036 | self->max_output_len = WRITE_BUF_SIZE; |
| 1037 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1038 | |
| 1039 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1040 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1041 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1042 | |
| 1043 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1044 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1045 | return NULL; |
| 1046 | } |
| 1047 | return self; |
| 1048 | } |
| 1049 | |
| 1050 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1051 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1052 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1053 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1054 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1055 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1056 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1057 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1058 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1059 | proto = PyLong_AsLong(protocol); |
| 1060 | if (proto < 0) { |
| 1061 | if (proto == -1 && PyErr_Occurred()) |
| 1062 | return -1; |
| 1063 | proto = HIGHEST_PROTOCOL; |
| 1064 | } |
| 1065 | else if (proto > HIGHEST_PROTOCOL) { |
| 1066 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1067 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1068 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1069 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1070 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1071 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1072 | self->bin = proto > 0; |
| 1073 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1074 | return 0; |
| 1075 | } |
| 1076 | |
| 1077 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1078 | be called once on a freshly created Pickler. */ |
| 1079 | static int |
| 1080 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1081 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1082 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1083 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1084 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1085 | if (self->write == NULL) { |
| 1086 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1087 | PyErr_SetString(PyExc_TypeError, |
| 1088 | "file must have a 'write' attribute"); |
| 1089 | return -1; |
| 1090 | } |
| 1091 | |
| 1092 | return 0; |
| 1093 | } |
| 1094 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1095 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1096 | own reference to `input`. */ |
| 1097 | static Py_ssize_t |
| 1098 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1099 | { |
| 1100 | if (self->buffer.buf != NULL) |
| 1101 | PyBuffer_Release(&self->buffer); |
| 1102 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1103 | return -1; |
| 1104 | self->input_buffer = self->buffer.buf; |
| 1105 | self->input_len = self->buffer.len; |
| 1106 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1107 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1108 | return self->input_len; |
| 1109 | } |
| 1110 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1111 | static int |
| 1112 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1113 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1114 | Py_ssize_t consumed; |
| 1115 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1116 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1117 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1118 | if (consumed <= 0) |
| 1119 | return 0; |
| 1120 | |
| 1121 | assert(self->peek); /* otherwise we did something wrong */ |
Martin Panter | 6245cb3 | 2016-04-15 02:14:19 +0000 | [diff] [blame] | 1122 | /* This makes a useless copy... */ |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1123 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1124 | if (r == NULL) |
| 1125 | return -1; |
| 1126 | Py_DECREF(r); |
| 1127 | |
| 1128 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1129 | return 0; |
| 1130 | } |
| 1131 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1132 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1133 | |
| 1134 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1135 | may be multiple pickle objects arranged contiguously in the same input |
| 1136 | buffer. |
| 1137 | |
| 1138 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1139 | bytes from the input stream/buffer. |
| 1140 | |
| 1141 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1142 | failure; on success, returns the number of bytes read from the file. |
| 1143 | |
| 1144 | On success, self->input_len will be 0; this is intentional so that when |
| 1145 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1146 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1147 | size to tell you how much data you can process. */ |
| 1148 | static Py_ssize_t |
| 1149 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1150 | { |
| 1151 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1152 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1153 | |
| 1154 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1155 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1156 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1157 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1158 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1159 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1160 | PyObject *empty_tuple = PyTuple_New(0); |
| 1161 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1162 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1163 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1164 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1165 | PyObject *len; |
| 1166 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1167 | if (self->peek && n < PREFETCH) { |
| 1168 | len = PyLong_FromSsize_t(PREFETCH); |
| 1169 | if (len == NULL) |
| 1170 | return -1; |
| 1171 | data = _Pickle_FastCall(self->peek, len); |
| 1172 | if (data == NULL) { |
| 1173 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1174 | return -1; |
| 1175 | /* peek() is probably not supported by the given file object */ |
| 1176 | PyErr_Clear(); |
| 1177 | Py_CLEAR(self->peek); |
| 1178 | } |
| 1179 | else { |
| 1180 | read_size = _Unpickler_SetStringInput(self, data); |
| 1181 | Py_DECREF(data); |
| 1182 | self->prefetched_idx = 0; |
| 1183 | if (n <= read_size) |
| 1184 | return n; |
| 1185 | } |
| 1186 | } |
| 1187 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1188 | if (len == NULL) |
| 1189 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1190 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1191 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1192 | if (data == NULL) |
| 1193 | return -1; |
| 1194 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1195 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1196 | Py_DECREF(data); |
| 1197 | return read_size; |
| 1198 | } |
| 1199 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1200 | /* Don't call it directly: use _Unpickler_Read() */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1201 | static Py_ssize_t |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1202 | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1203 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1204 | Py_ssize_t num_read; |
| 1205 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1206 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1207 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1208 | PickleState *st = _Pickle_GetGlobalState(); |
| 1209 | PyErr_SetString(st->UnpicklingError, |
| 1210 | "read would overflow (invalid bytecode)"); |
| 1211 | return -1; |
| 1212 | } |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1213 | |
| 1214 | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
| 1215 | assert(self->next_read_idx + n > self->input_len); |
| 1216 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1217 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1218 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1219 | return -1; |
| 1220 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1221 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1222 | if (num_read < 0) |
| 1223 | return -1; |
| 1224 | if (num_read < n) { |
| 1225 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1226 | return -1; |
| 1227 | } |
| 1228 | *s = self->input_buffer; |
| 1229 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1230 | return n; |
| 1231 | } |
| 1232 | |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1233 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1234 | |
| 1235 | This should be used for all data reads, rather than accessing the unpickler's |
| 1236 | input buffer directly. This method deals correctly with reading from input |
| 1237 | streams, which the input buffer doesn't deal with. |
| 1238 | |
| 1239 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1240 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1241 | should use this function's return value to know how many bytes you can |
| 1242 | consume. |
| 1243 | |
| 1244 | Returns -1 (with an exception set) on failure. On success, return the |
| 1245 | number of chars read. */ |
| 1246 | #define _Unpickler_Read(self, s, n) \ |
Victor Stinner | da23056 | 2016-05-20 21:16:59 +0200 | [diff] [blame] | 1247 | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
Victor Stinner | 19ed27e | 2016-05-20 11:42:37 +0200 | [diff] [blame] | 1248 | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
| 1249 | (self)->next_read_idx += (n), \ |
| 1250 | (n)) \ |
| 1251 | : _Unpickler_ReadImpl(self, (s), (n))) |
| 1252 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1253 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1254 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1255 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1256 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1257 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1258 | if (input_line == NULL) { |
| 1259 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1260 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1261 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1262 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1263 | memcpy(input_line, line, len); |
| 1264 | input_line[len] = '\0'; |
| 1265 | self->input_line = input_line; |
| 1266 | *result = self->input_line; |
| 1267 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1268 | } |
| 1269 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1270 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1271 | before hitting \n, return the data we found. |
| 1272 | |
| 1273 | Returns the number of chars read, or -1 on failure. */ |
| 1274 | static Py_ssize_t |
| 1275 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1276 | { |
| 1277 | Py_ssize_t i, num_read; |
| 1278 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1279 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1280 | if (self->input_buffer[i] == '\n') { |
| 1281 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1282 | num_read = i - self->next_read_idx + 1; |
| 1283 | self->next_read_idx = i + 1; |
| 1284 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1285 | } |
| 1286 | } |
| 1287 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1288 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1289 | if (num_read < 0) |
| 1290 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1291 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1292 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1293 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1294 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1295 | /* If we get here, we've run off the end of the input string. Return the |
| 1296 | remaining string and let the caller figure it out. */ |
| 1297 | *result = self->input_buffer + self->next_read_idx; |
| 1298 | num_read = i - self->next_read_idx; |
| 1299 | self->next_read_idx = i; |
| 1300 | return num_read; |
| 1301 | } |
| 1302 | |
| 1303 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1304 | will be modified in place. */ |
| 1305 | static int |
| 1306 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1307 | { |
| 1308 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1309 | |
| 1310 | assert(new_size > self->memo_size); |
| 1311 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1312 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1313 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1314 | PyErr_NoMemory(); |
| 1315 | return -1; |
| 1316 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1317 | for (i = self->memo_size; i < new_size; i++) |
| 1318 | self->memo[i] = NULL; |
| 1319 | self->memo_size = new_size; |
| 1320 | return 0; |
| 1321 | } |
| 1322 | |
| 1323 | /* Returns NULL if idx is out of bounds. */ |
| 1324 | static PyObject * |
| 1325 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1326 | { |
| 1327 | if (idx < 0 || idx >= self->memo_size) |
| 1328 | return NULL; |
| 1329 | |
| 1330 | return self->memo[idx]; |
| 1331 | } |
| 1332 | |
| 1333 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1334 | This takes its own reference to `value`. */ |
| 1335 | static int |
| 1336 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1337 | { |
| 1338 | PyObject *old_item; |
| 1339 | |
| 1340 | if (idx >= self->memo_size) { |
| 1341 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1342 | return -1; |
| 1343 | assert(idx < self->memo_size); |
| 1344 | } |
| 1345 | Py_INCREF(value); |
| 1346 | old_item = self->memo[idx]; |
| 1347 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1348 | if (old_item != NULL) { |
| 1349 | Py_DECREF(old_item); |
| 1350 | } |
| 1351 | else { |
| 1352 | self->memo_len++; |
| 1353 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1354 | return 0; |
| 1355 | } |
| 1356 | |
| 1357 | static PyObject ** |
| 1358 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1359 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1360 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1361 | if (memo == NULL) { |
| 1362 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1363 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1364 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1365 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1366 | return memo; |
| 1367 | } |
| 1368 | |
| 1369 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1370 | static void |
| 1371 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1372 | { |
| 1373 | Py_ssize_t i; |
| 1374 | PyObject **memo = self->memo; |
| 1375 | |
| 1376 | if (self->memo == NULL) |
| 1377 | return; |
| 1378 | self->memo = NULL; |
| 1379 | i = self->memo_size; |
| 1380 | while (--i >= 0) { |
| 1381 | Py_XDECREF(memo[i]); |
| 1382 | } |
| 1383 | PyMem_FREE(memo); |
| 1384 | } |
| 1385 | |
| 1386 | static UnpicklerObject * |
| 1387 | _Unpickler_New(void) |
| 1388 | { |
| 1389 | UnpicklerObject *self; |
| 1390 | |
| 1391 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1392 | if (self == NULL) |
| 1393 | return NULL; |
| 1394 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1395 | self->pers_func = NULL; |
| 1396 | self->input_buffer = NULL; |
| 1397 | self->input_line = NULL; |
| 1398 | self->input_len = 0; |
| 1399 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1400 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1401 | self->read = NULL; |
| 1402 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1403 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1404 | self->encoding = NULL; |
| 1405 | self->errors = NULL; |
| 1406 | self->marks = NULL; |
| 1407 | self->num_marks = 0; |
| 1408 | self->marks_size = 0; |
| 1409 | self->proto = 0; |
| 1410 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1411 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1412 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1413 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1414 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1415 | self->stack = (Pdata *)Pdata_New(); |
| 1416 | |
| 1417 | if (self->memo == NULL || self->stack == NULL) { |
| 1418 | Py_DECREF(self); |
| 1419 | return NULL; |
| 1420 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1421 | |
| 1422 | return self; |
| 1423 | } |
| 1424 | |
| 1425 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1426 | be called once on a freshly created Pickler. */ |
| 1427 | static int |
| 1428 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1429 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1430 | _Py_IDENTIFIER(peek); |
| 1431 | _Py_IDENTIFIER(read); |
| 1432 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1433 | |
| 1434 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1435 | if (self->peek == NULL) { |
| 1436 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1437 | PyErr_Clear(); |
| 1438 | else |
| 1439 | return -1; |
| 1440 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1441 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1442 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1443 | if (self->readline == NULL || self->read == NULL) { |
| 1444 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1445 | PyErr_SetString(PyExc_TypeError, |
| 1446 | "file must have 'read' and 'readline' attributes"); |
| 1447 | Py_CLEAR(self->read); |
| 1448 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1449 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1450 | return -1; |
| 1451 | } |
| 1452 | return 0; |
| 1453 | } |
| 1454 | |
| 1455 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1456 | be called once on a freshly created Pickler. */ |
| 1457 | static int |
| 1458 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1459 | const char *encoding, |
| 1460 | const char *errors) |
| 1461 | { |
| 1462 | if (encoding == NULL) |
| 1463 | encoding = "ASCII"; |
| 1464 | if (errors == NULL) |
| 1465 | errors = "strict"; |
| 1466 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1467 | self->encoding = _PyMem_Strdup(encoding); |
| 1468 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1469 | if (self->encoding == NULL || self->errors == NULL) { |
| 1470 | PyErr_NoMemory(); |
| 1471 | return -1; |
| 1472 | } |
| 1473 | return 0; |
| 1474 | } |
| 1475 | |
| 1476 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1477 | static int |
| 1478 | memo_get(PicklerObject *self, PyObject *key) |
| 1479 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1480 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1481 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1482 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1483 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1484 | value = PyMemoTable_Get(self->memo, key); |
| 1485 | if (value == NULL) { |
| 1486 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1487 | return -1; |
| 1488 | } |
| 1489 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1490 | if (!self->bin) { |
| 1491 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1492 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1493 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1494 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1495 | } |
| 1496 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1497 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1498 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1499 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1500 | len = 2; |
| 1501 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1502 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1503 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1504 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1505 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1506 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1507 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1508 | len = 5; |
| 1509 | } |
| 1510 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1511 | PickleState *st = _Pickle_GetGlobalState(); |
| 1512 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1513 | "memo id too large for LONG_BINGET"); |
| 1514 | return -1; |
| 1515 | } |
| 1516 | } |
| 1517 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1518 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1519 | return -1; |
| 1520 | |
| 1521 | return 0; |
| 1522 | } |
| 1523 | |
| 1524 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1525 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1526 | static int |
| 1527 | memo_put(PicklerObject *self, PyObject *obj) |
| 1528 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1529 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1530 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1531 | Py_ssize_t idx; |
| 1532 | |
| 1533 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1534 | |
| 1535 | if (self->fast) |
| 1536 | return 0; |
| 1537 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1538 | idx = PyMemoTable_Size(self->memo); |
| 1539 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1540 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1541 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1542 | if (self->proto >= 4) { |
| 1543 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1544 | return -1; |
| 1545 | return 0; |
| 1546 | } |
| 1547 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1548 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1549 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1550 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1551 | len = strlen(pdata); |
| 1552 | } |
| 1553 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1554 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1555 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1556 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1557 | len = 2; |
| 1558 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1559 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1560 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1561 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1562 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1563 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1564 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1565 | len = 5; |
| 1566 | } |
| 1567 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1568 | PickleState *st = _Pickle_GetGlobalState(); |
| 1569 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1570 | "memo id too large for LONG_BINPUT"); |
| 1571 | return -1; |
| 1572 | } |
| 1573 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1574 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1575 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1576 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1577 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1581 | get_dotted_path(PyObject *obj, PyObject *name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1582 | _Py_static_string(PyId_dot, "."); |
| 1583 | _Py_static_string(PyId_locals, "<locals>"); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1584 | PyObject *dotted_path; |
| 1585 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1586 | |
| 1587 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1588 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1589 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1590 | n = PyList_GET_SIZE(dotted_path); |
| 1591 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1592 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1593 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1594 | PyObject *result = PyUnicode_RichCompare( |
| 1595 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1596 | int is_equal = (result == Py_True); |
| 1597 | assert(PyBool_Check(result)); |
| 1598 | Py_DECREF(result); |
| 1599 | if (is_equal) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1600 | if (obj == NULL) |
| 1601 | PyErr_Format(PyExc_AttributeError, |
| 1602 | "Can't pickle local object %R", name); |
| 1603 | else |
| 1604 | PyErr_Format(PyExc_AttributeError, |
| 1605 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1606 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1607 | return NULL; |
| 1608 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1609 | } |
| 1610 | return dotted_path; |
| 1611 | } |
| 1612 | |
| 1613 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1614 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1615 | { |
| 1616 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1617 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1618 | |
| 1619 | assert(PyList_CheckExact(names)); |
| 1620 | Py_INCREF(obj); |
| 1621 | n = PyList_GET_SIZE(names); |
| 1622 | for (i = 0; i < n; i++) { |
| 1623 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1624 | Py_XDECREF(parent); |
| 1625 | parent = obj; |
| 1626 | obj = PyObject_GetAttr(parent, name); |
| 1627 | if (obj == NULL) { |
| 1628 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1629 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1630 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1631 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1632 | if (pparent != NULL) |
| 1633 | *pparent = parent; |
| 1634 | else |
| 1635 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1636 | return obj; |
| 1637 | } |
| 1638 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1639 | static void |
| 1640 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1641 | { |
| 1642 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1643 | PyErr_Clear(); |
| 1644 | PyErr_Format(PyExc_AttributeError, |
| 1645 | "Can't get attribute %R on %R", name, obj); |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | |
| 1650 | static PyObject * |
| 1651 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1652 | { |
| 1653 | PyObject *dotted_path, *attr; |
| 1654 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1655 | if (allow_qualname) { |
| 1656 | dotted_path = get_dotted_path(obj, name); |
| 1657 | if (dotted_path == NULL) |
| 1658 | return NULL; |
| 1659 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1660 | Py_DECREF(dotted_path); |
| 1661 | } |
| 1662 | else |
| 1663 | attr = PyObject_GetAttr(obj, name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1664 | if (attr == NULL) |
| 1665 | reformat_attribute_error(obj, name); |
| 1666 | return attr; |
| 1667 | } |
| 1668 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1669 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1670 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1671 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1672 | PyObject *module_name; |
| 1673 | PyObject *modules_dict; |
| 1674 | PyObject *module; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1675 | Py_ssize_t i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1676 | _Py_IDENTIFIER(__module__); |
| 1677 | _Py_IDENTIFIER(modules); |
| 1678 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1680 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1681 | |
| 1682 | if (module_name == NULL) { |
| 1683 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1684 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1685 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1686 | } |
| 1687 | else { |
| 1688 | /* In some rare cases (e.g., bound methods of extension types), |
| 1689 | __module__ can be None. If it is so, then search sys.modules for |
| 1690 | the module of global. */ |
| 1691 | if (module_name != Py_None) |
| 1692 | return module_name; |
| 1693 | Py_CLEAR(module_name); |
| 1694 | } |
| 1695 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1696 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1697 | /* Fallback on walking sys.modules */ |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1698 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1699 | if (modules_dict == NULL) { |
| 1700 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1701 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1702 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1703 | |
| 1704 | i = 0; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1705 | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
| 1706 | PyObject *candidate; |
| 1707 | if (PyUnicode_Check(module_name) && |
| 1708 | !PyUnicode_CompareWithASCIIString(module_name, "__main__")) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1709 | continue; |
| 1710 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1711 | continue; |
| 1712 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1713 | candidate = get_deep_attribute(module, dotted_path, NULL); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1714 | if (candidate == NULL) { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1715 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1716 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1717 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1718 | continue; |
| 1719 | } |
| 1720 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1721 | if (candidate == global) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1722 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1723 | Py_DECREF(candidate); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1724 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1725 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1726 | Py_DECREF(candidate); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1727 | } |
| 1728 | |
| 1729 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1730 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1731 | Py_INCREF(module_name); |
| 1732 | return module_name; |
| 1733 | } |
| 1734 | |
| 1735 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1736 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1737 | memoization disabled). If the nesting of a list or dict object exceed |
| 1738 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1739 | reference to the seen list or dict objects and check whether these objects |
| 1740 | are recursive. These are not strictly necessary, since save() has a |
| 1741 | hard-coded recursion limit, but they give a nicer error message than the |
| 1742 | typical RuntimeError. */ |
| 1743 | static int |
| 1744 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1745 | { |
| 1746 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1747 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1748 | PyObject *key = NULL; |
| 1749 | if (self->fast_memo == NULL) { |
| 1750 | self->fast_memo = PyDict_New(); |
| 1751 | if (self->fast_memo == NULL) { |
| 1752 | self->fast_nesting = -1; |
| 1753 | return 0; |
| 1754 | } |
| 1755 | } |
| 1756 | key = PyLong_FromVoidPtr(obj); |
| 1757 | if (key == NULL) |
| 1758 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1759 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1760 | Py_DECREF(key); |
| 1761 | PyErr_Format(PyExc_ValueError, |
| 1762 | "fast mode: can't pickle cyclic objects " |
| 1763 | "including object type %.200s at %p", |
| 1764 | obj->ob_type->tp_name, obj); |
| 1765 | self->fast_nesting = -1; |
| 1766 | return 0; |
| 1767 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1768 | if (PyErr_Occurred()) { |
| 1769 | return 0; |
| 1770 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1771 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1772 | Py_DECREF(key); |
| 1773 | self->fast_nesting = -1; |
| 1774 | return 0; |
| 1775 | } |
| 1776 | Py_DECREF(key); |
| 1777 | } |
| 1778 | return 1; |
| 1779 | } |
| 1780 | |
| 1781 | static int |
| 1782 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1783 | { |
| 1784 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1785 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1786 | if (key == NULL) |
| 1787 | return 0; |
| 1788 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1789 | Py_DECREF(key); |
| 1790 | return 0; |
| 1791 | } |
| 1792 | Py_DECREF(key); |
| 1793 | } |
| 1794 | return 1; |
| 1795 | } |
| 1796 | |
| 1797 | static int |
| 1798 | save_none(PicklerObject *self, PyObject *obj) |
| 1799 | { |
| 1800 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1801 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1802 | return -1; |
| 1803 | |
| 1804 | return 0; |
| 1805 | } |
| 1806 | |
| 1807 | static int |
| 1808 | save_bool(PicklerObject *self, PyObject *obj) |
| 1809 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1810 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1811 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1812 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1813 | return -1; |
| 1814 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1815 | else { |
| 1816 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1817 | * so that unpicklers written before bools were introduced unpickle them |
| 1818 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1819 | * Note that protocol 2 added direct ways to pickle bools. |
| 1820 | */ |
| 1821 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1822 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1823 | return -1; |
| 1824 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1825 | return 0; |
| 1826 | } |
| 1827 | |
| 1828 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1829 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1830 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1831 | PyObject *repr = NULL; |
| 1832 | Py_ssize_t size; |
| 1833 | long val; |
| 1834 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1835 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1836 | const char long_op = LONG; |
| 1837 | |
| 1838 | val= PyLong_AsLong(obj); |
| 1839 | if (val == -1 && PyErr_Occurred()) { |
| 1840 | /* out of range for int pickling */ |
| 1841 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1842 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1843 | else if (self->bin && |
| 1844 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1845 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1846 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1847 | |
| 1848 | Note: we can't use -0x80000000L in the above condition because some |
| 1849 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1850 | before applying the unary minus when sizeof(long) <= 4. The |
| 1851 | resulting value stays unsigned which is commonly not what we want, |
| 1852 | so MSVC happily warns us about it. However, that result would have |
| 1853 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1854 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1855 | char pdata[32]; |
| 1856 | Py_ssize_t len = 0; |
| 1857 | |
| 1858 | pdata[1] = (unsigned char)(val & 0xff); |
| 1859 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1860 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1861 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1862 | |
| 1863 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1864 | if (pdata[2] == 0) { |
| 1865 | pdata[0] = BININT1; |
| 1866 | len = 2; |
| 1867 | } |
| 1868 | else { |
| 1869 | pdata[0] = BININT2; |
| 1870 | len = 3; |
| 1871 | } |
| 1872 | } |
| 1873 | else { |
| 1874 | pdata[0] = BININT; |
| 1875 | len = 5; |
| 1876 | } |
| 1877 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1878 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1879 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1880 | |
| 1881 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1882 | } |
| 1883 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1884 | if (self->proto >= 2) { |
| 1885 | /* Linear-time pickling. */ |
| 1886 | size_t nbits; |
| 1887 | size_t nbytes; |
| 1888 | unsigned char *pdata; |
| 1889 | char header[5]; |
| 1890 | int i; |
| 1891 | int sign = _PyLong_Sign(obj); |
| 1892 | |
| 1893 | if (sign == 0) { |
| 1894 | header[0] = LONG1; |
| 1895 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1896 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1897 | goto error; |
| 1898 | return 0; |
| 1899 | } |
| 1900 | nbits = _PyLong_NumBits(obj); |
| 1901 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1902 | goto error; |
| 1903 | /* How many bytes do we need? There are nbits >> 3 full |
| 1904 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1905 | * are any leftover bits, then we clearly need another |
| 1906 | * byte. Wnat's not so obvious is that we *probably* |
| 1907 | * need another byte even if there aren't any leftovers: |
| 1908 | * the most-significant bit of the most-significant byte |
| 1909 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1910 | * opposite of the one we need. The exception is ints |
| 1911 | * 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] | 1912 | * its own 256's-complement, so has the right sign bit |
| 1913 | * even without the extra byte. That's a pain to check |
| 1914 | * for in advance, though, so we always grab an extra |
| 1915 | * byte at the start, and cut it back later if possible. |
| 1916 | */ |
| 1917 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1918 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1919 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1920 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1921 | goto error; |
| 1922 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1923 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1924 | if (repr == NULL) |
| 1925 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1926 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1927 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1928 | pdata, nbytes, |
| 1929 | 1 /* little endian */ , 1 /* signed */ ); |
| 1930 | if (i < 0) |
| 1931 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1932 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1933 | * needed. This is so iff the MSB is all redundant sign |
| 1934 | * bits. |
| 1935 | */ |
| 1936 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1937 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1938 | pdata[nbytes - 1] == 0xff && |
| 1939 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1940 | nbytes--; |
| 1941 | } |
| 1942 | |
| 1943 | if (nbytes < 256) { |
| 1944 | header[0] = LONG1; |
| 1945 | header[1] = (unsigned char)nbytes; |
| 1946 | size = 2; |
| 1947 | } |
| 1948 | else { |
| 1949 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1950 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1951 | for (i = 1; i < 5; i++) { |
| 1952 | header[i] = (unsigned char)(size & 0xff); |
| 1953 | size >>= 8; |
| 1954 | } |
| 1955 | size = 5; |
| 1956 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1957 | if (_Pickler_Write(self, header, size) < 0 || |
| 1958 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1959 | goto error; |
| 1960 | } |
| 1961 | else { |
| 1962 | char *string; |
| 1963 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1964 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1965 | the number of digits), in both directions. We add a trailing 'L' |
| 1966 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1967 | |
| 1968 | repr = PyObject_Repr(obj); |
| 1969 | if (repr == NULL) |
| 1970 | goto error; |
| 1971 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1972 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1973 | if (string == NULL) |
| 1974 | goto error; |
| 1975 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1976 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1977 | _Pickler_Write(self, string, size) < 0 || |
| 1978 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1979 | goto error; |
| 1980 | } |
| 1981 | |
| 1982 | if (0) { |
| 1983 | error: |
| 1984 | status = -1; |
| 1985 | } |
| 1986 | Py_XDECREF(repr); |
| 1987 | |
| 1988 | return status; |
| 1989 | } |
| 1990 | |
| 1991 | static int |
| 1992 | save_float(PicklerObject *self, PyObject *obj) |
| 1993 | { |
| 1994 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1995 | |
| 1996 | if (self->bin) { |
| 1997 | char pdata[9]; |
| 1998 | pdata[0] = BINFLOAT; |
| 1999 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 2000 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2001 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2002 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2003 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2004 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2005 | int result = -1; |
| 2006 | char *buf = NULL; |
| 2007 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2008 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2009 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2010 | goto done; |
| 2011 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 2012 | 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] | 2013 | if (!buf) { |
| 2014 | PyErr_NoMemory(); |
| 2015 | goto done; |
| 2016 | } |
| 2017 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2018 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2019 | goto done; |
| 2020 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2021 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2022 | goto done; |
| 2023 | |
| 2024 | result = 0; |
| 2025 | done: |
| 2026 | PyMem_Free(buf); |
| 2027 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2028 | } |
| 2029 | |
| 2030 | return 0; |
| 2031 | } |
| 2032 | |
| 2033 | static int |
| 2034 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2035 | { |
| 2036 | if (self->proto < 3) { |
| 2037 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2038 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2039 | the __reduce__ method) to permit bytes object unpickling. |
| 2040 | |
| 2041 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2042 | 2 'bytes' is just an alias for 'str' (which has different |
| 2043 | parameters than the actual bytes object), we use codecs.encode |
| 2044 | to create the appropriate 'str' object when unpickled using |
| 2045 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2046 | using Python 3. Again this is a hack and we don't need to do this |
| 2047 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2048 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2049 | int status; |
| 2050 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2051 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2052 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2053 | } |
| 2054 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2055 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2056 | PyObject *unicode_str = |
| 2057 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2058 | PyBytes_GET_SIZE(obj), |
| 2059 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2060 | _Py_IDENTIFIER(latin1); |
| 2061 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2062 | if (unicode_str == NULL) |
| 2063 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2064 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2065 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2066 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2067 | Py_DECREF(unicode_str); |
| 2068 | } |
| 2069 | |
| 2070 | if (reduce_value == NULL) |
| 2071 | return -1; |
| 2072 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2073 | /* save_reduce() will memoize the object automatically. */ |
| 2074 | status = save_reduce(self, reduce_value, obj); |
| 2075 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2076 | return status; |
| 2077 | } |
| 2078 | else { |
| 2079 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2080 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2081 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2082 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2083 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2084 | if (size < 0) |
| 2085 | return -1; |
| 2086 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2087 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2088 | header[0] = SHORT_BINBYTES; |
| 2089 | header[1] = (unsigned char)size; |
| 2090 | len = 2; |
| 2091 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2092 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2093 | header[0] = BINBYTES; |
| 2094 | header[1] = (unsigned char)(size & 0xff); |
| 2095 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2096 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2097 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2098 | len = 5; |
| 2099 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2100 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2101 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2102 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2103 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2104 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2105 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2106 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2107 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2108 | return -1; /* string too large */ |
| 2109 | } |
| 2110 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2111 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2112 | return -1; |
| 2113 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2114 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2115 | return -1; |
| 2116 | |
| 2117 | if (memo_put(self, obj) < 0) |
| 2118 | return -1; |
| 2119 | |
| 2120 | return 0; |
| 2121 | } |
| 2122 | } |
| 2123 | |
| 2124 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2125 | backslash and newline characters to \uXXXX escapes. */ |
| 2126 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2127 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2128 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2129 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2130 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2131 | void *data; |
| 2132 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2133 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2134 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2135 | if (PyUnicode_READY(obj)) |
| 2136 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2137 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2138 | _PyBytesWriter_Init(&writer); |
| 2139 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2140 | size = PyUnicode_GET_LENGTH(obj); |
| 2141 | data = PyUnicode_DATA(obj); |
| 2142 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2143 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2144 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2145 | if (p == NULL) |
| 2146 | goto error; |
| 2147 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2148 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2149 | for (i=0; i < size; i++) { |
| 2150 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2151 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2152 | if (ch >= 0x10000) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2153 | /* -1: substract 1 preallocated byte */ |
| 2154 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2155 | if (p == NULL) |
| 2156 | goto error; |
| 2157 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2158 | *p++ = '\\'; |
| 2159 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2160 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2161 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2162 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2163 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2164 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2165 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2166 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2167 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2168 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2169 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2170 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2171 | /* -1: substract 1 preallocated byte */ |
| 2172 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2173 | if (p == NULL) |
| 2174 | goto error; |
| 2175 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2176 | *p++ = '\\'; |
| 2177 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2178 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2179 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2180 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2181 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2182 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2183 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2184 | else |
| 2185 | *p++ = (char) ch; |
| 2186 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2187 | |
| 2188 | return _PyBytesWriter_Finish(&writer, p); |
| 2189 | |
| 2190 | error: |
| 2191 | _PyBytesWriter_Dealloc(&writer); |
| 2192 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
| 2195 | static int |
Serhiy Storchaka | ef1585e | 2015-12-25 20:01:53 +0200 | [diff] [blame] | 2196 | write_utf8(PicklerObject *self, const char *data, Py_ssize_t size) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2197 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2198 | char header[9]; |
| 2199 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2200 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2201 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2202 | if (size <= 0xff && self->proto >= 4) { |
| 2203 | header[0] = SHORT_BINUNICODE; |
| 2204 | header[1] = (unsigned char)(size & 0xff); |
| 2205 | len = 2; |
| 2206 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2207 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2208 | header[0] = BINUNICODE; |
| 2209 | header[1] = (unsigned char)(size & 0xff); |
| 2210 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2211 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2212 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2213 | len = 5; |
| 2214 | } |
| 2215 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2216 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2217 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2218 | len = 9; |
| 2219 | } |
| 2220 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2221 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2222 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2223 | return -1; |
| 2224 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2225 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2226 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2227 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2228 | if (_Pickler_Write(self, data, size) < 0) |
| 2229 | return -1; |
| 2230 | |
| 2231 | return 0; |
| 2232 | } |
| 2233 | |
| 2234 | static int |
| 2235 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2236 | { |
| 2237 | PyObject *encoded = NULL; |
| 2238 | Py_ssize_t size; |
| 2239 | char *data; |
| 2240 | int r; |
| 2241 | |
| 2242 | if (PyUnicode_READY(obj)) |
| 2243 | return -1; |
| 2244 | |
| 2245 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2246 | if (data != NULL) |
| 2247 | return write_utf8(self, data, size); |
| 2248 | |
| 2249 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2250 | "surrogatepass" error handler. */ |
| 2251 | PyErr_Clear(); |
| 2252 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2253 | if (encoded == NULL) |
| 2254 | return -1; |
| 2255 | |
| 2256 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2257 | PyBytes_GET_SIZE(encoded)); |
| 2258 | Py_DECREF(encoded); |
| 2259 | return r; |
| 2260 | } |
| 2261 | |
| 2262 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2263 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2264 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2265 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2266 | if (write_unicode_binary(self, obj) < 0) |
| 2267 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2268 | } |
| 2269 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2270 | PyObject *encoded; |
| 2271 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2272 | const char unicode_op = UNICODE; |
| 2273 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2274 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2275 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2276 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2278 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2279 | Py_DECREF(encoded); |
| 2280 | return -1; |
| 2281 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2282 | |
| 2283 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2284 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2285 | Py_DECREF(encoded); |
| 2286 | return -1; |
| 2287 | } |
| 2288 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2289 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2290 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2291 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2292 | } |
| 2293 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2294 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2295 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2296 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2300 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2301 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2302 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2303 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2304 | |
| 2305 | assert(PyTuple_Size(t) == len); |
| 2306 | |
| 2307 | for (i = 0; i < len; i++) { |
| 2308 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2309 | |
| 2310 | if (element == NULL) |
| 2311 | return -1; |
| 2312 | if (save(self, element, 0) < 0) |
| 2313 | return -1; |
| 2314 | } |
| 2315 | |
| 2316 | return 0; |
| 2317 | } |
| 2318 | |
| 2319 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2320 | * used across protocols to minimize the space needed to pickle them. |
| 2321 | * Tuples are also the only builtin immutable type that can be recursive |
| 2322 | * (a tuple can be reached from itself), and that requires some subtle |
| 2323 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2324 | */ |
| 2325 | static int |
| 2326 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2327 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2328 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2329 | |
| 2330 | const char mark_op = MARK; |
| 2331 | const char tuple_op = TUPLE; |
| 2332 | const char pop_op = POP; |
| 2333 | const char pop_mark_op = POP_MARK; |
| 2334 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2335 | |
| 2336 | if ((len = PyTuple_Size(obj)) < 0) |
| 2337 | return -1; |
| 2338 | |
| 2339 | if (len == 0) { |
| 2340 | char pdata[2]; |
| 2341 | |
| 2342 | if (self->proto) { |
| 2343 | pdata[0] = EMPTY_TUPLE; |
| 2344 | len = 1; |
| 2345 | } |
| 2346 | else { |
| 2347 | pdata[0] = MARK; |
| 2348 | pdata[1] = TUPLE; |
| 2349 | len = 2; |
| 2350 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2351 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2352 | return -1; |
| 2353 | return 0; |
| 2354 | } |
| 2355 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2356 | /* 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] | 2357 | * saving the tuple elements, the tuple must be recursive, in |
| 2358 | * which case we'll pop everything we put on the stack, and fetch |
| 2359 | * its value from the memo. |
| 2360 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2361 | if (len <= 3 && self->proto >= 2) { |
| 2362 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2363 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2364 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2365 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2366 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2367 | /* pop the len elements */ |
| 2368 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2369 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2370 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2371 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2372 | if (memo_get(self, obj) < 0) |
| 2373 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2374 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2375 | return 0; |
| 2376 | } |
| 2377 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2378 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2379 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2380 | } |
| 2381 | goto memoize; |
| 2382 | } |
| 2383 | |
| 2384 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2385 | * Generate MARK e1 e2 ... TUPLE |
| 2386 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2387 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2388 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2389 | |
| 2390 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2391 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2392 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2393 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2394 | /* pop the stack stuff we pushed */ |
| 2395 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2396 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2397 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2398 | } |
| 2399 | else { |
| 2400 | /* Note that we pop one more than len, to remove |
| 2401 | * the MARK too. |
| 2402 | */ |
| 2403 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2404 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2405 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2406 | } |
| 2407 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2408 | if (memo_get(self, obj) < 0) |
| 2409 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2410 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2411 | return 0; |
| 2412 | } |
| 2413 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2414 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2415 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
| 2418 | memoize: |
| 2419 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2420 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2421 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2422 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2423 | } |
| 2424 | |
| 2425 | /* iter is an iterator giving items, and we batch up chunks of |
| 2426 | * MARK item item ... item APPENDS |
| 2427 | * opcode sequences. Calling code should have arranged to first create an |
| 2428 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2429 | * Returns 0 on success, <0 on error. |
| 2430 | */ |
| 2431 | static int |
| 2432 | batch_list(PicklerObject *self, PyObject *iter) |
| 2433 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2434 | PyObject *obj = NULL; |
| 2435 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2436 | int i, n; |
| 2437 | |
| 2438 | const char mark_op = MARK; |
| 2439 | const char append_op = APPEND; |
| 2440 | const char appends_op = APPENDS; |
| 2441 | |
| 2442 | assert(iter != NULL); |
| 2443 | |
| 2444 | /* XXX: I think this function could be made faster by avoiding the |
| 2445 | iterator interface and fetching objects directly from list using |
| 2446 | PyList_GET_ITEM. |
| 2447 | */ |
| 2448 | |
| 2449 | if (self->proto == 0) { |
| 2450 | /* APPENDS isn't available; do one at a time. */ |
| 2451 | for (;;) { |
| 2452 | obj = PyIter_Next(iter); |
| 2453 | if (obj == NULL) { |
| 2454 | if (PyErr_Occurred()) |
| 2455 | return -1; |
| 2456 | break; |
| 2457 | } |
| 2458 | i = save(self, obj, 0); |
| 2459 | Py_DECREF(obj); |
| 2460 | if (i < 0) |
| 2461 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2462 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2463 | return -1; |
| 2464 | } |
| 2465 | return 0; |
| 2466 | } |
| 2467 | |
| 2468 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2469 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2470 | /* Get first item */ |
| 2471 | firstitem = PyIter_Next(iter); |
| 2472 | if (firstitem == NULL) { |
| 2473 | if (PyErr_Occurred()) |
| 2474 | goto error; |
| 2475 | |
| 2476 | /* nothing more to add */ |
| 2477 | break; |
| 2478 | } |
| 2479 | |
| 2480 | /* Try to get a second item */ |
| 2481 | obj = PyIter_Next(iter); |
| 2482 | if (obj == NULL) { |
| 2483 | if (PyErr_Occurred()) |
| 2484 | goto error; |
| 2485 | |
| 2486 | /* Only one item to write */ |
| 2487 | if (save(self, firstitem, 0) < 0) |
| 2488 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2489 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2490 | goto error; |
| 2491 | Py_CLEAR(firstitem); |
| 2492 | break; |
| 2493 | } |
| 2494 | |
| 2495 | /* More than one item to write */ |
| 2496 | |
| 2497 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2498 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2499 | goto error; |
| 2500 | |
| 2501 | if (save(self, firstitem, 0) < 0) |
| 2502 | goto error; |
| 2503 | Py_CLEAR(firstitem); |
| 2504 | n = 1; |
| 2505 | |
| 2506 | /* Fetch and save up to BATCHSIZE items */ |
| 2507 | while (obj) { |
| 2508 | if (save(self, obj, 0) < 0) |
| 2509 | goto error; |
| 2510 | Py_CLEAR(obj); |
| 2511 | n += 1; |
| 2512 | |
| 2513 | if (n == BATCHSIZE) |
| 2514 | break; |
| 2515 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2516 | obj = PyIter_Next(iter); |
| 2517 | if (obj == NULL) { |
| 2518 | if (PyErr_Occurred()) |
| 2519 | goto error; |
| 2520 | break; |
| 2521 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2522 | } |
| 2523 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2524 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2525 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2526 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2527 | } while (n == BATCHSIZE); |
| 2528 | return 0; |
| 2529 | |
| 2530 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2531 | Py_XDECREF(firstitem); |
| 2532 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2533 | return -1; |
| 2534 | } |
| 2535 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2536 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2537 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2538 | * MARK item item ... item APPENDS |
| 2539 | * opcode sequences. Calling code should have arranged to first create an |
| 2540 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2541 | * Returns 0 on success, -1 on error. |
| 2542 | * |
| 2543 | * This version is considerably faster than batch_list(), if less general. |
| 2544 | * |
| 2545 | * Note that this only works for protocols > 0. |
| 2546 | */ |
| 2547 | static int |
| 2548 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2549 | { |
| 2550 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2551 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2552 | |
| 2553 | const char append_op = APPEND; |
| 2554 | const char appends_op = APPENDS; |
| 2555 | const char mark_op = MARK; |
| 2556 | |
| 2557 | assert(obj != NULL); |
| 2558 | assert(self->proto > 0); |
| 2559 | assert(PyList_CheckExact(obj)); |
| 2560 | |
| 2561 | if (PyList_GET_SIZE(obj) == 1) { |
| 2562 | item = PyList_GET_ITEM(obj, 0); |
| 2563 | if (save(self, item, 0) < 0) |
| 2564 | return -1; |
| 2565 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2566 | return -1; |
| 2567 | return 0; |
| 2568 | } |
| 2569 | |
| 2570 | /* Write in batches of BATCHSIZE. */ |
| 2571 | total = 0; |
| 2572 | do { |
| 2573 | this_batch = 0; |
| 2574 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2575 | return -1; |
| 2576 | while (total < PyList_GET_SIZE(obj)) { |
| 2577 | item = PyList_GET_ITEM(obj, total); |
| 2578 | if (save(self, item, 0) < 0) |
| 2579 | return -1; |
| 2580 | total++; |
| 2581 | if (++this_batch == BATCHSIZE) |
| 2582 | break; |
| 2583 | } |
| 2584 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2585 | return -1; |
| 2586 | |
| 2587 | } while (total < PyList_GET_SIZE(obj)); |
| 2588 | |
| 2589 | return 0; |
| 2590 | } |
| 2591 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2592 | static int |
| 2593 | save_list(PicklerObject *self, PyObject *obj) |
| 2594 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2595 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2596 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2597 | int status = 0; |
| 2598 | |
| 2599 | if (self->fast && !fast_save_enter(self, obj)) |
| 2600 | goto error; |
| 2601 | |
| 2602 | /* Create an empty list. */ |
| 2603 | if (self->bin) { |
| 2604 | header[0] = EMPTY_LIST; |
| 2605 | len = 1; |
| 2606 | } |
| 2607 | else { |
| 2608 | header[0] = MARK; |
| 2609 | header[1] = LIST; |
| 2610 | len = 2; |
| 2611 | } |
| 2612 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2613 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2614 | goto error; |
| 2615 | |
| 2616 | /* Get list length, and bow out early if empty. */ |
| 2617 | if ((len = PyList_Size(obj)) < 0) |
| 2618 | goto error; |
| 2619 | |
| 2620 | if (memo_put(self, obj) < 0) |
| 2621 | goto error; |
| 2622 | |
| 2623 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2624 | /* Materialize the list elements. */ |
| 2625 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2626 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2627 | goto error; |
| 2628 | status = batch_list_exact(self, obj); |
| 2629 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2630 | } else { |
| 2631 | PyObject *iter = PyObject_GetIter(obj); |
| 2632 | if (iter == NULL) |
| 2633 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2634 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2635 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2636 | Py_DECREF(iter); |
| 2637 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2638 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2639 | status = batch_list(self, iter); |
| 2640 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2641 | Py_DECREF(iter); |
| 2642 | } |
| 2643 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2644 | if (0) { |
| 2645 | error: |
| 2646 | status = -1; |
| 2647 | } |
| 2648 | |
| 2649 | if (self->fast && !fast_save_leave(self, obj)) |
| 2650 | status = -1; |
| 2651 | |
| 2652 | return status; |
| 2653 | } |
| 2654 | |
| 2655 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2656 | * MARK key value ... key value SETITEMS |
| 2657 | * opcode sequences. Calling code should have arranged to first create an |
| 2658 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2659 | * Returns 0 on success, <0 on error. |
| 2660 | * |
| 2661 | * This is very much like batch_list(). The difference between saving |
| 2662 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2663 | * the C level, though, that attempts to combine these routines were too |
| 2664 | * ugly to bear. |
| 2665 | */ |
| 2666 | static int |
| 2667 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2668 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2669 | PyObject *obj = NULL; |
| 2670 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2671 | int i, n; |
| 2672 | |
| 2673 | const char mark_op = MARK; |
| 2674 | const char setitem_op = SETITEM; |
| 2675 | const char setitems_op = SETITEMS; |
| 2676 | |
| 2677 | assert(iter != NULL); |
| 2678 | |
| 2679 | if (self->proto == 0) { |
| 2680 | /* SETITEMS isn't available; do one at a time. */ |
| 2681 | for (;;) { |
| 2682 | obj = PyIter_Next(iter); |
| 2683 | if (obj == NULL) { |
| 2684 | if (PyErr_Occurred()) |
| 2685 | return -1; |
| 2686 | break; |
| 2687 | } |
| 2688 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2689 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2690 | "iterator must return 2-tuples"); |
| 2691 | return -1; |
| 2692 | } |
| 2693 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2694 | if (i >= 0) |
| 2695 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2696 | Py_DECREF(obj); |
| 2697 | if (i < 0) |
| 2698 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2699 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2700 | return -1; |
| 2701 | } |
| 2702 | return 0; |
| 2703 | } |
| 2704 | |
| 2705 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2706 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2707 | /* Get first item */ |
| 2708 | firstitem = PyIter_Next(iter); |
| 2709 | if (firstitem == NULL) { |
| 2710 | if (PyErr_Occurred()) |
| 2711 | goto error; |
| 2712 | |
| 2713 | /* nothing more to add */ |
| 2714 | break; |
| 2715 | } |
| 2716 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2717 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2718 | "iterator must return 2-tuples"); |
| 2719 | goto error; |
| 2720 | } |
| 2721 | |
| 2722 | /* Try to get a second item */ |
| 2723 | obj = PyIter_Next(iter); |
| 2724 | if (obj == NULL) { |
| 2725 | if (PyErr_Occurred()) |
| 2726 | goto error; |
| 2727 | |
| 2728 | /* Only one item to write */ |
| 2729 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2730 | goto error; |
| 2731 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2732 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2733 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2734 | goto error; |
| 2735 | Py_CLEAR(firstitem); |
| 2736 | break; |
| 2737 | } |
| 2738 | |
| 2739 | /* More than one item to write */ |
| 2740 | |
| 2741 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2742 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2743 | goto error; |
| 2744 | |
| 2745 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2746 | goto error; |
| 2747 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2748 | goto error; |
| 2749 | Py_CLEAR(firstitem); |
| 2750 | n = 1; |
| 2751 | |
| 2752 | /* Fetch and save up to BATCHSIZE items */ |
| 2753 | while (obj) { |
| 2754 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2755 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2756 | "iterator must return 2-tuples"); |
| 2757 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2758 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2759 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2760 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2761 | goto error; |
| 2762 | Py_CLEAR(obj); |
| 2763 | n += 1; |
| 2764 | |
| 2765 | if (n == BATCHSIZE) |
| 2766 | break; |
| 2767 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2768 | obj = PyIter_Next(iter); |
| 2769 | if (obj == NULL) { |
| 2770 | if (PyErr_Occurred()) |
| 2771 | goto error; |
| 2772 | break; |
| 2773 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2776 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2777 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2778 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2779 | } while (n == BATCHSIZE); |
| 2780 | return 0; |
| 2781 | |
| 2782 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2783 | Py_XDECREF(firstitem); |
| 2784 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2785 | return -1; |
| 2786 | } |
| 2787 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2788 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2789 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2790 | * MARK key value ... key value SETITEMS |
| 2791 | * opcode sequences. Calling code should have arranged to first create an |
| 2792 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2793 | * Returns 0 on success, -1 on error. |
| 2794 | * |
| 2795 | * Note that this currently doesn't work for protocol 0. |
| 2796 | */ |
| 2797 | static int |
| 2798 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2799 | { |
| 2800 | PyObject *key = NULL, *value = NULL; |
| 2801 | int i; |
| 2802 | Py_ssize_t dict_size, ppos = 0; |
| 2803 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2804 | const char mark_op = MARK; |
| 2805 | const char setitem_op = SETITEM; |
| 2806 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2807 | |
| 2808 | assert(obj != NULL); |
| 2809 | assert(self->proto > 0); |
| 2810 | |
| 2811 | dict_size = PyDict_Size(obj); |
| 2812 | |
| 2813 | /* Special-case len(d) == 1 to save space. */ |
| 2814 | if (dict_size == 1) { |
| 2815 | PyDict_Next(obj, &ppos, &key, &value); |
| 2816 | if (save(self, key, 0) < 0) |
| 2817 | return -1; |
| 2818 | if (save(self, value, 0) < 0) |
| 2819 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2820 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2821 | return -1; |
| 2822 | return 0; |
| 2823 | } |
| 2824 | |
| 2825 | /* Write in batches of BATCHSIZE. */ |
| 2826 | do { |
| 2827 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2828 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2829 | return -1; |
| 2830 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2831 | if (save(self, key, 0) < 0) |
| 2832 | return -1; |
| 2833 | if (save(self, value, 0) < 0) |
| 2834 | return -1; |
| 2835 | if (++i == BATCHSIZE) |
| 2836 | break; |
| 2837 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2838 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2839 | return -1; |
| 2840 | if (PyDict_Size(obj) != dict_size) { |
| 2841 | PyErr_Format( |
| 2842 | PyExc_RuntimeError, |
| 2843 | "dictionary changed size during iteration"); |
| 2844 | return -1; |
| 2845 | } |
| 2846 | |
| 2847 | } while (i == BATCHSIZE); |
| 2848 | return 0; |
| 2849 | } |
| 2850 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2851 | static int |
| 2852 | save_dict(PicklerObject *self, PyObject *obj) |
| 2853 | { |
| 2854 | PyObject *items, *iter; |
| 2855 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2856 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2857 | int status = 0; |
| 2858 | |
| 2859 | if (self->fast && !fast_save_enter(self, obj)) |
| 2860 | goto error; |
| 2861 | |
| 2862 | /* Create an empty dict. */ |
| 2863 | if (self->bin) { |
| 2864 | header[0] = EMPTY_DICT; |
| 2865 | len = 1; |
| 2866 | } |
| 2867 | else { |
| 2868 | header[0] = MARK; |
| 2869 | header[1] = DICT; |
| 2870 | len = 2; |
| 2871 | } |
| 2872 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2873 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2874 | goto error; |
| 2875 | |
| 2876 | /* Get dict size, and bow out early if empty. */ |
| 2877 | if ((len = PyDict_Size(obj)) < 0) |
| 2878 | goto error; |
| 2879 | |
| 2880 | if (memo_put(self, obj) < 0) |
| 2881 | goto error; |
| 2882 | |
| 2883 | if (len != 0) { |
| 2884 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2885 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2886 | /* We can take certain shortcuts if we know this is a dict and |
| 2887 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2888 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2889 | goto error; |
| 2890 | status = batch_dict_exact(self, obj); |
| 2891 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2892 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2893 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2894 | |
| 2895 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2896 | if (items == NULL) |
| 2897 | goto error; |
| 2898 | iter = PyObject_GetIter(items); |
| 2899 | Py_DECREF(items); |
| 2900 | if (iter == NULL) |
| 2901 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2902 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2903 | Py_DECREF(iter); |
| 2904 | goto error; |
| 2905 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2906 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2907 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2908 | Py_DECREF(iter); |
| 2909 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2910 | } |
| 2911 | |
| 2912 | if (0) { |
| 2913 | error: |
| 2914 | status = -1; |
| 2915 | } |
| 2916 | |
| 2917 | if (self->fast && !fast_save_leave(self, obj)) |
| 2918 | status = -1; |
| 2919 | |
| 2920 | return status; |
| 2921 | } |
| 2922 | |
| 2923 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2924 | save_set(PicklerObject *self, PyObject *obj) |
| 2925 | { |
| 2926 | PyObject *item; |
| 2927 | int i; |
| 2928 | Py_ssize_t set_size, ppos = 0; |
| 2929 | Py_hash_t hash; |
| 2930 | |
| 2931 | const char empty_set_op = EMPTY_SET; |
| 2932 | const char mark_op = MARK; |
| 2933 | const char additems_op = ADDITEMS; |
| 2934 | |
| 2935 | if (self->proto < 4) { |
| 2936 | PyObject *items; |
| 2937 | PyObject *reduce_value; |
| 2938 | int status; |
| 2939 | |
| 2940 | items = PySequence_List(obj); |
| 2941 | if (items == NULL) { |
| 2942 | return -1; |
| 2943 | } |
| 2944 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2945 | Py_DECREF(items); |
| 2946 | if (reduce_value == NULL) { |
| 2947 | return -1; |
| 2948 | } |
| 2949 | /* save_reduce() will memoize the object automatically. */ |
| 2950 | status = save_reduce(self, reduce_value, obj); |
| 2951 | Py_DECREF(reduce_value); |
| 2952 | return status; |
| 2953 | } |
| 2954 | |
| 2955 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2956 | return -1; |
| 2957 | |
| 2958 | if (memo_put(self, obj) < 0) |
| 2959 | return -1; |
| 2960 | |
| 2961 | set_size = PySet_GET_SIZE(obj); |
| 2962 | if (set_size == 0) |
| 2963 | return 0; /* nothing to do */ |
| 2964 | |
| 2965 | /* Write in batches of BATCHSIZE. */ |
| 2966 | do { |
| 2967 | i = 0; |
| 2968 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2969 | return -1; |
| 2970 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2971 | if (save(self, item, 0) < 0) |
| 2972 | return -1; |
| 2973 | if (++i == BATCHSIZE) |
| 2974 | break; |
| 2975 | } |
| 2976 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2977 | return -1; |
| 2978 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2979 | PyErr_Format( |
| 2980 | PyExc_RuntimeError, |
| 2981 | "set changed size during iteration"); |
| 2982 | return -1; |
| 2983 | } |
| 2984 | } while (i == BATCHSIZE); |
| 2985 | |
| 2986 | return 0; |
| 2987 | } |
| 2988 | |
| 2989 | static int |
| 2990 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2991 | { |
| 2992 | PyObject *iter; |
| 2993 | |
| 2994 | const char mark_op = MARK; |
| 2995 | const char frozenset_op = FROZENSET; |
| 2996 | |
| 2997 | if (self->fast && !fast_save_enter(self, obj)) |
| 2998 | return -1; |
| 2999 | |
| 3000 | if (self->proto < 4) { |
| 3001 | PyObject *items; |
| 3002 | PyObject *reduce_value; |
| 3003 | int status; |
| 3004 | |
| 3005 | items = PySequence_List(obj); |
| 3006 | if (items == NULL) { |
| 3007 | return -1; |
| 3008 | } |
| 3009 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 3010 | items); |
| 3011 | Py_DECREF(items); |
| 3012 | if (reduce_value == NULL) { |
| 3013 | return -1; |
| 3014 | } |
| 3015 | /* save_reduce() will memoize the object automatically. */ |
| 3016 | status = save_reduce(self, reduce_value, obj); |
| 3017 | Py_DECREF(reduce_value); |
| 3018 | return status; |
| 3019 | } |
| 3020 | |
| 3021 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3022 | return -1; |
| 3023 | |
| 3024 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3025 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3026 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3027 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3028 | for (;;) { |
| 3029 | PyObject *item; |
| 3030 | |
| 3031 | item = PyIter_Next(iter); |
| 3032 | if (item == NULL) { |
| 3033 | if (PyErr_Occurred()) { |
| 3034 | Py_DECREF(iter); |
| 3035 | return -1; |
| 3036 | } |
| 3037 | break; |
| 3038 | } |
| 3039 | if (save(self, item, 0) < 0) { |
| 3040 | Py_DECREF(item); |
| 3041 | Py_DECREF(iter); |
| 3042 | return -1; |
| 3043 | } |
| 3044 | Py_DECREF(item); |
| 3045 | } |
| 3046 | Py_DECREF(iter); |
| 3047 | |
| 3048 | /* If the object is already in the memo, this means it is |
| 3049 | recursive. In this case, throw away everything we put on the |
| 3050 | stack, and fetch the object back from the memo. */ |
| 3051 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3052 | const char pop_mark_op = POP_MARK; |
| 3053 | |
| 3054 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3055 | return -1; |
| 3056 | if (memo_get(self, obj) < 0) |
| 3057 | return -1; |
| 3058 | return 0; |
| 3059 | } |
| 3060 | |
| 3061 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3062 | return -1; |
| 3063 | if (memo_put(self, obj) < 0) |
| 3064 | return -1; |
| 3065 | |
| 3066 | return 0; |
| 3067 | } |
| 3068 | |
| 3069 | static int |
| 3070 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3071 | { |
| 3072 | PyObject *key; |
| 3073 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3074 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3075 | |
| 3076 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3077 | if (key == NULL) |
| 3078 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3079 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3080 | Py_DECREF(key); |
| 3081 | if (item) { |
| 3082 | PyObject *fixed_module_name; |
| 3083 | PyObject *fixed_global_name; |
| 3084 | |
| 3085 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3086 | PyErr_Format(PyExc_RuntimeError, |
| 3087 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3088 | "should be 2-tuples, not %.200s", |
| 3089 | Py_TYPE(item)->tp_name); |
| 3090 | return -1; |
| 3091 | } |
| 3092 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3093 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3094 | if (!PyUnicode_Check(fixed_module_name) || |
| 3095 | !PyUnicode_Check(fixed_global_name)) { |
| 3096 | PyErr_Format(PyExc_RuntimeError, |
| 3097 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3098 | "should be pairs of str, not (%.200s, %.200s)", |
| 3099 | Py_TYPE(fixed_module_name)->tp_name, |
| 3100 | Py_TYPE(fixed_global_name)->tp_name); |
| 3101 | return -1; |
| 3102 | } |
| 3103 | |
| 3104 | Py_CLEAR(*module_name); |
| 3105 | Py_CLEAR(*global_name); |
| 3106 | Py_INCREF(fixed_module_name); |
| 3107 | Py_INCREF(fixed_global_name); |
| 3108 | *module_name = fixed_module_name; |
| 3109 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3110 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3111 | } |
| 3112 | else if (PyErr_Occurred()) { |
| 3113 | return -1; |
| 3114 | } |
| 3115 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3116 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3117 | if (item) { |
| 3118 | if (!PyUnicode_Check(item)) { |
| 3119 | PyErr_Format(PyExc_RuntimeError, |
| 3120 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3121 | "should be strings, not %.200s", |
| 3122 | Py_TYPE(item)->tp_name); |
| 3123 | return -1; |
| 3124 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3125 | Py_INCREF(item); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 3126 | Py_XSETREF(*module_name, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3127 | } |
| 3128 | else if (PyErr_Occurred()) { |
| 3129 | return -1; |
| 3130 | } |
| 3131 | |
| 3132 | return 0; |
| 3133 | } |
| 3134 | |
| 3135 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3136 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3137 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3138 | PyObject *global_name = NULL; |
| 3139 | PyObject *module_name = NULL; |
| 3140 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3141 | PyObject *parent = NULL; |
| 3142 | PyObject *dotted_path = NULL; |
| 3143 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3144 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3145 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3146 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3147 | _Py_IDENTIFIER(__name__); |
| 3148 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3149 | |
| 3150 | const char global_op = GLOBAL; |
| 3151 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3152 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3153 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3154 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3155 | } |
| 3156 | else { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3157 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3158 | if (global_name == NULL) { |
| 3159 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3160 | goto error; |
| 3161 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3162 | } |
| 3163 | if (global_name == NULL) { |
| 3164 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3165 | if (global_name == NULL) |
| 3166 | goto error; |
| 3167 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3168 | } |
| 3169 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3170 | dotted_path = get_dotted_path(module, global_name); |
| 3171 | if (dotted_path == NULL) |
| 3172 | goto error; |
| 3173 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3174 | if (module_name == NULL) |
| 3175 | goto error; |
| 3176 | |
| 3177 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3178 | relative imports. |
| 3179 | |
| 3180 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3181 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3182 | custom import functions (IMHO, this would be a nice security |
| 3183 | feature). The import C API would need to be extended to support the |
| 3184 | extra parameters of __import__ to fix that. */ |
| 3185 | module = PyImport_Import(module_name); |
| 3186 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3187 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3188 | "Can't pickle %R: import of module %R failed", |
| 3189 | obj, module_name); |
| 3190 | goto error; |
| 3191 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3192 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3193 | Py_INCREF(lastname); |
| 3194 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3195 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3196 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3197 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3198 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3199 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3200 | goto error; |
| 3201 | } |
| 3202 | if (cls != obj) { |
| 3203 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3204 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3205 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3206 | obj, module_name, global_name); |
| 3207 | goto error; |
| 3208 | } |
| 3209 | Py_DECREF(cls); |
| 3210 | |
| 3211 | if (self->proto >= 2) { |
| 3212 | /* See whether this is in the extension registry, and if |
| 3213 | * so generate an EXT opcode. |
| 3214 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3215 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3216 | PyObject *code_obj; /* extension code as Python object */ |
| 3217 | long code; /* extension code as C value */ |
| 3218 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3219 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3220 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3221 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3222 | if (extension_key == NULL) { |
| 3223 | goto error; |
| 3224 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3225 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3226 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3227 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3228 | /* The object is not registered in the extension registry. |
| 3229 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3230 | if (code_obj == NULL) { |
| 3231 | if (PyErr_Occurred()) { |
| 3232 | goto error; |
| 3233 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3234 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3235 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3236 | |
| 3237 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3238 | of the value returned by the extension_registry. It should for |
| 3239 | consistency. */ |
| 3240 | |
| 3241 | /* Verify code_obj has the right type and value. */ |
| 3242 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3243 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3244 | "Can't pickle %R: extension code %R isn't an integer", |
| 3245 | obj, code_obj); |
| 3246 | goto error; |
| 3247 | } |
| 3248 | code = PyLong_AS_LONG(code_obj); |
| 3249 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3250 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3251 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3252 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3253 | goto error; |
| 3254 | } |
| 3255 | |
| 3256 | /* Generate an EXT opcode. */ |
| 3257 | if (code <= 0xff) { |
| 3258 | pdata[0] = EXT1; |
| 3259 | pdata[1] = (unsigned char)code; |
| 3260 | n = 2; |
| 3261 | } |
| 3262 | else if (code <= 0xffff) { |
| 3263 | pdata[0] = EXT2; |
| 3264 | pdata[1] = (unsigned char)(code & 0xff); |
| 3265 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3266 | n = 3; |
| 3267 | } |
| 3268 | else { |
| 3269 | pdata[0] = EXT4; |
| 3270 | pdata[1] = (unsigned char)(code & 0xff); |
| 3271 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3272 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3273 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3274 | n = 5; |
| 3275 | } |
| 3276 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3277 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3278 | goto error; |
| 3279 | } |
| 3280 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3281 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3282 | if (parent == module) { |
| 3283 | Py_INCREF(lastname); |
| 3284 | Py_DECREF(global_name); |
| 3285 | global_name = lastname; |
| 3286 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3287 | if (self->proto >= 4) { |
| 3288 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3289 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3290 | if (save(self, module_name, 0) < 0) |
| 3291 | goto error; |
| 3292 | if (save(self, global_name, 0) < 0) |
| 3293 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3294 | |
| 3295 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3296 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3297 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3298 | else if (parent != module) { |
| 3299 | PickleState *st = _Pickle_GetGlobalState(); |
| 3300 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3301 | st->getattr, parent, lastname); |
| 3302 | status = save_reduce(self, reduce_value, NULL); |
| 3303 | Py_DECREF(reduce_value); |
| 3304 | if (status < 0) |
| 3305 | goto error; |
| 3306 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3307 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3308 | /* Generate a normal global opcode if we are using a pickle |
| 3309 | protocol < 4, or if the object is not registered in the |
| 3310 | extension registry. */ |
| 3311 | PyObject *encoded; |
| 3312 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3313 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3314 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3315 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3316 | |
| 3317 | /* For protocol < 3 and if the user didn't request against doing |
| 3318 | so, we convert module names to the old 2.x module names. */ |
| 3319 | if (self->proto < 3 && self->fix_imports) { |
| 3320 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3321 | goto error; |
| 3322 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3323 | } |
| 3324 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3325 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3326 | both the module name and the global name using UTF-8. We do so |
| 3327 | only when we are using the pickle protocol newer than version |
| 3328 | 3. This is to ensure compatibility with older Unpickler running |
| 3329 | on Python 2.x. */ |
| 3330 | if (self->proto == 3) { |
| 3331 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3332 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3333 | else { |
| 3334 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3335 | } |
| 3336 | encoded = unicode_encoder(module_name); |
| 3337 | if (encoded == NULL) { |
| 3338 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3339 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3340 | "can't pickle module identifier '%S' using " |
| 3341 | "pickle protocol %i", |
| 3342 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3343 | goto error; |
| 3344 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3345 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3346 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3347 | Py_DECREF(encoded); |
| 3348 | goto error; |
| 3349 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3350 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3351 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3352 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3353 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3354 | /* Save the name of the module. */ |
| 3355 | encoded = unicode_encoder(global_name); |
| 3356 | if (encoded == NULL) { |
| 3357 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3358 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3359 | "can't pickle global identifier '%S' using " |
| 3360 | "pickle protocol %i", |
| 3361 | global_name, self->proto); |
| 3362 | goto error; |
| 3363 | } |
| 3364 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3365 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3366 | Py_DECREF(encoded); |
| 3367 | goto error; |
| 3368 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3369 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3370 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3371 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3372 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3373 | /* Memoize the object. */ |
| 3374 | if (memo_put(self, obj) < 0) |
| 3375 | goto error; |
| 3376 | } |
| 3377 | |
| 3378 | if (0) { |
| 3379 | error: |
| 3380 | status = -1; |
| 3381 | } |
| 3382 | Py_XDECREF(module_name); |
| 3383 | Py_XDECREF(global_name); |
| 3384 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3385 | Py_XDECREF(parent); |
| 3386 | Py_XDECREF(dotted_path); |
| 3387 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3388 | |
| 3389 | return status; |
| 3390 | } |
| 3391 | |
| 3392 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3393 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3394 | { |
| 3395 | PyObject *reduce_value; |
| 3396 | int status; |
| 3397 | |
| 3398 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3399 | if (reduce_value == NULL) { |
| 3400 | return -1; |
| 3401 | } |
| 3402 | status = save_reduce(self, reduce_value, obj); |
| 3403 | Py_DECREF(reduce_value); |
| 3404 | return status; |
| 3405 | } |
| 3406 | |
| 3407 | static int |
| 3408 | save_type(PicklerObject *self, PyObject *obj) |
| 3409 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3410 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3411 | return save_singleton_type(self, obj, Py_None); |
| 3412 | } |
| 3413 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3414 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3415 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3416 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3417 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3418 | } |
| 3419 | return save_global(self, obj, NULL); |
| 3420 | } |
| 3421 | |
| 3422 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3423 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3424 | { |
| 3425 | PyObject *pid = NULL; |
| 3426 | int status = 0; |
| 3427 | |
| 3428 | const char persid_op = PERSID; |
| 3429 | const char binpersid_op = BINPERSID; |
| 3430 | |
| 3431 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3432 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3433 | if (pid == NULL) |
| 3434 | return -1; |
| 3435 | |
| 3436 | if (pid != Py_None) { |
| 3437 | if (self->bin) { |
| 3438 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3439 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3440 | goto error; |
| 3441 | } |
| 3442 | else { |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3443 | PyObject *pid_str; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3444 | |
| 3445 | pid_str = PyObject_Str(pid); |
| 3446 | if (pid_str == NULL) |
| 3447 | goto error; |
| 3448 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3449 | /* XXX: Should it check whether the pid contains embedded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3450 | newlines? */ |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3451 | if (!PyUnicode_IS_ASCII(pid_str)) { |
| 3452 | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
| 3453 | "persistent IDs in protocol 0 must be " |
| 3454 | "ASCII strings"); |
| 3455 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3456 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3457 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3458 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3459 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3460 | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
| 3461 | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
| 3462 | _Pickler_Write(self, "\n", 1) < 0) { |
| 3463 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3464 | goto error; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 3465 | } |
| 3466 | Py_DECREF(pid_str); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3467 | } |
| 3468 | status = 1; |
| 3469 | } |
| 3470 | |
| 3471 | if (0) { |
| 3472 | error: |
| 3473 | status = -1; |
| 3474 | } |
| 3475 | Py_XDECREF(pid); |
| 3476 | |
| 3477 | return status; |
| 3478 | } |
| 3479 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3480 | static PyObject * |
| 3481 | get_class(PyObject *obj) |
| 3482 | { |
| 3483 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3484 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3485 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3486 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3487 | if (cls == NULL) { |
| 3488 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3489 | PyErr_Clear(); |
| 3490 | cls = (PyObject *) Py_TYPE(obj); |
| 3491 | Py_INCREF(cls); |
| 3492 | } |
| 3493 | } |
| 3494 | return cls; |
| 3495 | } |
| 3496 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3497 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3498 | * appropriate __reduce__ method for obj. |
| 3499 | */ |
| 3500 | static int |
| 3501 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3502 | { |
| 3503 | PyObject *callable; |
| 3504 | PyObject *argtup; |
| 3505 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3506 | PyObject *listitems = Py_None; |
| 3507 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3508 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3509 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3510 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3511 | |
| 3512 | const char reduce_op = REDUCE; |
| 3513 | const char build_op = BUILD; |
| 3514 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3515 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3516 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3517 | size = PyTuple_Size(args); |
| 3518 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3519 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3520 | "__reduce__ must contain 2 through 5 elements"); |
| 3521 | return -1; |
| 3522 | } |
| 3523 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3524 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3525 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3526 | return -1; |
| 3527 | |
| 3528 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3529 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3530 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3531 | return -1; |
| 3532 | } |
| 3533 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3534 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3535 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3536 | return -1; |
| 3537 | } |
| 3538 | |
| 3539 | if (state == Py_None) |
| 3540 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3541 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3542 | if (listitems == Py_None) |
| 3543 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3544 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3545 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3546 | "returned by __reduce__ must be an iterator, not %s", |
| 3547 | Py_TYPE(listitems)->tp_name); |
| 3548 | return -1; |
| 3549 | } |
| 3550 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3551 | if (dictitems == Py_None) |
| 3552 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3553 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3554 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3555 | "returned by __reduce__ must be an iterator, not %s", |
| 3556 | Py_TYPE(dictitems)->tp_name); |
| 3557 | return -1; |
| 3558 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3559 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3560 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3561 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3562 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3563 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3564 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3565 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3566 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3567 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3568 | } |
| 3569 | PyErr_Clear(); |
| 3570 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3571 | else if (PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3572 | _Py_IDENTIFIER(__newobj_ex__); |
| 3573 | use_newobj_ex = PyUnicode_Compare( |
| 3574 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3575 | if (!use_newobj_ex) { |
| 3576 | _Py_IDENTIFIER(__newobj__); |
| 3577 | use_newobj = PyUnicode_Compare( |
| 3578 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
| 3579 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3580 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3581 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3582 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3583 | |
| 3584 | if (use_newobj_ex) { |
| 3585 | PyObject *cls; |
| 3586 | PyObject *args; |
| 3587 | PyObject *kwargs; |
| 3588 | |
| 3589 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3590 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3591 | "length of the NEWOBJ_EX argument tuple must be " |
| 3592 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3593 | return -1; |
| 3594 | } |
| 3595 | |
| 3596 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3597 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3598 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3599 | "first item from NEWOBJ_EX argument tuple must " |
| 3600 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3601 | return -1; |
| 3602 | } |
| 3603 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3604 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3605 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3606 | "second item from NEWOBJ_EX argument tuple must " |
| 3607 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3608 | return -1; |
| 3609 | } |
| 3610 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3611 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3612 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3613 | "third item from NEWOBJ_EX argument tuple must " |
| 3614 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3615 | return -1; |
| 3616 | } |
| 3617 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3618 | if (self->proto >= 4) { |
| 3619 | if (save(self, cls, 0) < 0 || |
| 3620 | save(self, args, 0) < 0 || |
| 3621 | save(self, kwargs, 0) < 0 || |
| 3622 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3623 | return -1; |
| 3624 | } |
| 3625 | } |
| 3626 | else { |
| 3627 | PyObject *newargs; |
| 3628 | PyObject *cls_new; |
| 3629 | Py_ssize_t i; |
| 3630 | _Py_IDENTIFIER(__new__); |
| 3631 | |
| 3632 | newargs = PyTuple_New(Py_SIZE(args) + 2); |
| 3633 | if (newargs == NULL) |
| 3634 | return -1; |
| 3635 | |
| 3636 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 3637 | if (cls_new == NULL) { |
| 3638 | Py_DECREF(newargs); |
| 3639 | return -1; |
| 3640 | } |
| 3641 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 3642 | Py_INCREF(cls); |
| 3643 | PyTuple_SET_ITEM(newargs, 1, cls); |
| 3644 | for (i = 0; i < Py_SIZE(args); i++) { |
| 3645 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 3646 | Py_INCREF(item); |
| 3647 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 3648 | } |
| 3649 | |
| 3650 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 3651 | Py_DECREF(newargs); |
| 3652 | if (callable == NULL) |
| 3653 | return -1; |
| 3654 | |
| 3655 | newargs = PyTuple_New(0); |
| 3656 | if (newargs == NULL) { |
| 3657 | Py_DECREF(callable); |
| 3658 | return -1; |
| 3659 | } |
| 3660 | |
| 3661 | if (save(self, callable, 0) < 0 || |
| 3662 | save(self, newargs, 0) < 0 || |
| 3663 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 3664 | Py_DECREF(newargs); |
| 3665 | Py_DECREF(callable); |
| 3666 | return -1; |
| 3667 | } |
| 3668 | Py_DECREF(newargs); |
| 3669 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3670 | } |
| 3671 | } |
| 3672 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3673 | PyObject *cls; |
| 3674 | PyObject *newargtup; |
| 3675 | PyObject *obj_class; |
| 3676 | int p; |
| 3677 | |
| 3678 | /* Sanity checks. */ |
| 3679 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3680 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3681 | return -1; |
| 3682 | } |
| 3683 | |
| 3684 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3685 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3686 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3687 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3688 | return -1; |
| 3689 | } |
| 3690 | |
| 3691 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3692 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3693 | p = obj_class != cls; /* true iff a problem */ |
| 3694 | Py_DECREF(obj_class); |
| 3695 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3696 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3697 | "__newobj__ args has the wrong class"); |
| 3698 | return -1; |
| 3699 | } |
| 3700 | } |
| 3701 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3702 | what happen if the value returned by the __reduce__() method of |
| 3703 | some extension type contains another object of the same type. Ouch! |
| 3704 | |
| 3705 | Here is a quick example, that I ran into, to illustrate what I |
| 3706 | mean: |
| 3707 | |
| 3708 | >>> import pickle, copyreg |
| 3709 | >>> copyreg.dispatch_table.pop(complex) |
| 3710 | >>> pickle.dumps(1+2j) |
| 3711 | Traceback (most recent call last): |
| 3712 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3713 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3714 | |
| 3715 | Removing the complex class from copyreg.dispatch_table made the |
| 3716 | __reduce_ex__() method emit another complex object: |
| 3717 | |
| 3718 | >>> (1+1j).__reduce_ex__(2) |
| 3719 | (<function __newobj__ at 0xb7b71c3c>, |
| 3720 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3721 | |
| 3722 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3723 | ensued. Of course, the bug was in the complex class which had a |
| 3724 | broken __getnewargs__() that emitted another complex object. But, |
| 3725 | the point, here, is it is quite easy to end up with a broken reduce |
| 3726 | function. */ |
| 3727 | |
| 3728 | /* Save the class and its __new__ arguments. */ |
| 3729 | if (save(self, cls, 0) < 0) |
| 3730 | return -1; |
| 3731 | |
| 3732 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3733 | if (newargtup == NULL) |
| 3734 | return -1; |
| 3735 | |
| 3736 | p = save(self, newargtup, 0); |
| 3737 | Py_DECREF(newargtup); |
| 3738 | if (p < 0) |
| 3739 | return -1; |
| 3740 | |
| 3741 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3742 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3743 | return -1; |
| 3744 | } |
| 3745 | else { /* Not using NEWOBJ. */ |
| 3746 | if (save(self, callable, 0) < 0 || |
| 3747 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3748 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3749 | return -1; |
| 3750 | } |
| 3751 | |
| 3752 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3753 | the caller do not want to memoize the object. Not particularly useful, |
| 3754 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3755 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3756 | if (obj != NULL) { |
| 3757 | /* If the object is already in the memo, this means it is |
| 3758 | recursive. In this case, throw away everything we put on the |
| 3759 | stack, and fetch the object back from the memo. */ |
| 3760 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3761 | const char pop_op = POP; |
| 3762 | |
| 3763 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3764 | return -1; |
| 3765 | if (memo_get(self, obj) < 0) |
| 3766 | return -1; |
| 3767 | |
| 3768 | return 0; |
| 3769 | } |
| 3770 | else if (memo_put(self, obj) < 0) |
| 3771 | return -1; |
| 3772 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3773 | |
| 3774 | if (listitems && batch_list(self, listitems) < 0) |
| 3775 | return -1; |
| 3776 | |
| 3777 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3778 | return -1; |
| 3779 | |
| 3780 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3781 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3782 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3783 | return -1; |
| 3784 | } |
| 3785 | |
| 3786 | return 0; |
| 3787 | } |
| 3788 | |
| 3789 | static int |
| 3790 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3791 | { |
| 3792 | PyTypeObject *type; |
| 3793 | PyObject *reduce_func = NULL; |
| 3794 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3795 | int status = 0; |
| 3796 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3797 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3798 | return -1; |
| 3799 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3800 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3801 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3802 | |
| 3803 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3804 | on its returned object. */ |
| 3805 | if (!pers_save && self->pers_func) { |
| 3806 | /* save_pers() returns: |
| 3807 | -1 to signal an error; |
| 3808 | 0 if it did nothing successfully; |
| 3809 | 1 if a persistent id was saved. |
| 3810 | */ |
| 3811 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3812 | goto done; |
| 3813 | } |
| 3814 | |
| 3815 | type = Py_TYPE(obj); |
| 3816 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3817 | /* The old cPickle had an optimization that used switch-case statement |
| 3818 | dispatching on the first letter of the type name. This has was removed |
| 3819 | since benchmarks shown that this optimization was actually slowing |
| 3820 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3821 | |
| 3822 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3823 | |
| 3824 | if (obj == Py_None) { |
| 3825 | status = save_none(self, obj); |
| 3826 | goto done; |
| 3827 | } |
| 3828 | else if (obj == Py_False || obj == Py_True) { |
| 3829 | status = save_bool(self, obj); |
| 3830 | goto done; |
| 3831 | } |
| 3832 | else if (type == &PyLong_Type) { |
| 3833 | status = save_long(self, obj); |
| 3834 | goto done; |
| 3835 | } |
| 3836 | else if (type == &PyFloat_Type) { |
| 3837 | status = save_float(self, obj); |
| 3838 | goto done; |
| 3839 | } |
| 3840 | |
| 3841 | /* Check the memo to see if it has the object. If so, generate |
| 3842 | a GET (or BINGET) opcode, instead of pickling the object |
| 3843 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3844 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3845 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3846 | goto error; |
| 3847 | goto done; |
| 3848 | } |
| 3849 | |
| 3850 | if (type == &PyBytes_Type) { |
| 3851 | status = save_bytes(self, obj); |
| 3852 | goto done; |
| 3853 | } |
| 3854 | else if (type == &PyUnicode_Type) { |
| 3855 | status = save_unicode(self, obj); |
| 3856 | goto done; |
| 3857 | } |
| 3858 | else if (type == &PyDict_Type) { |
| 3859 | status = save_dict(self, obj); |
| 3860 | goto done; |
| 3861 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3862 | else if (type == &PySet_Type) { |
| 3863 | status = save_set(self, obj); |
| 3864 | goto done; |
| 3865 | } |
| 3866 | else if (type == &PyFrozenSet_Type) { |
| 3867 | status = save_frozenset(self, obj); |
| 3868 | goto done; |
| 3869 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3870 | else if (type == &PyList_Type) { |
| 3871 | status = save_list(self, obj); |
| 3872 | goto done; |
| 3873 | } |
| 3874 | else if (type == &PyTuple_Type) { |
| 3875 | status = save_tuple(self, obj); |
| 3876 | goto done; |
| 3877 | } |
| 3878 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3879 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3880 | goto done; |
| 3881 | } |
| 3882 | else if (type == &PyFunction_Type) { |
| 3883 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3884 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3885 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3886 | |
| 3887 | /* XXX: This part needs some unit tests. */ |
| 3888 | |
| 3889 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3890 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3891 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3892 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3893 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3894 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3895 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3896 | (PyObject *)type); |
| 3897 | if (reduce_func == NULL) { |
| 3898 | if (PyErr_Occurred()) { |
| 3899 | goto error; |
| 3900 | } |
| 3901 | } else { |
| 3902 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3903 | Increase the reference count to be consistent with |
| 3904 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3905 | Py_INCREF(reduce_func); |
| 3906 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3907 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3908 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3909 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3910 | if (reduce_func == NULL) { |
| 3911 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3912 | PyErr_Clear(); |
| 3913 | else |
| 3914 | goto error; |
| 3915 | } |
| 3916 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3917 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3918 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3919 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3920 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3921 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3922 | status = save_global(self, obj, NULL); |
| 3923 | goto done; |
| 3924 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3925 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3926 | _Py_IDENTIFIER(__reduce__); |
| 3927 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3928 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3929 | |
| 3930 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3931 | automatically defined as __reduce__. While this is convenient, this |
| 3932 | make it impossible to know which method was actually called. Of |
| 3933 | course, this is not a big deal. But still, it would be nice to let |
| 3934 | the user know which method was called when something go |
| 3935 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3936 | don't actually have to check for a __reduce__ method. */ |
| 3937 | |
| 3938 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3939 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3940 | if (reduce_func != NULL) { |
| 3941 | PyObject *proto; |
| 3942 | proto = PyLong_FromLong(self->proto); |
| 3943 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3944 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3945 | } |
| 3946 | } |
| 3947 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3948 | PickleState *st = _Pickle_GetGlobalState(); |
| 3949 | |
| 3950 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3951 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3952 | } |
| 3953 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3954 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3955 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3956 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3957 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3958 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3959 | PyObject *empty_tuple = PyTuple_New(0); |
| 3960 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3961 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3962 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3963 | } |
| 3964 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3965 | PyErr_Format(st->PicklingError, |
| 3966 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3967 | type->tp_name, obj); |
| 3968 | goto error; |
| 3969 | } |
| 3970 | } |
| 3971 | } |
| 3972 | |
| 3973 | if (reduce_value == NULL) |
| 3974 | goto error; |
| 3975 | |
| 3976 | if (PyUnicode_Check(reduce_value)) { |
| 3977 | status = save_global(self, obj, reduce_value); |
| 3978 | goto done; |
| 3979 | } |
| 3980 | |
| 3981 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3982 | PickleState *st = _Pickle_GetGlobalState(); |
| 3983 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3984 | "__reduce__ must return a string or tuple"); |
| 3985 | goto error; |
| 3986 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3987 | |
| 3988 | status = save_reduce(self, reduce_value, obj); |
| 3989 | |
| 3990 | if (0) { |
| 3991 | error: |
| 3992 | status = -1; |
| 3993 | } |
| 3994 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3995 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3996 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3997 | Py_XDECREF(reduce_func); |
| 3998 | Py_XDECREF(reduce_value); |
| 3999 | |
| 4000 | return status; |
| 4001 | } |
| 4002 | |
| 4003 | static int |
| 4004 | dump(PicklerObject *self, PyObject *obj) |
| 4005 | { |
| 4006 | const char stop_op = STOP; |
| 4007 | |
| 4008 | if (self->proto >= 2) { |
| 4009 | char header[2]; |
| 4010 | |
| 4011 | header[0] = PROTO; |
| 4012 | assert(self->proto >= 0 && self->proto < 256); |
| 4013 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4014 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4015 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4016 | if (self->proto >= 4) |
| 4017 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4018 | } |
| 4019 | |
| 4020 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4021 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4022 | return -1; |
| 4023 | |
| 4024 | return 0; |
| 4025 | } |
| 4026 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4027 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4028 | |
| 4029 | _pickle.Pickler.clear_memo |
| 4030 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4031 | Clears the pickler's "memo". |
| 4032 | |
| 4033 | The memo is the data structure that remembers which objects the |
| 4034 | pickler has already seen, so that shared or recursive objects are |
| 4035 | pickled by reference and not by value. This method is useful when |
| 4036 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4037 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4038 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4039 | static PyObject * |
| 4040 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4041 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4042 | { |
| 4043 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4044 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4045 | |
| 4046 | Py_RETURN_NONE; |
| 4047 | } |
| 4048 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4049 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4050 | |
| 4051 | _pickle.Pickler.dump |
| 4052 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4053 | obj: object |
| 4054 | / |
| 4055 | |
| 4056 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4057 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4058 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4059 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4060 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4061 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4062 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4063 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4064 | Developers often forget to call __init__() in their subclasses, which |
| 4065 | would trigger a segfault without this check. */ |
| 4066 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4067 | PickleState *st = _Pickle_GetGlobalState(); |
| 4068 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4069 | "Pickler.__init__() was not called by %s.__init__()", |
| 4070 | Py_TYPE(self)->tp_name); |
| 4071 | return NULL; |
| 4072 | } |
| 4073 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4074 | if (_Pickler_ClearBuffer(self) < 0) |
| 4075 | return NULL; |
| 4076 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4077 | if (dump(self, obj) < 0) |
| 4078 | return NULL; |
| 4079 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4080 | if (_Pickler_FlushToFile(self) < 0) |
| 4081 | return NULL; |
| 4082 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4083 | Py_RETURN_NONE; |
| 4084 | } |
| 4085 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4086 | /*[clinic input] |
| 4087 | |
| 4088 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4089 | |
| 4090 | Returns size in memory, in bytes. |
| 4091 | [clinic start generated code]*/ |
| 4092 | |
| 4093 | static Py_ssize_t |
| 4094 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4095 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4096 | { |
| 4097 | Py_ssize_t res, s; |
| 4098 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 4099 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4100 | if (self->memo != NULL) { |
| 4101 | res += sizeof(PyMemoTable); |
| 4102 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4103 | } |
| 4104 | if (self->output_buffer != NULL) { |
| 4105 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4106 | if (s == -1) |
| 4107 | return -1; |
| 4108 | res += s; |
| 4109 | } |
| 4110 | return res; |
| 4111 | } |
| 4112 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4113 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4114 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4115 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4116 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4117 | {NULL, NULL} /* sentinel */ |
| 4118 | }; |
| 4119 | |
| 4120 | static void |
| 4121 | Pickler_dealloc(PicklerObject *self) |
| 4122 | { |
| 4123 | PyObject_GC_UnTrack(self); |
| 4124 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4125 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4126 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4127 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4128 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4129 | Py_XDECREF(self->fast_memo); |
| 4130 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4131 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4132 | |
| 4133 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4134 | } |
| 4135 | |
| 4136 | static int |
| 4137 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4138 | { |
| 4139 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4140 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4141 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4142 | Py_VISIT(self->fast_memo); |
| 4143 | return 0; |
| 4144 | } |
| 4145 | |
| 4146 | static int |
| 4147 | Pickler_clear(PicklerObject *self) |
| 4148 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4149 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4150 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4151 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4152 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4153 | Py_CLEAR(self->fast_memo); |
| 4154 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4155 | if (self->memo != NULL) { |
| 4156 | PyMemoTable *memo = self->memo; |
| 4157 | self->memo = NULL; |
| 4158 | PyMemoTable_Del(memo); |
| 4159 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4160 | return 0; |
| 4161 | } |
| 4162 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4163 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4164 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4165 | |
| 4166 | _pickle.Pickler.__init__ |
| 4167 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4168 | file: object |
| 4169 | protocol: object = NULL |
| 4170 | fix_imports: bool = True |
| 4171 | |
| 4172 | This takes a binary file for writing a pickle data stream. |
| 4173 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4174 | The optional *protocol* argument tells the pickler to use the given |
| 4175 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4176 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4177 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4178 | Specifying a negative protocol version selects the highest protocol |
| 4179 | version supported. The higher the protocol used, the more recent the |
| 4180 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4181 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4182 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4183 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4184 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4185 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4186 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4187 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4188 | to map the new Python 3 names to the old module names used in Python |
| 4189 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4190 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4191 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4192 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4193 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4194 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4195 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4196 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4197 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4198 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4199 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4200 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4201 | if (self->write != NULL) |
| 4202 | (void)Pickler_clear(self); |
| 4203 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4204 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4205 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4206 | |
| 4207 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4208 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4209 | |
| 4210 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4211 | if (self->memo == NULL) { |
| 4212 | self->memo = PyMemoTable_New(); |
| 4213 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4214 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4215 | } |
| 4216 | self->output_len = 0; |
| 4217 | if (self->output_buffer == NULL) { |
| 4218 | self->max_output_len = WRITE_BUF_SIZE; |
| 4219 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4220 | self->max_output_len); |
| 4221 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4222 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4223 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4224 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4225 | self->fast = 0; |
| 4226 | self->fast_nesting = 0; |
| 4227 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4228 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4229 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4230 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4231 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4232 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4233 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4234 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4235 | self->dispatch_table = NULL; |
| 4236 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4237 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4238 | &PyId_dispatch_table); |
| 4239 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4240 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4241 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4242 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4243 | return 0; |
| 4244 | } |
| 4245 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4246 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4247 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4248 | * avoid breaking code like: |
| 4249 | * pickler.memo.clear() |
| 4250 | * and |
| 4251 | * pickler.memo = saved_memo |
| 4252 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4253 | * it. Note that we don't implement the entire mapping API here. This is |
| 4254 | * intentional, as these should be treated as black-box implementation details. |
| 4255 | */ |
| 4256 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4257 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4258 | _pickle.PicklerMemoProxy.clear |
| 4259 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4260 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4261 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4262 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4263 | static PyObject * |
| 4264 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4265 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4266 | { |
| 4267 | if (self->pickler->memo) |
| 4268 | PyMemoTable_Clear(self->pickler->memo); |
| 4269 | Py_RETURN_NONE; |
| 4270 | } |
| 4271 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4272 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4273 | _pickle.PicklerMemoProxy.copy |
| 4274 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4275 | Copy the memo to a new object. |
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 | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4278 | static PyObject * |
| 4279 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4280 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4281 | { |
| 4282 | Py_ssize_t i; |
| 4283 | PyMemoTable *memo; |
| 4284 | PyObject *new_memo = PyDict_New(); |
| 4285 | if (new_memo == NULL) |
| 4286 | return NULL; |
| 4287 | |
| 4288 | memo = self->pickler->memo; |
| 4289 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4290 | PyMemoEntry entry = memo->mt_table[i]; |
| 4291 | if (entry.me_key != NULL) { |
| 4292 | int status; |
| 4293 | PyObject *key, *value; |
| 4294 | |
| 4295 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4296 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4297 | |
| 4298 | if (key == NULL || value == NULL) { |
| 4299 | Py_XDECREF(key); |
| 4300 | Py_XDECREF(value); |
| 4301 | goto error; |
| 4302 | } |
| 4303 | status = PyDict_SetItem(new_memo, key, value); |
| 4304 | Py_DECREF(key); |
| 4305 | Py_DECREF(value); |
| 4306 | if (status < 0) |
| 4307 | goto error; |
| 4308 | } |
| 4309 | } |
| 4310 | return new_memo; |
| 4311 | |
| 4312 | error: |
| 4313 | Py_XDECREF(new_memo); |
| 4314 | return NULL; |
| 4315 | } |
| 4316 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4317 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4318 | _pickle.PicklerMemoProxy.__reduce__ |
| 4319 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4320 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4321 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4322 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4323 | static PyObject * |
| 4324 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4325 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4326 | { |
| 4327 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4328 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4329 | if (contents == NULL) |
| 4330 | return NULL; |
| 4331 | |
| 4332 | reduce_value = PyTuple_New(2); |
| 4333 | if (reduce_value == NULL) { |
| 4334 | Py_DECREF(contents); |
| 4335 | return NULL; |
| 4336 | } |
| 4337 | dict_args = PyTuple_New(1); |
| 4338 | if (dict_args == NULL) { |
| 4339 | Py_DECREF(contents); |
| 4340 | Py_DECREF(reduce_value); |
| 4341 | return NULL; |
| 4342 | } |
| 4343 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4344 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4345 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4346 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4347 | return reduce_value; |
| 4348 | } |
| 4349 | |
| 4350 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4351 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4352 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4353 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4354 | {NULL, NULL} /* sentinel */ |
| 4355 | }; |
| 4356 | |
| 4357 | static void |
| 4358 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4359 | { |
| 4360 | PyObject_GC_UnTrack(self); |
| 4361 | Py_XDECREF(self->pickler); |
| 4362 | PyObject_GC_Del((PyObject *)self); |
| 4363 | } |
| 4364 | |
| 4365 | static int |
| 4366 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4367 | visitproc visit, void *arg) |
| 4368 | { |
| 4369 | Py_VISIT(self->pickler); |
| 4370 | return 0; |
| 4371 | } |
| 4372 | |
| 4373 | static int |
| 4374 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4375 | { |
| 4376 | Py_CLEAR(self->pickler); |
| 4377 | return 0; |
| 4378 | } |
| 4379 | |
| 4380 | static PyTypeObject PicklerMemoProxyType = { |
| 4381 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4382 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4383 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4384 | 0, |
| 4385 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4386 | 0, /* tp_print */ |
| 4387 | 0, /* tp_getattr */ |
| 4388 | 0, /* tp_setattr */ |
| 4389 | 0, /* tp_compare */ |
| 4390 | 0, /* tp_repr */ |
| 4391 | 0, /* tp_as_number */ |
| 4392 | 0, /* tp_as_sequence */ |
| 4393 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4394 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4395 | 0, /* tp_call */ |
| 4396 | 0, /* tp_str */ |
| 4397 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4398 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4399 | 0, /* tp_as_buffer */ |
| 4400 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4401 | 0, /* tp_doc */ |
| 4402 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4403 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4404 | 0, /* tp_richcompare */ |
| 4405 | 0, /* tp_weaklistoffset */ |
| 4406 | 0, /* tp_iter */ |
| 4407 | 0, /* tp_iternext */ |
| 4408 | picklerproxy_methods, /* tp_methods */ |
| 4409 | }; |
| 4410 | |
| 4411 | static PyObject * |
| 4412 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4413 | { |
| 4414 | PicklerMemoProxyObject *self; |
| 4415 | |
| 4416 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4417 | if (self == NULL) |
| 4418 | return NULL; |
| 4419 | Py_INCREF(pickler); |
| 4420 | self->pickler = pickler; |
| 4421 | PyObject_GC_Track(self); |
| 4422 | return (PyObject *)self; |
| 4423 | } |
| 4424 | |
| 4425 | /*****************************************************************************/ |
| 4426 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4427 | static PyObject * |
| 4428 | Pickler_get_memo(PicklerObject *self) |
| 4429 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4430 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4431 | } |
| 4432 | |
| 4433 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4434 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4435 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4436 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4437 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4438 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4439 | PyErr_SetString(PyExc_TypeError, |
| 4440 | "attribute deletion is not supported"); |
| 4441 | return -1; |
| 4442 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4443 | |
| 4444 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4445 | PicklerObject *pickler = |
| 4446 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4447 | |
| 4448 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4449 | if (new_memo == NULL) |
| 4450 | return -1; |
| 4451 | } |
| 4452 | else if (PyDict_Check(obj)) { |
| 4453 | Py_ssize_t i = 0; |
| 4454 | PyObject *key, *value; |
| 4455 | |
| 4456 | new_memo = PyMemoTable_New(); |
| 4457 | if (new_memo == NULL) |
| 4458 | return -1; |
| 4459 | |
| 4460 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4461 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4462 | PyObject *memo_obj; |
| 4463 | |
| 4464 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4465 | PyErr_SetString(PyExc_TypeError, |
| 4466 | "'memo' values must be 2-item tuples"); |
| 4467 | goto error; |
| 4468 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4469 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4470 | if (memo_id == -1 && PyErr_Occurred()) |
| 4471 | goto error; |
| 4472 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4473 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4474 | goto error; |
| 4475 | } |
| 4476 | } |
| 4477 | else { |
| 4478 | PyErr_Format(PyExc_TypeError, |
Serhiy Storchaka | b6a9c97 | 2016-04-17 09:39:28 +0300 | [diff] [blame] | 4479 | "'memo' attribute must be a PicklerMemoProxy object" |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4480 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4481 | return -1; |
| 4482 | } |
| 4483 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4484 | PyMemoTable_Del(self->memo); |
| 4485 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4486 | |
| 4487 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4488 | |
| 4489 | error: |
| 4490 | if (new_memo) |
| 4491 | PyMemoTable_Del(new_memo); |
| 4492 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4493 | } |
| 4494 | |
| 4495 | static PyObject * |
| 4496 | Pickler_get_persid(PicklerObject *self) |
| 4497 | { |
| 4498 | if (self->pers_func == NULL) |
| 4499 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4500 | else |
| 4501 | Py_INCREF(self->pers_func); |
| 4502 | return self->pers_func; |
| 4503 | } |
| 4504 | |
| 4505 | static int |
| 4506 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4507 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4508 | if (value == NULL) { |
| 4509 | PyErr_SetString(PyExc_TypeError, |
| 4510 | "attribute deletion is not supported"); |
| 4511 | return -1; |
| 4512 | } |
| 4513 | if (!PyCallable_Check(value)) { |
| 4514 | PyErr_SetString(PyExc_TypeError, |
| 4515 | "persistent_id must be a callable taking one argument"); |
| 4516 | return -1; |
| 4517 | } |
| 4518 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4519 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 4520 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4521 | |
| 4522 | return 0; |
| 4523 | } |
| 4524 | |
| 4525 | static PyMemberDef Pickler_members[] = { |
| 4526 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4527 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4528 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4529 | {NULL} |
| 4530 | }; |
| 4531 | |
| 4532 | static PyGetSetDef Pickler_getsets[] = { |
| 4533 | {"memo", (getter)Pickler_get_memo, |
| 4534 | (setter)Pickler_set_memo}, |
| 4535 | {"persistent_id", (getter)Pickler_get_persid, |
| 4536 | (setter)Pickler_set_persid}, |
| 4537 | {NULL} |
| 4538 | }; |
| 4539 | |
| 4540 | static PyTypeObject Pickler_Type = { |
| 4541 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4542 | "_pickle.Pickler" , /*tp_name*/ |
| 4543 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4544 | 0, /*tp_itemsize*/ |
| 4545 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4546 | 0, /*tp_print*/ |
| 4547 | 0, /*tp_getattr*/ |
| 4548 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4549 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4550 | 0, /*tp_repr*/ |
| 4551 | 0, /*tp_as_number*/ |
| 4552 | 0, /*tp_as_sequence*/ |
| 4553 | 0, /*tp_as_mapping*/ |
| 4554 | 0, /*tp_hash*/ |
| 4555 | 0, /*tp_call*/ |
| 4556 | 0, /*tp_str*/ |
| 4557 | 0, /*tp_getattro*/ |
| 4558 | 0, /*tp_setattro*/ |
| 4559 | 0, /*tp_as_buffer*/ |
| 4560 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4561 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4562 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4563 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4564 | 0, /*tp_richcompare*/ |
| 4565 | 0, /*tp_weaklistoffset*/ |
| 4566 | 0, /*tp_iter*/ |
| 4567 | 0, /*tp_iternext*/ |
| 4568 | Pickler_methods, /*tp_methods*/ |
| 4569 | Pickler_members, /*tp_members*/ |
| 4570 | Pickler_getsets, /*tp_getset*/ |
| 4571 | 0, /*tp_base*/ |
| 4572 | 0, /*tp_dict*/ |
| 4573 | 0, /*tp_descr_get*/ |
| 4574 | 0, /*tp_descr_set*/ |
| 4575 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4576 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4577 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4578 | PyType_GenericNew, /*tp_new*/ |
| 4579 | PyObject_GC_Del, /*tp_free*/ |
| 4580 | 0, /*tp_is_gc*/ |
| 4581 | }; |
| 4582 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4583 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4584 | |
| 4585 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4586 | using directly the C version of find_class(), when find_class() is not |
| 4587 | overridden by a subclass. Although, this could become rather hackish. A |
| 4588 | simpler optimization would be to call the C function when self is not a |
| 4589 | subclass instance. */ |
| 4590 | static PyObject * |
| 4591 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4592 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4593 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4594 | |
| 4595 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4596 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4599 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4600 | marker(UnpicklerObject *self) |
| 4601 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4602 | Py_ssize_t mark; |
| 4603 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4604 | if (self->num_marks < 1) { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4605 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4606 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4607 | return -1; |
| 4608 | } |
| 4609 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 4610 | mark = self->marks[--self->num_marks]; |
| 4611 | self->stack->mark_set = self->num_marks != 0; |
| 4612 | self->stack->fence = self->num_marks ? |
| 4613 | self->marks[self->num_marks - 1] : 0; |
| 4614 | return mark; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4615 | } |
| 4616 | |
| 4617 | static int |
| 4618 | load_none(UnpicklerObject *self) |
| 4619 | { |
| 4620 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4621 | return 0; |
| 4622 | } |
| 4623 | |
| 4624 | static int |
| 4625 | bad_readline(void) |
| 4626 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4627 | PickleState *st = _Pickle_GetGlobalState(); |
| 4628 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4629 | return -1; |
| 4630 | } |
| 4631 | |
| 4632 | static int |
| 4633 | load_int(UnpicklerObject *self) |
| 4634 | { |
| 4635 | PyObject *value; |
| 4636 | char *endptr, *s; |
| 4637 | Py_ssize_t len; |
| 4638 | long x; |
| 4639 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4640 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4641 | return -1; |
| 4642 | if (len < 2) |
| 4643 | return bad_readline(); |
| 4644 | |
| 4645 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4646 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4647 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4648 | x = strtol(s, &endptr, 0); |
| 4649 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4650 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4651 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4652 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4653 | errno = 0; |
| 4654 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4655 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4656 | if (value == NULL) { |
| 4657 | PyErr_SetString(PyExc_ValueError, |
| 4658 | "could not convert string to int"); |
| 4659 | return -1; |
| 4660 | } |
| 4661 | } |
| 4662 | else { |
| 4663 | if (len == 3 && (x == 0 || x == 1)) { |
| 4664 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4665 | return -1; |
| 4666 | } |
| 4667 | else { |
| 4668 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4669 | return -1; |
| 4670 | } |
| 4671 | } |
| 4672 | |
| 4673 | PDATA_PUSH(self->stack, value, -1); |
| 4674 | return 0; |
| 4675 | } |
| 4676 | |
| 4677 | static int |
| 4678 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4679 | { |
| 4680 | assert(boolean == Py_True || boolean == Py_False); |
| 4681 | PDATA_APPEND(self->stack, boolean, -1); |
| 4682 | return 0; |
| 4683 | } |
| 4684 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4685 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4686 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4687 | */ |
| 4688 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4689 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4690 | { |
| 4691 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4692 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4693 | size_t x = 0; |
| 4694 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4695 | if (nbytes > (int)sizeof(size_t)) { |
| 4696 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4697 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4698 | */ |
| 4699 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4700 | if (s[i]) |
| 4701 | return -1; |
| 4702 | } |
| 4703 | nbytes = (int)sizeof(size_t); |
| 4704 | } |
| 4705 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4706 | x |= (size_t) s[i] << (8 * i); |
| 4707 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4708 | |
| 4709 | if (x > PY_SSIZE_T_MAX) |
| 4710 | return -1; |
| 4711 | else |
| 4712 | return (Py_ssize_t) x; |
| 4713 | } |
| 4714 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4715 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4716 | * 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] | 4717 | * 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] | 4718 | * of x-platform bugs. |
| 4719 | */ |
| 4720 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4721 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4722 | { |
| 4723 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4724 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4725 | long x = 0; |
| 4726 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4727 | for (i = 0; i < nbytes; i++) { |
| 4728 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4729 | } |
| 4730 | |
| 4731 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4732 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4733 | * to extend a BININT's sign bit to the full width. |
| 4734 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4735 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4736 | x |= -(x & (1L << 31)); |
| 4737 | } |
| 4738 | |
| 4739 | return x; |
| 4740 | } |
| 4741 | |
| 4742 | static int |
| 4743 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4744 | { |
| 4745 | PyObject *value; |
| 4746 | long x; |
| 4747 | |
| 4748 | x = calc_binint(s, size); |
| 4749 | |
| 4750 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4751 | return -1; |
| 4752 | |
| 4753 | PDATA_PUSH(self->stack, value, -1); |
| 4754 | return 0; |
| 4755 | } |
| 4756 | |
| 4757 | static int |
| 4758 | load_binint(UnpicklerObject *self) |
| 4759 | { |
| 4760 | char *s; |
| 4761 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4762 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4763 | return -1; |
| 4764 | |
| 4765 | return load_binintx(self, s, 4); |
| 4766 | } |
| 4767 | |
| 4768 | static int |
| 4769 | load_binint1(UnpicklerObject *self) |
| 4770 | { |
| 4771 | char *s; |
| 4772 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4773 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4774 | return -1; |
| 4775 | |
| 4776 | return load_binintx(self, s, 1); |
| 4777 | } |
| 4778 | |
| 4779 | static int |
| 4780 | load_binint2(UnpicklerObject *self) |
| 4781 | { |
| 4782 | char *s; |
| 4783 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4784 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4785 | return -1; |
| 4786 | |
| 4787 | return load_binintx(self, s, 2); |
| 4788 | } |
| 4789 | |
| 4790 | static int |
| 4791 | load_long(UnpicklerObject *self) |
| 4792 | { |
| 4793 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4794 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4795 | Py_ssize_t len; |
| 4796 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4797 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4798 | return -1; |
| 4799 | if (len < 2) |
| 4800 | return bad_readline(); |
| 4801 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4802 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4803 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4804 | compatibility with Python 3.0.0, we don't actually *require* |
| 4805 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4806 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4807 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4808 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4809 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4810 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4811 | return -1; |
| 4812 | |
| 4813 | PDATA_PUSH(self->stack, value, -1); |
| 4814 | return 0; |
| 4815 | } |
| 4816 | |
| 4817 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4818 | * data following. |
| 4819 | */ |
| 4820 | static int |
| 4821 | load_counted_long(UnpicklerObject *self, int size) |
| 4822 | { |
| 4823 | PyObject *value; |
| 4824 | char *nbytes; |
| 4825 | char *pdata; |
| 4826 | |
| 4827 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4828 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4829 | return -1; |
| 4830 | |
| 4831 | size = calc_binint(nbytes, size); |
| 4832 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4833 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4834 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4835 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4836 | "LONG pickle has negative byte count"); |
| 4837 | return -1; |
| 4838 | } |
| 4839 | |
| 4840 | if (size == 0) |
| 4841 | value = PyLong_FromLong(0L); |
| 4842 | else { |
| 4843 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4844 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4845 | return -1; |
| 4846 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4847 | 1 /* little endian */ , 1 /* signed */ ); |
| 4848 | } |
| 4849 | if (value == NULL) |
| 4850 | return -1; |
| 4851 | PDATA_PUSH(self->stack, value, -1); |
| 4852 | return 0; |
| 4853 | } |
| 4854 | |
| 4855 | static int |
| 4856 | load_float(UnpicklerObject *self) |
| 4857 | { |
| 4858 | PyObject *value; |
| 4859 | char *endptr, *s; |
| 4860 | Py_ssize_t len; |
| 4861 | double d; |
| 4862 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4863 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4864 | return -1; |
| 4865 | if (len < 2) |
| 4866 | return bad_readline(); |
| 4867 | |
| 4868 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4869 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4870 | if (d == -1.0 && PyErr_Occurred()) |
| 4871 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4872 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4873 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4874 | return -1; |
| 4875 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4876 | value = PyFloat_FromDouble(d); |
| 4877 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4878 | return -1; |
| 4879 | |
| 4880 | PDATA_PUSH(self->stack, value, -1); |
| 4881 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4882 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4883 | |
| 4884 | static int |
| 4885 | load_binfloat(UnpicklerObject *self) |
| 4886 | { |
| 4887 | PyObject *value; |
| 4888 | double x; |
| 4889 | char *s; |
| 4890 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4891 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4892 | return -1; |
| 4893 | |
| 4894 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4895 | if (x == -1.0 && PyErr_Occurred()) |
| 4896 | return -1; |
| 4897 | |
| 4898 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4899 | return -1; |
| 4900 | |
| 4901 | PDATA_PUSH(self->stack, value, -1); |
| 4902 | return 0; |
| 4903 | } |
| 4904 | |
| 4905 | static int |
| 4906 | load_string(UnpicklerObject *self) |
| 4907 | { |
| 4908 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4909 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4910 | Py_ssize_t len; |
| 4911 | char *s, *p; |
| 4912 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4913 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4914 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4915 | /* Strip the newline */ |
| 4916 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4917 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4918 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4919 | p = s + 1; |
| 4920 | len -= 2; |
| 4921 | } |
| 4922 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4923 | PickleState *st = _Pickle_GetGlobalState(); |
| 4924 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4925 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4926 | return -1; |
| 4927 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4928 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4929 | |
| 4930 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4931 | to encode, and then coerce the result to Unicode. */ |
| 4932 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4933 | if (bytes == NULL) |
| 4934 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4935 | |
| 4936 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4937 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4938 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4939 | obj = bytes; |
| 4940 | } |
| 4941 | else { |
| 4942 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4943 | Py_DECREF(bytes); |
| 4944 | if (obj == NULL) { |
| 4945 | return -1; |
| 4946 | } |
| 4947 | } |
| 4948 | |
| 4949 | PDATA_PUSH(self->stack, obj, -1); |
| 4950 | return 0; |
| 4951 | } |
| 4952 | |
| 4953 | static int |
| 4954 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4955 | { |
| 4956 | PyObject *obj; |
| 4957 | Py_ssize_t size; |
| 4958 | char *s; |
| 4959 | |
| 4960 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4961 | return -1; |
| 4962 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4963 | size = calc_binsize(s, nbytes); |
| 4964 | if (size < 0) { |
| 4965 | PickleState *st = _Pickle_GetGlobalState(); |
| 4966 | PyErr_Format(st->UnpicklingError, |
| 4967 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4968 | PY_SSIZE_T_MAX); |
| 4969 | return -1; |
| 4970 | } |
| 4971 | |
| 4972 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4973 | return -1; |
| 4974 | |
| 4975 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4976 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4977 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4978 | obj = PyBytes_FromStringAndSize(s, size); |
| 4979 | } |
| 4980 | else { |
| 4981 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4982 | } |
| 4983 | if (obj == NULL) { |
| 4984 | return -1; |
| 4985 | } |
| 4986 | |
| 4987 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4988 | return 0; |
| 4989 | } |
| 4990 | |
| 4991 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4992 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4993 | { |
| 4994 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4995 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4996 | char *s; |
| 4997 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4998 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4999 | return -1; |
| 5000 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5001 | size = calc_binsize(s, nbytes); |
| 5002 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5003 | PyErr_Format(PyExc_OverflowError, |
| 5004 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5005 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5006 | return -1; |
| 5007 | } |
| 5008 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5009 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5010 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5011 | |
| 5012 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5013 | if (bytes == NULL) |
| 5014 | return -1; |
| 5015 | |
| 5016 | PDATA_PUSH(self->stack, bytes, -1); |
| 5017 | return 0; |
| 5018 | } |
| 5019 | |
| 5020 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5021 | load_unicode(UnpicklerObject *self) |
| 5022 | { |
| 5023 | PyObject *str; |
| 5024 | Py_ssize_t len; |
| 5025 | char *s; |
| 5026 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5027 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5028 | return -1; |
| 5029 | if (len < 1) |
| 5030 | return bad_readline(); |
| 5031 | |
| 5032 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5033 | if (str == NULL) |
| 5034 | return -1; |
| 5035 | |
| 5036 | PDATA_PUSH(self->stack, str, -1); |
| 5037 | return 0; |
| 5038 | } |
| 5039 | |
| 5040 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5041 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5042 | { |
| 5043 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5044 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5045 | char *s; |
| 5046 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5047 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5048 | return -1; |
| 5049 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5050 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5051 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5052 | PyErr_Format(PyExc_OverflowError, |
| 5053 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5054 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5055 | return -1; |
| 5056 | } |
| 5057 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5058 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5059 | return -1; |
| 5060 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5061 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5062 | if (str == NULL) |
| 5063 | return -1; |
| 5064 | |
| 5065 | PDATA_PUSH(self->stack, str, -1); |
| 5066 | return 0; |
| 5067 | } |
| 5068 | |
| 5069 | static int |
Victor Stinner | 21b4711 | 2016-03-14 18:09:39 +0100 | [diff] [blame] | 5070 | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5071 | { |
| 5072 | PyObject *tuple; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5073 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5074 | if (Py_SIZE(self->stack) < len) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5075 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5076 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5077 | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5078 | if (tuple == NULL) |
| 5079 | return -1; |
| 5080 | PDATA_PUSH(self->stack, tuple, -1); |
| 5081 | return 0; |
| 5082 | } |
| 5083 | |
| 5084 | static int |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5085 | load_tuple(UnpicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5086 | { |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5087 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5088 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5089 | if ((i = marker(self)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5090 | return -1; |
| 5091 | |
Serhiy Storchaka | a49de6b | 2015-11-25 15:01:53 +0200 | [diff] [blame] | 5092 | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5093 | } |
| 5094 | |
| 5095 | static int |
| 5096 | load_empty_list(UnpicklerObject *self) |
| 5097 | { |
| 5098 | PyObject *list; |
| 5099 | |
| 5100 | if ((list = PyList_New(0)) == NULL) |
| 5101 | return -1; |
| 5102 | PDATA_PUSH(self->stack, list, -1); |
| 5103 | return 0; |
| 5104 | } |
| 5105 | |
| 5106 | static int |
| 5107 | load_empty_dict(UnpicklerObject *self) |
| 5108 | { |
| 5109 | PyObject *dict; |
| 5110 | |
| 5111 | if ((dict = PyDict_New()) == NULL) |
| 5112 | return -1; |
| 5113 | PDATA_PUSH(self->stack, dict, -1); |
| 5114 | return 0; |
| 5115 | } |
| 5116 | |
| 5117 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5118 | load_empty_set(UnpicklerObject *self) |
| 5119 | { |
| 5120 | PyObject *set; |
| 5121 | |
| 5122 | if ((set = PySet_New(NULL)) == NULL) |
| 5123 | return -1; |
| 5124 | PDATA_PUSH(self->stack, set, -1); |
| 5125 | return 0; |
| 5126 | } |
| 5127 | |
| 5128 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5129 | load_list(UnpicklerObject *self) |
| 5130 | { |
| 5131 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5132 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5133 | |
| 5134 | if ((i = marker(self)) < 0) |
| 5135 | return -1; |
| 5136 | |
| 5137 | list = Pdata_poplist(self->stack, i); |
| 5138 | if (list == NULL) |
| 5139 | return -1; |
| 5140 | PDATA_PUSH(self->stack, list, -1); |
| 5141 | return 0; |
| 5142 | } |
| 5143 | |
| 5144 | static int |
| 5145 | load_dict(UnpicklerObject *self) |
| 5146 | { |
| 5147 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5148 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5149 | |
| 5150 | if ((i = marker(self)) < 0) |
| 5151 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5152 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5153 | |
| 5154 | if ((dict = PyDict_New()) == NULL) |
| 5155 | return -1; |
| 5156 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5157 | if ((j - i) % 2 != 0) { |
| 5158 | PickleState *st = _Pickle_GetGlobalState(); |
| 5159 | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
Serhiy Storchaka | 3ac5380 | 2015-12-07 11:32:00 +0200 | [diff] [blame] | 5160 | Py_DECREF(dict); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5161 | return -1; |
| 5162 | } |
| 5163 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5164 | for (k = i + 1; k < j; k += 2) { |
| 5165 | key = self->stack->data[k - 1]; |
| 5166 | value = self->stack->data[k]; |
| 5167 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5168 | Py_DECREF(dict); |
| 5169 | return -1; |
| 5170 | } |
| 5171 | } |
| 5172 | Pdata_clear(self->stack, i); |
| 5173 | PDATA_PUSH(self->stack, dict, -1); |
| 5174 | return 0; |
| 5175 | } |
| 5176 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5177 | static int |
| 5178 | load_frozenset(UnpicklerObject *self) |
| 5179 | { |
| 5180 | PyObject *items; |
| 5181 | PyObject *frozenset; |
| 5182 | Py_ssize_t i; |
| 5183 | |
| 5184 | if ((i = marker(self)) < 0) |
| 5185 | return -1; |
| 5186 | |
| 5187 | items = Pdata_poptuple(self->stack, i); |
| 5188 | if (items == NULL) |
| 5189 | return -1; |
| 5190 | |
| 5191 | frozenset = PyFrozenSet_New(items); |
| 5192 | Py_DECREF(items); |
| 5193 | if (frozenset == NULL) |
| 5194 | return -1; |
| 5195 | |
| 5196 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5197 | return 0; |
| 5198 | } |
| 5199 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5200 | static PyObject * |
| 5201 | instantiate(PyObject *cls, PyObject *args) |
| 5202 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5203 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5204 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5205 | /* Caller must assure args are a tuple. Normally, args come from |
| 5206 | Pdata_poptuple which packs objects from the top of the stack |
| 5207 | into a newly created tuple. */ |
| 5208 | assert(PyTuple_Check(args)); |
| 5209 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5210 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5211 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5212 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5213 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5214 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5215 | |
| 5216 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5217 | } |
| 5218 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5219 | } |
| 5220 | |
| 5221 | static int |
| 5222 | load_obj(UnpicklerObject *self) |
| 5223 | { |
| 5224 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5225 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5226 | |
| 5227 | if ((i = marker(self)) < 0) |
| 5228 | return -1; |
| 5229 | |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5230 | if (Py_SIZE(self->stack) - i < 1) |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5231 | return Pdata_stack_underflow(self->stack); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5232 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5233 | args = Pdata_poptuple(self->stack, i + 1); |
| 5234 | if (args == NULL) |
| 5235 | return -1; |
| 5236 | |
| 5237 | PDATA_POP(self->stack, cls); |
| 5238 | if (cls) { |
| 5239 | obj = instantiate(cls, args); |
| 5240 | Py_DECREF(cls); |
| 5241 | } |
| 5242 | Py_DECREF(args); |
| 5243 | if (obj == NULL) |
| 5244 | return -1; |
| 5245 | |
| 5246 | PDATA_PUSH(self->stack, obj, -1); |
| 5247 | return 0; |
| 5248 | } |
| 5249 | |
| 5250 | static int |
| 5251 | load_inst(UnpicklerObject *self) |
| 5252 | { |
| 5253 | PyObject *cls = NULL; |
| 5254 | PyObject *args = NULL; |
| 5255 | PyObject *obj = NULL; |
| 5256 | PyObject *module_name; |
| 5257 | PyObject *class_name; |
| 5258 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5259 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5260 | char *s; |
| 5261 | |
| 5262 | if ((i = marker(self)) < 0) |
| 5263 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5264 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5265 | return -1; |
| 5266 | if (len < 2) |
| 5267 | return bad_readline(); |
| 5268 | |
| 5269 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5270 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5271 | supported by older protocols on Python 2.x. */ |
| 5272 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5273 | if (module_name == NULL) |
| 5274 | return -1; |
| 5275 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5276 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5277 | if (len < 2) { |
| 5278 | Py_DECREF(module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5279 | return bad_readline(); |
Serhiy Storchaka | ca28eba | 2015-12-01 00:18:23 +0200 | [diff] [blame] | 5280 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5281 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5282 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5283 | cls = find_class(self, module_name, class_name); |
| 5284 | Py_DECREF(class_name); |
| 5285 | } |
| 5286 | } |
| 5287 | Py_DECREF(module_name); |
| 5288 | |
| 5289 | if (cls == NULL) |
| 5290 | return -1; |
| 5291 | |
| 5292 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5293 | obj = instantiate(cls, args); |
| 5294 | Py_DECREF(args); |
| 5295 | } |
| 5296 | Py_DECREF(cls); |
| 5297 | |
| 5298 | if (obj == NULL) |
| 5299 | return -1; |
| 5300 | |
| 5301 | PDATA_PUSH(self->stack, obj, -1); |
| 5302 | return 0; |
| 5303 | } |
| 5304 | |
| 5305 | static int |
| 5306 | load_newobj(UnpicklerObject *self) |
| 5307 | { |
| 5308 | PyObject *args = NULL; |
| 5309 | PyObject *clsraw = NULL; |
| 5310 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5311 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5312 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5313 | |
| 5314 | /* Stack is ... cls argtuple, and we want to call |
| 5315 | * cls.__new__(cls, *argtuple). |
| 5316 | */ |
| 5317 | PDATA_POP(self->stack, args); |
| 5318 | if (args == NULL) |
| 5319 | goto error; |
| 5320 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5321 | PyErr_SetString(st->UnpicklingError, |
| 5322 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5323 | goto error; |
| 5324 | } |
| 5325 | |
| 5326 | PDATA_POP(self->stack, clsraw); |
| 5327 | cls = (PyTypeObject *)clsraw; |
| 5328 | if (cls == NULL) |
| 5329 | goto error; |
| 5330 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5331 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5332 | "isn't a type object"); |
| 5333 | goto error; |
| 5334 | } |
| 5335 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5336 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5337 | "has NULL tp_new"); |
| 5338 | goto error; |
| 5339 | } |
| 5340 | |
| 5341 | /* Call __new__. */ |
| 5342 | obj = cls->tp_new(cls, args, NULL); |
| 5343 | if (obj == NULL) |
| 5344 | goto error; |
| 5345 | |
| 5346 | Py_DECREF(args); |
| 5347 | Py_DECREF(clsraw); |
| 5348 | PDATA_PUSH(self->stack, obj, -1); |
| 5349 | return 0; |
| 5350 | |
| 5351 | error: |
| 5352 | Py_XDECREF(args); |
| 5353 | Py_XDECREF(clsraw); |
| 5354 | return -1; |
| 5355 | } |
| 5356 | |
| 5357 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5358 | load_newobj_ex(UnpicklerObject *self) |
| 5359 | { |
| 5360 | PyObject *cls, *args, *kwargs; |
| 5361 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5362 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5363 | |
| 5364 | PDATA_POP(self->stack, kwargs); |
| 5365 | if (kwargs == NULL) { |
| 5366 | return -1; |
| 5367 | } |
| 5368 | PDATA_POP(self->stack, args); |
| 5369 | if (args == NULL) { |
| 5370 | Py_DECREF(kwargs); |
| 5371 | return -1; |
| 5372 | } |
| 5373 | PDATA_POP(self->stack, cls); |
| 5374 | if (cls == NULL) { |
| 5375 | Py_DECREF(kwargs); |
| 5376 | Py_DECREF(args); |
| 5377 | return -1; |
| 5378 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5379 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5380 | if (!PyType_Check(cls)) { |
| 5381 | Py_DECREF(kwargs); |
| 5382 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5383 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5384 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5385 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5386 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5387 | return -1; |
| 5388 | } |
| 5389 | |
| 5390 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5391 | Py_DECREF(kwargs); |
| 5392 | Py_DECREF(args); |
| 5393 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5394 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5395 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5396 | return -1; |
| 5397 | } |
| 5398 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5399 | Py_DECREF(kwargs); |
| 5400 | Py_DECREF(args); |
| 5401 | Py_DECREF(cls); |
| 5402 | if (obj == NULL) { |
| 5403 | return -1; |
| 5404 | } |
| 5405 | PDATA_PUSH(self->stack, obj, -1); |
| 5406 | return 0; |
| 5407 | } |
| 5408 | |
| 5409 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5410 | load_global(UnpicklerObject *self) |
| 5411 | { |
| 5412 | PyObject *global = NULL; |
| 5413 | PyObject *module_name; |
| 5414 | PyObject *global_name; |
| 5415 | Py_ssize_t len; |
| 5416 | char *s; |
| 5417 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5418 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5419 | return -1; |
| 5420 | if (len < 2) |
| 5421 | return bad_readline(); |
| 5422 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5423 | if (!module_name) |
| 5424 | return -1; |
| 5425 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5426 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5427 | if (len < 2) { |
| 5428 | Py_DECREF(module_name); |
| 5429 | return bad_readline(); |
| 5430 | } |
| 5431 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5432 | if (global_name) { |
| 5433 | global = find_class(self, module_name, global_name); |
| 5434 | Py_DECREF(global_name); |
| 5435 | } |
| 5436 | } |
| 5437 | Py_DECREF(module_name); |
| 5438 | |
| 5439 | if (global == NULL) |
| 5440 | return -1; |
| 5441 | PDATA_PUSH(self->stack, global, -1); |
| 5442 | return 0; |
| 5443 | } |
| 5444 | |
| 5445 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5446 | load_stack_global(UnpicklerObject *self) |
| 5447 | { |
| 5448 | PyObject *global; |
| 5449 | PyObject *module_name; |
| 5450 | PyObject *global_name; |
| 5451 | |
| 5452 | PDATA_POP(self->stack, global_name); |
| 5453 | PDATA_POP(self->stack, module_name); |
| 5454 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5455 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5456 | PickleState *st = _Pickle_GetGlobalState(); |
| 5457 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5458 | Py_XDECREF(global_name); |
| 5459 | Py_XDECREF(module_name); |
| 5460 | return -1; |
| 5461 | } |
| 5462 | global = find_class(self, module_name, global_name); |
| 5463 | Py_DECREF(global_name); |
| 5464 | Py_DECREF(module_name); |
| 5465 | if (global == NULL) |
| 5466 | return -1; |
| 5467 | PDATA_PUSH(self->stack, global, -1); |
| 5468 | return 0; |
| 5469 | } |
| 5470 | |
| 5471 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5472 | load_persid(UnpicklerObject *self) |
| 5473 | { |
| 5474 | PyObject *pid; |
| 5475 | Py_ssize_t len; |
| 5476 | char *s; |
| 5477 | |
| 5478 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5479 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5480 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5481 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5482 | return bad_readline(); |
| 5483 | |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5484 | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5485 | if (pid == NULL) { |
| 5486 | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
| 5487 | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
| 5488 | "persistent IDs in protocol 0 must be " |
| 5489 | "ASCII strings"); |
| 5490 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5491 | return -1; |
Serhiy Storchaka | dec25af | 2016-07-17 11:24:17 +0300 | [diff] [blame] | 5492 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5493 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5494 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5495 | to pid first. */ |
| 5496 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5497 | if (pid == NULL) |
| 5498 | return -1; |
| 5499 | |
| 5500 | PDATA_PUSH(self->stack, pid, -1); |
| 5501 | return 0; |
| 5502 | } |
| 5503 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5504 | PickleState *st = _Pickle_GetGlobalState(); |
| 5505 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5506 | "A load persistent id instruction was encountered,\n" |
| 5507 | "but no persistent_load function was specified."); |
| 5508 | return -1; |
| 5509 | } |
| 5510 | } |
| 5511 | |
| 5512 | static int |
| 5513 | load_binpersid(UnpicklerObject *self) |
| 5514 | { |
| 5515 | PyObject *pid; |
| 5516 | |
| 5517 | if (self->pers_func) { |
| 5518 | PDATA_POP(self->stack, pid); |
| 5519 | if (pid == NULL) |
| 5520 | return -1; |
| 5521 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5522 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5523 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5524 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5525 | if (pid == NULL) |
| 5526 | return -1; |
| 5527 | |
| 5528 | PDATA_PUSH(self->stack, pid, -1); |
| 5529 | return 0; |
| 5530 | } |
| 5531 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5532 | PickleState *st = _Pickle_GetGlobalState(); |
| 5533 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5534 | "A load persistent id instruction was encountered,\n" |
| 5535 | "but no persistent_load function was specified."); |
| 5536 | return -1; |
| 5537 | } |
| 5538 | } |
| 5539 | |
| 5540 | static int |
| 5541 | load_pop(UnpicklerObject *self) |
| 5542 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5543 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5544 | |
| 5545 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5546 | * an object stack and a mark stack. We have to be clever and |
| 5547 | * 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] | 5548 | * mark stack first, and only signalling a stack underflow if |
| 5549 | * the object stack is empty and the mark stack doesn't match |
| 5550 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5551 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5552 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5553 | self->num_marks--; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5554 | self->stack->mark_set = self->num_marks != 0; |
| 5555 | self->stack->fence = self->num_marks ? |
| 5556 | self->marks[self->num_marks - 1] : 0; |
| 5557 | } else if (len <= self->stack->fence) |
| 5558 | return Pdata_stack_underflow(self->stack); |
| 5559 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5560 | len--; |
| 5561 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5562 | Py_SIZE(self->stack) = len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5563 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5564 | return 0; |
| 5565 | } |
| 5566 | |
| 5567 | static int |
| 5568 | load_pop_mark(UnpicklerObject *self) |
| 5569 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5570 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5571 | |
| 5572 | if ((i = marker(self)) < 0) |
| 5573 | return -1; |
| 5574 | |
| 5575 | Pdata_clear(self->stack, i); |
| 5576 | |
| 5577 | return 0; |
| 5578 | } |
| 5579 | |
| 5580 | static int |
| 5581 | load_dup(UnpicklerObject *self) |
| 5582 | { |
| 5583 | PyObject *last; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5584 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5585 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5586 | if (len <= self->stack->fence) |
| 5587 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5588 | last = self->stack->data[len - 1]; |
| 5589 | PDATA_APPEND(self->stack, last, -1); |
| 5590 | return 0; |
| 5591 | } |
| 5592 | |
| 5593 | static int |
| 5594 | load_get(UnpicklerObject *self) |
| 5595 | { |
| 5596 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5597 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5598 | Py_ssize_t len; |
| 5599 | char *s; |
| 5600 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5601 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5602 | return -1; |
| 5603 | if (len < 2) |
| 5604 | return bad_readline(); |
| 5605 | |
| 5606 | key = PyLong_FromString(s, NULL, 10); |
| 5607 | if (key == NULL) |
| 5608 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5609 | idx = PyLong_AsSsize_t(key); |
| 5610 | if (idx == -1 && PyErr_Occurred()) { |
| 5611 | Py_DECREF(key); |
| 5612 | return -1; |
| 5613 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5614 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5615 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5616 | if (value == NULL) { |
| 5617 | if (!PyErr_Occurred()) |
| 5618 | PyErr_SetObject(PyExc_KeyError, key); |
| 5619 | Py_DECREF(key); |
| 5620 | return -1; |
| 5621 | } |
| 5622 | Py_DECREF(key); |
| 5623 | |
| 5624 | PDATA_APPEND(self->stack, value, -1); |
| 5625 | return 0; |
| 5626 | } |
| 5627 | |
| 5628 | static int |
| 5629 | load_binget(UnpicklerObject *self) |
| 5630 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5631 | PyObject *value; |
| 5632 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5633 | char *s; |
| 5634 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5635 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5636 | return -1; |
| 5637 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5638 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5639 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5640 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5641 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5642 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5643 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5644 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5645 | Py_DECREF(key); |
| 5646 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5647 | return -1; |
| 5648 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5649 | |
| 5650 | PDATA_APPEND(self->stack, value, -1); |
| 5651 | return 0; |
| 5652 | } |
| 5653 | |
| 5654 | static int |
| 5655 | load_long_binget(UnpicklerObject *self) |
| 5656 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5657 | PyObject *value; |
| 5658 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5659 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5660 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5661 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5662 | return -1; |
| 5663 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5664 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5665 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5666 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5667 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5668 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5669 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5670 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5671 | Py_DECREF(key); |
| 5672 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5673 | return -1; |
| 5674 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5675 | |
| 5676 | PDATA_APPEND(self->stack, value, -1); |
| 5677 | return 0; |
| 5678 | } |
| 5679 | |
| 5680 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5681 | * the number of bytes following the opcode, holding the index (code) value. |
| 5682 | */ |
| 5683 | static int |
| 5684 | load_extension(UnpicklerObject *self, int nbytes) |
| 5685 | { |
| 5686 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5687 | long code; /* calc_binint returns long */ |
| 5688 | PyObject *py_code; /* code as a Python int */ |
| 5689 | PyObject *obj; /* the object to push */ |
| 5690 | PyObject *pair; /* (module_name, class_name) */ |
| 5691 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5692 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5693 | |
| 5694 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5695 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5696 | return -1; |
| 5697 | code = calc_binint(codebytes, nbytes); |
| 5698 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5699 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5700 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5701 | return -1; |
| 5702 | } |
| 5703 | |
| 5704 | /* Look for the code in the cache. */ |
| 5705 | py_code = PyLong_FromLong(code); |
| 5706 | if (py_code == NULL) |
| 5707 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5708 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5709 | if (obj != NULL) { |
| 5710 | /* Bingo. */ |
| 5711 | Py_DECREF(py_code); |
| 5712 | PDATA_APPEND(self->stack, obj, -1); |
| 5713 | return 0; |
| 5714 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5715 | if (PyErr_Occurred()) { |
| 5716 | Py_DECREF(py_code); |
| 5717 | return -1; |
| 5718 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5719 | |
| 5720 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5721 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5722 | if (pair == NULL) { |
| 5723 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5724 | if (!PyErr_Occurred()) { |
| 5725 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5726 | "code %ld", code); |
| 5727 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5728 | return -1; |
| 5729 | } |
| 5730 | /* Since the extension registry is manipulable via Python code, |
| 5731 | * confirm that pair is really a 2-tuple of strings. |
| 5732 | */ |
| 5733 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5734 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5735 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5736 | Py_DECREF(py_code); |
| 5737 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5738 | "isn't a 2-tuple of strings", code); |
| 5739 | return -1; |
| 5740 | } |
| 5741 | /* Load the object. */ |
| 5742 | obj = find_class(self, module_name, class_name); |
| 5743 | if (obj == NULL) { |
| 5744 | Py_DECREF(py_code); |
| 5745 | return -1; |
| 5746 | } |
| 5747 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5748 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5749 | Py_DECREF(py_code); |
| 5750 | if (code < 0) { |
| 5751 | Py_DECREF(obj); |
| 5752 | return -1; |
| 5753 | } |
| 5754 | PDATA_PUSH(self->stack, obj, -1); |
| 5755 | return 0; |
| 5756 | } |
| 5757 | |
| 5758 | static int |
| 5759 | load_put(UnpicklerObject *self) |
| 5760 | { |
| 5761 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5762 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5763 | Py_ssize_t len; |
| 5764 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5765 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5766 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5767 | return -1; |
| 5768 | if (len < 2) |
| 5769 | return bad_readline(); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5770 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5771 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5772 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5773 | |
| 5774 | key = PyLong_FromString(s, NULL, 10); |
| 5775 | if (key == NULL) |
| 5776 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5777 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5778 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5779 | if (idx < 0) { |
| 5780 | if (!PyErr_Occurred()) |
| 5781 | PyErr_SetString(PyExc_ValueError, |
| 5782 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5783 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5784 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5785 | |
| 5786 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5787 | } |
| 5788 | |
| 5789 | static int |
| 5790 | load_binput(UnpicklerObject *self) |
| 5791 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5792 | PyObject *value; |
| 5793 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5794 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5795 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5796 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5797 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5798 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5799 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5800 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5801 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5802 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5803 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5804 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5805 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5806 | } |
| 5807 | |
| 5808 | static int |
| 5809 | load_long_binput(UnpicklerObject *self) |
| 5810 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5811 | PyObject *value; |
| 5812 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5813 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5814 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5815 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5816 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5817 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5818 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5819 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5820 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5821 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5822 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5823 | if (idx < 0) { |
| 5824 | PyErr_SetString(PyExc_ValueError, |
| 5825 | "negative LONG_BINPUT argument"); |
| 5826 | return -1; |
| 5827 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5828 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5829 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5833 | load_memoize(UnpicklerObject *self) |
| 5834 | { |
| 5835 | PyObject *value; |
| 5836 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5837 | if (Py_SIZE(self->stack) <= self->stack->fence) |
| 5838 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5839 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5840 | |
| 5841 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5842 | } |
| 5843 | |
| 5844 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5845 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5846 | { |
| 5847 | PyObject *value; |
| 5848 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5849 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5850 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5851 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5852 | if (x > len || x <= self->stack->fence) |
| 5853 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5854 | if (len == x) /* nothing to do */ |
| 5855 | return 0; |
| 5856 | |
| 5857 | list = self->stack->data[x - 1]; |
| 5858 | |
| 5859 | if (PyList_Check(list)) { |
| 5860 | PyObject *slice; |
| 5861 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5862 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5863 | |
| 5864 | slice = Pdata_poplist(self->stack, x); |
| 5865 | if (!slice) |
| 5866 | return -1; |
| 5867 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5868 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5869 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5870 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5871 | } |
| 5872 | else { |
| 5873 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5874 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5875 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5876 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5877 | if (append_func == NULL) |
| 5878 | return -1; |
| 5879 | for (i = x; i < len; i++) { |
| 5880 | PyObject *result; |
| 5881 | |
| 5882 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5883 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5884 | if (result == NULL) { |
| 5885 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5886 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5887 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5888 | return -1; |
| 5889 | } |
| 5890 | Py_DECREF(result); |
| 5891 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5892 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5893 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | return 0; |
| 5897 | } |
| 5898 | |
| 5899 | static int |
| 5900 | load_append(UnpicklerObject *self) |
| 5901 | { |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5902 | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
| 5903 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5904 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5905 | } |
| 5906 | |
| 5907 | static int |
| 5908 | load_appends(UnpicklerObject *self) |
| 5909 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5910 | Py_ssize_t i = marker(self); |
| 5911 | if (i < 0) |
| 5912 | return -1; |
| 5913 | return do_append(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5914 | } |
| 5915 | |
| 5916 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5917 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5918 | { |
| 5919 | PyObject *value, *key; |
| 5920 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5921 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5922 | int status = 0; |
| 5923 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5924 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5925 | if (x > len || x <= self->stack->fence) |
| 5926 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5927 | if (len == x) /* nothing to do */ |
| 5928 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5929 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5930 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5931 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5932 | PyErr_SetString(st->UnpicklingError, |
| 5933 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5934 | return -1; |
| 5935 | } |
| 5936 | |
| 5937 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5938 | that supports the __setitem__ attribute. */ |
| 5939 | dict = self->stack->data[x - 1]; |
| 5940 | |
| 5941 | for (i = x + 1; i < len; i += 2) { |
| 5942 | key = self->stack->data[i - 1]; |
| 5943 | value = self->stack->data[i]; |
| 5944 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5945 | status = -1; |
| 5946 | break; |
| 5947 | } |
| 5948 | } |
| 5949 | |
| 5950 | Pdata_clear(self->stack, x); |
| 5951 | return status; |
| 5952 | } |
| 5953 | |
| 5954 | static int |
| 5955 | load_setitem(UnpicklerObject *self) |
| 5956 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5957 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5958 | } |
| 5959 | |
| 5960 | static int |
| 5961 | load_setitems(UnpicklerObject *self) |
| 5962 | { |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5963 | Py_ssize_t i = marker(self); |
| 5964 | if (i < 0) |
| 5965 | return -1; |
| 5966 | return do_setitems(self, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5967 | } |
| 5968 | |
| 5969 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5970 | load_additems(UnpicklerObject *self) |
| 5971 | { |
| 5972 | PyObject *set; |
| 5973 | Py_ssize_t mark, len, i; |
| 5974 | |
| 5975 | mark = marker(self); |
Serhiy Storchaka | e9b3074 | 2015-11-23 15:17:43 +0200 | [diff] [blame] | 5976 | if (mark < 0) |
| 5977 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5978 | len = Py_SIZE(self->stack); |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 5979 | if (mark > len || mark <= self->stack->fence) |
| 5980 | return Pdata_stack_underflow(self->stack); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5981 | if (len == mark) /* nothing to do */ |
| 5982 | return 0; |
| 5983 | |
| 5984 | set = self->stack->data[mark - 1]; |
| 5985 | |
| 5986 | if (PySet_Check(set)) { |
| 5987 | PyObject *items; |
| 5988 | int status; |
| 5989 | |
| 5990 | items = Pdata_poptuple(self->stack, mark); |
| 5991 | if (items == NULL) |
| 5992 | return -1; |
| 5993 | |
| 5994 | status = _PySet_Update(set, items); |
| 5995 | Py_DECREF(items); |
| 5996 | return status; |
| 5997 | } |
| 5998 | else { |
| 5999 | PyObject *add_func; |
| 6000 | _Py_IDENTIFIER(add); |
| 6001 | |
| 6002 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 6003 | if (add_func == NULL) |
| 6004 | return -1; |
| 6005 | for (i = mark; i < len; i++) { |
| 6006 | PyObject *result; |
| 6007 | PyObject *item; |
| 6008 | |
| 6009 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6010 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6011 | if (result == NULL) { |
| 6012 | Pdata_clear(self->stack, i + 1); |
| 6013 | Py_SIZE(self->stack) = mark; |
| 6014 | return -1; |
| 6015 | } |
| 6016 | Py_DECREF(result); |
| 6017 | } |
| 6018 | Py_SIZE(self->stack) = mark; |
| 6019 | } |
| 6020 | |
| 6021 | return 0; |
| 6022 | } |
| 6023 | |
| 6024 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6025 | load_build(UnpicklerObject *self) |
| 6026 | { |
| 6027 | PyObject *state, *inst, *slotstate; |
| 6028 | PyObject *setstate; |
| 6029 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6030 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6031 | |
| 6032 | /* Stack is ... instance, state. We want to leave instance at |
| 6033 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 6034 | */ |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6035 | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
| 6036 | return Pdata_stack_underflow(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6037 | |
| 6038 | PDATA_POP(self->stack, state); |
| 6039 | if (state == NULL) |
| 6040 | return -1; |
| 6041 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6042 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6043 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6044 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 6045 | if (setstate == NULL) { |
| 6046 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 6047 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 6048 | else { |
| 6049 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 6050 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 6051 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6052 | } |
| 6053 | else { |
| 6054 | PyObject *result; |
| 6055 | |
| 6056 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6057 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6058 | Py_DECREF(setstate); |
| 6059 | if (result == NULL) |
| 6060 | return -1; |
| 6061 | Py_DECREF(result); |
| 6062 | return 0; |
| 6063 | } |
| 6064 | |
| 6065 | /* A default __setstate__. First see whether state embeds a |
| 6066 | * slot state dict too (a proto 2 addition). |
| 6067 | */ |
| 6068 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 6069 | PyObject *tmp = state; |
| 6070 | |
| 6071 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6072 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6073 | Py_INCREF(state); |
| 6074 | Py_INCREF(slotstate); |
| 6075 | Py_DECREF(tmp); |
| 6076 | } |
| 6077 | else |
| 6078 | slotstate = NULL; |
| 6079 | |
| 6080 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6081 | if (state != Py_None) { |
| 6082 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6083 | PyObject *d_key, *d_value; |
| 6084 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6085 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6086 | |
| 6087 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6088 | PickleState *st = _Pickle_GetGlobalState(); |
| 6089 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6090 | goto error; |
| 6091 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6092 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6093 | if (dict == NULL) |
| 6094 | goto error; |
| 6095 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6096 | i = 0; |
| 6097 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6098 | /* normally the keys for instance attributes are |
| 6099 | interned. we should try to do that here. */ |
| 6100 | Py_INCREF(d_key); |
| 6101 | if (PyUnicode_CheckExact(d_key)) |
| 6102 | PyUnicode_InternInPlace(&d_key); |
| 6103 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6104 | Py_DECREF(d_key); |
| 6105 | goto error; |
| 6106 | } |
| 6107 | Py_DECREF(d_key); |
| 6108 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6109 | Py_DECREF(dict); |
| 6110 | } |
| 6111 | |
| 6112 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6113 | if (slotstate != NULL) { |
| 6114 | PyObject *d_key, *d_value; |
| 6115 | Py_ssize_t i; |
| 6116 | |
| 6117 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6118 | PickleState *st = _Pickle_GetGlobalState(); |
| 6119 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6120 | "slot state is not a dictionary"); |
| 6121 | goto error; |
| 6122 | } |
| 6123 | i = 0; |
| 6124 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6125 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6126 | goto error; |
| 6127 | } |
| 6128 | } |
| 6129 | |
| 6130 | if (0) { |
| 6131 | error: |
| 6132 | status = -1; |
| 6133 | } |
| 6134 | |
| 6135 | Py_DECREF(state); |
| 6136 | Py_XDECREF(slotstate); |
| 6137 | return status; |
| 6138 | } |
| 6139 | |
| 6140 | static int |
| 6141 | load_mark(UnpicklerObject *self) |
| 6142 | { |
| 6143 | |
| 6144 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6145 | * object stack and a mark stack. Here we push a mark onto the |
| 6146 | * mark stack. |
| 6147 | */ |
| 6148 | |
| 6149 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6150 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6151 | |
| 6152 | /* Use the size_t type to check for overflow. */ |
| 6153 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6154 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6155 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6156 | PyErr_NoMemory(); |
| 6157 | return -1; |
| 6158 | } |
| 6159 | |
| 6160 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6161 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6162 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6163 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6164 | if (self->marks == NULL) { |
| 6165 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6166 | PyErr_NoMemory(); |
| 6167 | return -1; |
| 6168 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6169 | self->marks_size = (Py_ssize_t)alloc; |
| 6170 | } |
| 6171 | |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6172 | self->stack->mark_set = 1; |
| 6173 | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6174 | |
| 6175 | return 0; |
| 6176 | } |
| 6177 | |
| 6178 | static int |
| 6179 | load_reduce(UnpicklerObject *self) |
| 6180 | { |
| 6181 | PyObject *callable = NULL; |
| 6182 | PyObject *argtup = NULL; |
| 6183 | PyObject *obj = NULL; |
| 6184 | |
| 6185 | PDATA_POP(self->stack, argtup); |
| 6186 | if (argtup == NULL) |
| 6187 | return -1; |
| 6188 | PDATA_POP(self->stack, callable); |
| 6189 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6190 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6191 | Py_DECREF(callable); |
| 6192 | } |
| 6193 | Py_DECREF(argtup); |
| 6194 | |
| 6195 | if (obj == NULL) |
| 6196 | return -1; |
| 6197 | |
| 6198 | PDATA_PUSH(self->stack, obj, -1); |
| 6199 | return 0; |
| 6200 | } |
| 6201 | |
| 6202 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6203 | * is the first opcode for protocols >= 2. |
| 6204 | */ |
| 6205 | static int |
| 6206 | load_proto(UnpicklerObject *self) |
| 6207 | { |
| 6208 | char *s; |
| 6209 | int i; |
| 6210 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6211 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6212 | return -1; |
| 6213 | |
| 6214 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6215 | if (i <= HIGHEST_PROTOCOL) { |
| 6216 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6217 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6218 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6219 | |
| 6220 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6221 | return -1; |
| 6222 | } |
| 6223 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6224 | static int |
| 6225 | load_frame(UnpicklerObject *self) |
| 6226 | { |
| 6227 | char *s; |
| 6228 | Py_ssize_t frame_len; |
| 6229 | |
| 6230 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6231 | return -1; |
| 6232 | |
| 6233 | frame_len = calc_binsize(s, 8); |
| 6234 | if (frame_len < 0) { |
| 6235 | PyErr_Format(PyExc_OverflowError, |
| 6236 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6237 | PY_SSIZE_T_MAX); |
| 6238 | return -1; |
| 6239 | } |
| 6240 | |
| 6241 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6242 | return -1; |
| 6243 | |
| 6244 | /* Rewind to start of frame */ |
| 6245 | self->next_read_idx -= frame_len; |
| 6246 | return 0; |
| 6247 | } |
| 6248 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6249 | static PyObject * |
| 6250 | load(UnpicklerObject *self) |
| 6251 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6252 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6253 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6254 | |
| 6255 | self->num_marks = 0; |
Serhiy Storchaka | 59fb634 | 2015-12-06 22:01:35 +0200 | [diff] [blame] | 6256 | self->stack->mark_set = 0; |
| 6257 | self->stack->fence = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6258 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6259 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6260 | Pdata_clear(self->stack, 0); |
| 6261 | |
| 6262 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6263 | #define OP(opcode, load_func) \ |
| 6264 | case opcode: if (load_func(self) < 0) break; continue; |
| 6265 | |
| 6266 | #define OP_ARG(opcode, load_func, arg) \ |
| 6267 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6268 | |
| 6269 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6270 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6271 | break; |
| 6272 | |
| 6273 | switch ((enum opcode)s[0]) { |
| 6274 | OP(NONE, load_none) |
| 6275 | OP(BININT, load_binint) |
| 6276 | OP(BININT1, load_binint1) |
| 6277 | OP(BININT2, load_binint2) |
| 6278 | OP(INT, load_int) |
| 6279 | OP(LONG, load_long) |
| 6280 | OP_ARG(LONG1, load_counted_long, 1) |
| 6281 | OP_ARG(LONG4, load_counted_long, 4) |
| 6282 | OP(FLOAT, load_float) |
| 6283 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6284 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6285 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6286 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6287 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6288 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6289 | OP(STRING, load_string) |
| 6290 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6291 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6292 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6293 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6294 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6295 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6296 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6297 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6298 | OP(TUPLE, load_tuple) |
| 6299 | OP(EMPTY_LIST, load_empty_list) |
| 6300 | OP(LIST, load_list) |
| 6301 | OP(EMPTY_DICT, load_empty_dict) |
| 6302 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6303 | OP(EMPTY_SET, load_empty_set) |
| 6304 | OP(ADDITEMS, load_additems) |
| 6305 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6306 | OP(OBJ, load_obj) |
| 6307 | OP(INST, load_inst) |
| 6308 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6309 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6310 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6311 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6312 | OP(APPEND, load_append) |
| 6313 | OP(APPENDS, load_appends) |
| 6314 | OP(BUILD, load_build) |
| 6315 | OP(DUP, load_dup) |
| 6316 | OP(BINGET, load_binget) |
| 6317 | OP(LONG_BINGET, load_long_binget) |
| 6318 | OP(GET, load_get) |
| 6319 | OP(MARK, load_mark) |
| 6320 | OP(BINPUT, load_binput) |
| 6321 | OP(LONG_BINPUT, load_long_binput) |
| 6322 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6323 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6324 | OP(POP, load_pop) |
| 6325 | OP(POP_MARK, load_pop_mark) |
| 6326 | OP(SETITEM, load_setitem) |
| 6327 | OP(SETITEMS, load_setitems) |
| 6328 | OP(PERSID, load_persid) |
| 6329 | OP(BINPERSID, load_binpersid) |
| 6330 | OP(REDUCE, load_reduce) |
| 6331 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6332 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6333 | OP_ARG(EXT1, load_extension, 1) |
| 6334 | OP_ARG(EXT2, load_extension, 2) |
| 6335 | OP_ARG(EXT4, load_extension, 4) |
| 6336 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6337 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6338 | |
| 6339 | case STOP: |
| 6340 | break; |
| 6341 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6342 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6343 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6344 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6345 | } |
| 6346 | else { |
| 6347 | PickleState *st = _Pickle_GetGlobalState(); |
| 6348 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6349 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6350 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6351 | return NULL; |
| 6352 | } |
| 6353 | |
| 6354 | break; /* and we are done! */ |
| 6355 | } |
| 6356 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6357 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6358 | return NULL; |
| 6359 | } |
| 6360 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6361 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6362 | return NULL; |
| 6363 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6364 | PDATA_POP(self->stack, value); |
| 6365 | return value; |
| 6366 | } |
| 6367 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6368 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6369 | |
| 6370 | _pickle.Unpickler.load |
| 6371 | |
| 6372 | Load a pickle. |
| 6373 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6374 | Read a pickled object representation from the open file object given |
| 6375 | in the constructor, and return the reconstituted object hierarchy |
| 6376 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6377 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6378 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6379 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6380 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6381 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6382 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6383 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6384 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6385 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6386 | segfaulting if a subclass overridden __init__ with a function that does |
| 6387 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6388 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6389 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6390 | PickleState *st = _Pickle_GetGlobalState(); |
| 6391 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6392 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6393 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6394 | return NULL; |
| 6395 | } |
| 6396 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6397 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6398 | } |
| 6399 | |
| 6400 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6401 | function is used for loading any global (i.e., functions), not just |
| 6402 | classes. The name is kept only for backward compatibility. */ |
| 6403 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6404 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6405 | |
| 6406 | _pickle.Unpickler.find_class |
| 6407 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6408 | module_name: object |
| 6409 | global_name: object |
| 6410 | / |
| 6411 | |
| 6412 | Return an object from a specified module. |
| 6413 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6414 | If necessary, the module will be imported. Subclasses may override |
| 6415 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6416 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6417 | |
| 6418 | This method is called whenever a class or a function object is |
| 6419 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6420 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6421 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6422 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6423 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6424 | PyObject *module_name, |
| 6425 | PyObject *global_name) |
| 6426 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6427 | { |
| 6428 | PyObject *global; |
| 6429 | PyObject *modules_dict; |
| 6430 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6431 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6432 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6433 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6434 | Python 3.x. We do this only with old pickle protocols and when the |
| 6435 | user has not disabled the feature. */ |
| 6436 | if (self->proto < 3 && self->fix_imports) { |
| 6437 | PyObject *key; |
| 6438 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6439 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6440 | |
| 6441 | /* Check if the global (i.e., a function or a class) was renamed |
| 6442 | or moved to another module. */ |
| 6443 | key = PyTuple_Pack(2, module_name, global_name); |
| 6444 | if (key == NULL) |
| 6445 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6446 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6447 | Py_DECREF(key); |
| 6448 | if (item) { |
| 6449 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6450 | PyErr_Format(PyExc_RuntimeError, |
| 6451 | "_compat_pickle.NAME_MAPPING values should be " |
| 6452 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6453 | return NULL; |
| 6454 | } |
| 6455 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6456 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6457 | if (!PyUnicode_Check(module_name) || |
| 6458 | !PyUnicode_Check(global_name)) { |
| 6459 | PyErr_Format(PyExc_RuntimeError, |
| 6460 | "_compat_pickle.NAME_MAPPING values should be " |
| 6461 | "pairs of str, not (%.200s, %.200s)", |
| 6462 | Py_TYPE(module_name)->tp_name, |
| 6463 | Py_TYPE(global_name)->tp_name); |
| 6464 | return NULL; |
| 6465 | } |
| 6466 | } |
| 6467 | else if (PyErr_Occurred()) { |
| 6468 | return NULL; |
| 6469 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6470 | else { |
| 6471 | /* Check if the module was renamed. */ |
| 6472 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6473 | if (item) { |
| 6474 | if (!PyUnicode_Check(item)) { |
| 6475 | PyErr_Format(PyExc_RuntimeError, |
| 6476 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6477 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6478 | return NULL; |
| 6479 | } |
| 6480 | module_name = item; |
| 6481 | } |
| 6482 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6483 | return NULL; |
| 6484 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6485 | } |
| 6486 | } |
| 6487 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6488 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6489 | if (modules_dict == NULL) { |
| 6490 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6491 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6492 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6493 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6494 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6495 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6496 | if (PyErr_Occurred()) |
| 6497 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6498 | module = PyImport_Import(module_name); |
| 6499 | if (module == NULL) |
| 6500 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6501 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6502 | Py_DECREF(module); |
| 6503 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6504 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6505 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6506 | } |
| 6507 | return global; |
| 6508 | } |
| 6509 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6510 | /*[clinic input] |
| 6511 | |
| 6512 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6513 | |
| 6514 | Returns size in memory, in bytes. |
| 6515 | [clinic start generated code]*/ |
| 6516 | |
| 6517 | static Py_ssize_t |
| 6518 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6519 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6520 | { |
| 6521 | Py_ssize_t res; |
| 6522 | |
Serhiy Storchaka | 5c4064e | 2015-12-19 20:05:25 +0200 | [diff] [blame] | 6523 | res = _PyObject_SIZE(Py_TYPE(self)); |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6524 | if (self->memo != NULL) |
| 6525 | res += self->memo_size * sizeof(PyObject *); |
| 6526 | if (self->marks != NULL) |
| 6527 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6528 | if (self->input_line != NULL) |
| 6529 | res += strlen(self->input_line) + 1; |
| 6530 | if (self->encoding != NULL) |
| 6531 | res += strlen(self->encoding) + 1; |
| 6532 | if (self->errors != NULL) |
| 6533 | res += strlen(self->errors) + 1; |
| 6534 | return res; |
| 6535 | } |
| 6536 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6537 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6538 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6539 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6540 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6541 | {NULL, NULL} /* sentinel */ |
| 6542 | }; |
| 6543 | |
| 6544 | static void |
| 6545 | Unpickler_dealloc(UnpicklerObject *self) |
| 6546 | { |
| 6547 | PyObject_GC_UnTrack((PyObject *)self); |
| 6548 | Py_XDECREF(self->readline); |
| 6549 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6550 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6551 | Py_XDECREF(self->stack); |
| 6552 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6553 | if (self->buffer.buf != NULL) { |
| 6554 | PyBuffer_Release(&self->buffer); |
| 6555 | self->buffer.buf = NULL; |
| 6556 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6557 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6558 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6559 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6560 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6561 | PyMem_Free(self->encoding); |
| 6562 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6563 | |
| 6564 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6565 | } |
| 6566 | |
| 6567 | static int |
| 6568 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6569 | { |
| 6570 | Py_VISIT(self->readline); |
| 6571 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6572 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6573 | Py_VISIT(self->stack); |
| 6574 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6575 | return 0; |
| 6576 | } |
| 6577 | |
| 6578 | static int |
| 6579 | Unpickler_clear(UnpicklerObject *self) |
| 6580 | { |
| 6581 | Py_CLEAR(self->readline); |
| 6582 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6583 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6584 | Py_CLEAR(self->stack); |
| 6585 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6586 | if (self->buffer.buf != NULL) { |
| 6587 | PyBuffer_Release(&self->buffer); |
| 6588 | self->buffer.buf = NULL; |
| 6589 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6590 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6591 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6592 | PyMem_Free(self->marks); |
| 6593 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6594 | PyMem_Free(self->input_line); |
| 6595 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6596 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6597 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6598 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6599 | self->errors = NULL; |
| 6600 | |
| 6601 | return 0; |
| 6602 | } |
| 6603 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6604 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6605 | |
| 6606 | _pickle.Unpickler.__init__ |
| 6607 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6608 | file: object |
| 6609 | * |
| 6610 | fix_imports: bool = True |
| 6611 | encoding: str = 'ASCII' |
| 6612 | errors: str = 'strict' |
| 6613 | |
| 6614 | This takes a binary file for reading a pickle data stream. |
| 6615 | |
| 6616 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6617 | protocol argument is needed. Bytes past the pickled object's |
| 6618 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6619 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6620 | The argument *file* must have two methods, a read() method that takes |
| 6621 | an integer argument, and a readline() method that requires no |
| 6622 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6623 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6624 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6625 | |
| 6626 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6627 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6628 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6629 | 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] | 6630 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6631 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6632 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6633 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6634 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6635 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6636 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6637 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6638 | int fix_imports, const char *encoding, |
| 6639 | const char *errors) |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 6640 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6641 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6642 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6643 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6644 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6645 | if (self->read != NULL) |
| 6646 | (void)Unpickler_clear(self); |
| 6647 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6648 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6649 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6650 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6651 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6652 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6653 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6654 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6655 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6656 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6657 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6658 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6659 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6660 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6661 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6662 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6663 | } |
| 6664 | else { |
| 6665 | self->pers_func = NULL; |
| 6666 | } |
| 6667 | |
| 6668 | self->stack = (Pdata *)Pdata_New(); |
| 6669 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6670 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6671 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6672 | self->memo_size = 32; |
| 6673 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6674 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6675 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6676 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6677 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6678 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6679 | return 0; |
| 6680 | } |
| 6681 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6682 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6683 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6684 | * avoid breaking code like: |
| 6685 | * unpickler.memo.clear() |
| 6686 | * and |
| 6687 | * unpickler.memo = saved_memo |
| 6688 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6689 | * it. Note that we don't implement the entire mapping API here. This is |
| 6690 | * intentional, as these should be treated as black-box implementation details. |
| 6691 | * |
| 6692 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6693 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6694 | */ |
| 6695 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6696 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6697 | _pickle.UnpicklerMemoProxy.clear |
| 6698 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6699 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6700 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6701 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6702 | static PyObject * |
| 6703 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6704 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6705 | { |
| 6706 | _Unpickler_MemoCleanup(self->unpickler); |
| 6707 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6708 | if (self->unpickler->memo == NULL) |
| 6709 | return NULL; |
| 6710 | Py_RETURN_NONE; |
| 6711 | } |
| 6712 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6713 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6714 | _pickle.UnpicklerMemoProxy.copy |
| 6715 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6716 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6717 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6718 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6719 | static PyObject * |
| 6720 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6721 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6722 | { |
| 6723 | Py_ssize_t i; |
| 6724 | PyObject *new_memo = PyDict_New(); |
| 6725 | if (new_memo == NULL) |
| 6726 | return NULL; |
| 6727 | |
| 6728 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6729 | int status; |
| 6730 | PyObject *key, *value; |
| 6731 | |
| 6732 | value = self->unpickler->memo[i]; |
| 6733 | if (value == NULL) |
| 6734 | continue; |
| 6735 | |
| 6736 | key = PyLong_FromSsize_t(i); |
| 6737 | if (key == NULL) |
| 6738 | goto error; |
| 6739 | status = PyDict_SetItem(new_memo, key, value); |
| 6740 | Py_DECREF(key); |
| 6741 | if (status < 0) |
| 6742 | goto error; |
| 6743 | } |
| 6744 | return new_memo; |
| 6745 | |
| 6746 | error: |
| 6747 | Py_DECREF(new_memo); |
| 6748 | return NULL; |
| 6749 | } |
| 6750 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6751 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6752 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6753 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6754 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6755 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6756 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6757 | static PyObject * |
| 6758 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6759 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6760 | { |
| 6761 | PyObject *reduce_value; |
| 6762 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6763 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6764 | if (contents == NULL) |
| 6765 | return NULL; |
| 6766 | |
| 6767 | reduce_value = PyTuple_New(2); |
| 6768 | if (reduce_value == NULL) { |
| 6769 | Py_DECREF(contents); |
| 6770 | return NULL; |
| 6771 | } |
| 6772 | constructor_args = PyTuple_New(1); |
| 6773 | if (constructor_args == NULL) { |
| 6774 | Py_DECREF(contents); |
| 6775 | Py_DECREF(reduce_value); |
| 6776 | return NULL; |
| 6777 | } |
| 6778 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6779 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6780 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6781 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6782 | return reduce_value; |
| 6783 | } |
| 6784 | |
| 6785 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6786 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6787 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6788 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6789 | {NULL, NULL} /* sentinel */ |
| 6790 | }; |
| 6791 | |
| 6792 | static void |
| 6793 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6794 | { |
| 6795 | PyObject_GC_UnTrack(self); |
| 6796 | Py_XDECREF(self->unpickler); |
| 6797 | PyObject_GC_Del((PyObject *)self); |
| 6798 | } |
| 6799 | |
| 6800 | static int |
| 6801 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6802 | visitproc visit, void *arg) |
| 6803 | { |
| 6804 | Py_VISIT(self->unpickler); |
| 6805 | return 0; |
| 6806 | } |
| 6807 | |
| 6808 | static int |
| 6809 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6810 | { |
| 6811 | Py_CLEAR(self->unpickler); |
| 6812 | return 0; |
| 6813 | } |
| 6814 | |
| 6815 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6816 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6817 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6818 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6819 | 0, |
| 6820 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6821 | 0, /* tp_print */ |
| 6822 | 0, /* tp_getattr */ |
| 6823 | 0, /* tp_setattr */ |
| 6824 | 0, /* tp_compare */ |
| 6825 | 0, /* tp_repr */ |
| 6826 | 0, /* tp_as_number */ |
| 6827 | 0, /* tp_as_sequence */ |
| 6828 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6829 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6830 | 0, /* tp_call */ |
| 6831 | 0, /* tp_str */ |
| 6832 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6833 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6834 | 0, /* tp_as_buffer */ |
| 6835 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6836 | 0, /* tp_doc */ |
| 6837 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6838 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6839 | 0, /* tp_richcompare */ |
| 6840 | 0, /* tp_weaklistoffset */ |
| 6841 | 0, /* tp_iter */ |
| 6842 | 0, /* tp_iternext */ |
| 6843 | unpicklerproxy_methods, /* tp_methods */ |
| 6844 | }; |
| 6845 | |
| 6846 | static PyObject * |
| 6847 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6848 | { |
| 6849 | UnpicklerMemoProxyObject *self; |
| 6850 | |
| 6851 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6852 | &UnpicklerMemoProxyType); |
| 6853 | if (self == NULL) |
| 6854 | return NULL; |
| 6855 | Py_INCREF(unpickler); |
| 6856 | self->unpickler = unpickler; |
| 6857 | PyObject_GC_Track(self); |
| 6858 | return (PyObject *)self; |
| 6859 | } |
| 6860 | |
| 6861 | /*****************************************************************************/ |
| 6862 | |
| 6863 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6864 | static PyObject * |
| 6865 | Unpickler_get_memo(UnpicklerObject *self) |
| 6866 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6867 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6871 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6872 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6873 | PyObject **new_memo; |
| 6874 | Py_ssize_t new_memo_size = 0; |
| 6875 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6876 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6877 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6878 | PyErr_SetString(PyExc_TypeError, |
| 6879 | "attribute deletion is not supported"); |
| 6880 | return -1; |
| 6881 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6882 | |
| 6883 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6884 | UnpicklerObject *unpickler = |
| 6885 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6886 | |
| 6887 | new_memo_size = unpickler->memo_size; |
| 6888 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6889 | if (new_memo == NULL) |
| 6890 | return -1; |
| 6891 | |
| 6892 | for (i = 0; i < new_memo_size; i++) { |
| 6893 | Py_XINCREF(unpickler->memo[i]); |
| 6894 | new_memo[i] = unpickler->memo[i]; |
| 6895 | } |
| 6896 | } |
| 6897 | else if (PyDict_Check(obj)) { |
| 6898 | Py_ssize_t i = 0; |
| 6899 | PyObject *key, *value; |
| 6900 | |
| 6901 | new_memo_size = PyDict_Size(obj); |
| 6902 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6903 | if (new_memo == NULL) |
| 6904 | return -1; |
| 6905 | |
| 6906 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6907 | Py_ssize_t idx; |
| 6908 | if (!PyLong_Check(key)) { |
| 6909 | PyErr_SetString(PyExc_TypeError, |
| 6910 | "memo key must be integers"); |
| 6911 | goto error; |
| 6912 | } |
| 6913 | idx = PyLong_AsSsize_t(key); |
| 6914 | if (idx == -1 && PyErr_Occurred()) |
| 6915 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6916 | if (idx < 0) { |
| 6917 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6918 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6919 | goto error; |
| 6920 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6921 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6922 | goto error; |
| 6923 | } |
| 6924 | } |
| 6925 | else { |
| 6926 | PyErr_Format(PyExc_TypeError, |
| 6927 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6928 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6929 | return -1; |
| 6930 | } |
| 6931 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6932 | _Unpickler_MemoCleanup(self); |
| 6933 | self->memo_size = new_memo_size; |
| 6934 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6935 | |
| 6936 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6937 | |
| 6938 | error: |
| 6939 | if (new_memo_size) { |
| 6940 | i = new_memo_size; |
| 6941 | while (--i >= 0) { |
| 6942 | Py_XDECREF(new_memo[i]); |
| 6943 | } |
| 6944 | PyMem_FREE(new_memo); |
| 6945 | } |
| 6946 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6947 | } |
| 6948 | |
| 6949 | static PyObject * |
| 6950 | Unpickler_get_persload(UnpicklerObject *self) |
| 6951 | { |
| 6952 | if (self->pers_func == NULL) |
| 6953 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6954 | else |
| 6955 | Py_INCREF(self->pers_func); |
| 6956 | return self->pers_func; |
| 6957 | } |
| 6958 | |
| 6959 | static int |
| 6960 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6961 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6962 | if (value == NULL) { |
| 6963 | PyErr_SetString(PyExc_TypeError, |
| 6964 | "attribute deletion is not supported"); |
| 6965 | return -1; |
| 6966 | } |
| 6967 | if (!PyCallable_Check(value)) { |
| 6968 | PyErr_SetString(PyExc_TypeError, |
| 6969 | "persistent_load must be a callable taking " |
| 6970 | "one argument"); |
| 6971 | return -1; |
| 6972 | } |
| 6973 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6974 | Py_INCREF(value); |
Serhiy Storchaka | ec39756 | 2016-04-06 09:50:03 +0300 | [diff] [blame] | 6975 | Py_XSETREF(self->pers_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6976 | |
| 6977 | return 0; |
| 6978 | } |
| 6979 | |
| 6980 | static PyGetSetDef Unpickler_getsets[] = { |
| 6981 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6982 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6983 | (setter)Unpickler_set_persload}, |
| 6984 | {NULL} |
| 6985 | }; |
| 6986 | |
| 6987 | static PyTypeObject Unpickler_Type = { |
| 6988 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6989 | "_pickle.Unpickler", /*tp_name*/ |
| 6990 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6991 | 0, /*tp_itemsize*/ |
| 6992 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6993 | 0, /*tp_print*/ |
| 6994 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6995 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6996 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6997 | 0, /*tp_repr*/ |
| 6998 | 0, /*tp_as_number*/ |
| 6999 | 0, /*tp_as_sequence*/ |
| 7000 | 0, /*tp_as_mapping*/ |
| 7001 | 0, /*tp_hash*/ |
| 7002 | 0, /*tp_call*/ |
| 7003 | 0, /*tp_str*/ |
| 7004 | 0, /*tp_getattro*/ |
| 7005 | 0, /*tp_setattro*/ |
| 7006 | 0, /*tp_as_buffer*/ |
| 7007 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7008 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7009 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 7010 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 7011 | 0, /*tp_richcompare*/ |
| 7012 | 0, /*tp_weaklistoffset*/ |
| 7013 | 0, /*tp_iter*/ |
| 7014 | 0, /*tp_iternext*/ |
| 7015 | Unpickler_methods, /*tp_methods*/ |
| 7016 | 0, /*tp_members*/ |
| 7017 | Unpickler_getsets, /*tp_getset*/ |
| 7018 | 0, /*tp_base*/ |
| 7019 | 0, /*tp_dict*/ |
| 7020 | 0, /*tp_descr_get*/ |
| 7021 | 0, /*tp_descr_set*/ |
| 7022 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 7023 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7024 | PyType_GenericAlloc, /*tp_alloc*/ |
| 7025 | PyType_GenericNew, /*tp_new*/ |
| 7026 | PyObject_GC_Del, /*tp_free*/ |
| 7027 | 0, /*tp_is_gc*/ |
| 7028 | }; |
| 7029 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7030 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7031 | |
| 7032 | _pickle.dump |
| 7033 | |
| 7034 | obj: object |
| 7035 | file: object |
| 7036 | protocol: object = NULL |
| 7037 | * |
| 7038 | fix_imports: bool = True |
| 7039 | |
| 7040 | Write a pickled representation of obj to the open file object file. |
| 7041 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7042 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7043 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7044 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7045 | The optional *protocol* argument tells the pickler to use the given |
| 7046 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 7047 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7048 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7049 | Specifying a negative protocol version selects the highest protocol |
| 7050 | version supported. The higher the protocol used, the more recent the |
| 7051 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7052 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7053 | The *file* argument must have a write() method that accepts a single |
| 7054 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7055 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7056 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7057 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7058 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7059 | to map the new Python 3 names to the old module names used in Python |
| 7060 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7061 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7062 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7063 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7064 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7065 | PyObject *protocol, int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7066 | /*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7067 | { |
| 7068 | PicklerObject *pickler = _Pickler_New(); |
| 7069 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7070 | if (pickler == NULL) |
| 7071 | return NULL; |
| 7072 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7073 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7074 | goto error; |
| 7075 | |
| 7076 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7077 | goto error; |
| 7078 | |
| 7079 | if (dump(pickler, obj) < 0) |
| 7080 | goto error; |
| 7081 | |
| 7082 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7083 | goto error; |
| 7084 | |
| 7085 | Py_DECREF(pickler); |
| 7086 | Py_RETURN_NONE; |
| 7087 | |
| 7088 | error: |
| 7089 | Py_XDECREF(pickler); |
| 7090 | return NULL; |
| 7091 | } |
| 7092 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7093 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7094 | |
| 7095 | _pickle.dumps |
| 7096 | |
| 7097 | obj: object |
| 7098 | protocol: object = NULL |
| 7099 | * |
| 7100 | fix_imports: bool = True |
| 7101 | |
| 7102 | Return the pickled representation of the object as a bytes object. |
| 7103 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7104 | The optional *protocol* argument tells the pickler to use the given |
| 7105 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7106 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7107 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7108 | Specifying a negative protocol version selects the highest protocol |
| 7109 | version supported. The higher the protocol used, the more recent the |
| 7110 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7111 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7112 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7113 | try to map the new Python 3 names to the old module names used in |
| 7114 | 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] | 7115 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7116 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7117 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7118 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7119 | int fix_imports) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7120 | /*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7121 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7122 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7123 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7124 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7125 | if (pickler == NULL) |
| 7126 | return NULL; |
| 7127 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7128 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7129 | goto error; |
| 7130 | |
| 7131 | if (dump(pickler, obj) < 0) |
| 7132 | goto error; |
| 7133 | |
| 7134 | result = _Pickler_GetString(pickler); |
| 7135 | Py_DECREF(pickler); |
| 7136 | return result; |
| 7137 | |
| 7138 | error: |
| 7139 | Py_XDECREF(pickler); |
| 7140 | return NULL; |
| 7141 | } |
| 7142 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7143 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7144 | |
| 7145 | _pickle.load |
| 7146 | |
| 7147 | file: object |
| 7148 | * |
| 7149 | fix_imports: bool = True |
| 7150 | encoding: str = 'ASCII' |
| 7151 | errors: str = 'strict' |
| 7152 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7153 | 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] | 7154 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7155 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7156 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7157 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7158 | The protocol version of the pickle is detected automatically, so no |
| 7159 | protocol argument is needed. Bytes past the pickled object's |
| 7160 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7161 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7162 | The argument *file* must have two methods, a read() method that takes |
| 7163 | an integer argument, and a readline() method that requires no |
| 7164 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7165 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7166 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7167 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7168 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7169 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7170 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7171 | map the old Python 2 names to the new names used in Python 3. The |
| 7172 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7173 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7174 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7175 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7176 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7177 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7178 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7179 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7180 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7181 | /*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7182 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7183 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7184 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7185 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7186 | if (unpickler == NULL) |
| 7187 | return NULL; |
| 7188 | |
| 7189 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7190 | goto error; |
| 7191 | |
| 7192 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7193 | goto error; |
| 7194 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7195 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7196 | |
| 7197 | result = load(unpickler); |
| 7198 | Py_DECREF(unpickler); |
| 7199 | return result; |
| 7200 | |
| 7201 | error: |
| 7202 | Py_XDECREF(unpickler); |
| 7203 | return NULL; |
| 7204 | } |
| 7205 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7206 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7207 | |
| 7208 | _pickle.loads |
| 7209 | |
| 7210 | data: object |
| 7211 | * |
| 7212 | fix_imports: bool = True |
| 7213 | encoding: str = 'ASCII' |
| 7214 | errors: str = 'strict' |
| 7215 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7216 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7217 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7218 | The protocol version of the pickle is detected automatically, so no |
| 7219 | protocol argument is needed. Bytes past the pickled object's |
| 7220 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7221 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7222 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 7223 | which are used to control compatibility support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7224 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7225 | map the old Python 2 names to the new names used in Python 3. The |
| 7226 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7227 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7228 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7229 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7230 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7231 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7232 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7233 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7234 | const char *encoding, const char *errors) |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 7235 | /*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7236 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7237 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7238 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7239 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7240 | if (unpickler == NULL) |
| 7241 | return NULL; |
| 7242 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7243 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7244 | goto error; |
| 7245 | |
| 7246 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7247 | goto error; |
| 7248 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7249 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7250 | |
| 7251 | result = load(unpickler); |
| 7252 | Py_DECREF(unpickler); |
| 7253 | return result; |
| 7254 | |
| 7255 | error: |
| 7256 | Py_XDECREF(unpickler); |
| 7257 | return NULL; |
| 7258 | } |
| 7259 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7260 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7261 | _PICKLE_DUMP_METHODDEF |
| 7262 | _PICKLE_DUMPS_METHODDEF |
| 7263 | _PICKLE_LOAD_METHODDEF |
| 7264 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7265 | {NULL, NULL} /* sentinel */ |
| 7266 | }; |
| 7267 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7268 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7269 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7270 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7271 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7272 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7273 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7274 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7275 | static void |
| 7276 | pickle_free(PyObject *m) |
| 7277 | { |
| 7278 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7279 | } |
| 7280 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7281 | static int |
| 7282 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7283 | { |
| 7284 | PickleState *st = _Pickle_GetState(m); |
| 7285 | Py_VISIT(st->PickleError); |
| 7286 | Py_VISIT(st->PicklingError); |
| 7287 | Py_VISIT(st->UnpicklingError); |
| 7288 | Py_VISIT(st->dispatch_table); |
| 7289 | Py_VISIT(st->extension_registry); |
| 7290 | Py_VISIT(st->extension_cache); |
| 7291 | Py_VISIT(st->inverted_registry); |
| 7292 | Py_VISIT(st->name_mapping_2to3); |
| 7293 | Py_VISIT(st->import_mapping_2to3); |
| 7294 | Py_VISIT(st->name_mapping_3to2); |
| 7295 | Py_VISIT(st->import_mapping_3to2); |
| 7296 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7297 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7298 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7299 | } |
| 7300 | |
| 7301 | static struct PyModuleDef _picklemodule = { |
| 7302 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7303 | "_pickle", /* m_name */ |
| 7304 | pickle_module_doc, /* m_doc */ |
| 7305 | sizeof(PickleState), /* m_size */ |
| 7306 | pickle_methods, /* m_methods */ |
| 7307 | NULL, /* m_reload */ |
| 7308 | pickle_traverse, /* m_traverse */ |
| 7309 | pickle_clear, /* m_clear */ |
| 7310 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7311 | }; |
| 7312 | |
| 7313 | PyMODINIT_FUNC |
| 7314 | PyInit__pickle(void) |
| 7315 | { |
| 7316 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7317 | PickleState *st; |
| 7318 | |
| 7319 | m = PyState_FindModule(&_picklemodule); |
| 7320 | if (m) { |
| 7321 | Py_INCREF(m); |
| 7322 | return m; |
| 7323 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7324 | |
| 7325 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7326 | return NULL; |
| 7327 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7328 | return NULL; |
| 7329 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7330 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7331 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7332 | return NULL; |
| 7333 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7334 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7335 | |
| 7336 | /* Create the module and add the functions. */ |
| 7337 | m = PyModule_Create(&_picklemodule); |
| 7338 | if (m == NULL) |
| 7339 | return NULL; |
| 7340 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7341 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7342 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7343 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7344 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7345 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7346 | return NULL; |
| 7347 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7348 | st = _Pickle_GetState(m); |
| 7349 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7350 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7351 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7352 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7353 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7354 | st->PicklingError = \ |
| 7355 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7356 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7357 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7358 | st->UnpicklingError = \ |
| 7359 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7360 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7361 | return NULL; |
| 7362 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7363 | Py_INCREF(st->PickleError); |
| 7364 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7365 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7366 | Py_INCREF(st->PicklingError); |
| 7367 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7368 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7369 | Py_INCREF(st->UnpicklingError); |
| 7370 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7371 | return NULL; |
| 7372 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7373 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7374 | return NULL; |
| 7375 | |
| 7376 | return m; |
| 7377 | } |