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 | |
| 373 | static int |
| 374 | stack_underflow(void) |
| 375 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 376 | PickleState *st = _Pickle_GetGlobalState(); |
| 377 | PyErr_SetString(st->UnpicklingError, "unpickling stack underflow"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 378 | return -1; |
| 379 | } |
| 380 | |
| 381 | /* Internal data type used as the unpickling stack. */ |
| 382 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 383 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 384 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 385 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 386 | } Pdata; |
| 387 | |
| 388 | static void |
| 389 | Pdata_dealloc(Pdata *self) |
| 390 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 391 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 392 | while (--i >= 0) { |
| 393 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 394 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 395 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 396 | PyObject_Del(self); |
| 397 | } |
| 398 | |
| 399 | static PyTypeObject Pdata_Type = { |
| 400 | PyVarObject_HEAD_INIT(NULL, 0) |
| 401 | "_pickle.Pdata", /*tp_name*/ |
| 402 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 403 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 404 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 405 | }; |
| 406 | |
| 407 | static PyObject * |
| 408 | Pdata_New(void) |
| 409 | { |
| 410 | Pdata *self; |
| 411 | |
| 412 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 413 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 414 | Py_SIZE(self) = 0; |
| 415 | self->allocated = 8; |
| 416 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 417 | if (self->data) |
| 418 | return (PyObject *)self; |
| 419 | Py_DECREF(self); |
| 420 | return PyErr_NoMemory(); |
| 421 | } |
| 422 | |
| 423 | |
| 424 | /* Retain only the initial clearto items. If clearto >= the current |
| 425 | * number of items, this is a (non-erroneous) NOP. |
| 426 | */ |
| 427 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 428 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 429 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 430 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 431 | |
| 432 | if (clearto < 0) |
| 433 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 434 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 435 | return 0; |
| 436 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 437 | while (--i >= clearto) { |
| 438 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 439 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 440 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 441 | return 0; |
| 442 | } |
| 443 | |
| 444 | static int |
| 445 | Pdata_grow(Pdata *self) |
| 446 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 447 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 448 | size_t allocated = (size_t)self->allocated; |
| 449 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 450 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 451 | new_allocated = (allocated >> 3) + 6; |
| 452 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 453 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 454 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 455 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 456 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 457 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 458 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 459 | |
| 460 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 461 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 462 | return 0; |
| 463 | |
| 464 | nomemory: |
| 465 | PyErr_NoMemory(); |
| 466 | return -1; |
| 467 | } |
| 468 | |
| 469 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 470 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 471 | * is raised and V is set to NULL. |
| 472 | */ |
| 473 | static PyObject * |
| 474 | Pdata_pop(Pdata *self) |
| 475 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 476 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 477 | if (Py_SIZE(self) == 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 478 | PyErr_SetString(st->UnpicklingError, "bad pickle data"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 479 | return NULL; |
| 480 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 481 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 482 | } |
| 483 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 484 | |
| 485 | static int |
| 486 | Pdata_push(Pdata *self, PyObject *obj) |
| 487 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 488 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 489 | return -1; |
| 490 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 491 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 492 | return 0; |
| 493 | } |
| 494 | |
| 495 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 496 | #define PDATA_PUSH(D, O, ER) do { \ |
| 497 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 498 | |
| 499 | /* Push an object on stack, adding a new reference to the object. */ |
| 500 | #define PDATA_APPEND(D, O, ER) do { \ |
| 501 | Py_INCREF((O)); \ |
| 502 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 503 | |
| 504 | static PyObject * |
| 505 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 506 | { |
| 507 | PyObject *tuple; |
| 508 | Py_ssize_t len, i, j; |
| 509 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 510 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 511 | tuple = PyTuple_New(len); |
| 512 | if (tuple == NULL) |
| 513 | return NULL; |
| 514 | for (i = start, j = 0; j < len; i++, j++) |
| 515 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 516 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 517 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 518 | return tuple; |
| 519 | } |
| 520 | |
| 521 | static PyObject * |
| 522 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 523 | { |
| 524 | PyObject *list; |
| 525 | Py_ssize_t len, i, j; |
| 526 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 527 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 528 | list = PyList_New(len); |
| 529 | if (list == NULL) |
| 530 | return NULL; |
| 531 | for (i = start, j = 0; j < len; i++, j++) |
| 532 | PyList_SET_ITEM(list, j, self->data[i]); |
| 533 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 534 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 535 | return list; |
| 536 | } |
| 537 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 538 | typedef struct { |
| 539 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 540 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 541 | } PyMemoEntry; |
| 542 | |
| 543 | typedef struct { |
| 544 | Py_ssize_t mt_mask; |
| 545 | Py_ssize_t mt_used; |
| 546 | Py_ssize_t mt_allocated; |
| 547 | PyMemoEntry *mt_table; |
| 548 | } PyMemoTable; |
| 549 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 550 | typedef struct PicklerObject { |
| 551 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 552 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 553 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 554 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 555 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 556 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 557 | |
| 558 | PyObject *write; /* write() method of the output stream. */ |
| 559 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 560 | flushing to the stream. */ |
| 561 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 562 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 563 | int proto; /* Pickle protocol number, >= 0 */ |
| 564 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 565 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 566 | Py_ssize_t frame_start; /* Position in output_buffer where the |
| 567 | where the current frame begins. -1 if there |
| 568 | is no frame currently open. */ |
| 569 | |
| 570 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 571 | int fast; /* Enable fast mode if set to a true value. |
| 572 | The fast mode disable the usage of memo, |
| 573 | therefore speeding the pickling process by |
| 574 | not generating superfluous PUT opcodes. It |
| 575 | should not be used if with self-referential |
| 576 | objects. */ |
| 577 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 578 | int fix_imports; /* Indicate whether Pickler should fix |
| 579 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 580 | PyObject *fast_memo; |
| 581 | } PicklerObject; |
| 582 | |
| 583 | typedef struct UnpicklerObject { |
| 584 | PyObject_HEAD |
| 585 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 586 | |
| 587 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 588 | is unnecessary, since the keys are contiguous ints. */ |
| 589 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 590 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 591 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 592 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 593 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 594 | |
| 595 | Py_buffer buffer; |
| 596 | char *input_buffer; |
| 597 | char *input_line; |
| 598 | Py_ssize_t input_len; |
| 599 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 600 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 601 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 602 | PyObject *read; /* read() method of the input stream. */ |
| 603 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 604 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 605 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 606 | char *encoding; /* Name of the encoding to be used for |
| 607 | decoding strings pickled using Python |
| 608 | 2.x. The default value is "ASCII" */ |
| 609 | char *errors; /* Name of errors handling scheme to used when |
| 610 | decoding strings. The default value is |
| 611 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 612 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 613 | objects. */ |
| 614 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 615 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 616 | int proto; /* Protocol of the pickle loaded. */ |
| 617 | int fix_imports; /* Indicate whether Unpickler should fix |
| 618 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 619 | } UnpicklerObject; |
| 620 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 621 | typedef struct { |
| 622 | PyObject_HEAD |
| 623 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 624 | } PicklerMemoProxyObject; |
| 625 | |
| 626 | typedef struct { |
| 627 | PyObject_HEAD |
| 628 | UnpicklerObject *unpickler; |
| 629 | } UnpicklerMemoProxyObject; |
| 630 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 631 | /* Forward declarations */ |
| 632 | static int save(PicklerObject *, PyObject *, int); |
| 633 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 634 | static PyTypeObject Pickler_Type; |
| 635 | static PyTypeObject Unpickler_Type; |
| 636 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 637 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 638 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 639 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 640 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 641 | 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] | 642 | a bunch of unnecessary object creation. This makes a huge performance |
| 643 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 645 | #define MT_MINSIZE 8 |
| 646 | #define PERTURB_SHIFT 5 |
| 647 | |
| 648 | |
| 649 | static PyMemoTable * |
| 650 | PyMemoTable_New(void) |
| 651 | { |
| 652 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 653 | if (memo == NULL) { |
| 654 | PyErr_NoMemory(); |
| 655 | return NULL; |
| 656 | } |
| 657 | |
| 658 | memo->mt_used = 0; |
| 659 | memo->mt_allocated = MT_MINSIZE; |
| 660 | memo->mt_mask = MT_MINSIZE - 1; |
| 661 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 662 | if (memo->mt_table == NULL) { |
| 663 | PyMem_FREE(memo); |
| 664 | PyErr_NoMemory(); |
| 665 | return NULL; |
| 666 | } |
| 667 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 668 | |
| 669 | return memo; |
| 670 | } |
| 671 | |
| 672 | static PyMemoTable * |
| 673 | PyMemoTable_Copy(PyMemoTable *self) |
| 674 | { |
| 675 | Py_ssize_t i; |
| 676 | PyMemoTable *new = PyMemoTable_New(); |
| 677 | if (new == NULL) |
| 678 | return NULL; |
| 679 | |
| 680 | new->mt_used = self->mt_used; |
| 681 | new->mt_allocated = self->mt_allocated; |
| 682 | new->mt_mask = self->mt_mask; |
| 683 | /* The table we get from _New() is probably smaller than we wanted. |
| 684 | Free it and allocate one that's the right size. */ |
| 685 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 686 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 687 | if (new->mt_table == NULL) { |
| 688 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 689 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 690 | return NULL; |
| 691 | } |
| 692 | for (i = 0; i < self->mt_allocated; i++) { |
| 693 | Py_XINCREF(self->mt_table[i].me_key); |
| 694 | } |
| 695 | memcpy(new->mt_table, self->mt_table, |
| 696 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 697 | |
| 698 | return new; |
| 699 | } |
| 700 | |
| 701 | static Py_ssize_t |
| 702 | PyMemoTable_Size(PyMemoTable *self) |
| 703 | { |
| 704 | return self->mt_used; |
| 705 | } |
| 706 | |
| 707 | static int |
| 708 | PyMemoTable_Clear(PyMemoTable *self) |
| 709 | { |
| 710 | Py_ssize_t i = self->mt_allocated; |
| 711 | |
| 712 | while (--i >= 0) { |
| 713 | Py_XDECREF(self->mt_table[i].me_key); |
| 714 | } |
| 715 | self->mt_used = 0; |
| 716 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 717 | return 0; |
| 718 | } |
| 719 | |
| 720 | static void |
| 721 | PyMemoTable_Del(PyMemoTable *self) |
| 722 | { |
| 723 | if (self == NULL) |
| 724 | return; |
| 725 | PyMemoTable_Clear(self); |
| 726 | |
| 727 | PyMem_FREE(self->mt_table); |
| 728 | PyMem_FREE(self); |
| 729 | } |
| 730 | |
| 731 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 732 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 733 | static PyMemoEntry * |
| 734 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 735 | { |
| 736 | size_t i; |
| 737 | size_t perturb; |
| 738 | size_t mask = (size_t)self->mt_mask; |
| 739 | PyMemoEntry *table = self->mt_table; |
| 740 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 741 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 742 | |
| 743 | i = hash & mask; |
| 744 | entry = &table[i]; |
| 745 | if (entry->me_key == NULL || entry->me_key == key) |
| 746 | return entry; |
| 747 | |
| 748 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 749 | i = (i << 2) + i + perturb + 1; |
| 750 | entry = &table[i & mask]; |
| 751 | if (entry->me_key == NULL || entry->me_key == key) |
| 752 | return entry; |
| 753 | } |
| 754 | assert(0); /* Never reached */ |
| 755 | return NULL; |
| 756 | } |
| 757 | |
| 758 | /* Returns -1 on failure, 0 on success. */ |
| 759 | static int |
| 760 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 761 | { |
| 762 | PyMemoEntry *oldtable = NULL; |
| 763 | PyMemoEntry *oldentry, *newentry; |
| 764 | Py_ssize_t new_size = MT_MINSIZE; |
| 765 | Py_ssize_t to_process; |
| 766 | |
| 767 | assert(min_size > 0); |
| 768 | |
| 769 | /* Find the smallest valid table size >= min_size. */ |
| 770 | while (new_size < min_size && new_size > 0) |
| 771 | new_size <<= 1; |
| 772 | if (new_size <= 0) { |
| 773 | PyErr_NoMemory(); |
| 774 | return -1; |
| 775 | } |
| 776 | /* new_size needs to be a power of two. */ |
| 777 | assert((new_size & (new_size - 1)) == 0); |
| 778 | |
| 779 | /* Allocate new table. */ |
| 780 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 781 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 782 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 783 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 784 | PyErr_NoMemory(); |
| 785 | return -1; |
| 786 | } |
| 787 | self->mt_allocated = new_size; |
| 788 | self->mt_mask = new_size - 1; |
| 789 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 790 | |
| 791 | /* Copy entries from the old table. */ |
| 792 | to_process = self->mt_used; |
| 793 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 794 | if (oldentry->me_key != NULL) { |
| 795 | to_process--; |
| 796 | /* newentry is a pointer to a chunk of the new |
| 797 | mt_table, so we're setting the key:value pair |
| 798 | in-place. */ |
| 799 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 800 | newentry->me_key = oldentry->me_key; |
| 801 | newentry->me_value = oldentry->me_value; |
| 802 | } |
| 803 | } |
| 804 | |
| 805 | /* Deallocate the old table. */ |
| 806 | PyMem_FREE(oldtable); |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 811 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 812 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 813 | { |
| 814 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 815 | if (entry->me_key == NULL) |
| 816 | return NULL; |
| 817 | return &entry->me_value; |
| 818 | } |
| 819 | |
| 820 | /* Returns -1 on failure, 0 on success. */ |
| 821 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 822 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 823 | { |
| 824 | PyMemoEntry *entry; |
| 825 | |
| 826 | assert(key != NULL); |
| 827 | |
| 828 | entry = _PyMemoTable_Lookup(self, key); |
| 829 | if (entry->me_key != NULL) { |
| 830 | entry->me_value = value; |
| 831 | return 0; |
| 832 | } |
| 833 | Py_INCREF(key); |
| 834 | entry->me_key = key; |
| 835 | entry->me_value = value; |
| 836 | self->mt_used++; |
| 837 | |
| 838 | /* If we added a key, we can safely resize. Otherwise just return! |
| 839 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 840 | * |
| 841 | * Quadrupling the size improves average table sparseness |
| 842 | * (reducing collisions) at the cost of some memory. It also halves |
| 843 | * the number of expensive resize operations in a growing memo table. |
| 844 | * |
| 845 | * Very large memo tables (over 50K items) use doubling instead. |
| 846 | * This may help applications with severe memory constraints. |
| 847 | */ |
| 848 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 849 | return 0; |
| 850 | return _PyMemoTable_ResizeTable(self, |
| 851 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 852 | } |
| 853 | |
| 854 | #undef MT_MINSIZE |
| 855 | #undef PERTURB_SHIFT |
| 856 | |
| 857 | /*************************************************************************/ |
| 858 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 859 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 860 | static int |
| 861 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 862 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 863 | Py_CLEAR(self->output_buffer); |
| 864 | self->output_buffer = |
| 865 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 866 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 867 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 868 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 869 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 870 | return 0; |
| 871 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 872 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 873 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 874 | _write_size64(char *out, size_t value) |
| 875 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 876 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 877 | |
Serhiy Storchaka | fad85aa | 2015-11-07 15:42:38 +0200 | [diff] [blame] | 878 | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 879 | |
| 880 | for (i = 0; i < sizeof(size_t); i++) { |
| 881 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 882 | } |
| 883 | for (i = sizeof(size_t); i < 8; i++) { |
| 884 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 885 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 889 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 890 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 891 | qdata[0] = FRAME; |
| 892 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 893 | } |
| 894 | |
| 895 | static int |
| 896 | _Pickler_CommitFrame(PicklerObject *self) |
| 897 | { |
| 898 | size_t frame_len; |
| 899 | char *qdata; |
| 900 | |
| 901 | if (!self->framing || self->frame_start == -1) |
| 902 | return 0; |
| 903 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 904 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 905 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 906 | self->frame_start = -1; |
| 907 | return 0; |
| 908 | } |
| 909 | |
| 910 | static int |
| 911 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 912 | { |
| 913 | Py_ssize_t frame_len; |
| 914 | |
| 915 | if (!self->framing || self->frame_start == -1) |
| 916 | return 0; |
| 917 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 918 | if (frame_len >= FRAME_SIZE_TARGET) |
| 919 | return _Pickler_CommitFrame(self); |
| 920 | else |
| 921 | return 0; |
| 922 | } |
| 923 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 924 | static PyObject * |
| 925 | _Pickler_GetString(PicklerObject *self) |
| 926 | { |
| 927 | PyObject *output_buffer = self->output_buffer; |
| 928 | |
| 929 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 930 | |
| 931 | if (_Pickler_CommitFrame(self)) |
| 932 | return NULL; |
| 933 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 934 | self->output_buffer = NULL; |
| 935 | /* Resize down to exact size */ |
| 936 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 937 | return NULL; |
| 938 | return output_buffer; |
| 939 | } |
| 940 | |
| 941 | static int |
| 942 | _Pickler_FlushToFile(PicklerObject *self) |
| 943 | { |
| 944 | PyObject *output, *result; |
| 945 | |
| 946 | assert(self->write != NULL); |
| 947 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 948 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 949 | output = _Pickler_GetString(self); |
| 950 | if (output == NULL) |
| 951 | return -1; |
| 952 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 953 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 954 | Py_XDECREF(result); |
| 955 | return (result == NULL) ? -1 : 0; |
| 956 | } |
| 957 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 958 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 959 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 960 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 961 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 962 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 963 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 964 | |
| 965 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 966 | need_new_frame = (self->framing && self->frame_start == -1); |
| 967 | |
| 968 | if (need_new_frame) |
| 969 | n = data_len + FRAME_HEADER_SIZE; |
| 970 | else |
| 971 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 972 | |
| 973 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 974 | if (required > self->max_output_len) { |
| 975 | /* Make place in buffer for the pickle chunk */ |
| 976 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 977 | PyErr_NoMemory(); |
| 978 | return -1; |
| 979 | } |
| 980 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 981 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 982 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 983 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 984 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 985 | if (need_new_frame) { |
| 986 | /* Setup new frame */ |
| 987 | Py_ssize_t frame_start = self->output_len; |
| 988 | self->frame_start = frame_start; |
| 989 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 990 | /* Write an invalid value, for debugging */ |
| 991 | buffer[frame_start + i] = 0xFE; |
| 992 | } |
| 993 | self->output_len += FRAME_HEADER_SIZE; |
| 994 | } |
| 995 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 996 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 997 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 998 | buffer[self->output_len + i] = s[i]; |
| 999 | } |
| 1000 | } |
| 1001 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1002 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1003 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1004 | self->output_len += data_len; |
| 1005 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1008 | static PicklerObject * |
| 1009 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1010 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1011 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1012 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1013 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1014 | if (self == NULL) |
| 1015 | return NULL; |
| 1016 | |
| 1017 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1018 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1019 | self->write = NULL; |
| 1020 | self->proto = 0; |
| 1021 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1022 | self->framing = 0; |
| 1023 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1024 | self->fast = 0; |
| 1025 | self->fast_nesting = 0; |
| 1026 | self->fix_imports = 0; |
| 1027 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1028 | self->max_output_len = WRITE_BUF_SIZE; |
| 1029 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1030 | |
| 1031 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1032 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1033 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1034 | |
| 1035 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1036 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1037 | return NULL; |
| 1038 | } |
| 1039 | return self; |
| 1040 | } |
| 1041 | |
| 1042 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1043 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1044 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1045 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1046 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1047 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1048 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1049 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1050 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1051 | proto = PyLong_AsLong(protocol); |
| 1052 | if (proto < 0) { |
| 1053 | if (proto == -1 && PyErr_Occurred()) |
| 1054 | return -1; |
| 1055 | proto = HIGHEST_PROTOCOL; |
| 1056 | } |
| 1057 | else if (proto > HIGHEST_PROTOCOL) { |
| 1058 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1059 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1060 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1061 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1062 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1063 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1064 | self->bin = proto > 0; |
| 1065 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1066 | return 0; |
| 1067 | } |
| 1068 | |
| 1069 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1070 | be called once on a freshly created Pickler. */ |
| 1071 | static int |
| 1072 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1073 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1074 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1075 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1076 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1077 | if (self->write == NULL) { |
| 1078 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1079 | PyErr_SetString(PyExc_TypeError, |
| 1080 | "file must have a 'write' attribute"); |
| 1081 | return -1; |
| 1082 | } |
| 1083 | |
| 1084 | return 0; |
| 1085 | } |
| 1086 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1087 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1088 | own reference to `input`. */ |
| 1089 | static Py_ssize_t |
| 1090 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1091 | { |
| 1092 | if (self->buffer.buf != NULL) |
| 1093 | PyBuffer_Release(&self->buffer); |
| 1094 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1095 | return -1; |
| 1096 | self->input_buffer = self->buffer.buf; |
| 1097 | self->input_len = self->buffer.len; |
| 1098 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1099 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1100 | return self->input_len; |
| 1101 | } |
| 1102 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1103 | static int |
| 1104 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1105 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1106 | Py_ssize_t consumed; |
| 1107 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1108 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1109 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1110 | if (consumed <= 0) |
| 1111 | return 0; |
| 1112 | |
| 1113 | assert(self->peek); /* otherwise we did something wrong */ |
| 1114 | /* This makes an useless copy... */ |
| 1115 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1116 | if (r == NULL) |
| 1117 | return -1; |
| 1118 | Py_DECREF(r); |
| 1119 | |
| 1120 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1121 | return 0; |
| 1122 | } |
| 1123 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1124 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1125 | |
| 1126 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1127 | may be multiple pickle objects arranged contiguously in the same input |
| 1128 | buffer. |
| 1129 | |
| 1130 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1131 | bytes from the input stream/buffer. |
| 1132 | |
| 1133 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1134 | failure; on success, returns the number of bytes read from the file. |
| 1135 | |
| 1136 | On success, self->input_len will be 0; this is intentional so that when |
| 1137 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1138 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1139 | size to tell you how much data you can process. */ |
| 1140 | static Py_ssize_t |
| 1141 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1142 | { |
| 1143 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1144 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1145 | |
| 1146 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1147 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1148 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1149 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1150 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1151 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1152 | PyObject *empty_tuple = PyTuple_New(0); |
| 1153 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1154 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1155 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1156 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1157 | PyObject *len; |
| 1158 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1159 | if (self->peek && n < PREFETCH) { |
| 1160 | len = PyLong_FromSsize_t(PREFETCH); |
| 1161 | if (len == NULL) |
| 1162 | return -1; |
| 1163 | data = _Pickle_FastCall(self->peek, len); |
| 1164 | if (data == NULL) { |
| 1165 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1166 | return -1; |
| 1167 | /* peek() is probably not supported by the given file object */ |
| 1168 | PyErr_Clear(); |
| 1169 | Py_CLEAR(self->peek); |
| 1170 | } |
| 1171 | else { |
| 1172 | read_size = _Unpickler_SetStringInput(self, data); |
| 1173 | Py_DECREF(data); |
| 1174 | self->prefetched_idx = 0; |
| 1175 | if (n <= read_size) |
| 1176 | return n; |
| 1177 | } |
| 1178 | } |
| 1179 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1180 | if (len == NULL) |
| 1181 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1182 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1183 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1184 | if (data == NULL) |
| 1185 | return -1; |
| 1186 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1187 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1188 | Py_DECREF(data); |
| 1189 | return read_size; |
| 1190 | } |
| 1191 | |
| 1192 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1193 | |
| 1194 | This should be used for all data reads, rather than accessing the unpickler's |
| 1195 | input buffer directly. This method deals correctly with reading from input |
| 1196 | streams, which the input buffer doesn't deal with. |
| 1197 | |
| 1198 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1199 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1200 | should use this function's return value to know how many bytes you can |
| 1201 | consume. |
| 1202 | |
| 1203 | Returns -1 (with an exception set) on failure. On success, return the |
| 1204 | number of chars read. */ |
| 1205 | static Py_ssize_t |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1206 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1207 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1208 | Py_ssize_t num_read; |
| 1209 | |
Benjamin Peterson | 6aa1564 | 2015-09-27 01:16:03 -0700 | [diff] [blame] | 1210 | *s = NULL; |
Benjamin Peterson | e48cf7e | 2015-09-26 00:08:34 -0700 | [diff] [blame] | 1211 | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
| 1212 | PickleState *st = _Pickle_GetGlobalState(); |
| 1213 | PyErr_SetString(st->UnpicklingError, |
| 1214 | "read would overflow (invalid bytecode)"); |
| 1215 | return -1; |
| 1216 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1217 | if (self->next_read_idx + n <= self->input_len) { |
| 1218 | *s = self->input_buffer + self->next_read_idx; |
| 1219 | self->next_read_idx += n; |
| 1220 | return n; |
| 1221 | } |
| 1222 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1223 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1224 | return -1; |
| 1225 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1226 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1227 | if (num_read < 0) |
| 1228 | return -1; |
| 1229 | if (num_read < n) { |
| 1230 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1231 | return -1; |
| 1232 | } |
| 1233 | *s = self->input_buffer; |
| 1234 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1235 | return n; |
| 1236 | } |
| 1237 | |
| 1238 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1239 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1240 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1241 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1242 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1243 | if (input_line == NULL) { |
| 1244 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1245 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1246 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1247 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1248 | memcpy(input_line, line, len); |
| 1249 | input_line[len] = '\0'; |
| 1250 | self->input_line = input_line; |
| 1251 | *result = self->input_line; |
| 1252 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1255 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1256 | before hitting \n, return the data we found. |
| 1257 | |
| 1258 | Returns the number of chars read, or -1 on failure. */ |
| 1259 | static Py_ssize_t |
| 1260 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1261 | { |
| 1262 | Py_ssize_t i, num_read; |
| 1263 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1264 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1265 | if (self->input_buffer[i] == '\n') { |
| 1266 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1267 | num_read = i - self->next_read_idx + 1; |
| 1268 | self->next_read_idx = i + 1; |
| 1269 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1270 | } |
| 1271 | } |
| 1272 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1273 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1274 | if (num_read < 0) |
| 1275 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1276 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1277 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1278 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1279 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1280 | /* If we get here, we've run off the end of the input string. Return the |
| 1281 | remaining string and let the caller figure it out. */ |
| 1282 | *result = self->input_buffer + self->next_read_idx; |
| 1283 | num_read = i - self->next_read_idx; |
| 1284 | self->next_read_idx = i; |
| 1285 | return num_read; |
| 1286 | } |
| 1287 | |
| 1288 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1289 | will be modified in place. */ |
| 1290 | static int |
| 1291 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1292 | { |
| 1293 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1294 | |
| 1295 | assert(new_size > self->memo_size); |
| 1296 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1297 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1298 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1299 | PyErr_NoMemory(); |
| 1300 | return -1; |
| 1301 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1302 | for (i = self->memo_size; i < new_size; i++) |
| 1303 | self->memo[i] = NULL; |
| 1304 | self->memo_size = new_size; |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
| 1308 | /* Returns NULL if idx is out of bounds. */ |
| 1309 | static PyObject * |
| 1310 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1311 | { |
| 1312 | if (idx < 0 || idx >= self->memo_size) |
| 1313 | return NULL; |
| 1314 | |
| 1315 | return self->memo[idx]; |
| 1316 | } |
| 1317 | |
| 1318 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1319 | This takes its own reference to `value`. */ |
| 1320 | static int |
| 1321 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1322 | { |
| 1323 | PyObject *old_item; |
| 1324 | |
| 1325 | if (idx >= self->memo_size) { |
| 1326 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1327 | return -1; |
| 1328 | assert(idx < self->memo_size); |
| 1329 | } |
| 1330 | Py_INCREF(value); |
| 1331 | old_item = self->memo[idx]; |
| 1332 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1333 | if (old_item != NULL) { |
| 1334 | Py_DECREF(old_item); |
| 1335 | } |
| 1336 | else { |
| 1337 | self->memo_len++; |
| 1338 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1339 | return 0; |
| 1340 | } |
| 1341 | |
| 1342 | static PyObject ** |
| 1343 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1344 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1345 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1346 | if (memo == NULL) { |
| 1347 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1348 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1349 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1350 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1351 | return memo; |
| 1352 | } |
| 1353 | |
| 1354 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1355 | static void |
| 1356 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1357 | { |
| 1358 | Py_ssize_t i; |
| 1359 | PyObject **memo = self->memo; |
| 1360 | |
| 1361 | if (self->memo == NULL) |
| 1362 | return; |
| 1363 | self->memo = NULL; |
| 1364 | i = self->memo_size; |
| 1365 | while (--i >= 0) { |
| 1366 | Py_XDECREF(memo[i]); |
| 1367 | } |
| 1368 | PyMem_FREE(memo); |
| 1369 | } |
| 1370 | |
| 1371 | static UnpicklerObject * |
| 1372 | _Unpickler_New(void) |
| 1373 | { |
| 1374 | UnpicklerObject *self; |
| 1375 | |
| 1376 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1377 | if (self == NULL) |
| 1378 | return NULL; |
| 1379 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1380 | self->pers_func = NULL; |
| 1381 | self->input_buffer = NULL; |
| 1382 | self->input_line = NULL; |
| 1383 | self->input_len = 0; |
| 1384 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1385 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1386 | self->read = NULL; |
| 1387 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1388 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1389 | self->encoding = NULL; |
| 1390 | self->errors = NULL; |
| 1391 | self->marks = NULL; |
| 1392 | self->num_marks = 0; |
| 1393 | self->marks_size = 0; |
| 1394 | self->proto = 0; |
| 1395 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1396 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1397 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1398 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1399 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1400 | self->stack = (Pdata *)Pdata_New(); |
| 1401 | |
| 1402 | if (self->memo == NULL || self->stack == NULL) { |
| 1403 | Py_DECREF(self); |
| 1404 | return NULL; |
| 1405 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1406 | |
| 1407 | return self; |
| 1408 | } |
| 1409 | |
| 1410 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1411 | be called once on a freshly created Pickler. */ |
| 1412 | static int |
| 1413 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1414 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1415 | _Py_IDENTIFIER(peek); |
| 1416 | _Py_IDENTIFIER(read); |
| 1417 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1418 | |
| 1419 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1420 | if (self->peek == NULL) { |
| 1421 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1422 | PyErr_Clear(); |
| 1423 | else |
| 1424 | return -1; |
| 1425 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1426 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1427 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1428 | if (self->readline == NULL || self->read == NULL) { |
| 1429 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1430 | PyErr_SetString(PyExc_TypeError, |
| 1431 | "file must have 'read' and 'readline' attributes"); |
| 1432 | Py_CLEAR(self->read); |
| 1433 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1434 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1435 | return -1; |
| 1436 | } |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1441 | be called once on a freshly created Pickler. */ |
| 1442 | static int |
| 1443 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1444 | const char *encoding, |
| 1445 | const char *errors) |
| 1446 | { |
| 1447 | if (encoding == NULL) |
| 1448 | encoding = "ASCII"; |
| 1449 | if (errors == NULL) |
| 1450 | errors = "strict"; |
| 1451 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1452 | self->encoding = _PyMem_Strdup(encoding); |
| 1453 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1454 | if (self->encoding == NULL || self->errors == NULL) { |
| 1455 | PyErr_NoMemory(); |
| 1456 | return -1; |
| 1457 | } |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1462 | static int |
| 1463 | memo_get(PicklerObject *self, PyObject *key) |
| 1464 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1465 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1466 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1467 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1468 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1469 | value = PyMemoTable_Get(self->memo, key); |
| 1470 | if (value == NULL) { |
| 1471 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1472 | return -1; |
| 1473 | } |
| 1474 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1475 | if (!self->bin) { |
| 1476 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1477 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1478 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1479 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1480 | } |
| 1481 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1482 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1483 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1484 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1485 | len = 2; |
| 1486 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1487 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1488 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1489 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1490 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1491 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1492 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1493 | len = 5; |
| 1494 | } |
| 1495 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1496 | PickleState *st = _Pickle_GetGlobalState(); |
| 1497 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1498 | "memo id too large for LONG_BINGET"); |
| 1499 | return -1; |
| 1500 | } |
| 1501 | } |
| 1502 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1503 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1504 | return -1; |
| 1505 | |
| 1506 | return 0; |
| 1507 | } |
| 1508 | |
| 1509 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1510 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1511 | static int |
| 1512 | memo_put(PicklerObject *self, PyObject *obj) |
| 1513 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1514 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1515 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1516 | Py_ssize_t idx; |
| 1517 | |
| 1518 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1519 | |
| 1520 | if (self->fast) |
| 1521 | return 0; |
| 1522 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1523 | idx = PyMemoTable_Size(self->memo); |
| 1524 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1525 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1526 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1527 | if (self->proto >= 4) { |
| 1528 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1529 | return -1; |
| 1530 | return 0; |
| 1531 | } |
| 1532 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1533 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1534 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1535 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1536 | len = strlen(pdata); |
| 1537 | } |
| 1538 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1539 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1540 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1541 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1542 | len = 2; |
| 1543 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1544 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1545 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1546 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1547 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1548 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1549 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1550 | len = 5; |
| 1551 | } |
| 1552 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1553 | PickleState *st = _Pickle_GetGlobalState(); |
| 1554 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1555 | "memo id too large for LONG_BINPUT"); |
| 1556 | return -1; |
| 1557 | } |
| 1558 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1559 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1560 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1561 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1562 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
| 1565 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1566 | get_dotted_path(PyObject *obj, PyObject *name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1567 | _Py_static_string(PyId_dot, "."); |
| 1568 | _Py_static_string(PyId_locals, "<locals>"); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1569 | PyObject *dotted_path; |
| 1570 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1571 | |
| 1572 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1573 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1574 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1575 | n = PyList_GET_SIZE(dotted_path); |
| 1576 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1577 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1578 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1579 | PyObject *result = PyUnicode_RichCompare( |
| 1580 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1581 | int is_equal = (result == Py_True); |
| 1582 | assert(PyBool_Check(result)); |
| 1583 | Py_DECREF(result); |
| 1584 | if (is_equal) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1585 | if (obj == NULL) |
| 1586 | PyErr_Format(PyExc_AttributeError, |
| 1587 | "Can't pickle local object %R", name); |
| 1588 | else |
| 1589 | PyErr_Format(PyExc_AttributeError, |
| 1590 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1591 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1592 | return NULL; |
| 1593 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1594 | } |
| 1595 | return dotted_path; |
| 1596 | } |
| 1597 | |
| 1598 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1599 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1600 | { |
| 1601 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1602 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1603 | |
| 1604 | assert(PyList_CheckExact(names)); |
| 1605 | Py_INCREF(obj); |
| 1606 | n = PyList_GET_SIZE(names); |
| 1607 | for (i = 0; i < n; i++) { |
| 1608 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1609 | Py_XDECREF(parent); |
| 1610 | parent = obj; |
| 1611 | obj = PyObject_GetAttr(parent, name); |
| 1612 | if (obj == NULL) { |
| 1613 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1614 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1615 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1616 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1617 | if (pparent != NULL) |
| 1618 | *pparent = parent; |
| 1619 | else |
| 1620 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1621 | return obj; |
| 1622 | } |
| 1623 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1624 | static void |
| 1625 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1626 | { |
| 1627 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1628 | PyErr_Clear(); |
| 1629 | PyErr_Format(PyExc_AttributeError, |
| 1630 | "Can't get attribute %R on %R", name, obj); |
| 1631 | } |
| 1632 | } |
| 1633 | |
| 1634 | |
| 1635 | static PyObject * |
| 1636 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1637 | { |
| 1638 | PyObject *dotted_path, *attr; |
| 1639 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1640 | if (allow_qualname) { |
| 1641 | dotted_path = get_dotted_path(obj, name); |
| 1642 | if (dotted_path == NULL) |
| 1643 | return NULL; |
| 1644 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1645 | Py_DECREF(dotted_path); |
| 1646 | } |
| 1647 | else |
| 1648 | attr = PyObject_GetAttr(obj, name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1649 | if (attr == NULL) |
| 1650 | reformat_attribute_error(obj, name); |
| 1651 | return attr; |
| 1652 | } |
| 1653 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1654 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1655 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1656 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1657 | PyObject *module_name; |
| 1658 | PyObject *modules_dict; |
| 1659 | PyObject *module; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1660 | Py_ssize_t i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1661 | _Py_IDENTIFIER(__module__); |
| 1662 | _Py_IDENTIFIER(modules); |
| 1663 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1664 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1665 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1666 | |
| 1667 | if (module_name == NULL) { |
| 1668 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1669 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1670 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1671 | } |
| 1672 | else { |
| 1673 | /* In some rare cases (e.g., bound methods of extension types), |
| 1674 | __module__ can be None. If it is so, then search sys.modules for |
| 1675 | the module of global. */ |
| 1676 | if (module_name != Py_None) |
| 1677 | return module_name; |
| 1678 | Py_CLEAR(module_name); |
| 1679 | } |
| 1680 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1681 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1682 | /* Fallback on walking sys.modules */ |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1683 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1684 | if (modules_dict == NULL) { |
| 1685 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1686 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1687 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1688 | |
| 1689 | i = 0; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1690 | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
| 1691 | PyObject *candidate; |
| 1692 | if (PyUnicode_Check(module_name) && |
| 1693 | !PyUnicode_CompareWithASCIIString(module_name, "__main__")) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1694 | continue; |
| 1695 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1696 | continue; |
| 1697 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1698 | candidate = get_deep_attribute(module, dotted_path, NULL); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1699 | if (candidate == NULL) { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1700 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1701 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1702 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1703 | continue; |
| 1704 | } |
| 1705 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1706 | if (candidate == global) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1707 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1708 | Py_DECREF(candidate); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1709 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1710 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1711 | Py_DECREF(candidate); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
| 1714 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1715 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1716 | Py_INCREF(module_name); |
| 1717 | return module_name; |
| 1718 | } |
| 1719 | |
| 1720 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1721 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1722 | memoization disabled). If the nesting of a list or dict object exceed |
| 1723 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1724 | reference to the seen list or dict objects and check whether these objects |
| 1725 | are recursive. These are not strictly necessary, since save() has a |
| 1726 | hard-coded recursion limit, but they give a nicer error message than the |
| 1727 | typical RuntimeError. */ |
| 1728 | static int |
| 1729 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1730 | { |
| 1731 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1732 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1733 | PyObject *key = NULL; |
| 1734 | if (self->fast_memo == NULL) { |
| 1735 | self->fast_memo = PyDict_New(); |
| 1736 | if (self->fast_memo == NULL) { |
| 1737 | self->fast_nesting = -1; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | } |
| 1741 | key = PyLong_FromVoidPtr(obj); |
| 1742 | if (key == NULL) |
| 1743 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1744 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1745 | Py_DECREF(key); |
| 1746 | PyErr_Format(PyExc_ValueError, |
| 1747 | "fast mode: can't pickle cyclic objects " |
| 1748 | "including object type %.200s at %p", |
| 1749 | obj->ob_type->tp_name, obj); |
| 1750 | self->fast_nesting = -1; |
| 1751 | return 0; |
| 1752 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1753 | if (PyErr_Occurred()) { |
| 1754 | return 0; |
| 1755 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1756 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1757 | Py_DECREF(key); |
| 1758 | self->fast_nesting = -1; |
| 1759 | return 0; |
| 1760 | } |
| 1761 | Py_DECREF(key); |
| 1762 | } |
| 1763 | return 1; |
| 1764 | } |
| 1765 | |
| 1766 | static int |
| 1767 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1768 | { |
| 1769 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1770 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1771 | if (key == NULL) |
| 1772 | return 0; |
| 1773 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1774 | Py_DECREF(key); |
| 1775 | return 0; |
| 1776 | } |
| 1777 | Py_DECREF(key); |
| 1778 | } |
| 1779 | return 1; |
| 1780 | } |
| 1781 | |
| 1782 | static int |
| 1783 | save_none(PicklerObject *self, PyObject *obj) |
| 1784 | { |
| 1785 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1786 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1787 | return -1; |
| 1788 | |
| 1789 | return 0; |
| 1790 | } |
| 1791 | |
| 1792 | static int |
| 1793 | save_bool(PicklerObject *self, PyObject *obj) |
| 1794 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1795 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1796 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1797 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1798 | return -1; |
| 1799 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1800 | else { |
| 1801 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1802 | * so that unpicklers written before bools were introduced unpickle them |
| 1803 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1804 | * Note that protocol 2 added direct ways to pickle bools. |
| 1805 | */ |
| 1806 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1807 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1808 | return -1; |
| 1809 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1810 | return 0; |
| 1811 | } |
| 1812 | |
| 1813 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1814 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1815 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1816 | PyObject *repr = NULL; |
| 1817 | Py_ssize_t size; |
| 1818 | long val; |
| 1819 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1820 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1821 | const char long_op = LONG; |
| 1822 | |
| 1823 | val= PyLong_AsLong(obj); |
| 1824 | if (val == -1 && PyErr_Occurred()) { |
| 1825 | /* out of range for int pickling */ |
| 1826 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1827 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1828 | else if (self->bin && |
| 1829 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1830 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1831 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1832 | |
| 1833 | Note: we can't use -0x80000000L in the above condition because some |
| 1834 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1835 | before applying the unary minus when sizeof(long) <= 4. The |
| 1836 | resulting value stays unsigned which is commonly not what we want, |
| 1837 | so MSVC happily warns us about it. However, that result would have |
| 1838 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1839 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1840 | char pdata[32]; |
| 1841 | Py_ssize_t len = 0; |
| 1842 | |
| 1843 | pdata[1] = (unsigned char)(val & 0xff); |
| 1844 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1845 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1846 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1847 | |
| 1848 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1849 | if (pdata[2] == 0) { |
| 1850 | pdata[0] = BININT1; |
| 1851 | len = 2; |
| 1852 | } |
| 1853 | else { |
| 1854 | pdata[0] = BININT2; |
| 1855 | len = 3; |
| 1856 | } |
| 1857 | } |
| 1858 | else { |
| 1859 | pdata[0] = BININT; |
| 1860 | len = 5; |
| 1861 | } |
| 1862 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1863 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1864 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1865 | |
| 1866 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1869 | if (self->proto >= 2) { |
| 1870 | /* Linear-time pickling. */ |
| 1871 | size_t nbits; |
| 1872 | size_t nbytes; |
| 1873 | unsigned char *pdata; |
| 1874 | char header[5]; |
| 1875 | int i; |
| 1876 | int sign = _PyLong_Sign(obj); |
| 1877 | |
| 1878 | if (sign == 0) { |
| 1879 | header[0] = LONG1; |
| 1880 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1881 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1882 | goto error; |
| 1883 | return 0; |
| 1884 | } |
| 1885 | nbits = _PyLong_NumBits(obj); |
| 1886 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1887 | goto error; |
| 1888 | /* How many bytes do we need? There are nbits >> 3 full |
| 1889 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1890 | * are any leftover bits, then we clearly need another |
| 1891 | * byte. Wnat's not so obvious is that we *probably* |
| 1892 | * need another byte even if there aren't any leftovers: |
| 1893 | * the most-significant bit of the most-significant byte |
| 1894 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1895 | * opposite of the one we need. The exception is ints |
| 1896 | * 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] | 1897 | * its own 256's-complement, so has the right sign bit |
| 1898 | * even without the extra byte. That's a pain to check |
| 1899 | * for in advance, though, so we always grab an extra |
| 1900 | * byte at the start, and cut it back later if possible. |
| 1901 | */ |
| 1902 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1903 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1904 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1905 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1906 | goto error; |
| 1907 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1908 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1909 | if (repr == NULL) |
| 1910 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1911 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1912 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1913 | pdata, nbytes, |
| 1914 | 1 /* little endian */ , 1 /* signed */ ); |
| 1915 | if (i < 0) |
| 1916 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1917 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1918 | * needed. This is so iff the MSB is all redundant sign |
| 1919 | * bits. |
| 1920 | */ |
| 1921 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1922 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1923 | pdata[nbytes - 1] == 0xff && |
| 1924 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1925 | nbytes--; |
| 1926 | } |
| 1927 | |
| 1928 | if (nbytes < 256) { |
| 1929 | header[0] = LONG1; |
| 1930 | header[1] = (unsigned char)nbytes; |
| 1931 | size = 2; |
| 1932 | } |
| 1933 | else { |
| 1934 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1935 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1936 | for (i = 1; i < 5; i++) { |
| 1937 | header[i] = (unsigned char)(size & 0xff); |
| 1938 | size >>= 8; |
| 1939 | } |
| 1940 | size = 5; |
| 1941 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1942 | if (_Pickler_Write(self, header, size) < 0 || |
| 1943 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1944 | goto error; |
| 1945 | } |
| 1946 | else { |
| 1947 | char *string; |
| 1948 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1949 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1950 | the number of digits), in both directions. We add a trailing 'L' |
| 1951 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1952 | |
| 1953 | repr = PyObject_Repr(obj); |
| 1954 | if (repr == NULL) |
| 1955 | goto error; |
| 1956 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1957 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1958 | if (string == NULL) |
| 1959 | goto error; |
| 1960 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1961 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1962 | _Pickler_Write(self, string, size) < 0 || |
| 1963 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1964 | goto error; |
| 1965 | } |
| 1966 | |
| 1967 | if (0) { |
| 1968 | error: |
| 1969 | status = -1; |
| 1970 | } |
| 1971 | Py_XDECREF(repr); |
| 1972 | |
| 1973 | return status; |
| 1974 | } |
| 1975 | |
| 1976 | static int |
| 1977 | save_float(PicklerObject *self, PyObject *obj) |
| 1978 | { |
| 1979 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1980 | |
| 1981 | if (self->bin) { |
| 1982 | char pdata[9]; |
| 1983 | pdata[0] = BINFLOAT; |
| 1984 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1985 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1986 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1987 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1988 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1989 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1990 | int result = -1; |
| 1991 | char *buf = NULL; |
| 1992 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1994 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1995 | goto done; |
| 1996 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 1997 | 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] | 1998 | if (!buf) { |
| 1999 | PyErr_NoMemory(); |
| 2000 | goto done; |
| 2001 | } |
| 2002 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2003 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2004 | goto done; |
| 2005 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2006 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 2007 | goto done; |
| 2008 | |
| 2009 | result = 0; |
| 2010 | done: |
| 2011 | PyMem_Free(buf); |
| 2012 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | return 0; |
| 2016 | } |
| 2017 | |
| 2018 | static int |
| 2019 | save_bytes(PicklerObject *self, PyObject *obj) |
| 2020 | { |
| 2021 | if (self->proto < 3) { |
| 2022 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2023 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2024 | the __reduce__ method) to permit bytes object unpickling. |
| 2025 | |
| 2026 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2027 | 2 'bytes' is just an alias for 'str' (which has different |
| 2028 | parameters than the actual bytes object), we use codecs.encode |
| 2029 | to create the appropriate 'str' object when unpickled using |
| 2030 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2031 | using Python 3. Again this is a hack and we don't need to do this |
| 2032 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2033 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2034 | int status; |
| 2035 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2036 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2037 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2038 | } |
| 2039 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2040 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2041 | PyObject *unicode_str = |
| 2042 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2043 | PyBytes_GET_SIZE(obj), |
| 2044 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2045 | _Py_IDENTIFIER(latin1); |
| 2046 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2047 | if (unicode_str == NULL) |
| 2048 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2049 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2050 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2051 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2052 | Py_DECREF(unicode_str); |
| 2053 | } |
| 2054 | |
| 2055 | if (reduce_value == NULL) |
| 2056 | return -1; |
| 2057 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2058 | /* save_reduce() will memoize the object automatically. */ |
| 2059 | status = save_reduce(self, reduce_value, obj); |
| 2060 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2061 | return status; |
| 2062 | } |
| 2063 | else { |
| 2064 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2065 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2066 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2067 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2068 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2069 | if (size < 0) |
| 2070 | return -1; |
| 2071 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2072 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2073 | header[0] = SHORT_BINBYTES; |
| 2074 | header[1] = (unsigned char)size; |
| 2075 | len = 2; |
| 2076 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2077 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2078 | header[0] = BINBYTES; |
| 2079 | header[1] = (unsigned char)(size & 0xff); |
| 2080 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2081 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2082 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2083 | len = 5; |
| 2084 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2085 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2086 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2087 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2088 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2089 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2090 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2091 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2092 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2093 | return -1; /* string too large */ |
| 2094 | } |
| 2095 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2096 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2097 | return -1; |
| 2098 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2099 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2100 | return -1; |
| 2101 | |
| 2102 | if (memo_put(self, obj) < 0) |
| 2103 | return -1; |
| 2104 | |
| 2105 | return 0; |
| 2106 | } |
| 2107 | } |
| 2108 | |
| 2109 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2110 | backslash and newline characters to \uXXXX escapes. */ |
| 2111 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2112 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2113 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2114 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2115 | Py_ssize_t i, size; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2116 | void *data; |
| 2117 | unsigned int kind; |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2118 | _PyBytesWriter writer; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2119 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2120 | if (PyUnicode_READY(obj)) |
| 2121 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2122 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2123 | _PyBytesWriter_Init(&writer); |
| 2124 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2125 | size = PyUnicode_GET_LENGTH(obj); |
| 2126 | data = PyUnicode_DATA(obj); |
| 2127 | kind = PyUnicode_KIND(obj); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2128 | |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2129 | p = _PyBytesWriter_Alloc(&writer, size); |
| 2130 | if (p == NULL) |
| 2131 | goto error; |
| 2132 | writer.overallocate = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2133 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2134 | for (i=0; i < size; i++) { |
| 2135 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2136 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2137 | if (ch >= 0x10000) { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2138 | /* -1: substract 1 preallocated byte */ |
| 2139 | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
| 2140 | if (p == NULL) |
| 2141 | goto error; |
| 2142 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2143 | *p++ = '\\'; |
| 2144 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2145 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2146 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2147 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2148 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2149 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2150 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2151 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2152 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2153 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2154 | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2155 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2156 | /* -1: substract 1 preallocated byte */ |
| 2157 | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
| 2158 | if (p == NULL) |
| 2159 | goto error; |
| 2160 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2161 | *p++ = '\\'; |
| 2162 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2163 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2164 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2165 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2166 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2167 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2168 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2169 | else |
| 2170 | *p++ = (char) ch; |
| 2171 | } |
Victor Stinner | 358af13 | 2015-10-12 22:36:57 +0200 | [diff] [blame] | 2172 | |
| 2173 | return _PyBytesWriter_Finish(&writer, p); |
| 2174 | |
| 2175 | error: |
| 2176 | _PyBytesWriter_Dealloc(&writer); |
| 2177 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2178 | } |
| 2179 | |
| 2180 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2181 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2182 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2183 | char header[9]; |
| 2184 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2185 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2186 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2187 | if (size <= 0xff && self->proto >= 4) { |
| 2188 | header[0] = SHORT_BINUNICODE; |
| 2189 | header[1] = (unsigned char)(size & 0xff); |
| 2190 | len = 2; |
| 2191 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2192 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2193 | header[0] = BINUNICODE; |
| 2194 | header[1] = (unsigned char)(size & 0xff); |
| 2195 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2196 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2197 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2198 | len = 5; |
| 2199 | } |
| 2200 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2201 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2202 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2203 | len = 9; |
| 2204 | } |
| 2205 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2206 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2207 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2208 | return -1; |
| 2209 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2210 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2211 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2212 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2213 | if (_Pickler_Write(self, data, size) < 0) |
| 2214 | return -1; |
| 2215 | |
| 2216 | return 0; |
| 2217 | } |
| 2218 | |
| 2219 | static int |
| 2220 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2221 | { |
| 2222 | PyObject *encoded = NULL; |
| 2223 | Py_ssize_t size; |
| 2224 | char *data; |
| 2225 | int r; |
| 2226 | |
| 2227 | if (PyUnicode_READY(obj)) |
| 2228 | return -1; |
| 2229 | |
| 2230 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2231 | if (data != NULL) |
| 2232 | return write_utf8(self, data, size); |
| 2233 | |
| 2234 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2235 | "surrogatepass" error handler. */ |
| 2236 | PyErr_Clear(); |
| 2237 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2238 | if (encoded == NULL) |
| 2239 | return -1; |
| 2240 | |
| 2241 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2242 | PyBytes_GET_SIZE(encoded)); |
| 2243 | Py_DECREF(encoded); |
| 2244 | return r; |
| 2245 | } |
| 2246 | |
| 2247 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2248 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2249 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2250 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2251 | if (write_unicode_binary(self, obj) < 0) |
| 2252 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2253 | } |
| 2254 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2255 | PyObject *encoded; |
| 2256 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2257 | const char unicode_op = UNICODE; |
| 2258 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2259 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2260 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2261 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2262 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2263 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2264 | Py_DECREF(encoded); |
| 2265 | return -1; |
| 2266 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2267 | |
| 2268 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2269 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2270 | Py_DECREF(encoded); |
| 2271 | return -1; |
| 2272 | } |
| 2273 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2274 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2275 | if (_Pickler_Write(self, "\n", 1) < 0) |
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 | } |
| 2278 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2279 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2280 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2281 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2282 | } |
| 2283 | |
| 2284 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2285 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2286 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2287 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2288 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2289 | |
| 2290 | assert(PyTuple_Size(t) == len); |
| 2291 | |
| 2292 | for (i = 0; i < len; i++) { |
| 2293 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2294 | |
| 2295 | if (element == NULL) |
| 2296 | return -1; |
| 2297 | if (save(self, element, 0) < 0) |
| 2298 | return -1; |
| 2299 | } |
| 2300 | |
| 2301 | return 0; |
| 2302 | } |
| 2303 | |
| 2304 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2305 | * used across protocols to minimize the space needed to pickle them. |
| 2306 | * Tuples are also the only builtin immutable type that can be recursive |
| 2307 | * (a tuple can be reached from itself), and that requires some subtle |
| 2308 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2309 | */ |
| 2310 | static int |
| 2311 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2312 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2313 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2314 | |
| 2315 | const char mark_op = MARK; |
| 2316 | const char tuple_op = TUPLE; |
| 2317 | const char pop_op = POP; |
| 2318 | const char pop_mark_op = POP_MARK; |
| 2319 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2320 | |
| 2321 | if ((len = PyTuple_Size(obj)) < 0) |
| 2322 | return -1; |
| 2323 | |
| 2324 | if (len == 0) { |
| 2325 | char pdata[2]; |
| 2326 | |
| 2327 | if (self->proto) { |
| 2328 | pdata[0] = EMPTY_TUPLE; |
| 2329 | len = 1; |
| 2330 | } |
| 2331 | else { |
| 2332 | pdata[0] = MARK; |
| 2333 | pdata[1] = TUPLE; |
| 2334 | len = 2; |
| 2335 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2336 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2337 | return -1; |
| 2338 | return 0; |
| 2339 | } |
| 2340 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2341 | /* 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] | 2342 | * saving the tuple elements, the tuple must be recursive, in |
| 2343 | * which case we'll pop everything we put on the stack, and fetch |
| 2344 | * its value from the memo. |
| 2345 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2346 | if (len <= 3 && self->proto >= 2) { |
| 2347 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2348 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2349 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2350 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2351 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2352 | /* pop the len elements */ |
| 2353 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2354 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2355 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2356 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2357 | if (memo_get(self, obj) < 0) |
| 2358 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2359 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2360 | return 0; |
| 2361 | } |
| 2362 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2363 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2364 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2365 | } |
| 2366 | goto memoize; |
| 2367 | } |
| 2368 | |
| 2369 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2370 | * Generate MARK e1 e2 ... TUPLE |
| 2371 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2372 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2373 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2374 | |
| 2375 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2376 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2378 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2379 | /* pop the stack stuff we pushed */ |
| 2380 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2381 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2382 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2383 | } |
| 2384 | else { |
| 2385 | /* Note that we pop one more than len, to remove |
| 2386 | * the MARK too. |
| 2387 | */ |
| 2388 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2389 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2390 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2391 | } |
| 2392 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2393 | if (memo_get(self, obj) < 0) |
| 2394 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2395 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2396 | return 0; |
| 2397 | } |
| 2398 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2399 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2400 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2401 | } |
| 2402 | |
| 2403 | memoize: |
| 2404 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2405 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2406 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2407 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2408 | } |
| 2409 | |
| 2410 | /* iter is an iterator giving items, and we batch up chunks of |
| 2411 | * MARK item item ... item APPENDS |
| 2412 | * opcode sequences. Calling code should have arranged to first create an |
| 2413 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2414 | * Returns 0 on success, <0 on error. |
| 2415 | */ |
| 2416 | static int |
| 2417 | batch_list(PicklerObject *self, PyObject *iter) |
| 2418 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2419 | PyObject *obj = NULL; |
| 2420 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2421 | int i, n; |
| 2422 | |
| 2423 | const char mark_op = MARK; |
| 2424 | const char append_op = APPEND; |
| 2425 | const char appends_op = APPENDS; |
| 2426 | |
| 2427 | assert(iter != NULL); |
| 2428 | |
| 2429 | /* XXX: I think this function could be made faster by avoiding the |
| 2430 | iterator interface and fetching objects directly from list using |
| 2431 | PyList_GET_ITEM. |
| 2432 | */ |
| 2433 | |
| 2434 | if (self->proto == 0) { |
| 2435 | /* APPENDS isn't available; do one at a time. */ |
| 2436 | for (;;) { |
| 2437 | obj = PyIter_Next(iter); |
| 2438 | if (obj == NULL) { |
| 2439 | if (PyErr_Occurred()) |
| 2440 | return -1; |
| 2441 | break; |
| 2442 | } |
| 2443 | i = save(self, obj, 0); |
| 2444 | Py_DECREF(obj); |
| 2445 | if (i < 0) |
| 2446 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2447 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2448 | return -1; |
| 2449 | } |
| 2450 | return 0; |
| 2451 | } |
| 2452 | |
| 2453 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2454 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2455 | /* Get first item */ |
| 2456 | firstitem = PyIter_Next(iter); |
| 2457 | if (firstitem == NULL) { |
| 2458 | if (PyErr_Occurred()) |
| 2459 | goto error; |
| 2460 | |
| 2461 | /* nothing more to add */ |
| 2462 | break; |
| 2463 | } |
| 2464 | |
| 2465 | /* Try to get a second item */ |
| 2466 | obj = PyIter_Next(iter); |
| 2467 | if (obj == NULL) { |
| 2468 | if (PyErr_Occurred()) |
| 2469 | goto error; |
| 2470 | |
| 2471 | /* Only one item to write */ |
| 2472 | if (save(self, firstitem, 0) < 0) |
| 2473 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2474 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2475 | goto error; |
| 2476 | Py_CLEAR(firstitem); |
| 2477 | break; |
| 2478 | } |
| 2479 | |
| 2480 | /* More than one item to write */ |
| 2481 | |
| 2482 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2483 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2484 | goto error; |
| 2485 | |
| 2486 | if (save(self, firstitem, 0) < 0) |
| 2487 | goto error; |
| 2488 | Py_CLEAR(firstitem); |
| 2489 | n = 1; |
| 2490 | |
| 2491 | /* Fetch and save up to BATCHSIZE items */ |
| 2492 | while (obj) { |
| 2493 | if (save(self, obj, 0) < 0) |
| 2494 | goto error; |
| 2495 | Py_CLEAR(obj); |
| 2496 | n += 1; |
| 2497 | |
| 2498 | if (n == BATCHSIZE) |
| 2499 | break; |
| 2500 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2501 | obj = PyIter_Next(iter); |
| 2502 | if (obj == NULL) { |
| 2503 | if (PyErr_Occurred()) |
| 2504 | goto error; |
| 2505 | break; |
| 2506 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2507 | } |
| 2508 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2509 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2510 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2511 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2512 | } while (n == BATCHSIZE); |
| 2513 | return 0; |
| 2514 | |
| 2515 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2516 | Py_XDECREF(firstitem); |
| 2517 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2518 | return -1; |
| 2519 | } |
| 2520 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2521 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2522 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2523 | * MARK item item ... item APPENDS |
| 2524 | * opcode sequences. Calling code should have arranged to first create an |
| 2525 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2526 | * Returns 0 on success, -1 on error. |
| 2527 | * |
| 2528 | * This version is considerably faster than batch_list(), if less general. |
| 2529 | * |
| 2530 | * Note that this only works for protocols > 0. |
| 2531 | */ |
| 2532 | static int |
| 2533 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2534 | { |
| 2535 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2536 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2537 | |
| 2538 | const char append_op = APPEND; |
| 2539 | const char appends_op = APPENDS; |
| 2540 | const char mark_op = MARK; |
| 2541 | |
| 2542 | assert(obj != NULL); |
| 2543 | assert(self->proto > 0); |
| 2544 | assert(PyList_CheckExact(obj)); |
| 2545 | |
| 2546 | if (PyList_GET_SIZE(obj) == 1) { |
| 2547 | item = PyList_GET_ITEM(obj, 0); |
| 2548 | if (save(self, item, 0) < 0) |
| 2549 | return -1; |
| 2550 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2551 | return -1; |
| 2552 | return 0; |
| 2553 | } |
| 2554 | |
| 2555 | /* Write in batches of BATCHSIZE. */ |
| 2556 | total = 0; |
| 2557 | do { |
| 2558 | this_batch = 0; |
| 2559 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2560 | return -1; |
| 2561 | while (total < PyList_GET_SIZE(obj)) { |
| 2562 | item = PyList_GET_ITEM(obj, total); |
| 2563 | if (save(self, item, 0) < 0) |
| 2564 | return -1; |
| 2565 | total++; |
| 2566 | if (++this_batch == BATCHSIZE) |
| 2567 | break; |
| 2568 | } |
| 2569 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2570 | return -1; |
| 2571 | |
| 2572 | } while (total < PyList_GET_SIZE(obj)); |
| 2573 | |
| 2574 | return 0; |
| 2575 | } |
| 2576 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2577 | static int |
| 2578 | save_list(PicklerObject *self, PyObject *obj) |
| 2579 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2580 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2581 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2582 | int status = 0; |
| 2583 | |
| 2584 | if (self->fast && !fast_save_enter(self, obj)) |
| 2585 | goto error; |
| 2586 | |
| 2587 | /* Create an empty list. */ |
| 2588 | if (self->bin) { |
| 2589 | header[0] = EMPTY_LIST; |
| 2590 | len = 1; |
| 2591 | } |
| 2592 | else { |
| 2593 | header[0] = MARK; |
| 2594 | header[1] = LIST; |
| 2595 | len = 2; |
| 2596 | } |
| 2597 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2598 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2599 | goto error; |
| 2600 | |
| 2601 | /* Get list length, and bow out early if empty. */ |
| 2602 | if ((len = PyList_Size(obj)) < 0) |
| 2603 | goto error; |
| 2604 | |
| 2605 | if (memo_put(self, obj) < 0) |
| 2606 | goto error; |
| 2607 | |
| 2608 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2609 | /* Materialize the list elements. */ |
| 2610 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2611 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2612 | goto error; |
| 2613 | status = batch_list_exact(self, obj); |
| 2614 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2615 | } else { |
| 2616 | PyObject *iter = PyObject_GetIter(obj); |
| 2617 | if (iter == NULL) |
| 2618 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2620 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2621 | Py_DECREF(iter); |
| 2622 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2623 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2624 | status = batch_list(self, iter); |
| 2625 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2626 | Py_DECREF(iter); |
| 2627 | } |
| 2628 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2629 | if (0) { |
| 2630 | error: |
| 2631 | status = -1; |
| 2632 | } |
| 2633 | |
| 2634 | if (self->fast && !fast_save_leave(self, obj)) |
| 2635 | status = -1; |
| 2636 | |
| 2637 | return status; |
| 2638 | } |
| 2639 | |
| 2640 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2641 | * MARK key value ... key value SETITEMS |
| 2642 | * opcode sequences. Calling code should have arranged to first create an |
| 2643 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2644 | * Returns 0 on success, <0 on error. |
| 2645 | * |
| 2646 | * This is very much like batch_list(). The difference between saving |
| 2647 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2648 | * the C level, though, that attempts to combine these routines were too |
| 2649 | * ugly to bear. |
| 2650 | */ |
| 2651 | static int |
| 2652 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2653 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2654 | PyObject *obj = NULL; |
| 2655 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2656 | int i, n; |
| 2657 | |
| 2658 | const char mark_op = MARK; |
| 2659 | const char setitem_op = SETITEM; |
| 2660 | const char setitems_op = SETITEMS; |
| 2661 | |
| 2662 | assert(iter != NULL); |
| 2663 | |
| 2664 | if (self->proto == 0) { |
| 2665 | /* SETITEMS isn't available; do one at a time. */ |
| 2666 | for (;;) { |
| 2667 | obj = PyIter_Next(iter); |
| 2668 | if (obj == NULL) { |
| 2669 | if (PyErr_Occurred()) |
| 2670 | return -1; |
| 2671 | break; |
| 2672 | } |
| 2673 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2674 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2675 | "iterator must return 2-tuples"); |
| 2676 | return -1; |
| 2677 | } |
| 2678 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2679 | if (i >= 0) |
| 2680 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2681 | Py_DECREF(obj); |
| 2682 | if (i < 0) |
| 2683 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2684 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2685 | return -1; |
| 2686 | } |
| 2687 | return 0; |
| 2688 | } |
| 2689 | |
| 2690 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2691 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2692 | /* Get first item */ |
| 2693 | firstitem = PyIter_Next(iter); |
| 2694 | if (firstitem == NULL) { |
| 2695 | if (PyErr_Occurred()) |
| 2696 | goto error; |
| 2697 | |
| 2698 | /* nothing more to add */ |
| 2699 | break; |
| 2700 | } |
| 2701 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2702 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2703 | "iterator must return 2-tuples"); |
| 2704 | goto error; |
| 2705 | } |
| 2706 | |
| 2707 | /* Try to get a second item */ |
| 2708 | obj = PyIter_Next(iter); |
| 2709 | if (obj == NULL) { |
| 2710 | if (PyErr_Occurred()) |
| 2711 | goto error; |
| 2712 | |
| 2713 | /* Only one item to write */ |
| 2714 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2715 | goto error; |
| 2716 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2717 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2718 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2719 | goto error; |
| 2720 | Py_CLEAR(firstitem); |
| 2721 | break; |
| 2722 | } |
| 2723 | |
| 2724 | /* More than one item to write */ |
| 2725 | |
| 2726 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2727 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2728 | goto error; |
| 2729 | |
| 2730 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2731 | goto error; |
| 2732 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2733 | goto error; |
| 2734 | Py_CLEAR(firstitem); |
| 2735 | n = 1; |
| 2736 | |
| 2737 | /* Fetch and save up to BATCHSIZE items */ |
| 2738 | while (obj) { |
| 2739 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2740 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2741 | "iterator must return 2-tuples"); |
| 2742 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2743 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2744 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2745 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2746 | goto error; |
| 2747 | Py_CLEAR(obj); |
| 2748 | n += 1; |
| 2749 | |
| 2750 | if (n == BATCHSIZE) |
| 2751 | break; |
| 2752 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2753 | obj = PyIter_Next(iter); |
| 2754 | if (obj == NULL) { |
| 2755 | if (PyErr_Occurred()) |
| 2756 | goto error; |
| 2757 | break; |
| 2758 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2759 | } |
| 2760 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2761 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2762 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2763 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2764 | } while (n == BATCHSIZE); |
| 2765 | return 0; |
| 2766 | |
| 2767 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2768 | Py_XDECREF(firstitem); |
| 2769 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2770 | return -1; |
| 2771 | } |
| 2772 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2773 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2774 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2775 | * MARK key value ... key value SETITEMS |
| 2776 | * opcode sequences. Calling code should have arranged to first create an |
| 2777 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2778 | * Returns 0 on success, -1 on error. |
| 2779 | * |
| 2780 | * Note that this currently doesn't work for protocol 0. |
| 2781 | */ |
| 2782 | static int |
| 2783 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2784 | { |
| 2785 | PyObject *key = NULL, *value = NULL; |
| 2786 | int i; |
| 2787 | Py_ssize_t dict_size, ppos = 0; |
| 2788 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2789 | const char mark_op = MARK; |
| 2790 | const char setitem_op = SETITEM; |
| 2791 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2792 | |
| 2793 | assert(obj != NULL); |
| 2794 | assert(self->proto > 0); |
| 2795 | |
| 2796 | dict_size = PyDict_Size(obj); |
| 2797 | |
| 2798 | /* Special-case len(d) == 1 to save space. */ |
| 2799 | if (dict_size == 1) { |
| 2800 | PyDict_Next(obj, &ppos, &key, &value); |
| 2801 | if (save(self, key, 0) < 0) |
| 2802 | return -1; |
| 2803 | if (save(self, value, 0) < 0) |
| 2804 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2805 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2806 | return -1; |
| 2807 | return 0; |
| 2808 | } |
| 2809 | |
| 2810 | /* Write in batches of BATCHSIZE. */ |
| 2811 | do { |
| 2812 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2813 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2814 | return -1; |
| 2815 | while (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; |
| 2820 | if (++i == BATCHSIZE) |
| 2821 | break; |
| 2822 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2823 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2824 | return -1; |
| 2825 | if (PyDict_Size(obj) != dict_size) { |
| 2826 | PyErr_Format( |
| 2827 | PyExc_RuntimeError, |
| 2828 | "dictionary changed size during iteration"); |
| 2829 | return -1; |
| 2830 | } |
| 2831 | |
| 2832 | } while (i == BATCHSIZE); |
| 2833 | return 0; |
| 2834 | } |
| 2835 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2836 | static int |
| 2837 | save_dict(PicklerObject *self, PyObject *obj) |
| 2838 | { |
| 2839 | PyObject *items, *iter; |
| 2840 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2841 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2842 | int status = 0; |
| 2843 | |
| 2844 | if (self->fast && !fast_save_enter(self, obj)) |
| 2845 | goto error; |
| 2846 | |
| 2847 | /* Create an empty dict. */ |
| 2848 | if (self->bin) { |
| 2849 | header[0] = EMPTY_DICT; |
| 2850 | len = 1; |
| 2851 | } |
| 2852 | else { |
| 2853 | header[0] = MARK; |
| 2854 | header[1] = DICT; |
| 2855 | len = 2; |
| 2856 | } |
| 2857 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2858 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2859 | goto error; |
| 2860 | |
| 2861 | /* Get dict size, and bow out early if empty. */ |
| 2862 | if ((len = PyDict_Size(obj)) < 0) |
| 2863 | goto error; |
| 2864 | |
| 2865 | if (memo_put(self, obj) < 0) |
| 2866 | goto error; |
| 2867 | |
| 2868 | if (len != 0) { |
| 2869 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2870 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2871 | /* We can take certain shortcuts if we know this is a dict and |
| 2872 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2873 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2874 | goto error; |
| 2875 | status = batch_dict_exact(self, obj); |
| 2876 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2877 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2878 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2879 | |
| 2880 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2881 | if (items == NULL) |
| 2882 | goto error; |
| 2883 | iter = PyObject_GetIter(items); |
| 2884 | Py_DECREF(items); |
| 2885 | if (iter == NULL) |
| 2886 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2887 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2888 | Py_DECREF(iter); |
| 2889 | goto error; |
| 2890 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2891 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2892 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2893 | Py_DECREF(iter); |
| 2894 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | if (0) { |
| 2898 | error: |
| 2899 | status = -1; |
| 2900 | } |
| 2901 | |
| 2902 | if (self->fast && !fast_save_leave(self, obj)) |
| 2903 | status = -1; |
| 2904 | |
| 2905 | return status; |
| 2906 | } |
| 2907 | |
| 2908 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2909 | save_set(PicklerObject *self, PyObject *obj) |
| 2910 | { |
| 2911 | PyObject *item; |
| 2912 | int i; |
| 2913 | Py_ssize_t set_size, ppos = 0; |
| 2914 | Py_hash_t hash; |
| 2915 | |
| 2916 | const char empty_set_op = EMPTY_SET; |
| 2917 | const char mark_op = MARK; |
| 2918 | const char additems_op = ADDITEMS; |
| 2919 | |
| 2920 | if (self->proto < 4) { |
| 2921 | PyObject *items; |
| 2922 | PyObject *reduce_value; |
| 2923 | int status; |
| 2924 | |
| 2925 | items = PySequence_List(obj); |
| 2926 | if (items == NULL) { |
| 2927 | return -1; |
| 2928 | } |
| 2929 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2930 | Py_DECREF(items); |
| 2931 | if (reduce_value == NULL) { |
| 2932 | return -1; |
| 2933 | } |
| 2934 | /* save_reduce() will memoize the object automatically. */ |
| 2935 | status = save_reduce(self, reduce_value, obj); |
| 2936 | Py_DECREF(reduce_value); |
| 2937 | return status; |
| 2938 | } |
| 2939 | |
| 2940 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2941 | return -1; |
| 2942 | |
| 2943 | if (memo_put(self, obj) < 0) |
| 2944 | return -1; |
| 2945 | |
| 2946 | set_size = PySet_GET_SIZE(obj); |
| 2947 | if (set_size == 0) |
| 2948 | return 0; /* nothing to do */ |
| 2949 | |
| 2950 | /* Write in batches of BATCHSIZE. */ |
| 2951 | do { |
| 2952 | i = 0; |
| 2953 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2954 | return -1; |
| 2955 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2956 | if (save(self, item, 0) < 0) |
| 2957 | return -1; |
| 2958 | if (++i == BATCHSIZE) |
| 2959 | break; |
| 2960 | } |
| 2961 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2962 | return -1; |
| 2963 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2964 | PyErr_Format( |
| 2965 | PyExc_RuntimeError, |
| 2966 | "set changed size during iteration"); |
| 2967 | return -1; |
| 2968 | } |
| 2969 | } while (i == BATCHSIZE); |
| 2970 | |
| 2971 | return 0; |
| 2972 | } |
| 2973 | |
| 2974 | static int |
| 2975 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2976 | { |
| 2977 | PyObject *iter; |
| 2978 | |
| 2979 | const char mark_op = MARK; |
| 2980 | const char frozenset_op = FROZENSET; |
| 2981 | |
| 2982 | if (self->fast && !fast_save_enter(self, obj)) |
| 2983 | return -1; |
| 2984 | |
| 2985 | if (self->proto < 4) { |
| 2986 | PyObject *items; |
| 2987 | PyObject *reduce_value; |
| 2988 | int status; |
| 2989 | |
| 2990 | items = PySequence_List(obj); |
| 2991 | if (items == NULL) { |
| 2992 | return -1; |
| 2993 | } |
| 2994 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2995 | items); |
| 2996 | Py_DECREF(items); |
| 2997 | if (reduce_value == NULL) { |
| 2998 | return -1; |
| 2999 | } |
| 3000 | /* save_reduce() will memoize the object automatically. */ |
| 3001 | status = save_reduce(self, reduce_value, obj); |
| 3002 | Py_DECREF(reduce_value); |
| 3003 | return status; |
| 3004 | } |
| 3005 | |
| 3006 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 3007 | return -1; |
| 3008 | |
| 3009 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3010 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 3011 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 3012 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3013 | for (;;) { |
| 3014 | PyObject *item; |
| 3015 | |
| 3016 | item = PyIter_Next(iter); |
| 3017 | if (item == NULL) { |
| 3018 | if (PyErr_Occurred()) { |
| 3019 | Py_DECREF(iter); |
| 3020 | return -1; |
| 3021 | } |
| 3022 | break; |
| 3023 | } |
| 3024 | if (save(self, item, 0) < 0) { |
| 3025 | Py_DECREF(item); |
| 3026 | Py_DECREF(iter); |
| 3027 | return -1; |
| 3028 | } |
| 3029 | Py_DECREF(item); |
| 3030 | } |
| 3031 | Py_DECREF(iter); |
| 3032 | |
| 3033 | /* If the object is already in the memo, this means it is |
| 3034 | recursive. In this case, throw away everything we put on the |
| 3035 | stack, and fetch the object back from the memo. */ |
| 3036 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3037 | const char pop_mark_op = POP_MARK; |
| 3038 | |
| 3039 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3040 | return -1; |
| 3041 | if (memo_get(self, obj) < 0) |
| 3042 | return -1; |
| 3043 | return 0; |
| 3044 | } |
| 3045 | |
| 3046 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3047 | return -1; |
| 3048 | if (memo_put(self, obj) < 0) |
| 3049 | return -1; |
| 3050 | |
| 3051 | return 0; |
| 3052 | } |
| 3053 | |
| 3054 | static int |
| 3055 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3056 | { |
| 3057 | PyObject *key; |
| 3058 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3059 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3060 | |
| 3061 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3062 | if (key == NULL) |
| 3063 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3064 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3065 | Py_DECREF(key); |
| 3066 | if (item) { |
| 3067 | PyObject *fixed_module_name; |
| 3068 | PyObject *fixed_global_name; |
| 3069 | |
| 3070 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3071 | PyErr_Format(PyExc_RuntimeError, |
| 3072 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3073 | "should be 2-tuples, not %.200s", |
| 3074 | Py_TYPE(item)->tp_name); |
| 3075 | return -1; |
| 3076 | } |
| 3077 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3078 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3079 | if (!PyUnicode_Check(fixed_module_name) || |
| 3080 | !PyUnicode_Check(fixed_global_name)) { |
| 3081 | PyErr_Format(PyExc_RuntimeError, |
| 3082 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3083 | "should be pairs of str, not (%.200s, %.200s)", |
| 3084 | Py_TYPE(fixed_module_name)->tp_name, |
| 3085 | Py_TYPE(fixed_global_name)->tp_name); |
| 3086 | return -1; |
| 3087 | } |
| 3088 | |
| 3089 | Py_CLEAR(*module_name); |
| 3090 | Py_CLEAR(*global_name); |
| 3091 | Py_INCREF(fixed_module_name); |
| 3092 | Py_INCREF(fixed_global_name); |
| 3093 | *module_name = fixed_module_name; |
| 3094 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3095 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3096 | } |
| 3097 | else if (PyErr_Occurred()) { |
| 3098 | return -1; |
| 3099 | } |
| 3100 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3101 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3102 | if (item) { |
| 3103 | if (!PyUnicode_Check(item)) { |
| 3104 | PyErr_Format(PyExc_RuntimeError, |
| 3105 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3106 | "should be strings, not %.200s", |
| 3107 | Py_TYPE(item)->tp_name); |
| 3108 | return -1; |
| 3109 | } |
| 3110 | Py_CLEAR(*module_name); |
| 3111 | Py_INCREF(item); |
| 3112 | *module_name = item; |
| 3113 | } |
| 3114 | else if (PyErr_Occurred()) { |
| 3115 | return -1; |
| 3116 | } |
| 3117 | |
| 3118 | return 0; |
| 3119 | } |
| 3120 | |
| 3121 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3122 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3123 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3124 | PyObject *global_name = NULL; |
| 3125 | PyObject *module_name = NULL; |
| 3126 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3127 | PyObject *parent = NULL; |
| 3128 | PyObject *dotted_path = NULL; |
| 3129 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3130 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3131 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3132 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3133 | _Py_IDENTIFIER(__name__); |
| 3134 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3135 | |
| 3136 | const char global_op = GLOBAL; |
| 3137 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3138 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3139 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3140 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3141 | } |
| 3142 | else { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3143 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3144 | if (global_name == NULL) { |
| 3145 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3146 | goto error; |
| 3147 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3148 | } |
| 3149 | if (global_name == NULL) { |
| 3150 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3151 | if (global_name == NULL) |
| 3152 | goto error; |
| 3153 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3154 | } |
| 3155 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3156 | dotted_path = get_dotted_path(module, global_name); |
| 3157 | if (dotted_path == NULL) |
| 3158 | goto error; |
| 3159 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3160 | if (module_name == NULL) |
| 3161 | goto error; |
| 3162 | |
| 3163 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3164 | relative imports. |
| 3165 | |
| 3166 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3167 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3168 | custom import functions (IMHO, this would be a nice security |
| 3169 | feature). The import C API would need to be extended to support the |
| 3170 | extra parameters of __import__ to fix that. */ |
| 3171 | module = PyImport_Import(module_name); |
| 3172 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3173 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3174 | "Can't pickle %R: import of module %R failed", |
| 3175 | obj, module_name); |
| 3176 | goto error; |
| 3177 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3178 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3179 | Py_INCREF(lastname); |
| 3180 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3181 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3182 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3183 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3184 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3185 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3186 | goto error; |
| 3187 | } |
| 3188 | if (cls != obj) { |
| 3189 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3190 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3191 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3192 | obj, module_name, global_name); |
| 3193 | goto error; |
| 3194 | } |
| 3195 | Py_DECREF(cls); |
| 3196 | |
| 3197 | if (self->proto >= 2) { |
| 3198 | /* See whether this is in the extension registry, and if |
| 3199 | * so generate an EXT opcode. |
| 3200 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3201 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3202 | PyObject *code_obj; /* extension code as Python object */ |
| 3203 | long code; /* extension code as C value */ |
| 3204 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3205 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3206 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3207 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3208 | if (extension_key == NULL) { |
| 3209 | goto error; |
| 3210 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3211 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3212 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3213 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3214 | /* The object is not registered in the extension registry. |
| 3215 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3216 | if (code_obj == NULL) { |
| 3217 | if (PyErr_Occurred()) { |
| 3218 | goto error; |
| 3219 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3220 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3221 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3222 | |
| 3223 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3224 | of the value returned by the extension_registry. It should for |
| 3225 | consistency. */ |
| 3226 | |
| 3227 | /* Verify code_obj has the right type and value. */ |
| 3228 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3229 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3230 | "Can't pickle %R: extension code %R isn't an integer", |
| 3231 | obj, code_obj); |
| 3232 | goto error; |
| 3233 | } |
| 3234 | code = PyLong_AS_LONG(code_obj); |
| 3235 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3236 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3237 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3238 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3239 | goto error; |
| 3240 | } |
| 3241 | |
| 3242 | /* Generate an EXT opcode. */ |
| 3243 | if (code <= 0xff) { |
| 3244 | pdata[0] = EXT1; |
| 3245 | pdata[1] = (unsigned char)code; |
| 3246 | n = 2; |
| 3247 | } |
| 3248 | else if (code <= 0xffff) { |
| 3249 | pdata[0] = EXT2; |
| 3250 | pdata[1] = (unsigned char)(code & 0xff); |
| 3251 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3252 | n = 3; |
| 3253 | } |
| 3254 | else { |
| 3255 | pdata[0] = EXT4; |
| 3256 | pdata[1] = (unsigned char)(code & 0xff); |
| 3257 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3258 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3259 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3260 | n = 5; |
| 3261 | } |
| 3262 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3263 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3264 | goto error; |
| 3265 | } |
| 3266 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3267 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3268 | if (parent == module) { |
| 3269 | Py_INCREF(lastname); |
| 3270 | Py_DECREF(global_name); |
| 3271 | global_name = lastname; |
| 3272 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3273 | if (self->proto >= 4) { |
| 3274 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3275 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3276 | if (save(self, module_name, 0) < 0) |
| 3277 | goto error; |
| 3278 | if (save(self, global_name, 0) < 0) |
| 3279 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3280 | |
| 3281 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3282 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3283 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3284 | else if (parent != module) { |
| 3285 | PickleState *st = _Pickle_GetGlobalState(); |
| 3286 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3287 | st->getattr, parent, lastname); |
| 3288 | status = save_reduce(self, reduce_value, NULL); |
| 3289 | Py_DECREF(reduce_value); |
| 3290 | if (status < 0) |
| 3291 | goto error; |
| 3292 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3293 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3294 | /* Generate a normal global opcode if we are using a pickle |
| 3295 | protocol < 4, or if the object is not registered in the |
| 3296 | extension registry. */ |
| 3297 | PyObject *encoded; |
| 3298 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3299 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3300 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3301 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3302 | |
| 3303 | /* For protocol < 3 and if the user didn't request against doing |
| 3304 | so, we convert module names to the old 2.x module names. */ |
| 3305 | if (self->proto < 3 && self->fix_imports) { |
| 3306 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3307 | goto error; |
| 3308 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3309 | } |
| 3310 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3311 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3312 | both the module name and the global name using UTF-8. We do so |
| 3313 | only when we are using the pickle protocol newer than version |
| 3314 | 3. This is to ensure compatibility with older Unpickler running |
| 3315 | on Python 2.x. */ |
| 3316 | if (self->proto == 3) { |
| 3317 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3318 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3319 | else { |
| 3320 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3321 | } |
| 3322 | encoded = unicode_encoder(module_name); |
| 3323 | if (encoded == NULL) { |
| 3324 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3325 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3326 | "can't pickle module identifier '%S' using " |
| 3327 | "pickle protocol %i", |
| 3328 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3329 | goto error; |
| 3330 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3331 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3332 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3333 | Py_DECREF(encoded); |
| 3334 | goto error; |
| 3335 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3336 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3337 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3338 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3339 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3340 | /* Save the name of the module. */ |
| 3341 | encoded = unicode_encoder(global_name); |
| 3342 | if (encoded == NULL) { |
| 3343 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3344 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3345 | "can't pickle global identifier '%S' using " |
| 3346 | "pickle protocol %i", |
| 3347 | global_name, self->proto); |
| 3348 | goto error; |
| 3349 | } |
| 3350 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3351 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3352 | Py_DECREF(encoded); |
| 3353 | goto error; |
| 3354 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3355 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3356 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3357 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3358 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3359 | /* Memoize the object. */ |
| 3360 | if (memo_put(self, obj) < 0) |
| 3361 | goto error; |
| 3362 | } |
| 3363 | |
| 3364 | if (0) { |
| 3365 | error: |
| 3366 | status = -1; |
| 3367 | } |
| 3368 | Py_XDECREF(module_name); |
| 3369 | Py_XDECREF(global_name); |
| 3370 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3371 | Py_XDECREF(parent); |
| 3372 | Py_XDECREF(dotted_path); |
| 3373 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3374 | |
| 3375 | return status; |
| 3376 | } |
| 3377 | |
| 3378 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3379 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3380 | { |
| 3381 | PyObject *reduce_value; |
| 3382 | int status; |
| 3383 | |
| 3384 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3385 | if (reduce_value == NULL) { |
| 3386 | return -1; |
| 3387 | } |
| 3388 | status = save_reduce(self, reduce_value, obj); |
| 3389 | Py_DECREF(reduce_value); |
| 3390 | return status; |
| 3391 | } |
| 3392 | |
| 3393 | static int |
| 3394 | save_type(PicklerObject *self, PyObject *obj) |
| 3395 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3396 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3397 | return save_singleton_type(self, obj, Py_None); |
| 3398 | } |
| 3399 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3400 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3401 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3402 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3403 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3404 | } |
| 3405 | return save_global(self, obj, NULL); |
| 3406 | } |
| 3407 | |
| 3408 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3409 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3410 | { |
| 3411 | PyObject *pid = NULL; |
| 3412 | int status = 0; |
| 3413 | |
| 3414 | const char persid_op = PERSID; |
| 3415 | const char binpersid_op = BINPERSID; |
| 3416 | |
| 3417 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3418 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3419 | if (pid == NULL) |
| 3420 | return -1; |
| 3421 | |
| 3422 | if (pid != Py_None) { |
| 3423 | if (self->bin) { |
| 3424 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3425 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3426 | goto error; |
| 3427 | } |
| 3428 | else { |
| 3429 | PyObject *pid_str = NULL; |
| 3430 | char *pid_ascii_bytes; |
| 3431 | Py_ssize_t size; |
| 3432 | |
| 3433 | pid_str = PyObject_Str(pid); |
| 3434 | if (pid_str == NULL) |
| 3435 | goto error; |
| 3436 | |
| 3437 | /* XXX: Should it check whether the persistent id only contains |
| 3438 | ASCII characters? And what if the pid contains embedded |
| 3439 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3440 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3441 | Py_DECREF(pid_str); |
| 3442 | if (pid_ascii_bytes == NULL) |
| 3443 | goto error; |
| 3444 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3445 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3446 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3447 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3448 | goto error; |
| 3449 | } |
| 3450 | status = 1; |
| 3451 | } |
| 3452 | |
| 3453 | if (0) { |
| 3454 | error: |
| 3455 | status = -1; |
| 3456 | } |
| 3457 | Py_XDECREF(pid); |
| 3458 | |
| 3459 | return status; |
| 3460 | } |
| 3461 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3462 | static PyObject * |
| 3463 | get_class(PyObject *obj) |
| 3464 | { |
| 3465 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3466 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3467 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3468 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3469 | if (cls == NULL) { |
| 3470 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3471 | PyErr_Clear(); |
| 3472 | cls = (PyObject *) Py_TYPE(obj); |
| 3473 | Py_INCREF(cls); |
| 3474 | } |
| 3475 | } |
| 3476 | return cls; |
| 3477 | } |
| 3478 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3479 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3480 | * appropriate __reduce__ method for obj. |
| 3481 | */ |
| 3482 | static int |
| 3483 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3484 | { |
| 3485 | PyObject *callable; |
| 3486 | PyObject *argtup; |
| 3487 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3488 | PyObject *listitems = Py_None; |
| 3489 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3490 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3491 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3492 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3493 | |
| 3494 | const char reduce_op = REDUCE; |
| 3495 | const char build_op = BUILD; |
| 3496 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3497 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3498 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3499 | size = PyTuple_Size(args); |
| 3500 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3501 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3502 | "__reduce__ must contain 2 through 5 elements"); |
| 3503 | return -1; |
| 3504 | } |
| 3505 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3506 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3507 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3508 | return -1; |
| 3509 | |
| 3510 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3511 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3512 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3513 | return -1; |
| 3514 | } |
| 3515 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3516 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3517 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3518 | return -1; |
| 3519 | } |
| 3520 | |
| 3521 | if (state == Py_None) |
| 3522 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3523 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3524 | if (listitems == Py_None) |
| 3525 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3526 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3527 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3528 | "returned by __reduce__ must be an iterator, not %s", |
| 3529 | Py_TYPE(listitems)->tp_name); |
| 3530 | return -1; |
| 3531 | } |
| 3532 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3533 | if (dictitems == Py_None) |
| 3534 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3535 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3536 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3537 | "returned by __reduce__ must be an iterator, not %s", |
| 3538 | Py_TYPE(dictitems)->tp_name); |
| 3539 | return -1; |
| 3540 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3541 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3542 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3543 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3544 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3545 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3546 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3547 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3548 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3549 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3550 | } |
| 3551 | PyErr_Clear(); |
| 3552 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3553 | else if (PyUnicode_Check(name)) { |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3554 | _Py_IDENTIFIER(__newobj_ex__); |
| 3555 | use_newobj_ex = PyUnicode_Compare( |
| 3556 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3557 | if (!use_newobj_ex) { |
| 3558 | _Py_IDENTIFIER(__newobj__); |
| 3559 | use_newobj = PyUnicode_Compare( |
| 3560 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
| 3561 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3562 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3563 | Py_XDECREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3564 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3565 | |
| 3566 | if (use_newobj_ex) { |
| 3567 | PyObject *cls; |
| 3568 | PyObject *args; |
| 3569 | PyObject *kwargs; |
| 3570 | |
| 3571 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3572 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3573 | "length of the NEWOBJ_EX argument tuple must be " |
| 3574 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3575 | return -1; |
| 3576 | } |
| 3577 | |
| 3578 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3579 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3580 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3581 | "first item from NEWOBJ_EX argument tuple must " |
| 3582 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3583 | return -1; |
| 3584 | } |
| 3585 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3586 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3587 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3588 | "second item from NEWOBJ_EX argument tuple must " |
| 3589 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3590 | return -1; |
| 3591 | } |
| 3592 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3593 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3594 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3595 | "third item from NEWOBJ_EX argument tuple must " |
| 3596 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3597 | return -1; |
| 3598 | } |
| 3599 | |
Serhiy Storchaka | 0d554d7 | 2015-10-10 22:42:18 +0300 | [diff] [blame] | 3600 | if (self->proto >= 4) { |
| 3601 | if (save(self, cls, 0) < 0 || |
| 3602 | save(self, args, 0) < 0 || |
| 3603 | save(self, kwargs, 0) < 0 || |
| 3604 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3605 | return -1; |
| 3606 | } |
| 3607 | } |
| 3608 | else { |
| 3609 | PyObject *newargs; |
| 3610 | PyObject *cls_new; |
| 3611 | Py_ssize_t i; |
| 3612 | _Py_IDENTIFIER(__new__); |
| 3613 | |
| 3614 | newargs = PyTuple_New(Py_SIZE(args) + 2); |
| 3615 | if (newargs == NULL) |
| 3616 | return -1; |
| 3617 | |
| 3618 | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
| 3619 | if (cls_new == NULL) { |
| 3620 | Py_DECREF(newargs); |
| 3621 | return -1; |
| 3622 | } |
| 3623 | PyTuple_SET_ITEM(newargs, 0, cls_new); |
| 3624 | Py_INCREF(cls); |
| 3625 | PyTuple_SET_ITEM(newargs, 1, cls); |
| 3626 | for (i = 0; i < Py_SIZE(args); i++) { |
| 3627 | PyObject *item = PyTuple_GET_ITEM(args, i); |
| 3628 | Py_INCREF(item); |
| 3629 | PyTuple_SET_ITEM(newargs, i + 2, item); |
| 3630 | } |
| 3631 | |
| 3632 | callable = PyObject_Call(st->partial, newargs, kwargs); |
| 3633 | Py_DECREF(newargs); |
| 3634 | if (callable == NULL) |
| 3635 | return -1; |
| 3636 | |
| 3637 | newargs = PyTuple_New(0); |
| 3638 | if (newargs == NULL) { |
| 3639 | Py_DECREF(callable); |
| 3640 | return -1; |
| 3641 | } |
| 3642 | |
| 3643 | if (save(self, callable, 0) < 0 || |
| 3644 | save(self, newargs, 0) < 0 || |
| 3645 | _Pickler_Write(self, &reduce_op, 1) < 0) { |
| 3646 | Py_DECREF(newargs); |
| 3647 | Py_DECREF(callable); |
| 3648 | return -1; |
| 3649 | } |
| 3650 | Py_DECREF(newargs); |
| 3651 | Py_DECREF(callable); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3652 | } |
| 3653 | } |
| 3654 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3655 | PyObject *cls; |
| 3656 | PyObject *newargtup; |
| 3657 | PyObject *obj_class; |
| 3658 | int p; |
| 3659 | |
| 3660 | /* Sanity checks. */ |
| 3661 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3662 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3663 | return -1; |
| 3664 | } |
| 3665 | |
| 3666 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3667 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3668 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3669 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3670 | return -1; |
| 3671 | } |
| 3672 | |
| 3673 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3674 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3675 | p = obj_class != cls; /* true iff a problem */ |
| 3676 | Py_DECREF(obj_class); |
| 3677 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3678 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3679 | "__newobj__ args has the wrong class"); |
| 3680 | return -1; |
| 3681 | } |
| 3682 | } |
| 3683 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3684 | what happen if the value returned by the __reduce__() method of |
| 3685 | some extension type contains another object of the same type. Ouch! |
| 3686 | |
| 3687 | Here is a quick example, that I ran into, to illustrate what I |
| 3688 | mean: |
| 3689 | |
| 3690 | >>> import pickle, copyreg |
| 3691 | >>> copyreg.dispatch_table.pop(complex) |
| 3692 | >>> pickle.dumps(1+2j) |
| 3693 | Traceback (most recent call last): |
| 3694 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 3695 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3696 | |
| 3697 | Removing the complex class from copyreg.dispatch_table made the |
| 3698 | __reduce_ex__() method emit another complex object: |
| 3699 | |
| 3700 | >>> (1+1j).__reduce_ex__(2) |
| 3701 | (<function __newobj__ at 0xb7b71c3c>, |
| 3702 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3703 | |
| 3704 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3705 | ensued. Of course, the bug was in the complex class which had a |
| 3706 | broken __getnewargs__() that emitted another complex object. But, |
| 3707 | the point, here, is it is quite easy to end up with a broken reduce |
| 3708 | function. */ |
| 3709 | |
| 3710 | /* Save the class and its __new__ arguments. */ |
| 3711 | if (save(self, cls, 0) < 0) |
| 3712 | return -1; |
| 3713 | |
| 3714 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3715 | if (newargtup == NULL) |
| 3716 | return -1; |
| 3717 | |
| 3718 | p = save(self, newargtup, 0); |
| 3719 | Py_DECREF(newargtup); |
| 3720 | if (p < 0) |
| 3721 | return -1; |
| 3722 | |
| 3723 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3724 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3725 | return -1; |
| 3726 | } |
| 3727 | else { /* Not using NEWOBJ. */ |
| 3728 | if (save(self, callable, 0) < 0 || |
| 3729 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3730 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3731 | return -1; |
| 3732 | } |
| 3733 | |
| 3734 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3735 | the caller do not want to memoize the object. Not particularly useful, |
| 3736 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3737 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3738 | if (obj != NULL) { |
| 3739 | /* If the object is already in the memo, this means it is |
| 3740 | recursive. In this case, throw away everything we put on the |
| 3741 | stack, and fetch the object back from the memo. */ |
| 3742 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3743 | const char pop_op = POP; |
| 3744 | |
| 3745 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3746 | return -1; |
| 3747 | if (memo_get(self, obj) < 0) |
| 3748 | return -1; |
| 3749 | |
| 3750 | return 0; |
| 3751 | } |
| 3752 | else if (memo_put(self, obj) < 0) |
| 3753 | return -1; |
| 3754 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3755 | |
| 3756 | if (listitems && batch_list(self, listitems) < 0) |
| 3757 | return -1; |
| 3758 | |
| 3759 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3760 | return -1; |
| 3761 | |
| 3762 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3763 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3764 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3765 | return -1; |
| 3766 | } |
| 3767 | |
| 3768 | return 0; |
| 3769 | } |
| 3770 | |
| 3771 | static int |
| 3772 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3773 | { |
| 3774 | PyTypeObject *type; |
| 3775 | PyObject *reduce_func = NULL; |
| 3776 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3777 | int status = 0; |
| 3778 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3779 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3780 | return -1; |
| 3781 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3782 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3783 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3784 | |
| 3785 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3786 | on its returned object. */ |
| 3787 | if (!pers_save && self->pers_func) { |
| 3788 | /* save_pers() returns: |
| 3789 | -1 to signal an error; |
| 3790 | 0 if it did nothing successfully; |
| 3791 | 1 if a persistent id was saved. |
| 3792 | */ |
| 3793 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3794 | goto done; |
| 3795 | } |
| 3796 | |
| 3797 | type = Py_TYPE(obj); |
| 3798 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3799 | /* The old cPickle had an optimization that used switch-case statement |
| 3800 | dispatching on the first letter of the type name. This has was removed |
| 3801 | since benchmarks shown that this optimization was actually slowing |
| 3802 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3803 | |
| 3804 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3805 | |
| 3806 | if (obj == Py_None) { |
| 3807 | status = save_none(self, obj); |
| 3808 | goto done; |
| 3809 | } |
| 3810 | else if (obj == Py_False || obj == Py_True) { |
| 3811 | status = save_bool(self, obj); |
| 3812 | goto done; |
| 3813 | } |
| 3814 | else if (type == &PyLong_Type) { |
| 3815 | status = save_long(self, obj); |
| 3816 | goto done; |
| 3817 | } |
| 3818 | else if (type == &PyFloat_Type) { |
| 3819 | status = save_float(self, obj); |
| 3820 | goto done; |
| 3821 | } |
| 3822 | |
| 3823 | /* Check the memo to see if it has the object. If so, generate |
| 3824 | a GET (or BINGET) opcode, instead of pickling the object |
| 3825 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3826 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3827 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3828 | goto error; |
| 3829 | goto done; |
| 3830 | } |
| 3831 | |
| 3832 | if (type == &PyBytes_Type) { |
| 3833 | status = save_bytes(self, obj); |
| 3834 | goto done; |
| 3835 | } |
| 3836 | else if (type == &PyUnicode_Type) { |
| 3837 | status = save_unicode(self, obj); |
| 3838 | goto done; |
| 3839 | } |
| 3840 | else if (type == &PyDict_Type) { |
| 3841 | status = save_dict(self, obj); |
| 3842 | goto done; |
| 3843 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3844 | else if (type == &PySet_Type) { |
| 3845 | status = save_set(self, obj); |
| 3846 | goto done; |
| 3847 | } |
| 3848 | else if (type == &PyFrozenSet_Type) { |
| 3849 | status = save_frozenset(self, obj); |
| 3850 | goto done; |
| 3851 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3852 | else if (type == &PyList_Type) { |
| 3853 | status = save_list(self, obj); |
| 3854 | goto done; |
| 3855 | } |
| 3856 | else if (type == &PyTuple_Type) { |
| 3857 | status = save_tuple(self, obj); |
| 3858 | goto done; |
| 3859 | } |
| 3860 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3861 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3862 | goto done; |
| 3863 | } |
| 3864 | else if (type == &PyFunction_Type) { |
| 3865 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3866 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3867 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3868 | |
| 3869 | /* XXX: This part needs some unit tests. */ |
| 3870 | |
| 3871 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3872 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3873 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3874 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3875 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3876 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3877 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3878 | (PyObject *)type); |
| 3879 | if (reduce_func == NULL) { |
| 3880 | if (PyErr_Occurred()) { |
| 3881 | goto error; |
| 3882 | } |
| 3883 | } else { |
| 3884 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3885 | Increase the reference count to be consistent with |
| 3886 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3887 | Py_INCREF(reduce_func); |
| 3888 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3889 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3890 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3891 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3892 | if (reduce_func == NULL) { |
| 3893 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3894 | PyErr_Clear(); |
| 3895 | else |
| 3896 | goto error; |
| 3897 | } |
| 3898 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3899 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3900 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3901 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3902 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3903 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3904 | status = save_global(self, obj, NULL); |
| 3905 | goto done; |
| 3906 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3907 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3908 | _Py_IDENTIFIER(__reduce__); |
| 3909 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3910 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3911 | |
| 3912 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3913 | automatically defined as __reduce__. While this is convenient, this |
| 3914 | make it impossible to know which method was actually called. Of |
| 3915 | course, this is not a big deal. But still, it would be nice to let |
| 3916 | the user know which method was called when something go |
| 3917 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3918 | don't actually have to check for a __reduce__ method. */ |
| 3919 | |
| 3920 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3921 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3922 | if (reduce_func != NULL) { |
| 3923 | PyObject *proto; |
| 3924 | proto = PyLong_FromLong(self->proto); |
| 3925 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3926 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3927 | } |
| 3928 | } |
| 3929 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3930 | PickleState *st = _Pickle_GetGlobalState(); |
| 3931 | |
| 3932 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3933 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3934 | } |
| 3935 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3936 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3937 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3938 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3939 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3940 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3941 | PyObject *empty_tuple = PyTuple_New(0); |
| 3942 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3943 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3944 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3945 | } |
| 3946 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3947 | PyErr_Format(st->PicklingError, |
| 3948 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3949 | type->tp_name, obj); |
| 3950 | goto error; |
| 3951 | } |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | if (reduce_value == NULL) |
| 3956 | goto error; |
| 3957 | |
| 3958 | if (PyUnicode_Check(reduce_value)) { |
| 3959 | status = save_global(self, obj, reduce_value); |
| 3960 | goto done; |
| 3961 | } |
| 3962 | |
| 3963 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3964 | PickleState *st = _Pickle_GetGlobalState(); |
| 3965 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3966 | "__reduce__ must return a string or tuple"); |
| 3967 | goto error; |
| 3968 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3969 | |
| 3970 | status = save_reduce(self, reduce_value, obj); |
| 3971 | |
| 3972 | if (0) { |
| 3973 | error: |
| 3974 | status = -1; |
| 3975 | } |
| 3976 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3977 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3978 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3979 | Py_XDECREF(reduce_func); |
| 3980 | Py_XDECREF(reduce_value); |
| 3981 | |
| 3982 | return status; |
| 3983 | } |
| 3984 | |
| 3985 | static int |
| 3986 | dump(PicklerObject *self, PyObject *obj) |
| 3987 | { |
| 3988 | const char stop_op = STOP; |
| 3989 | |
| 3990 | if (self->proto >= 2) { |
| 3991 | char header[2]; |
| 3992 | |
| 3993 | header[0] = PROTO; |
| 3994 | assert(self->proto >= 0 && self->proto < 256); |
| 3995 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3996 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3997 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3998 | if (self->proto >= 4) |
| 3999 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4000 | } |
| 4001 | |
| 4002 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4003 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4004 | return -1; |
| 4005 | |
| 4006 | return 0; |
| 4007 | } |
| 4008 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4009 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4010 | |
| 4011 | _pickle.Pickler.clear_memo |
| 4012 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4013 | Clears the pickler's "memo". |
| 4014 | |
| 4015 | The memo is the data structure that remembers which objects the |
| 4016 | pickler has already seen, so that shared or recursive objects are |
| 4017 | pickled by reference and not by value. This method is useful when |
| 4018 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4019 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4020 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4021 | static PyObject * |
| 4022 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4023 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4024 | { |
| 4025 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4026 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4027 | |
| 4028 | Py_RETURN_NONE; |
| 4029 | } |
| 4030 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4031 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4032 | |
| 4033 | _pickle.Pickler.dump |
| 4034 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4035 | obj: object |
| 4036 | / |
| 4037 | |
| 4038 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4039 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4040 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4041 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4042 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4043 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4044 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4045 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 4046 | Developers often forget to call __init__() in their subclasses, which |
| 4047 | would trigger a segfault without this check. */ |
| 4048 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4049 | PickleState *st = _Pickle_GetGlobalState(); |
| 4050 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 4051 | "Pickler.__init__() was not called by %s.__init__()", |
| 4052 | Py_TYPE(self)->tp_name); |
| 4053 | return NULL; |
| 4054 | } |
| 4055 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4056 | if (_Pickler_ClearBuffer(self) < 0) |
| 4057 | return NULL; |
| 4058 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4059 | if (dump(self, obj) < 0) |
| 4060 | return NULL; |
| 4061 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4062 | if (_Pickler_FlushToFile(self) < 0) |
| 4063 | return NULL; |
| 4064 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4065 | Py_RETURN_NONE; |
| 4066 | } |
| 4067 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4068 | /*[clinic input] |
| 4069 | |
| 4070 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4071 | |
| 4072 | Returns size in memory, in bytes. |
| 4073 | [clinic start generated code]*/ |
| 4074 | |
| 4075 | static Py_ssize_t |
| 4076 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4077 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4078 | { |
| 4079 | Py_ssize_t res, s; |
| 4080 | |
| 4081 | res = sizeof(PicklerObject); |
| 4082 | if (self->memo != NULL) { |
| 4083 | res += sizeof(PyMemoTable); |
| 4084 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4085 | } |
| 4086 | if (self->output_buffer != NULL) { |
| 4087 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4088 | if (s == -1) |
| 4089 | return -1; |
| 4090 | res += s; |
| 4091 | } |
| 4092 | return res; |
| 4093 | } |
| 4094 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4095 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4096 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4097 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4098 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4099 | {NULL, NULL} /* sentinel */ |
| 4100 | }; |
| 4101 | |
| 4102 | static void |
| 4103 | Pickler_dealloc(PicklerObject *self) |
| 4104 | { |
| 4105 | PyObject_GC_UnTrack(self); |
| 4106 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4107 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4108 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4109 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4110 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4111 | Py_XDECREF(self->fast_memo); |
| 4112 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4113 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4114 | |
| 4115 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4116 | } |
| 4117 | |
| 4118 | static int |
| 4119 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4120 | { |
| 4121 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4122 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4123 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4124 | Py_VISIT(self->fast_memo); |
| 4125 | return 0; |
| 4126 | } |
| 4127 | |
| 4128 | static int |
| 4129 | Pickler_clear(PicklerObject *self) |
| 4130 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4131 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4132 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4133 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4134 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4135 | Py_CLEAR(self->fast_memo); |
| 4136 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4137 | if (self->memo != NULL) { |
| 4138 | PyMemoTable *memo = self->memo; |
| 4139 | self->memo = NULL; |
| 4140 | PyMemoTable_Del(memo); |
| 4141 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4142 | return 0; |
| 4143 | } |
| 4144 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4145 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4146 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4147 | |
| 4148 | _pickle.Pickler.__init__ |
| 4149 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4150 | file: object |
| 4151 | protocol: object = NULL |
| 4152 | fix_imports: bool = True |
| 4153 | |
| 4154 | This takes a binary file for writing a pickle data stream. |
| 4155 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4156 | The optional *protocol* argument tells the pickler to use the given |
| 4157 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4158 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4159 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4160 | Specifying a negative protocol version selects the highest protocol |
| 4161 | version supported. The higher the protocol used, the more recent the |
| 4162 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4163 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4164 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4165 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 4166 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4167 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4168 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4169 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4170 | to map the new Python 3 names to the old module names used in Python |
| 4171 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4172 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4173 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4174 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4175 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4176 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 4177 | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4178 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4179 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4180 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4181 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4182 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4183 | if (self->write != NULL) |
| 4184 | (void)Pickler_clear(self); |
| 4185 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4186 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4187 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4188 | |
| 4189 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4190 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4191 | |
| 4192 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4193 | if (self->memo == NULL) { |
| 4194 | self->memo = PyMemoTable_New(); |
| 4195 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4196 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4197 | } |
| 4198 | self->output_len = 0; |
| 4199 | if (self->output_buffer == NULL) { |
| 4200 | self->max_output_len = WRITE_BUF_SIZE; |
| 4201 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4202 | self->max_output_len); |
| 4203 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4204 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4205 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4206 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4207 | self->fast = 0; |
| 4208 | self->fast_nesting = 0; |
| 4209 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4210 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4211 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4212 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4213 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4214 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4215 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4216 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4217 | self->dispatch_table = NULL; |
| 4218 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4219 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4220 | &PyId_dispatch_table); |
| 4221 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4222 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4223 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4224 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4225 | return 0; |
| 4226 | } |
| 4227 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4228 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4229 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4230 | * avoid breaking code like: |
| 4231 | * pickler.memo.clear() |
| 4232 | * and |
| 4233 | * pickler.memo = saved_memo |
| 4234 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4235 | * it. Note that we don't implement the entire mapping API here. This is |
| 4236 | * intentional, as these should be treated as black-box implementation details. |
| 4237 | */ |
| 4238 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4239 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4240 | _pickle.PicklerMemoProxy.clear |
| 4241 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4242 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4243 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4244 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4245 | static PyObject * |
| 4246 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4247 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4248 | { |
| 4249 | if (self->pickler->memo) |
| 4250 | PyMemoTable_Clear(self->pickler->memo); |
| 4251 | Py_RETURN_NONE; |
| 4252 | } |
| 4253 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4254 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4255 | _pickle.PicklerMemoProxy.copy |
| 4256 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4257 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4258 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4259 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4260 | static PyObject * |
| 4261 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4262 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4263 | { |
| 4264 | Py_ssize_t i; |
| 4265 | PyMemoTable *memo; |
| 4266 | PyObject *new_memo = PyDict_New(); |
| 4267 | if (new_memo == NULL) |
| 4268 | return NULL; |
| 4269 | |
| 4270 | memo = self->pickler->memo; |
| 4271 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4272 | PyMemoEntry entry = memo->mt_table[i]; |
| 4273 | if (entry.me_key != NULL) { |
| 4274 | int status; |
| 4275 | PyObject *key, *value; |
| 4276 | |
| 4277 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4278 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4279 | |
| 4280 | if (key == NULL || value == NULL) { |
| 4281 | Py_XDECREF(key); |
| 4282 | Py_XDECREF(value); |
| 4283 | goto error; |
| 4284 | } |
| 4285 | status = PyDict_SetItem(new_memo, key, value); |
| 4286 | Py_DECREF(key); |
| 4287 | Py_DECREF(value); |
| 4288 | if (status < 0) |
| 4289 | goto error; |
| 4290 | } |
| 4291 | } |
| 4292 | return new_memo; |
| 4293 | |
| 4294 | error: |
| 4295 | Py_XDECREF(new_memo); |
| 4296 | return NULL; |
| 4297 | } |
| 4298 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4299 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4300 | _pickle.PicklerMemoProxy.__reduce__ |
| 4301 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4302 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4303 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4304 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4305 | static PyObject * |
| 4306 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4307 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4308 | { |
| 4309 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4310 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4311 | if (contents == NULL) |
| 4312 | return NULL; |
| 4313 | |
| 4314 | reduce_value = PyTuple_New(2); |
| 4315 | if (reduce_value == NULL) { |
| 4316 | Py_DECREF(contents); |
| 4317 | return NULL; |
| 4318 | } |
| 4319 | dict_args = PyTuple_New(1); |
| 4320 | if (dict_args == NULL) { |
| 4321 | Py_DECREF(contents); |
| 4322 | Py_DECREF(reduce_value); |
| 4323 | return NULL; |
| 4324 | } |
| 4325 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4326 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4327 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4328 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4329 | return reduce_value; |
| 4330 | } |
| 4331 | |
| 4332 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4333 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4334 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4335 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4336 | {NULL, NULL} /* sentinel */ |
| 4337 | }; |
| 4338 | |
| 4339 | static void |
| 4340 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4341 | { |
| 4342 | PyObject_GC_UnTrack(self); |
| 4343 | Py_XDECREF(self->pickler); |
| 4344 | PyObject_GC_Del((PyObject *)self); |
| 4345 | } |
| 4346 | |
| 4347 | static int |
| 4348 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4349 | visitproc visit, void *arg) |
| 4350 | { |
| 4351 | Py_VISIT(self->pickler); |
| 4352 | return 0; |
| 4353 | } |
| 4354 | |
| 4355 | static int |
| 4356 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4357 | { |
| 4358 | Py_CLEAR(self->pickler); |
| 4359 | return 0; |
| 4360 | } |
| 4361 | |
| 4362 | static PyTypeObject PicklerMemoProxyType = { |
| 4363 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4364 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4365 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4366 | 0, |
| 4367 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4368 | 0, /* tp_print */ |
| 4369 | 0, /* tp_getattr */ |
| 4370 | 0, /* tp_setattr */ |
| 4371 | 0, /* tp_compare */ |
| 4372 | 0, /* tp_repr */ |
| 4373 | 0, /* tp_as_number */ |
| 4374 | 0, /* tp_as_sequence */ |
| 4375 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4376 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4377 | 0, /* tp_call */ |
| 4378 | 0, /* tp_str */ |
| 4379 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4380 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4381 | 0, /* tp_as_buffer */ |
| 4382 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4383 | 0, /* tp_doc */ |
| 4384 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4385 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4386 | 0, /* tp_richcompare */ |
| 4387 | 0, /* tp_weaklistoffset */ |
| 4388 | 0, /* tp_iter */ |
| 4389 | 0, /* tp_iternext */ |
| 4390 | picklerproxy_methods, /* tp_methods */ |
| 4391 | }; |
| 4392 | |
| 4393 | static PyObject * |
| 4394 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4395 | { |
| 4396 | PicklerMemoProxyObject *self; |
| 4397 | |
| 4398 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4399 | if (self == NULL) |
| 4400 | return NULL; |
| 4401 | Py_INCREF(pickler); |
| 4402 | self->pickler = pickler; |
| 4403 | PyObject_GC_Track(self); |
| 4404 | return (PyObject *)self; |
| 4405 | } |
| 4406 | |
| 4407 | /*****************************************************************************/ |
| 4408 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4409 | static PyObject * |
| 4410 | Pickler_get_memo(PicklerObject *self) |
| 4411 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4412 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4413 | } |
| 4414 | |
| 4415 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4416 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4417 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4418 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4419 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4420 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4421 | PyErr_SetString(PyExc_TypeError, |
| 4422 | "attribute deletion is not supported"); |
| 4423 | return -1; |
| 4424 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4425 | |
| 4426 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4427 | PicklerObject *pickler = |
| 4428 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4429 | |
| 4430 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4431 | if (new_memo == NULL) |
| 4432 | return -1; |
| 4433 | } |
| 4434 | else if (PyDict_Check(obj)) { |
| 4435 | Py_ssize_t i = 0; |
| 4436 | PyObject *key, *value; |
| 4437 | |
| 4438 | new_memo = PyMemoTable_New(); |
| 4439 | if (new_memo == NULL) |
| 4440 | return -1; |
| 4441 | |
| 4442 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4443 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4444 | PyObject *memo_obj; |
| 4445 | |
| 4446 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4447 | PyErr_SetString(PyExc_TypeError, |
| 4448 | "'memo' values must be 2-item tuples"); |
| 4449 | goto error; |
| 4450 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4451 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4452 | if (memo_id == -1 && PyErr_Occurred()) |
| 4453 | goto error; |
| 4454 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4455 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4456 | goto error; |
| 4457 | } |
| 4458 | } |
| 4459 | else { |
| 4460 | PyErr_Format(PyExc_TypeError, |
| 4461 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4462 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4463 | return -1; |
| 4464 | } |
| 4465 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4466 | PyMemoTable_Del(self->memo); |
| 4467 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4468 | |
| 4469 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4470 | |
| 4471 | error: |
| 4472 | if (new_memo) |
| 4473 | PyMemoTable_Del(new_memo); |
| 4474 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4475 | } |
| 4476 | |
| 4477 | static PyObject * |
| 4478 | Pickler_get_persid(PicklerObject *self) |
| 4479 | { |
| 4480 | if (self->pers_func == NULL) |
| 4481 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4482 | else |
| 4483 | Py_INCREF(self->pers_func); |
| 4484 | return self->pers_func; |
| 4485 | } |
| 4486 | |
| 4487 | static int |
| 4488 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4489 | { |
| 4490 | PyObject *tmp; |
| 4491 | |
| 4492 | if (value == NULL) { |
| 4493 | PyErr_SetString(PyExc_TypeError, |
| 4494 | "attribute deletion is not supported"); |
| 4495 | return -1; |
| 4496 | } |
| 4497 | if (!PyCallable_Check(value)) { |
| 4498 | PyErr_SetString(PyExc_TypeError, |
| 4499 | "persistent_id must be a callable taking one argument"); |
| 4500 | return -1; |
| 4501 | } |
| 4502 | |
| 4503 | tmp = self->pers_func; |
| 4504 | Py_INCREF(value); |
| 4505 | self->pers_func = value; |
| 4506 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4507 | |
| 4508 | return 0; |
| 4509 | } |
| 4510 | |
| 4511 | static PyMemberDef Pickler_members[] = { |
| 4512 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4513 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4514 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4515 | {NULL} |
| 4516 | }; |
| 4517 | |
| 4518 | static PyGetSetDef Pickler_getsets[] = { |
| 4519 | {"memo", (getter)Pickler_get_memo, |
| 4520 | (setter)Pickler_set_memo}, |
| 4521 | {"persistent_id", (getter)Pickler_get_persid, |
| 4522 | (setter)Pickler_set_persid}, |
| 4523 | {NULL} |
| 4524 | }; |
| 4525 | |
| 4526 | static PyTypeObject Pickler_Type = { |
| 4527 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4528 | "_pickle.Pickler" , /*tp_name*/ |
| 4529 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4530 | 0, /*tp_itemsize*/ |
| 4531 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4532 | 0, /*tp_print*/ |
| 4533 | 0, /*tp_getattr*/ |
| 4534 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4535 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4536 | 0, /*tp_repr*/ |
| 4537 | 0, /*tp_as_number*/ |
| 4538 | 0, /*tp_as_sequence*/ |
| 4539 | 0, /*tp_as_mapping*/ |
| 4540 | 0, /*tp_hash*/ |
| 4541 | 0, /*tp_call*/ |
| 4542 | 0, /*tp_str*/ |
| 4543 | 0, /*tp_getattro*/ |
| 4544 | 0, /*tp_setattro*/ |
| 4545 | 0, /*tp_as_buffer*/ |
| 4546 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4547 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4548 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4549 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4550 | 0, /*tp_richcompare*/ |
| 4551 | 0, /*tp_weaklistoffset*/ |
| 4552 | 0, /*tp_iter*/ |
| 4553 | 0, /*tp_iternext*/ |
| 4554 | Pickler_methods, /*tp_methods*/ |
| 4555 | Pickler_members, /*tp_members*/ |
| 4556 | Pickler_getsets, /*tp_getset*/ |
| 4557 | 0, /*tp_base*/ |
| 4558 | 0, /*tp_dict*/ |
| 4559 | 0, /*tp_descr_get*/ |
| 4560 | 0, /*tp_descr_set*/ |
| 4561 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4562 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4563 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4564 | PyType_GenericNew, /*tp_new*/ |
| 4565 | PyObject_GC_Del, /*tp_free*/ |
| 4566 | 0, /*tp_is_gc*/ |
| 4567 | }; |
| 4568 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4569 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4570 | |
| 4571 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4572 | using directly the C version of find_class(), when find_class() is not |
| 4573 | overridden by a subclass. Although, this could become rather hackish. A |
| 4574 | simpler optimization would be to call the C function when self is not a |
| 4575 | subclass instance. */ |
| 4576 | static PyObject * |
| 4577 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4578 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4579 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4580 | |
| 4581 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4582 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4583 | } |
| 4584 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4585 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4586 | marker(UnpicklerObject *self) |
| 4587 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4588 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4589 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4590 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4591 | return -1; |
| 4592 | } |
| 4593 | |
| 4594 | return self->marks[--self->num_marks]; |
| 4595 | } |
| 4596 | |
| 4597 | static int |
| 4598 | load_none(UnpicklerObject *self) |
| 4599 | { |
| 4600 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4601 | return 0; |
| 4602 | } |
| 4603 | |
| 4604 | static int |
| 4605 | bad_readline(void) |
| 4606 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4607 | PickleState *st = _Pickle_GetGlobalState(); |
| 4608 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4609 | return -1; |
| 4610 | } |
| 4611 | |
| 4612 | static int |
| 4613 | load_int(UnpicklerObject *self) |
| 4614 | { |
| 4615 | PyObject *value; |
| 4616 | char *endptr, *s; |
| 4617 | Py_ssize_t len; |
| 4618 | long x; |
| 4619 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4620 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4621 | return -1; |
| 4622 | if (len < 2) |
| 4623 | return bad_readline(); |
| 4624 | |
| 4625 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4626 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4627 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4628 | x = strtol(s, &endptr, 0); |
| 4629 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4630 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4631 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4632 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4633 | errno = 0; |
| 4634 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4635 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4636 | if (value == NULL) { |
| 4637 | PyErr_SetString(PyExc_ValueError, |
| 4638 | "could not convert string to int"); |
| 4639 | return -1; |
| 4640 | } |
| 4641 | } |
| 4642 | else { |
| 4643 | if (len == 3 && (x == 0 || x == 1)) { |
| 4644 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4645 | return -1; |
| 4646 | } |
| 4647 | else { |
| 4648 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4649 | return -1; |
| 4650 | } |
| 4651 | } |
| 4652 | |
| 4653 | PDATA_PUSH(self->stack, value, -1); |
| 4654 | return 0; |
| 4655 | } |
| 4656 | |
| 4657 | static int |
| 4658 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4659 | { |
| 4660 | assert(boolean == Py_True || boolean == Py_False); |
| 4661 | PDATA_APPEND(self->stack, boolean, -1); |
| 4662 | return 0; |
| 4663 | } |
| 4664 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4665 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4666 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4667 | */ |
| 4668 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4669 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4670 | { |
| 4671 | unsigned char *s = (unsigned char *)bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4672 | int i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4673 | size_t x = 0; |
| 4674 | |
Serhiy Storchaka | e060619 | 2015-09-29 22:10:07 +0300 | [diff] [blame] | 4675 | if (nbytes > (int)sizeof(size_t)) { |
| 4676 | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
| 4677 | * have 64-bit size that can't be represented on 32-bit platform. |
| 4678 | */ |
| 4679 | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
| 4680 | if (s[i]) |
| 4681 | return -1; |
| 4682 | } |
| 4683 | nbytes = (int)sizeof(size_t); |
| 4684 | } |
| 4685 | for (i = 0; i < nbytes; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4686 | x |= (size_t) s[i] << (8 * i); |
| 4687 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4688 | |
| 4689 | if (x > PY_SSIZE_T_MAX) |
| 4690 | return -1; |
| 4691 | else |
| 4692 | return (Py_ssize_t) x; |
| 4693 | } |
| 4694 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4695 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4696 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4697 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4698 | * of x-platform bugs. |
| 4699 | */ |
| 4700 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4701 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4702 | { |
| 4703 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4704 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4705 | long x = 0; |
| 4706 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4707 | for (i = 0; i < nbytes; i++) { |
| 4708 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4709 | } |
| 4710 | |
| 4711 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4712 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4713 | * to extend a BININT's sign bit to the full width. |
| 4714 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4715 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4716 | x |= -(x & (1L << 31)); |
| 4717 | } |
| 4718 | |
| 4719 | return x; |
| 4720 | } |
| 4721 | |
| 4722 | static int |
| 4723 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4724 | { |
| 4725 | PyObject *value; |
| 4726 | long x; |
| 4727 | |
| 4728 | x = calc_binint(s, size); |
| 4729 | |
| 4730 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4731 | return -1; |
| 4732 | |
| 4733 | PDATA_PUSH(self->stack, value, -1); |
| 4734 | return 0; |
| 4735 | } |
| 4736 | |
| 4737 | static int |
| 4738 | load_binint(UnpicklerObject *self) |
| 4739 | { |
| 4740 | char *s; |
| 4741 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4742 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4743 | return -1; |
| 4744 | |
| 4745 | return load_binintx(self, s, 4); |
| 4746 | } |
| 4747 | |
| 4748 | static int |
| 4749 | load_binint1(UnpicklerObject *self) |
| 4750 | { |
| 4751 | char *s; |
| 4752 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4753 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4754 | return -1; |
| 4755 | |
| 4756 | return load_binintx(self, s, 1); |
| 4757 | } |
| 4758 | |
| 4759 | static int |
| 4760 | load_binint2(UnpicklerObject *self) |
| 4761 | { |
| 4762 | char *s; |
| 4763 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4764 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4765 | return -1; |
| 4766 | |
| 4767 | return load_binintx(self, s, 2); |
| 4768 | } |
| 4769 | |
| 4770 | static int |
| 4771 | load_long(UnpicklerObject *self) |
| 4772 | { |
| 4773 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4774 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4775 | Py_ssize_t len; |
| 4776 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4777 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4778 | return -1; |
| 4779 | if (len < 2) |
| 4780 | return bad_readline(); |
| 4781 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4782 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4783 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4784 | compatibility with Python 3.0.0, we don't actually *require* |
| 4785 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4786 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4787 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4788 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4789 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4790 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4791 | return -1; |
| 4792 | |
| 4793 | PDATA_PUSH(self->stack, value, -1); |
| 4794 | return 0; |
| 4795 | } |
| 4796 | |
| 4797 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4798 | * data following. |
| 4799 | */ |
| 4800 | static int |
| 4801 | load_counted_long(UnpicklerObject *self, int size) |
| 4802 | { |
| 4803 | PyObject *value; |
| 4804 | char *nbytes; |
| 4805 | char *pdata; |
| 4806 | |
| 4807 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4808 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4809 | return -1; |
| 4810 | |
| 4811 | size = calc_binint(nbytes, size); |
| 4812 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4813 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4814 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4815 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4816 | "LONG pickle has negative byte count"); |
| 4817 | return -1; |
| 4818 | } |
| 4819 | |
| 4820 | if (size == 0) |
| 4821 | value = PyLong_FromLong(0L); |
| 4822 | else { |
| 4823 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4824 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4825 | return -1; |
| 4826 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4827 | 1 /* little endian */ , 1 /* signed */ ); |
| 4828 | } |
| 4829 | if (value == NULL) |
| 4830 | return -1; |
| 4831 | PDATA_PUSH(self->stack, value, -1); |
| 4832 | return 0; |
| 4833 | } |
| 4834 | |
| 4835 | static int |
| 4836 | load_float(UnpicklerObject *self) |
| 4837 | { |
| 4838 | PyObject *value; |
| 4839 | char *endptr, *s; |
| 4840 | Py_ssize_t len; |
| 4841 | double d; |
| 4842 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4843 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4844 | return -1; |
| 4845 | if (len < 2) |
| 4846 | return bad_readline(); |
| 4847 | |
| 4848 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4849 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4850 | if (d == -1.0 && PyErr_Occurred()) |
| 4851 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4852 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4853 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4854 | return -1; |
| 4855 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4856 | value = PyFloat_FromDouble(d); |
| 4857 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4858 | return -1; |
| 4859 | |
| 4860 | PDATA_PUSH(self->stack, value, -1); |
| 4861 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4862 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4863 | |
| 4864 | static int |
| 4865 | load_binfloat(UnpicklerObject *self) |
| 4866 | { |
| 4867 | PyObject *value; |
| 4868 | double x; |
| 4869 | char *s; |
| 4870 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4871 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4872 | return -1; |
| 4873 | |
| 4874 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4875 | if (x == -1.0 && PyErr_Occurred()) |
| 4876 | return -1; |
| 4877 | |
| 4878 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4879 | return -1; |
| 4880 | |
| 4881 | PDATA_PUSH(self->stack, value, -1); |
| 4882 | return 0; |
| 4883 | } |
| 4884 | |
| 4885 | static int |
| 4886 | load_string(UnpicklerObject *self) |
| 4887 | { |
| 4888 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4889 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4890 | Py_ssize_t len; |
| 4891 | char *s, *p; |
| 4892 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4893 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4894 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4895 | /* Strip the newline */ |
| 4896 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4897 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4898 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4899 | p = s + 1; |
| 4900 | len -= 2; |
| 4901 | } |
| 4902 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4903 | PickleState *st = _Pickle_GetGlobalState(); |
| 4904 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4905 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4906 | return -1; |
| 4907 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4908 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4909 | |
| 4910 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4911 | to encode, and then coerce the result to Unicode. */ |
| 4912 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4913 | if (bytes == NULL) |
| 4914 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4915 | |
| 4916 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4917 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4918 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4919 | obj = bytes; |
| 4920 | } |
| 4921 | else { |
| 4922 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4923 | Py_DECREF(bytes); |
| 4924 | if (obj == NULL) { |
| 4925 | return -1; |
| 4926 | } |
| 4927 | } |
| 4928 | |
| 4929 | PDATA_PUSH(self->stack, obj, -1); |
| 4930 | return 0; |
| 4931 | } |
| 4932 | |
| 4933 | static int |
| 4934 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4935 | { |
| 4936 | PyObject *obj; |
| 4937 | Py_ssize_t size; |
| 4938 | char *s; |
| 4939 | |
| 4940 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4941 | return -1; |
| 4942 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4943 | size = calc_binsize(s, nbytes); |
| 4944 | if (size < 0) { |
| 4945 | PickleState *st = _Pickle_GetGlobalState(); |
| 4946 | PyErr_Format(st->UnpicklingError, |
| 4947 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4948 | PY_SSIZE_T_MAX); |
| 4949 | return -1; |
| 4950 | } |
| 4951 | |
| 4952 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4953 | return -1; |
| 4954 | |
| 4955 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4956 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4957 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4958 | obj = PyBytes_FromStringAndSize(s, size); |
| 4959 | } |
| 4960 | else { |
| 4961 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4962 | } |
| 4963 | if (obj == NULL) { |
| 4964 | return -1; |
| 4965 | } |
| 4966 | |
| 4967 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4968 | return 0; |
| 4969 | } |
| 4970 | |
| 4971 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4972 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4973 | { |
| 4974 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4975 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4976 | char *s; |
| 4977 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4978 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4979 | return -1; |
| 4980 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4981 | size = calc_binsize(s, nbytes); |
| 4982 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4983 | PyErr_Format(PyExc_OverflowError, |
| 4984 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4985 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4986 | return -1; |
| 4987 | } |
| 4988 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4989 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4990 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4991 | |
| 4992 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4993 | if (bytes == NULL) |
| 4994 | return -1; |
| 4995 | |
| 4996 | PDATA_PUSH(self->stack, bytes, -1); |
| 4997 | return 0; |
| 4998 | } |
| 4999 | |
| 5000 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5001 | load_unicode(UnpicklerObject *self) |
| 5002 | { |
| 5003 | PyObject *str; |
| 5004 | Py_ssize_t len; |
| 5005 | char *s; |
| 5006 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5007 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5008 | return -1; |
| 5009 | if (len < 1) |
| 5010 | return bad_readline(); |
| 5011 | |
| 5012 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 5013 | if (str == NULL) |
| 5014 | return -1; |
| 5015 | |
| 5016 | PDATA_PUSH(self->stack, str, -1); |
| 5017 | return 0; |
| 5018 | } |
| 5019 | |
| 5020 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5021 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5022 | { |
| 5023 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5024 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5025 | char *s; |
| 5026 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5027 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5028 | return -1; |
| 5029 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5030 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5031 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5032 | PyErr_Format(PyExc_OverflowError, |
| 5033 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 5034 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5035 | return -1; |
| 5036 | } |
| 5037 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5038 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5039 | return -1; |
| 5040 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 5041 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5042 | if (str == NULL) |
| 5043 | return -1; |
| 5044 | |
| 5045 | PDATA_PUSH(self->stack, str, -1); |
| 5046 | return 0; |
| 5047 | } |
| 5048 | |
| 5049 | static int |
| 5050 | load_tuple(UnpicklerObject *self) |
| 5051 | { |
| 5052 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5053 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5054 | |
| 5055 | if ((i = marker(self)) < 0) |
| 5056 | return -1; |
| 5057 | |
| 5058 | tuple = Pdata_poptuple(self->stack, i); |
| 5059 | if (tuple == NULL) |
| 5060 | return -1; |
| 5061 | PDATA_PUSH(self->stack, tuple, -1); |
| 5062 | return 0; |
| 5063 | } |
| 5064 | |
| 5065 | static int |
| 5066 | load_counted_tuple(UnpicklerObject *self, int len) |
| 5067 | { |
| 5068 | PyObject *tuple; |
| 5069 | |
| 5070 | tuple = PyTuple_New(len); |
| 5071 | if (tuple == NULL) |
| 5072 | return -1; |
| 5073 | |
| 5074 | while (--len >= 0) { |
| 5075 | PyObject *item; |
| 5076 | |
| 5077 | PDATA_POP(self->stack, item); |
| 5078 | if (item == NULL) |
| 5079 | return -1; |
| 5080 | PyTuple_SET_ITEM(tuple, len, item); |
| 5081 | } |
| 5082 | PDATA_PUSH(self->stack, tuple, -1); |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | static int |
| 5087 | load_empty_list(UnpicklerObject *self) |
| 5088 | { |
| 5089 | PyObject *list; |
| 5090 | |
| 5091 | if ((list = PyList_New(0)) == NULL) |
| 5092 | return -1; |
| 5093 | PDATA_PUSH(self->stack, list, -1); |
| 5094 | return 0; |
| 5095 | } |
| 5096 | |
| 5097 | static int |
| 5098 | load_empty_dict(UnpicklerObject *self) |
| 5099 | { |
| 5100 | PyObject *dict; |
| 5101 | |
| 5102 | if ((dict = PyDict_New()) == NULL) |
| 5103 | return -1; |
| 5104 | PDATA_PUSH(self->stack, dict, -1); |
| 5105 | return 0; |
| 5106 | } |
| 5107 | |
| 5108 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5109 | load_empty_set(UnpicklerObject *self) |
| 5110 | { |
| 5111 | PyObject *set; |
| 5112 | |
| 5113 | if ((set = PySet_New(NULL)) == NULL) |
| 5114 | return -1; |
| 5115 | PDATA_PUSH(self->stack, set, -1); |
| 5116 | return 0; |
| 5117 | } |
| 5118 | |
| 5119 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5120 | load_list(UnpicklerObject *self) |
| 5121 | { |
| 5122 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5123 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5124 | |
| 5125 | if ((i = marker(self)) < 0) |
| 5126 | return -1; |
| 5127 | |
| 5128 | list = Pdata_poplist(self->stack, i); |
| 5129 | if (list == NULL) |
| 5130 | return -1; |
| 5131 | PDATA_PUSH(self->stack, list, -1); |
| 5132 | return 0; |
| 5133 | } |
| 5134 | |
| 5135 | static int |
| 5136 | load_dict(UnpicklerObject *self) |
| 5137 | { |
| 5138 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5139 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5140 | |
| 5141 | if ((i = marker(self)) < 0) |
| 5142 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5143 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5144 | |
| 5145 | if ((dict = PyDict_New()) == NULL) |
| 5146 | return -1; |
| 5147 | |
| 5148 | for (k = i + 1; k < j; k += 2) { |
| 5149 | key = self->stack->data[k - 1]; |
| 5150 | value = self->stack->data[k]; |
| 5151 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5152 | Py_DECREF(dict); |
| 5153 | return -1; |
| 5154 | } |
| 5155 | } |
| 5156 | Pdata_clear(self->stack, i); |
| 5157 | PDATA_PUSH(self->stack, dict, -1); |
| 5158 | return 0; |
| 5159 | } |
| 5160 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5161 | static int |
| 5162 | load_frozenset(UnpicklerObject *self) |
| 5163 | { |
| 5164 | PyObject *items; |
| 5165 | PyObject *frozenset; |
| 5166 | Py_ssize_t i; |
| 5167 | |
| 5168 | if ((i = marker(self)) < 0) |
| 5169 | return -1; |
| 5170 | |
| 5171 | items = Pdata_poptuple(self->stack, i); |
| 5172 | if (items == NULL) |
| 5173 | return -1; |
| 5174 | |
| 5175 | frozenset = PyFrozenSet_New(items); |
| 5176 | Py_DECREF(items); |
| 5177 | if (frozenset == NULL) |
| 5178 | return -1; |
| 5179 | |
| 5180 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5181 | return 0; |
| 5182 | } |
| 5183 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5184 | static PyObject * |
| 5185 | instantiate(PyObject *cls, PyObject *args) |
| 5186 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5187 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5188 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5189 | /* Caller must assure args are a tuple. Normally, args come from |
| 5190 | Pdata_poptuple which packs objects from the top of the stack |
| 5191 | into a newly created tuple. */ |
| 5192 | assert(PyTuple_Check(args)); |
| 5193 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5194 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5195 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5196 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5197 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5198 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5199 | |
| 5200 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5201 | } |
| 5202 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5203 | } |
| 5204 | |
| 5205 | static int |
| 5206 | load_obj(UnpicklerObject *self) |
| 5207 | { |
| 5208 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5209 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5210 | |
| 5211 | if ((i = marker(self)) < 0) |
| 5212 | return -1; |
| 5213 | |
| 5214 | args = Pdata_poptuple(self->stack, i + 1); |
| 5215 | if (args == NULL) |
| 5216 | return -1; |
| 5217 | |
| 5218 | PDATA_POP(self->stack, cls); |
| 5219 | if (cls) { |
| 5220 | obj = instantiate(cls, args); |
| 5221 | Py_DECREF(cls); |
| 5222 | } |
| 5223 | Py_DECREF(args); |
| 5224 | if (obj == NULL) |
| 5225 | return -1; |
| 5226 | |
| 5227 | PDATA_PUSH(self->stack, obj, -1); |
| 5228 | return 0; |
| 5229 | } |
| 5230 | |
| 5231 | static int |
| 5232 | load_inst(UnpicklerObject *self) |
| 5233 | { |
| 5234 | PyObject *cls = NULL; |
| 5235 | PyObject *args = NULL; |
| 5236 | PyObject *obj = NULL; |
| 5237 | PyObject *module_name; |
| 5238 | PyObject *class_name; |
| 5239 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5240 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5241 | char *s; |
| 5242 | |
| 5243 | if ((i = marker(self)) < 0) |
| 5244 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5245 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5246 | return -1; |
| 5247 | if (len < 2) |
| 5248 | return bad_readline(); |
| 5249 | |
| 5250 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5251 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5252 | supported by older protocols on Python 2.x. */ |
| 5253 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5254 | if (module_name == NULL) |
| 5255 | return -1; |
| 5256 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5257 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5258 | if (len < 2) |
| 5259 | return bad_readline(); |
| 5260 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5261 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5262 | cls = find_class(self, module_name, class_name); |
| 5263 | Py_DECREF(class_name); |
| 5264 | } |
| 5265 | } |
| 5266 | Py_DECREF(module_name); |
| 5267 | |
| 5268 | if (cls == NULL) |
| 5269 | return -1; |
| 5270 | |
| 5271 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5272 | obj = instantiate(cls, args); |
| 5273 | Py_DECREF(args); |
| 5274 | } |
| 5275 | Py_DECREF(cls); |
| 5276 | |
| 5277 | if (obj == NULL) |
| 5278 | return -1; |
| 5279 | |
| 5280 | PDATA_PUSH(self->stack, obj, -1); |
| 5281 | return 0; |
| 5282 | } |
| 5283 | |
| 5284 | static int |
| 5285 | load_newobj(UnpicklerObject *self) |
| 5286 | { |
| 5287 | PyObject *args = NULL; |
| 5288 | PyObject *clsraw = NULL; |
| 5289 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5290 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5291 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5292 | |
| 5293 | /* Stack is ... cls argtuple, and we want to call |
| 5294 | * cls.__new__(cls, *argtuple). |
| 5295 | */ |
| 5296 | PDATA_POP(self->stack, args); |
| 5297 | if (args == NULL) |
| 5298 | goto error; |
| 5299 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5300 | PyErr_SetString(st->UnpicklingError, |
| 5301 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5302 | goto error; |
| 5303 | } |
| 5304 | |
| 5305 | PDATA_POP(self->stack, clsraw); |
| 5306 | cls = (PyTypeObject *)clsraw; |
| 5307 | if (cls == NULL) |
| 5308 | goto error; |
| 5309 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5310 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5311 | "isn't a type object"); |
| 5312 | goto error; |
| 5313 | } |
| 5314 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5315 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5316 | "has NULL tp_new"); |
| 5317 | goto error; |
| 5318 | } |
| 5319 | |
| 5320 | /* Call __new__. */ |
| 5321 | obj = cls->tp_new(cls, args, NULL); |
| 5322 | if (obj == NULL) |
| 5323 | goto error; |
| 5324 | |
| 5325 | Py_DECREF(args); |
| 5326 | Py_DECREF(clsraw); |
| 5327 | PDATA_PUSH(self->stack, obj, -1); |
| 5328 | return 0; |
| 5329 | |
| 5330 | error: |
| 5331 | Py_XDECREF(args); |
| 5332 | Py_XDECREF(clsraw); |
| 5333 | return -1; |
| 5334 | } |
| 5335 | |
| 5336 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5337 | load_newobj_ex(UnpicklerObject *self) |
| 5338 | { |
| 5339 | PyObject *cls, *args, *kwargs; |
| 5340 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5341 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5342 | |
| 5343 | PDATA_POP(self->stack, kwargs); |
| 5344 | if (kwargs == NULL) { |
| 5345 | return -1; |
| 5346 | } |
| 5347 | PDATA_POP(self->stack, args); |
| 5348 | if (args == NULL) { |
| 5349 | Py_DECREF(kwargs); |
| 5350 | return -1; |
| 5351 | } |
| 5352 | PDATA_POP(self->stack, cls); |
| 5353 | if (cls == NULL) { |
| 5354 | Py_DECREF(kwargs); |
| 5355 | Py_DECREF(args); |
| 5356 | return -1; |
| 5357 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5358 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5359 | if (!PyType_Check(cls)) { |
| 5360 | Py_DECREF(kwargs); |
| 5361 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5362 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5363 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5364 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5365 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5366 | return -1; |
| 5367 | } |
| 5368 | |
| 5369 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5370 | Py_DECREF(kwargs); |
| 5371 | Py_DECREF(args); |
| 5372 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5373 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5374 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5375 | return -1; |
| 5376 | } |
| 5377 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5378 | Py_DECREF(kwargs); |
| 5379 | Py_DECREF(args); |
| 5380 | Py_DECREF(cls); |
| 5381 | if (obj == NULL) { |
| 5382 | return -1; |
| 5383 | } |
| 5384 | PDATA_PUSH(self->stack, obj, -1); |
| 5385 | return 0; |
| 5386 | } |
| 5387 | |
| 5388 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5389 | load_global(UnpicklerObject *self) |
| 5390 | { |
| 5391 | PyObject *global = NULL; |
| 5392 | PyObject *module_name; |
| 5393 | PyObject *global_name; |
| 5394 | Py_ssize_t len; |
| 5395 | char *s; |
| 5396 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5397 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5398 | return -1; |
| 5399 | if (len < 2) |
| 5400 | return bad_readline(); |
| 5401 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5402 | if (!module_name) |
| 5403 | return -1; |
| 5404 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5405 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5406 | if (len < 2) { |
| 5407 | Py_DECREF(module_name); |
| 5408 | return bad_readline(); |
| 5409 | } |
| 5410 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5411 | if (global_name) { |
| 5412 | global = find_class(self, module_name, global_name); |
| 5413 | Py_DECREF(global_name); |
| 5414 | } |
| 5415 | } |
| 5416 | Py_DECREF(module_name); |
| 5417 | |
| 5418 | if (global == NULL) |
| 5419 | return -1; |
| 5420 | PDATA_PUSH(self->stack, global, -1); |
| 5421 | return 0; |
| 5422 | } |
| 5423 | |
| 5424 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5425 | load_stack_global(UnpicklerObject *self) |
| 5426 | { |
| 5427 | PyObject *global; |
| 5428 | PyObject *module_name; |
| 5429 | PyObject *global_name; |
| 5430 | |
| 5431 | PDATA_POP(self->stack, global_name); |
| 5432 | PDATA_POP(self->stack, module_name); |
| 5433 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5434 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5435 | PickleState *st = _Pickle_GetGlobalState(); |
| 5436 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5437 | Py_XDECREF(global_name); |
| 5438 | Py_XDECREF(module_name); |
| 5439 | return -1; |
| 5440 | } |
| 5441 | global = find_class(self, module_name, global_name); |
| 5442 | Py_DECREF(global_name); |
| 5443 | Py_DECREF(module_name); |
| 5444 | if (global == NULL) |
| 5445 | return -1; |
| 5446 | PDATA_PUSH(self->stack, global, -1); |
| 5447 | return 0; |
| 5448 | } |
| 5449 | |
| 5450 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5451 | load_persid(UnpicklerObject *self) |
| 5452 | { |
| 5453 | PyObject *pid; |
| 5454 | Py_ssize_t len; |
| 5455 | char *s; |
| 5456 | |
| 5457 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5458 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5459 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5460 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5461 | return bad_readline(); |
| 5462 | |
| 5463 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5464 | if (pid == NULL) |
| 5465 | return -1; |
| 5466 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5467 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5468 | to pid first. */ |
| 5469 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5470 | if (pid == NULL) |
| 5471 | return -1; |
| 5472 | |
| 5473 | PDATA_PUSH(self->stack, pid, -1); |
| 5474 | return 0; |
| 5475 | } |
| 5476 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5477 | PickleState *st = _Pickle_GetGlobalState(); |
| 5478 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5479 | "A load persistent id instruction was encountered,\n" |
| 5480 | "but no persistent_load function was specified."); |
| 5481 | return -1; |
| 5482 | } |
| 5483 | } |
| 5484 | |
| 5485 | static int |
| 5486 | load_binpersid(UnpicklerObject *self) |
| 5487 | { |
| 5488 | PyObject *pid; |
| 5489 | |
| 5490 | if (self->pers_func) { |
| 5491 | PDATA_POP(self->stack, pid); |
| 5492 | if (pid == NULL) |
| 5493 | return -1; |
| 5494 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5495 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5496 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5497 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5498 | if (pid == NULL) |
| 5499 | return -1; |
| 5500 | |
| 5501 | PDATA_PUSH(self->stack, pid, -1); |
| 5502 | return 0; |
| 5503 | } |
| 5504 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5505 | PickleState *st = _Pickle_GetGlobalState(); |
| 5506 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5507 | "A load persistent id instruction was encountered,\n" |
| 5508 | "but no persistent_load function was specified."); |
| 5509 | return -1; |
| 5510 | } |
| 5511 | } |
| 5512 | |
| 5513 | static int |
| 5514 | load_pop(UnpicklerObject *self) |
| 5515 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5516 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5517 | |
| 5518 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5519 | * an object stack and a mark stack. We have to be clever and |
| 5520 | * 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] | 5521 | * mark stack first, and only signalling a stack underflow if |
| 5522 | * the object stack is empty and the mark stack doesn't match |
| 5523 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5524 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5525 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5526 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5527 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5528 | len--; |
| 5529 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5530 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5531 | } else { |
| 5532 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5533 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5534 | return 0; |
| 5535 | } |
| 5536 | |
| 5537 | static int |
| 5538 | load_pop_mark(UnpicklerObject *self) |
| 5539 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5540 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5541 | |
| 5542 | if ((i = marker(self)) < 0) |
| 5543 | return -1; |
| 5544 | |
| 5545 | Pdata_clear(self->stack, i); |
| 5546 | |
| 5547 | return 0; |
| 5548 | } |
| 5549 | |
| 5550 | static int |
| 5551 | load_dup(UnpicklerObject *self) |
| 5552 | { |
| 5553 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5554 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5555 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5556 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5557 | return stack_underflow(); |
| 5558 | last = self->stack->data[len - 1]; |
| 5559 | PDATA_APPEND(self->stack, last, -1); |
| 5560 | return 0; |
| 5561 | } |
| 5562 | |
| 5563 | static int |
| 5564 | load_get(UnpicklerObject *self) |
| 5565 | { |
| 5566 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5567 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5568 | Py_ssize_t len; |
| 5569 | char *s; |
| 5570 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5571 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5572 | return -1; |
| 5573 | if (len < 2) |
| 5574 | return bad_readline(); |
| 5575 | |
| 5576 | key = PyLong_FromString(s, NULL, 10); |
| 5577 | if (key == NULL) |
| 5578 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5579 | idx = PyLong_AsSsize_t(key); |
| 5580 | if (idx == -1 && PyErr_Occurred()) { |
| 5581 | Py_DECREF(key); |
| 5582 | return -1; |
| 5583 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5584 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5585 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5586 | if (value == NULL) { |
| 5587 | if (!PyErr_Occurred()) |
| 5588 | PyErr_SetObject(PyExc_KeyError, key); |
| 5589 | Py_DECREF(key); |
| 5590 | return -1; |
| 5591 | } |
| 5592 | Py_DECREF(key); |
| 5593 | |
| 5594 | PDATA_APPEND(self->stack, value, -1); |
| 5595 | return 0; |
| 5596 | } |
| 5597 | |
| 5598 | static int |
| 5599 | load_binget(UnpicklerObject *self) |
| 5600 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5601 | PyObject *value; |
| 5602 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5603 | char *s; |
| 5604 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5605 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5606 | return -1; |
| 5607 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5608 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5609 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5610 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5611 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5612 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5613 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5614 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5615 | Py_DECREF(key); |
| 5616 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5617 | return -1; |
| 5618 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5619 | |
| 5620 | PDATA_APPEND(self->stack, value, -1); |
| 5621 | return 0; |
| 5622 | } |
| 5623 | |
| 5624 | static int |
| 5625 | load_long_binget(UnpicklerObject *self) |
| 5626 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5627 | PyObject *value; |
| 5628 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5629 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5630 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5631 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5632 | return -1; |
| 5633 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5634 | idx = calc_binsize(s, 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5635 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5636 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5637 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5638 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5639 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5640 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5641 | Py_DECREF(key); |
| 5642 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5643 | return -1; |
| 5644 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5645 | |
| 5646 | PDATA_APPEND(self->stack, value, -1); |
| 5647 | return 0; |
| 5648 | } |
| 5649 | |
| 5650 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5651 | * the number of bytes following the opcode, holding the index (code) value. |
| 5652 | */ |
| 5653 | static int |
| 5654 | load_extension(UnpicklerObject *self, int nbytes) |
| 5655 | { |
| 5656 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5657 | long code; /* calc_binint returns long */ |
| 5658 | PyObject *py_code; /* code as a Python int */ |
| 5659 | PyObject *obj; /* the object to push */ |
| 5660 | PyObject *pair; /* (module_name, class_name) */ |
| 5661 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5662 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5663 | |
| 5664 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5665 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5666 | return -1; |
| 5667 | code = calc_binint(codebytes, nbytes); |
| 5668 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5669 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5670 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5671 | return -1; |
| 5672 | } |
| 5673 | |
| 5674 | /* Look for the code in the cache. */ |
| 5675 | py_code = PyLong_FromLong(code); |
| 5676 | if (py_code == NULL) |
| 5677 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5678 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5679 | if (obj != NULL) { |
| 5680 | /* Bingo. */ |
| 5681 | Py_DECREF(py_code); |
| 5682 | PDATA_APPEND(self->stack, obj, -1); |
| 5683 | return 0; |
| 5684 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5685 | if (PyErr_Occurred()) { |
| 5686 | Py_DECREF(py_code); |
| 5687 | return -1; |
| 5688 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5689 | |
| 5690 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5691 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5692 | if (pair == NULL) { |
| 5693 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5694 | if (!PyErr_Occurred()) { |
| 5695 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5696 | "code %ld", code); |
| 5697 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5698 | return -1; |
| 5699 | } |
| 5700 | /* Since the extension registry is manipulable via Python code, |
| 5701 | * confirm that pair is really a 2-tuple of strings. |
| 5702 | */ |
| 5703 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5704 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5705 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5706 | Py_DECREF(py_code); |
| 5707 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5708 | "isn't a 2-tuple of strings", code); |
| 5709 | return -1; |
| 5710 | } |
| 5711 | /* Load the object. */ |
| 5712 | obj = find_class(self, module_name, class_name); |
| 5713 | if (obj == NULL) { |
| 5714 | Py_DECREF(py_code); |
| 5715 | return -1; |
| 5716 | } |
| 5717 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5718 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5719 | Py_DECREF(py_code); |
| 5720 | if (code < 0) { |
| 5721 | Py_DECREF(obj); |
| 5722 | return -1; |
| 5723 | } |
| 5724 | PDATA_PUSH(self->stack, obj, -1); |
| 5725 | return 0; |
| 5726 | } |
| 5727 | |
| 5728 | static int |
| 5729 | load_put(UnpicklerObject *self) |
| 5730 | { |
| 5731 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5732 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5733 | Py_ssize_t len; |
| 5734 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5735 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5736 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5737 | return -1; |
| 5738 | if (len < 2) |
| 5739 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5740 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5741 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5742 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5743 | |
| 5744 | key = PyLong_FromString(s, NULL, 10); |
| 5745 | if (key == NULL) |
| 5746 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5747 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5748 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5749 | if (idx < 0) { |
| 5750 | if (!PyErr_Occurred()) |
| 5751 | PyErr_SetString(PyExc_ValueError, |
| 5752 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5753 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5754 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5755 | |
| 5756 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
| 5759 | static int |
| 5760 | load_binput(UnpicklerObject *self) |
| 5761 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5762 | PyObject *value; |
| 5763 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 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 (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5767 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5768 | |
| 5769 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5770 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5771 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5772 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5773 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5774 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5775 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5776 | } |
| 5777 | |
| 5778 | static int |
| 5779 | load_long_binput(UnpicklerObject *self) |
| 5780 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5781 | PyObject *value; |
| 5782 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5783 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5784 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5785 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5786 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5787 | |
| 5788 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5789 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5790 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5791 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5792 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5793 | if (idx < 0) { |
| 5794 | PyErr_SetString(PyExc_ValueError, |
| 5795 | "negative LONG_BINPUT argument"); |
| 5796 | return -1; |
| 5797 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5798 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5799 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5800 | } |
| 5801 | |
| 5802 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5803 | load_memoize(UnpicklerObject *self) |
| 5804 | { |
| 5805 | PyObject *value; |
| 5806 | |
| 5807 | if (Py_SIZE(self->stack) <= 0) |
| 5808 | return stack_underflow(); |
| 5809 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5810 | |
| 5811 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5812 | } |
| 5813 | |
| 5814 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5815 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5816 | { |
| 5817 | PyObject *value; |
| 5818 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5819 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5820 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5821 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5822 | if (x > len || x <= 0) |
| 5823 | return stack_underflow(); |
| 5824 | if (len == x) /* nothing to do */ |
| 5825 | return 0; |
| 5826 | |
| 5827 | list = self->stack->data[x - 1]; |
| 5828 | |
| 5829 | if (PyList_Check(list)) { |
| 5830 | PyObject *slice; |
| 5831 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5832 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5833 | |
| 5834 | slice = Pdata_poplist(self->stack, x); |
| 5835 | if (!slice) |
| 5836 | return -1; |
| 5837 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5838 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5839 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5840 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5841 | } |
| 5842 | else { |
| 5843 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5844 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5845 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5846 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5847 | if (append_func == NULL) |
| 5848 | return -1; |
| 5849 | for (i = x; i < len; i++) { |
| 5850 | PyObject *result; |
| 5851 | |
| 5852 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5853 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5854 | if (result == NULL) { |
| 5855 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5856 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5857 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5858 | return -1; |
| 5859 | } |
| 5860 | Py_DECREF(result); |
| 5861 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5862 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5863 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5864 | } |
| 5865 | |
| 5866 | return 0; |
| 5867 | } |
| 5868 | |
| 5869 | static int |
| 5870 | load_append(UnpicklerObject *self) |
| 5871 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5872 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5873 | } |
| 5874 | |
| 5875 | static int |
| 5876 | load_appends(UnpicklerObject *self) |
| 5877 | { |
| 5878 | return do_append(self, marker(self)); |
| 5879 | } |
| 5880 | |
| 5881 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5882 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5883 | { |
| 5884 | PyObject *value, *key; |
| 5885 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5886 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5887 | int status = 0; |
| 5888 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5889 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5890 | if (x > len || x <= 0) |
| 5891 | return stack_underflow(); |
| 5892 | if (len == x) /* nothing to do */ |
| 5893 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5894 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5895 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5896 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5897 | PyErr_SetString(st->UnpicklingError, |
| 5898 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5899 | return -1; |
| 5900 | } |
| 5901 | |
| 5902 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5903 | that supports the __setitem__ attribute. */ |
| 5904 | dict = self->stack->data[x - 1]; |
| 5905 | |
| 5906 | for (i = x + 1; i < len; i += 2) { |
| 5907 | key = self->stack->data[i - 1]; |
| 5908 | value = self->stack->data[i]; |
| 5909 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5910 | status = -1; |
| 5911 | break; |
| 5912 | } |
| 5913 | } |
| 5914 | |
| 5915 | Pdata_clear(self->stack, x); |
| 5916 | return status; |
| 5917 | } |
| 5918 | |
| 5919 | static int |
| 5920 | load_setitem(UnpicklerObject *self) |
| 5921 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5922 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5923 | } |
| 5924 | |
| 5925 | static int |
| 5926 | load_setitems(UnpicklerObject *self) |
| 5927 | { |
| 5928 | return do_setitems(self, marker(self)); |
| 5929 | } |
| 5930 | |
| 5931 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5932 | load_additems(UnpicklerObject *self) |
| 5933 | { |
| 5934 | PyObject *set; |
| 5935 | Py_ssize_t mark, len, i; |
| 5936 | |
| 5937 | mark = marker(self); |
| 5938 | len = Py_SIZE(self->stack); |
| 5939 | if (mark > len || mark <= 0) |
| 5940 | return stack_underflow(); |
| 5941 | if (len == mark) /* nothing to do */ |
| 5942 | return 0; |
| 5943 | |
| 5944 | set = self->stack->data[mark - 1]; |
| 5945 | |
| 5946 | if (PySet_Check(set)) { |
| 5947 | PyObject *items; |
| 5948 | int status; |
| 5949 | |
| 5950 | items = Pdata_poptuple(self->stack, mark); |
| 5951 | if (items == NULL) |
| 5952 | return -1; |
| 5953 | |
| 5954 | status = _PySet_Update(set, items); |
| 5955 | Py_DECREF(items); |
| 5956 | return status; |
| 5957 | } |
| 5958 | else { |
| 5959 | PyObject *add_func; |
| 5960 | _Py_IDENTIFIER(add); |
| 5961 | |
| 5962 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5963 | if (add_func == NULL) |
| 5964 | return -1; |
| 5965 | for (i = mark; i < len; i++) { |
| 5966 | PyObject *result; |
| 5967 | PyObject *item; |
| 5968 | |
| 5969 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5970 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5971 | if (result == NULL) { |
| 5972 | Pdata_clear(self->stack, i + 1); |
| 5973 | Py_SIZE(self->stack) = mark; |
| 5974 | return -1; |
| 5975 | } |
| 5976 | Py_DECREF(result); |
| 5977 | } |
| 5978 | Py_SIZE(self->stack) = mark; |
| 5979 | } |
| 5980 | |
| 5981 | return 0; |
| 5982 | } |
| 5983 | |
| 5984 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5985 | load_build(UnpicklerObject *self) |
| 5986 | { |
| 5987 | PyObject *state, *inst, *slotstate; |
| 5988 | PyObject *setstate; |
| 5989 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5990 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5991 | |
| 5992 | /* Stack is ... instance, state. We want to leave instance at |
| 5993 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5994 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5995 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5996 | return stack_underflow(); |
| 5997 | |
| 5998 | PDATA_POP(self->stack, state); |
| 5999 | if (state == NULL) |
| 6000 | return -1; |
| 6001 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6002 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6003 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6004 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 6005 | if (setstate == NULL) { |
| 6006 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 6007 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 6008 | else { |
| 6009 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 6010 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 6011 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6012 | } |
| 6013 | else { |
| 6014 | PyObject *result; |
| 6015 | |
| 6016 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 6017 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6018 | Py_DECREF(setstate); |
| 6019 | if (result == NULL) |
| 6020 | return -1; |
| 6021 | Py_DECREF(result); |
| 6022 | return 0; |
| 6023 | } |
| 6024 | |
| 6025 | /* A default __setstate__. First see whether state embeds a |
| 6026 | * slot state dict too (a proto 2 addition). |
| 6027 | */ |
| 6028 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 6029 | PyObject *tmp = state; |
| 6030 | |
| 6031 | state = PyTuple_GET_ITEM(tmp, 0); |
| 6032 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 6033 | Py_INCREF(state); |
| 6034 | Py_INCREF(slotstate); |
| 6035 | Py_DECREF(tmp); |
| 6036 | } |
| 6037 | else |
| 6038 | slotstate = NULL; |
| 6039 | |
| 6040 | /* Set inst.__dict__ from the state dict (if any). */ |
| 6041 | if (state != Py_None) { |
| 6042 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6043 | PyObject *d_key, *d_value; |
| 6044 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 6045 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6046 | |
| 6047 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6048 | PickleState *st = _Pickle_GetGlobalState(); |
| 6049 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6050 | goto error; |
| 6051 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6052 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6053 | if (dict == NULL) |
| 6054 | goto error; |
| 6055 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 6056 | i = 0; |
| 6057 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 6058 | /* normally the keys for instance attributes are |
| 6059 | interned. we should try to do that here. */ |
| 6060 | Py_INCREF(d_key); |
| 6061 | if (PyUnicode_CheckExact(d_key)) |
| 6062 | PyUnicode_InternInPlace(&d_key); |
| 6063 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 6064 | Py_DECREF(d_key); |
| 6065 | goto error; |
| 6066 | } |
| 6067 | Py_DECREF(d_key); |
| 6068 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6069 | Py_DECREF(dict); |
| 6070 | } |
| 6071 | |
| 6072 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 6073 | if (slotstate != NULL) { |
| 6074 | PyObject *d_key, *d_value; |
| 6075 | Py_ssize_t i; |
| 6076 | |
| 6077 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6078 | PickleState *st = _Pickle_GetGlobalState(); |
| 6079 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6080 | "slot state is not a dictionary"); |
| 6081 | goto error; |
| 6082 | } |
| 6083 | i = 0; |
| 6084 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6085 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6086 | goto error; |
| 6087 | } |
| 6088 | } |
| 6089 | |
| 6090 | if (0) { |
| 6091 | error: |
| 6092 | status = -1; |
| 6093 | } |
| 6094 | |
| 6095 | Py_DECREF(state); |
| 6096 | Py_XDECREF(slotstate); |
| 6097 | return status; |
| 6098 | } |
| 6099 | |
| 6100 | static int |
| 6101 | load_mark(UnpicklerObject *self) |
| 6102 | { |
| 6103 | |
| 6104 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6105 | * object stack and a mark stack. Here we push a mark onto the |
| 6106 | * mark stack. |
| 6107 | */ |
| 6108 | |
| 6109 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6110 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6111 | |
| 6112 | /* Use the size_t type to check for overflow. */ |
| 6113 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6114 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6115 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6116 | PyErr_NoMemory(); |
| 6117 | return -1; |
| 6118 | } |
| 6119 | |
| 6120 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6121 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6122 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6123 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6124 | if (self->marks == NULL) { |
| 6125 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6126 | PyErr_NoMemory(); |
| 6127 | return -1; |
| 6128 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6129 | self->marks_size = (Py_ssize_t)alloc; |
| 6130 | } |
| 6131 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6132 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6133 | |
| 6134 | return 0; |
| 6135 | } |
| 6136 | |
| 6137 | static int |
| 6138 | load_reduce(UnpicklerObject *self) |
| 6139 | { |
| 6140 | PyObject *callable = NULL; |
| 6141 | PyObject *argtup = NULL; |
| 6142 | PyObject *obj = NULL; |
| 6143 | |
| 6144 | PDATA_POP(self->stack, argtup); |
| 6145 | if (argtup == NULL) |
| 6146 | return -1; |
| 6147 | PDATA_POP(self->stack, callable); |
| 6148 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6149 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6150 | Py_DECREF(callable); |
| 6151 | } |
| 6152 | Py_DECREF(argtup); |
| 6153 | |
| 6154 | if (obj == NULL) |
| 6155 | return -1; |
| 6156 | |
| 6157 | PDATA_PUSH(self->stack, obj, -1); |
| 6158 | return 0; |
| 6159 | } |
| 6160 | |
| 6161 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6162 | * is the first opcode for protocols >= 2. |
| 6163 | */ |
| 6164 | static int |
| 6165 | load_proto(UnpicklerObject *self) |
| 6166 | { |
| 6167 | char *s; |
| 6168 | int i; |
| 6169 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6170 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6171 | return -1; |
| 6172 | |
| 6173 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6174 | if (i <= HIGHEST_PROTOCOL) { |
| 6175 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6176 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6177 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6178 | |
| 6179 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6180 | return -1; |
| 6181 | } |
| 6182 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6183 | static int |
| 6184 | load_frame(UnpicklerObject *self) |
| 6185 | { |
| 6186 | char *s; |
| 6187 | Py_ssize_t frame_len; |
| 6188 | |
| 6189 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6190 | return -1; |
| 6191 | |
| 6192 | frame_len = calc_binsize(s, 8); |
| 6193 | if (frame_len < 0) { |
| 6194 | PyErr_Format(PyExc_OverflowError, |
| 6195 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6196 | PY_SSIZE_T_MAX); |
| 6197 | return -1; |
| 6198 | } |
| 6199 | |
| 6200 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6201 | return -1; |
| 6202 | |
| 6203 | /* Rewind to start of frame */ |
| 6204 | self->next_read_idx -= frame_len; |
| 6205 | return 0; |
| 6206 | } |
| 6207 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6208 | static PyObject * |
| 6209 | load(UnpicklerObject *self) |
| 6210 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6211 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6212 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6213 | |
| 6214 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6215 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6216 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6217 | Pdata_clear(self->stack, 0); |
| 6218 | |
| 6219 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6220 | #define OP(opcode, load_func) \ |
| 6221 | case opcode: if (load_func(self) < 0) break; continue; |
| 6222 | |
| 6223 | #define OP_ARG(opcode, load_func, arg) \ |
| 6224 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6225 | |
| 6226 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6227 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6228 | break; |
| 6229 | |
| 6230 | switch ((enum opcode)s[0]) { |
| 6231 | OP(NONE, load_none) |
| 6232 | OP(BININT, load_binint) |
| 6233 | OP(BININT1, load_binint1) |
| 6234 | OP(BININT2, load_binint2) |
| 6235 | OP(INT, load_int) |
| 6236 | OP(LONG, load_long) |
| 6237 | OP_ARG(LONG1, load_counted_long, 1) |
| 6238 | OP_ARG(LONG4, load_counted_long, 4) |
| 6239 | OP(FLOAT, load_float) |
| 6240 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6241 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6242 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6243 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6244 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6245 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6246 | OP(STRING, load_string) |
| 6247 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6248 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6249 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6250 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6251 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6252 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6253 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6254 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6255 | OP(TUPLE, load_tuple) |
| 6256 | OP(EMPTY_LIST, load_empty_list) |
| 6257 | OP(LIST, load_list) |
| 6258 | OP(EMPTY_DICT, load_empty_dict) |
| 6259 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6260 | OP(EMPTY_SET, load_empty_set) |
| 6261 | OP(ADDITEMS, load_additems) |
| 6262 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6263 | OP(OBJ, load_obj) |
| 6264 | OP(INST, load_inst) |
| 6265 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6266 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6267 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6268 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6269 | OP(APPEND, load_append) |
| 6270 | OP(APPENDS, load_appends) |
| 6271 | OP(BUILD, load_build) |
| 6272 | OP(DUP, load_dup) |
| 6273 | OP(BINGET, load_binget) |
| 6274 | OP(LONG_BINGET, load_long_binget) |
| 6275 | OP(GET, load_get) |
| 6276 | OP(MARK, load_mark) |
| 6277 | OP(BINPUT, load_binput) |
| 6278 | OP(LONG_BINPUT, load_long_binput) |
| 6279 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6280 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6281 | OP(POP, load_pop) |
| 6282 | OP(POP_MARK, load_pop_mark) |
| 6283 | OP(SETITEM, load_setitem) |
| 6284 | OP(SETITEMS, load_setitems) |
| 6285 | OP(PERSID, load_persid) |
| 6286 | OP(BINPERSID, load_binpersid) |
| 6287 | OP(REDUCE, load_reduce) |
| 6288 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6289 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6290 | OP_ARG(EXT1, load_extension, 1) |
| 6291 | OP_ARG(EXT2, load_extension, 2) |
| 6292 | OP_ARG(EXT4, load_extension, 4) |
| 6293 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6294 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6295 | |
| 6296 | case STOP: |
| 6297 | break; |
| 6298 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6299 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6300 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6301 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6302 | } |
| 6303 | else { |
| 6304 | PickleState *st = _Pickle_GetGlobalState(); |
| 6305 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6306 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6307 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6308 | return NULL; |
| 6309 | } |
| 6310 | |
| 6311 | break; /* and we are done! */ |
| 6312 | } |
| 6313 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6314 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6315 | return NULL; |
| 6316 | } |
| 6317 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6318 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6319 | return NULL; |
| 6320 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6321 | PDATA_POP(self->stack, value); |
| 6322 | return value; |
| 6323 | } |
| 6324 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6325 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6326 | |
| 6327 | _pickle.Unpickler.load |
| 6328 | |
| 6329 | Load a pickle. |
| 6330 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6331 | Read a pickled object representation from the open file object given |
| 6332 | in the constructor, and return the reconstituted object hierarchy |
| 6333 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6334 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6335 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6336 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6337 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6338 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6339 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6340 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6341 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6342 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6343 | segfaulting if a subclass overridden __init__ with a function that does |
| 6344 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6345 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6346 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6347 | PickleState *st = _Pickle_GetGlobalState(); |
| 6348 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6349 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6350 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6351 | return NULL; |
| 6352 | } |
| 6353 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6354 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6355 | } |
| 6356 | |
| 6357 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6358 | function is used for loading any global (i.e., functions), not just |
| 6359 | classes. The name is kept only for backward compatibility. */ |
| 6360 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6361 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6362 | |
| 6363 | _pickle.Unpickler.find_class |
| 6364 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6365 | module_name: object |
| 6366 | global_name: object |
| 6367 | / |
| 6368 | |
| 6369 | Return an object from a specified module. |
| 6370 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6371 | If necessary, the module will be imported. Subclasses may override |
| 6372 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6373 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6374 | |
| 6375 | This method is called whenever a class or a function object is |
| 6376 | needed. Both arguments passed are str objects. |
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 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6379 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6380 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6381 | PyObject *module_name, |
| 6382 | PyObject *global_name) |
| 6383 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6384 | { |
| 6385 | PyObject *global; |
| 6386 | PyObject *modules_dict; |
| 6387 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6388 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6389 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6390 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6391 | Python 3.x. We do this only with old pickle protocols and when the |
| 6392 | user has not disabled the feature. */ |
| 6393 | if (self->proto < 3 && self->fix_imports) { |
| 6394 | PyObject *key; |
| 6395 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6396 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6397 | |
| 6398 | /* Check if the global (i.e., a function or a class) was renamed |
| 6399 | or moved to another module. */ |
| 6400 | key = PyTuple_Pack(2, module_name, global_name); |
| 6401 | if (key == NULL) |
| 6402 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6403 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6404 | Py_DECREF(key); |
| 6405 | if (item) { |
| 6406 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6407 | PyErr_Format(PyExc_RuntimeError, |
| 6408 | "_compat_pickle.NAME_MAPPING values should be " |
| 6409 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6410 | return NULL; |
| 6411 | } |
| 6412 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6413 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6414 | if (!PyUnicode_Check(module_name) || |
| 6415 | !PyUnicode_Check(global_name)) { |
| 6416 | PyErr_Format(PyExc_RuntimeError, |
| 6417 | "_compat_pickle.NAME_MAPPING values should be " |
| 6418 | "pairs of str, not (%.200s, %.200s)", |
| 6419 | Py_TYPE(module_name)->tp_name, |
| 6420 | Py_TYPE(global_name)->tp_name); |
| 6421 | return NULL; |
| 6422 | } |
| 6423 | } |
| 6424 | else if (PyErr_Occurred()) { |
| 6425 | return NULL; |
| 6426 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6427 | else { |
| 6428 | /* Check if the module was renamed. */ |
| 6429 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6430 | if (item) { |
| 6431 | if (!PyUnicode_Check(item)) { |
| 6432 | PyErr_Format(PyExc_RuntimeError, |
| 6433 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6434 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6435 | return NULL; |
| 6436 | } |
| 6437 | module_name = item; |
| 6438 | } |
| 6439 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6440 | return NULL; |
| 6441 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6442 | } |
| 6443 | } |
| 6444 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6445 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6446 | if (modules_dict == NULL) { |
| 6447 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6448 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6449 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6450 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6451 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6452 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6453 | if (PyErr_Occurred()) |
| 6454 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6455 | module = PyImport_Import(module_name); |
| 6456 | if (module == NULL) |
| 6457 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6458 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6459 | Py_DECREF(module); |
| 6460 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6461 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6462 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6463 | } |
| 6464 | return global; |
| 6465 | } |
| 6466 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6467 | /*[clinic input] |
| 6468 | |
| 6469 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6470 | |
| 6471 | Returns size in memory, in bytes. |
| 6472 | [clinic start generated code]*/ |
| 6473 | |
| 6474 | static Py_ssize_t |
| 6475 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6476 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6477 | { |
| 6478 | Py_ssize_t res; |
| 6479 | |
| 6480 | res = sizeof(UnpicklerObject); |
| 6481 | if (self->memo != NULL) |
| 6482 | res += self->memo_size * sizeof(PyObject *); |
| 6483 | if (self->marks != NULL) |
| 6484 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6485 | if (self->input_line != NULL) |
| 6486 | res += strlen(self->input_line) + 1; |
| 6487 | if (self->encoding != NULL) |
| 6488 | res += strlen(self->encoding) + 1; |
| 6489 | if (self->errors != NULL) |
| 6490 | res += strlen(self->errors) + 1; |
| 6491 | return res; |
| 6492 | } |
| 6493 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6494 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6495 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6496 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6497 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6498 | {NULL, NULL} /* sentinel */ |
| 6499 | }; |
| 6500 | |
| 6501 | static void |
| 6502 | Unpickler_dealloc(UnpicklerObject *self) |
| 6503 | { |
| 6504 | PyObject_GC_UnTrack((PyObject *)self); |
| 6505 | Py_XDECREF(self->readline); |
| 6506 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6507 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6508 | Py_XDECREF(self->stack); |
| 6509 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6510 | if (self->buffer.buf != NULL) { |
| 6511 | PyBuffer_Release(&self->buffer); |
| 6512 | self->buffer.buf = NULL; |
| 6513 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6514 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6515 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6516 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6517 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6518 | PyMem_Free(self->encoding); |
| 6519 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6520 | |
| 6521 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6522 | } |
| 6523 | |
| 6524 | static int |
| 6525 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6526 | { |
| 6527 | Py_VISIT(self->readline); |
| 6528 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6529 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6530 | Py_VISIT(self->stack); |
| 6531 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6532 | return 0; |
| 6533 | } |
| 6534 | |
| 6535 | static int |
| 6536 | Unpickler_clear(UnpicklerObject *self) |
| 6537 | { |
| 6538 | Py_CLEAR(self->readline); |
| 6539 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6540 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6541 | Py_CLEAR(self->stack); |
| 6542 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6543 | if (self->buffer.buf != NULL) { |
| 6544 | PyBuffer_Release(&self->buffer); |
| 6545 | self->buffer.buf = NULL; |
| 6546 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6547 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6548 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6549 | PyMem_Free(self->marks); |
| 6550 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6551 | PyMem_Free(self->input_line); |
| 6552 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6553 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6554 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6555 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6556 | self->errors = NULL; |
| 6557 | |
| 6558 | return 0; |
| 6559 | } |
| 6560 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6561 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6562 | |
| 6563 | _pickle.Unpickler.__init__ |
| 6564 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6565 | file: object |
| 6566 | * |
| 6567 | fix_imports: bool = True |
| 6568 | encoding: str = 'ASCII' |
| 6569 | errors: str = 'strict' |
| 6570 | |
| 6571 | This takes a binary file for reading a pickle data stream. |
| 6572 | |
| 6573 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6574 | protocol argument is needed. Bytes past the pickled object's |
| 6575 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6576 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6577 | The argument *file* must have two methods, a read() method that takes |
| 6578 | an integer argument, and a readline() method that requires no |
| 6579 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 6580 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6581 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6582 | |
| 6583 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6584 | which are used to control compatiblity support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6585 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6586 | 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] | 6587 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6588 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6589 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6590 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6591 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6592 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6593 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6594 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6595 | int fix_imports, const char *encoding, |
| 6596 | const char *errors) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 6597 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=04ece661aa884837]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6598 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6599 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6600 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6601 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6602 | if (self->read != NULL) |
| 6603 | (void)Unpickler_clear(self); |
| 6604 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6605 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6606 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6607 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6608 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6609 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6610 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6611 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6612 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6613 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6614 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6615 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6616 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6617 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6618 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6619 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6620 | } |
| 6621 | else { |
| 6622 | self->pers_func = NULL; |
| 6623 | } |
| 6624 | |
| 6625 | self->stack = (Pdata *)Pdata_New(); |
| 6626 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6627 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6628 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6629 | self->memo_size = 32; |
| 6630 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6631 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6632 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6633 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6634 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6635 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6636 | return 0; |
| 6637 | } |
| 6638 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6639 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6640 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6641 | * avoid breaking code like: |
| 6642 | * unpickler.memo.clear() |
| 6643 | * and |
| 6644 | * unpickler.memo = saved_memo |
| 6645 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6646 | * it. Note that we don't implement the entire mapping API here. This is |
| 6647 | * intentional, as these should be treated as black-box implementation details. |
| 6648 | * |
| 6649 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6650 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6651 | */ |
| 6652 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6653 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6654 | _pickle.UnpicklerMemoProxy.clear |
| 6655 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6656 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6657 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6658 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6659 | static PyObject * |
| 6660 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6661 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6662 | { |
| 6663 | _Unpickler_MemoCleanup(self->unpickler); |
| 6664 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6665 | if (self->unpickler->memo == NULL) |
| 6666 | return NULL; |
| 6667 | Py_RETURN_NONE; |
| 6668 | } |
| 6669 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6670 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6671 | _pickle.UnpicklerMemoProxy.copy |
| 6672 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6673 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6674 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6675 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6676 | static PyObject * |
| 6677 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6678 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6679 | { |
| 6680 | Py_ssize_t i; |
| 6681 | PyObject *new_memo = PyDict_New(); |
| 6682 | if (new_memo == NULL) |
| 6683 | return NULL; |
| 6684 | |
| 6685 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6686 | int status; |
| 6687 | PyObject *key, *value; |
| 6688 | |
| 6689 | value = self->unpickler->memo[i]; |
| 6690 | if (value == NULL) |
| 6691 | continue; |
| 6692 | |
| 6693 | key = PyLong_FromSsize_t(i); |
| 6694 | if (key == NULL) |
| 6695 | goto error; |
| 6696 | status = PyDict_SetItem(new_memo, key, value); |
| 6697 | Py_DECREF(key); |
| 6698 | if (status < 0) |
| 6699 | goto error; |
| 6700 | } |
| 6701 | return new_memo; |
| 6702 | |
| 6703 | error: |
| 6704 | Py_DECREF(new_memo); |
| 6705 | return NULL; |
| 6706 | } |
| 6707 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6708 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6709 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6710 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6711 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6712 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6713 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6714 | static PyObject * |
| 6715 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6716 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6717 | { |
| 6718 | PyObject *reduce_value; |
| 6719 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6720 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6721 | if (contents == NULL) |
| 6722 | return NULL; |
| 6723 | |
| 6724 | reduce_value = PyTuple_New(2); |
| 6725 | if (reduce_value == NULL) { |
| 6726 | Py_DECREF(contents); |
| 6727 | return NULL; |
| 6728 | } |
| 6729 | constructor_args = PyTuple_New(1); |
| 6730 | if (constructor_args == NULL) { |
| 6731 | Py_DECREF(contents); |
| 6732 | Py_DECREF(reduce_value); |
| 6733 | return NULL; |
| 6734 | } |
| 6735 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6736 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6737 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6738 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6739 | return reduce_value; |
| 6740 | } |
| 6741 | |
| 6742 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6743 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6744 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6745 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6746 | {NULL, NULL} /* sentinel */ |
| 6747 | }; |
| 6748 | |
| 6749 | static void |
| 6750 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6751 | { |
| 6752 | PyObject_GC_UnTrack(self); |
| 6753 | Py_XDECREF(self->unpickler); |
| 6754 | PyObject_GC_Del((PyObject *)self); |
| 6755 | } |
| 6756 | |
| 6757 | static int |
| 6758 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6759 | visitproc visit, void *arg) |
| 6760 | { |
| 6761 | Py_VISIT(self->unpickler); |
| 6762 | return 0; |
| 6763 | } |
| 6764 | |
| 6765 | static int |
| 6766 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6767 | { |
| 6768 | Py_CLEAR(self->unpickler); |
| 6769 | return 0; |
| 6770 | } |
| 6771 | |
| 6772 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6773 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6774 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6775 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6776 | 0, |
| 6777 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6778 | 0, /* tp_print */ |
| 6779 | 0, /* tp_getattr */ |
| 6780 | 0, /* tp_setattr */ |
| 6781 | 0, /* tp_compare */ |
| 6782 | 0, /* tp_repr */ |
| 6783 | 0, /* tp_as_number */ |
| 6784 | 0, /* tp_as_sequence */ |
| 6785 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6786 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6787 | 0, /* tp_call */ |
| 6788 | 0, /* tp_str */ |
| 6789 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6790 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6791 | 0, /* tp_as_buffer */ |
| 6792 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6793 | 0, /* tp_doc */ |
| 6794 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6795 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6796 | 0, /* tp_richcompare */ |
| 6797 | 0, /* tp_weaklistoffset */ |
| 6798 | 0, /* tp_iter */ |
| 6799 | 0, /* tp_iternext */ |
| 6800 | unpicklerproxy_methods, /* tp_methods */ |
| 6801 | }; |
| 6802 | |
| 6803 | static PyObject * |
| 6804 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6805 | { |
| 6806 | UnpicklerMemoProxyObject *self; |
| 6807 | |
| 6808 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6809 | &UnpicklerMemoProxyType); |
| 6810 | if (self == NULL) |
| 6811 | return NULL; |
| 6812 | Py_INCREF(unpickler); |
| 6813 | self->unpickler = unpickler; |
| 6814 | PyObject_GC_Track(self); |
| 6815 | return (PyObject *)self; |
| 6816 | } |
| 6817 | |
| 6818 | /*****************************************************************************/ |
| 6819 | |
| 6820 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6821 | static PyObject * |
| 6822 | Unpickler_get_memo(UnpicklerObject *self) |
| 6823 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6824 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
| 6827 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6828 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6829 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6830 | PyObject **new_memo; |
| 6831 | Py_ssize_t new_memo_size = 0; |
| 6832 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6833 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6834 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6835 | PyErr_SetString(PyExc_TypeError, |
| 6836 | "attribute deletion is not supported"); |
| 6837 | return -1; |
| 6838 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6839 | |
| 6840 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6841 | UnpicklerObject *unpickler = |
| 6842 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6843 | |
| 6844 | new_memo_size = unpickler->memo_size; |
| 6845 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6846 | if (new_memo == NULL) |
| 6847 | return -1; |
| 6848 | |
| 6849 | for (i = 0; i < new_memo_size; i++) { |
| 6850 | Py_XINCREF(unpickler->memo[i]); |
| 6851 | new_memo[i] = unpickler->memo[i]; |
| 6852 | } |
| 6853 | } |
| 6854 | else if (PyDict_Check(obj)) { |
| 6855 | Py_ssize_t i = 0; |
| 6856 | PyObject *key, *value; |
| 6857 | |
| 6858 | new_memo_size = PyDict_Size(obj); |
| 6859 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6860 | if (new_memo == NULL) |
| 6861 | return -1; |
| 6862 | |
| 6863 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6864 | Py_ssize_t idx; |
| 6865 | if (!PyLong_Check(key)) { |
| 6866 | PyErr_SetString(PyExc_TypeError, |
| 6867 | "memo key must be integers"); |
| 6868 | goto error; |
| 6869 | } |
| 6870 | idx = PyLong_AsSsize_t(key); |
| 6871 | if (idx == -1 && PyErr_Occurred()) |
| 6872 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6873 | if (idx < 0) { |
| 6874 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6875 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6876 | goto error; |
| 6877 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6878 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6879 | goto error; |
| 6880 | } |
| 6881 | } |
| 6882 | else { |
| 6883 | PyErr_Format(PyExc_TypeError, |
| 6884 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6885 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6886 | return -1; |
| 6887 | } |
| 6888 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6889 | _Unpickler_MemoCleanup(self); |
| 6890 | self->memo_size = new_memo_size; |
| 6891 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6892 | |
| 6893 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6894 | |
| 6895 | error: |
| 6896 | if (new_memo_size) { |
| 6897 | i = new_memo_size; |
| 6898 | while (--i >= 0) { |
| 6899 | Py_XDECREF(new_memo[i]); |
| 6900 | } |
| 6901 | PyMem_FREE(new_memo); |
| 6902 | } |
| 6903 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6904 | } |
| 6905 | |
| 6906 | static PyObject * |
| 6907 | Unpickler_get_persload(UnpicklerObject *self) |
| 6908 | { |
| 6909 | if (self->pers_func == NULL) |
| 6910 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6911 | else |
| 6912 | Py_INCREF(self->pers_func); |
| 6913 | return self->pers_func; |
| 6914 | } |
| 6915 | |
| 6916 | static int |
| 6917 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6918 | { |
| 6919 | PyObject *tmp; |
| 6920 | |
| 6921 | if (value == NULL) { |
| 6922 | PyErr_SetString(PyExc_TypeError, |
| 6923 | "attribute deletion is not supported"); |
| 6924 | return -1; |
| 6925 | } |
| 6926 | if (!PyCallable_Check(value)) { |
| 6927 | PyErr_SetString(PyExc_TypeError, |
| 6928 | "persistent_load must be a callable taking " |
| 6929 | "one argument"); |
| 6930 | return -1; |
| 6931 | } |
| 6932 | |
| 6933 | tmp = self->pers_func; |
| 6934 | Py_INCREF(value); |
| 6935 | self->pers_func = value; |
| 6936 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6937 | |
| 6938 | return 0; |
| 6939 | } |
| 6940 | |
| 6941 | static PyGetSetDef Unpickler_getsets[] = { |
| 6942 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6943 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6944 | (setter)Unpickler_set_persload}, |
| 6945 | {NULL} |
| 6946 | }; |
| 6947 | |
| 6948 | static PyTypeObject Unpickler_Type = { |
| 6949 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6950 | "_pickle.Unpickler", /*tp_name*/ |
| 6951 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6952 | 0, /*tp_itemsize*/ |
| 6953 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6954 | 0, /*tp_print*/ |
| 6955 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6956 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6957 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6958 | 0, /*tp_repr*/ |
| 6959 | 0, /*tp_as_number*/ |
| 6960 | 0, /*tp_as_sequence*/ |
| 6961 | 0, /*tp_as_mapping*/ |
| 6962 | 0, /*tp_hash*/ |
| 6963 | 0, /*tp_call*/ |
| 6964 | 0, /*tp_str*/ |
| 6965 | 0, /*tp_getattro*/ |
| 6966 | 0, /*tp_setattro*/ |
| 6967 | 0, /*tp_as_buffer*/ |
| 6968 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6969 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6970 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6971 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6972 | 0, /*tp_richcompare*/ |
| 6973 | 0, /*tp_weaklistoffset*/ |
| 6974 | 0, /*tp_iter*/ |
| 6975 | 0, /*tp_iternext*/ |
| 6976 | Unpickler_methods, /*tp_methods*/ |
| 6977 | 0, /*tp_members*/ |
| 6978 | Unpickler_getsets, /*tp_getset*/ |
| 6979 | 0, /*tp_base*/ |
| 6980 | 0, /*tp_dict*/ |
| 6981 | 0, /*tp_descr_get*/ |
| 6982 | 0, /*tp_descr_set*/ |
| 6983 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6984 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6985 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6986 | PyType_GenericNew, /*tp_new*/ |
| 6987 | PyObject_GC_Del, /*tp_free*/ |
| 6988 | 0, /*tp_is_gc*/ |
| 6989 | }; |
| 6990 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6991 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6992 | |
| 6993 | _pickle.dump |
| 6994 | |
| 6995 | obj: object |
| 6996 | file: object |
| 6997 | protocol: object = NULL |
| 6998 | * |
| 6999 | fix_imports: bool = True |
| 7000 | |
| 7001 | Write a pickled representation of obj to the open file object file. |
| 7002 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7003 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 7004 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7005 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7006 | The optional *protocol* argument tells the pickler to use the given |
| 7007 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 7008 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7009 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7010 | Specifying a negative protocol version selects the highest protocol |
| 7011 | version supported. The higher the protocol used, the more recent the |
| 7012 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7013 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7014 | The *file* argument must have a write() method that accepts a single |
| 7015 | bytes argument. It can thus be a file object opened for binary |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7016 | writing, an io.BytesIO instance, or any other custom object that meets |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7017 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7018 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7019 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 7020 | to map the new Python 3 names to the old module names used in Python |
| 7021 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7022 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7023 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7024 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7025 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, |
| 7026 | PyObject *protocol, int fix_imports) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 7027 | /*[clinic end generated code: output=0de7dff89c406816 input=830f8a64cef6f042]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7028 | { |
| 7029 | PicklerObject *pickler = _Pickler_New(); |
| 7030 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7031 | if (pickler == NULL) |
| 7032 | return NULL; |
| 7033 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7034 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7035 | goto error; |
| 7036 | |
| 7037 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 7038 | goto error; |
| 7039 | |
| 7040 | if (dump(pickler, obj) < 0) |
| 7041 | goto error; |
| 7042 | |
| 7043 | if (_Pickler_FlushToFile(pickler) < 0) |
| 7044 | goto error; |
| 7045 | |
| 7046 | Py_DECREF(pickler); |
| 7047 | Py_RETURN_NONE; |
| 7048 | |
| 7049 | error: |
| 7050 | Py_XDECREF(pickler); |
| 7051 | return NULL; |
| 7052 | } |
| 7053 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7054 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7055 | |
| 7056 | _pickle.dumps |
| 7057 | |
| 7058 | obj: object |
| 7059 | protocol: object = NULL |
| 7060 | * |
| 7061 | fix_imports: bool = True |
| 7062 | |
| 7063 | Return the pickled representation of the object as a bytes object. |
| 7064 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7065 | The optional *protocol* argument tells the pickler to use the given |
| 7066 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 7067 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7068 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7069 | Specifying a negative protocol version selects the highest protocol |
| 7070 | version supported. The higher the protocol used, the more recent the |
| 7071 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7072 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7073 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 7074 | try to map the new Python 3 names to the old module names used in |
| 7075 | 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] | 7076 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7077 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7078 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7079 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, |
| 7080 | int fix_imports) |
| 7081 | /*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7082 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7083 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7084 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7085 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7086 | if (pickler == NULL) |
| 7087 | return NULL; |
| 7088 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7089 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7090 | goto error; |
| 7091 | |
| 7092 | if (dump(pickler, obj) < 0) |
| 7093 | goto error; |
| 7094 | |
| 7095 | result = _Pickler_GetString(pickler); |
| 7096 | Py_DECREF(pickler); |
| 7097 | return result; |
| 7098 | |
| 7099 | error: |
| 7100 | Py_XDECREF(pickler); |
| 7101 | return NULL; |
| 7102 | } |
| 7103 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7104 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7105 | |
| 7106 | _pickle.load |
| 7107 | |
| 7108 | file: object |
| 7109 | * |
| 7110 | fix_imports: bool = True |
| 7111 | encoding: str = 'ASCII' |
| 7112 | errors: str = 'strict' |
| 7113 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7114 | 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] | 7115 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7116 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7117 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7118 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7119 | The protocol version of the pickle is detected automatically, so no |
| 7120 | protocol argument is needed. Bytes past the pickled object's |
| 7121 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7122 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7123 | The argument *file* must have two methods, a read() method that takes |
| 7124 | an integer argument, and a readline() method that requires no |
| 7125 | arguments. Both methods should return bytes. Thus *file* can be a |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 7126 | binary file object opened for reading, an io.BytesIO object, or any |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7127 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7128 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7129 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7130 | which are used to control compatiblity support for pickle stream |
| 7131 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7132 | map the old Python 2 names to the new names used in Python 3. The |
| 7133 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7134 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7135 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7136 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7137 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7138 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7139 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7140 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, |
| 7141 | const char *encoding, const char *errors) |
Martin Panter | 2eb819f | 2015-11-02 04:04:57 +0000 | [diff] [blame] | 7142 | /*[clinic end generated code: output=798f1c57cb2b4eb1 input=2df7c7a1e6742204]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7143 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7144 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7145 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7146 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7147 | if (unpickler == NULL) |
| 7148 | return NULL; |
| 7149 | |
| 7150 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7151 | goto error; |
| 7152 | |
| 7153 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7154 | goto error; |
| 7155 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7156 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7157 | |
| 7158 | result = load(unpickler); |
| 7159 | Py_DECREF(unpickler); |
| 7160 | return result; |
| 7161 | |
| 7162 | error: |
| 7163 | Py_XDECREF(unpickler); |
| 7164 | return NULL; |
| 7165 | } |
| 7166 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7167 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7168 | |
| 7169 | _pickle.loads |
| 7170 | |
| 7171 | data: object |
| 7172 | * |
| 7173 | fix_imports: bool = True |
| 7174 | encoding: str = 'ASCII' |
| 7175 | errors: str = 'strict' |
| 7176 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7177 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7178 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7179 | The protocol version of the pickle is detected automatically, so no |
| 7180 | protocol argument is needed. Bytes past the pickled object's |
| 7181 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7182 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7183 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7184 | which are used to control compatiblity support for pickle stream |
| 7185 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7186 | map the old Python 2 names to the new names used in Python 3. The |
| 7187 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7188 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7189 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7190 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7191 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7192 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7193 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7194 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, |
| 7195 | const char *encoding, const char *errors) |
| 7196 | /*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7197 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7198 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7199 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7200 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7201 | if (unpickler == NULL) |
| 7202 | return NULL; |
| 7203 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7204 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7205 | goto error; |
| 7206 | |
| 7207 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7208 | goto error; |
| 7209 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7210 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7211 | |
| 7212 | result = load(unpickler); |
| 7213 | Py_DECREF(unpickler); |
| 7214 | return result; |
| 7215 | |
| 7216 | error: |
| 7217 | Py_XDECREF(unpickler); |
| 7218 | return NULL; |
| 7219 | } |
| 7220 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7221 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7222 | _PICKLE_DUMP_METHODDEF |
| 7223 | _PICKLE_DUMPS_METHODDEF |
| 7224 | _PICKLE_LOAD_METHODDEF |
| 7225 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7226 | {NULL, NULL} /* sentinel */ |
| 7227 | }; |
| 7228 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7229 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7230 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7231 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7232 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7233 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7234 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7235 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7236 | static void |
| 7237 | pickle_free(PyObject *m) |
| 7238 | { |
| 7239 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7240 | } |
| 7241 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7242 | static int |
| 7243 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7244 | { |
| 7245 | PickleState *st = _Pickle_GetState(m); |
| 7246 | Py_VISIT(st->PickleError); |
| 7247 | Py_VISIT(st->PicklingError); |
| 7248 | Py_VISIT(st->UnpicklingError); |
| 7249 | Py_VISIT(st->dispatch_table); |
| 7250 | Py_VISIT(st->extension_registry); |
| 7251 | Py_VISIT(st->extension_cache); |
| 7252 | Py_VISIT(st->inverted_registry); |
| 7253 | Py_VISIT(st->name_mapping_2to3); |
| 7254 | Py_VISIT(st->import_mapping_2to3); |
| 7255 | Py_VISIT(st->name_mapping_3to2); |
| 7256 | Py_VISIT(st->import_mapping_3to2); |
| 7257 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7258 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7259 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7260 | } |
| 7261 | |
| 7262 | static struct PyModuleDef _picklemodule = { |
| 7263 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7264 | "_pickle", /* m_name */ |
| 7265 | pickle_module_doc, /* m_doc */ |
| 7266 | sizeof(PickleState), /* m_size */ |
| 7267 | pickle_methods, /* m_methods */ |
| 7268 | NULL, /* m_reload */ |
| 7269 | pickle_traverse, /* m_traverse */ |
| 7270 | pickle_clear, /* m_clear */ |
| 7271 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7272 | }; |
| 7273 | |
| 7274 | PyMODINIT_FUNC |
| 7275 | PyInit__pickle(void) |
| 7276 | { |
| 7277 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7278 | PickleState *st; |
| 7279 | |
| 7280 | m = PyState_FindModule(&_picklemodule); |
| 7281 | if (m) { |
| 7282 | Py_INCREF(m); |
| 7283 | return m; |
| 7284 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7285 | |
| 7286 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7287 | return NULL; |
| 7288 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7289 | return NULL; |
| 7290 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7291 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7292 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7293 | return NULL; |
| 7294 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7295 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7296 | |
| 7297 | /* Create the module and add the functions. */ |
| 7298 | m = PyModule_Create(&_picklemodule); |
| 7299 | if (m == NULL) |
| 7300 | return NULL; |
| 7301 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7302 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7303 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7304 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7305 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7306 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7307 | return NULL; |
| 7308 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7309 | st = _Pickle_GetState(m); |
| 7310 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7311 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7312 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7313 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7314 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7315 | st->PicklingError = \ |
| 7316 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7317 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7318 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7319 | st->UnpicklingError = \ |
| 7320 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7321 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7322 | return NULL; |
| 7323 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7324 | Py_INCREF(st->PickleError); |
| 7325 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7326 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7327 | Py_INCREF(st->PicklingError); |
| 7328 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7329 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7330 | Py_INCREF(st->UnpicklingError); |
| 7331 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7332 | return NULL; |
| 7333 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7334 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7335 | return NULL; |
| 7336 | |
| 7337 | return m; |
| 7338 | } |