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; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 156 | } PickleState; |
| 157 | |
| 158 | /* Forward declaration of the _pickle module definition. */ |
| 159 | static struct PyModuleDef _picklemodule; |
| 160 | |
| 161 | /* Given a module object, get its per-module state. */ |
| 162 | static PickleState * |
| 163 | _Pickle_GetState(PyObject *module) |
| 164 | { |
| 165 | return (PickleState *)PyModule_GetState(module); |
| 166 | } |
| 167 | |
| 168 | /* Find the module instance imported in the currently running sub-interpreter |
| 169 | and get its state. */ |
| 170 | static PickleState * |
| 171 | _Pickle_GetGlobalState(void) |
| 172 | { |
| 173 | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
| 174 | } |
| 175 | |
| 176 | /* Clear the given pickle module state. */ |
| 177 | static void |
| 178 | _Pickle_ClearState(PickleState *st) |
| 179 | { |
| 180 | Py_CLEAR(st->PickleError); |
| 181 | Py_CLEAR(st->PicklingError); |
| 182 | Py_CLEAR(st->UnpicklingError); |
| 183 | Py_CLEAR(st->dispatch_table); |
| 184 | Py_CLEAR(st->extension_registry); |
| 185 | Py_CLEAR(st->extension_cache); |
| 186 | Py_CLEAR(st->inverted_registry); |
| 187 | Py_CLEAR(st->name_mapping_2to3); |
| 188 | Py_CLEAR(st->import_mapping_2to3); |
| 189 | Py_CLEAR(st->name_mapping_3to2); |
| 190 | Py_CLEAR(st->import_mapping_3to2); |
| 191 | Py_CLEAR(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 192 | Py_CLEAR(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* Initialize the given pickle module state. */ |
| 196 | static int |
| 197 | _Pickle_InitState(PickleState *st) |
| 198 | { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 199 | PyObject *builtins; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 200 | PyObject *copyreg = NULL; |
| 201 | PyObject *compat_pickle = NULL; |
| 202 | PyObject *codecs = NULL; |
| 203 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 204 | builtins = PyEval_GetBuiltins(); |
| 205 | if (builtins == NULL) |
| 206 | goto error; |
| 207 | st->getattr = PyDict_GetItemString(builtins, "getattr"); |
| 208 | if (st->getattr == NULL) |
| 209 | goto error; |
| 210 | Py_INCREF(st->getattr); |
| 211 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 212 | copyreg = PyImport_ImportModule("copyreg"); |
| 213 | if (!copyreg) |
| 214 | goto error; |
| 215 | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
| 216 | if (!st->dispatch_table) |
| 217 | goto error; |
| 218 | if (!PyDict_CheckExact(st->dispatch_table)) { |
| 219 | PyErr_Format(PyExc_RuntimeError, |
| 220 | "copyreg.dispatch_table should be a dict, not %.200s", |
| 221 | Py_TYPE(st->dispatch_table)->tp_name); |
| 222 | goto error; |
| 223 | } |
| 224 | st->extension_registry = \ |
| 225 | PyObject_GetAttrString(copyreg, "_extension_registry"); |
| 226 | if (!st->extension_registry) |
| 227 | goto error; |
| 228 | if (!PyDict_CheckExact(st->extension_registry)) { |
| 229 | PyErr_Format(PyExc_RuntimeError, |
| 230 | "copyreg._extension_registry should be a dict, " |
| 231 | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
| 232 | goto error; |
| 233 | } |
| 234 | st->inverted_registry = \ |
| 235 | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
| 236 | if (!st->inverted_registry) |
| 237 | goto error; |
| 238 | if (!PyDict_CheckExact(st->inverted_registry)) { |
| 239 | PyErr_Format(PyExc_RuntimeError, |
| 240 | "copyreg._inverted_registry should be a dict, " |
| 241 | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
| 242 | goto error; |
| 243 | } |
| 244 | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
| 245 | if (!st->extension_cache) |
| 246 | goto error; |
| 247 | if (!PyDict_CheckExact(st->extension_cache)) { |
| 248 | PyErr_Format(PyExc_RuntimeError, |
| 249 | "copyreg._extension_cache should be a dict, " |
| 250 | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
| 251 | goto error; |
| 252 | } |
| 253 | Py_CLEAR(copyreg); |
| 254 | |
| 255 | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
| 256 | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
| 257 | if (!compat_pickle) |
| 258 | goto error; |
| 259 | st->name_mapping_2to3 = \ |
| 260 | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
| 261 | if (!st->name_mapping_2to3) |
| 262 | goto error; |
| 263 | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
| 264 | PyErr_Format(PyExc_RuntimeError, |
| 265 | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
| 266 | Py_TYPE(st->name_mapping_2to3)->tp_name); |
| 267 | goto error; |
| 268 | } |
| 269 | st->import_mapping_2to3 = \ |
| 270 | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
| 271 | if (!st->import_mapping_2to3) |
| 272 | goto error; |
| 273 | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
| 274 | PyErr_Format(PyExc_RuntimeError, |
| 275 | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
| 276 | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
| 277 | goto error; |
| 278 | } |
| 279 | /* ... and the 3.x -> 2.x mapping tables */ |
| 280 | st->name_mapping_3to2 = \ |
| 281 | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
| 282 | if (!st->name_mapping_3to2) |
| 283 | goto error; |
| 284 | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
| 285 | PyErr_Format(PyExc_RuntimeError, |
| 286 | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
| 287 | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
| 288 | goto error; |
| 289 | } |
| 290 | st->import_mapping_3to2 = \ |
| 291 | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
| 292 | if (!st->import_mapping_3to2) |
| 293 | goto error; |
| 294 | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
| 295 | PyErr_Format(PyExc_RuntimeError, |
| 296 | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
| 297 | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
| 298 | goto error; |
| 299 | } |
| 300 | Py_CLEAR(compat_pickle); |
| 301 | |
| 302 | codecs = PyImport_ImportModule("codecs"); |
| 303 | if (codecs == NULL) |
| 304 | goto error; |
| 305 | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
| 306 | if (st->codecs_encode == NULL) { |
| 307 | goto error; |
| 308 | } |
| 309 | if (!PyCallable_Check(st->codecs_encode)) { |
| 310 | PyErr_Format(PyExc_RuntimeError, |
| 311 | "codecs.encode should be a callable, not %.200s", |
| 312 | Py_TYPE(st->codecs_encode)->tp_name); |
| 313 | goto error; |
| 314 | } |
| 315 | Py_CLEAR(codecs); |
| 316 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 317 | return 0; |
| 318 | |
| 319 | error: |
| 320 | Py_CLEAR(copyreg); |
| 321 | Py_CLEAR(compat_pickle); |
| 322 | Py_CLEAR(codecs); |
| 323 | _Pickle_ClearState(st); |
| 324 | return -1; |
| 325 | } |
| 326 | |
| 327 | /* Helper for calling a function with a single argument quickly. |
| 328 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 329 | This function steals the reference of the given argument. */ |
| 330 | static PyObject * |
| 331 | _Pickle_FastCall(PyObject *func, PyObject *obj) |
| 332 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 333 | PyObject *result; |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 334 | PyObject *arg_tuple = PyTuple_New(1); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 335 | |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 336 | /* Note: this function used to reuse the argument tuple. This used to give |
| 337 | a slight performance boost with older pickle implementations where many |
| 338 | unbuffered reads occurred (thus needing many function calls). |
| 339 | |
| 340 | However, this optimization was removed because it was too complicated |
| 341 | to get right. It abused the C API for tuples to mutate them which led |
| 342 | to subtle reference counting and concurrency bugs. Furthermore, the |
| 343 | introduction of protocol 4 and the prefetching optimization via peek() |
| 344 | significantly reduced the number of function calls we do. Thus, the |
| 345 | benefits became marginal at best. */ |
| 346 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 347 | if (arg_tuple == NULL) { |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 348 | Py_DECREF(obj); |
| 349 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 350 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 351 | PyTuple_SET_ITEM(arg_tuple, 0, obj); |
| 352 | result = PyObject_Call(func, arg_tuple, NULL); |
Alexandre Vassalotti | b13e6bc | 2013-11-28 14:56:09 -0800 | [diff] [blame] | 353 | Py_CLEAR(arg_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 354 | return result; |
| 355 | } |
| 356 | |
| 357 | /*************************************************************************/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 358 | |
| 359 | static int |
| 360 | stack_underflow(void) |
| 361 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 362 | PickleState *st = _Pickle_GetGlobalState(); |
| 363 | PyErr_SetString(st->UnpicklingError, "unpickling stack underflow"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 364 | return -1; |
| 365 | } |
| 366 | |
| 367 | /* Internal data type used as the unpickling stack. */ |
| 368 | typedef struct { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 369 | PyObject_VAR_HEAD |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 370 | PyObject **data; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 371 | Py_ssize_t allocated; /* number of slots in data allocated */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 372 | } Pdata; |
| 373 | |
| 374 | static void |
| 375 | Pdata_dealloc(Pdata *self) |
| 376 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 377 | Py_ssize_t i = Py_SIZE(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 378 | while (--i >= 0) { |
| 379 | Py_DECREF(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 380 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 381 | PyMem_FREE(self->data); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 382 | PyObject_Del(self); |
| 383 | } |
| 384 | |
| 385 | static PyTypeObject Pdata_Type = { |
| 386 | PyVarObject_HEAD_INIT(NULL, 0) |
| 387 | "_pickle.Pdata", /*tp_name*/ |
| 388 | sizeof(Pdata), /*tp_basicsize*/ |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 389 | sizeof(PyObject *), /*tp_itemsize*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 390 | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
| 391 | }; |
| 392 | |
| 393 | static PyObject * |
| 394 | Pdata_New(void) |
| 395 | { |
| 396 | Pdata *self; |
| 397 | |
| 398 | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
| 399 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 400 | Py_SIZE(self) = 0; |
| 401 | self->allocated = 8; |
| 402 | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 403 | if (self->data) |
| 404 | return (PyObject *)self; |
| 405 | Py_DECREF(self); |
| 406 | return PyErr_NoMemory(); |
| 407 | } |
| 408 | |
| 409 | |
| 410 | /* Retain only the initial clearto items. If clearto >= the current |
| 411 | * number of items, this is a (non-erroneous) NOP. |
| 412 | */ |
| 413 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 414 | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 415 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 416 | Py_ssize_t i = Py_SIZE(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 417 | |
| 418 | if (clearto < 0) |
| 419 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 420 | if (clearto >= i) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 421 | return 0; |
| 422 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 423 | while (--i >= clearto) { |
| 424 | Py_CLEAR(self->data[i]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 425 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 426 | Py_SIZE(self) = clearto; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static int |
| 431 | Pdata_grow(Pdata *self) |
| 432 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 433 | PyObject **data = self->data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 434 | size_t allocated = (size_t)self->allocated; |
| 435 | size_t new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 436 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 437 | new_allocated = (allocated >> 3) + 6; |
| 438 | /* check for integer overflow */ |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 439 | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 440 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 441 | new_allocated += allocated; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 442 | PyMem_RESIZE(data, PyObject *, new_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 443 | if (data == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 444 | goto nomemory; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 445 | |
| 446 | self->data = data; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 447 | self->allocated = (Py_ssize_t)new_allocated; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 448 | return 0; |
| 449 | |
| 450 | nomemory: |
| 451 | PyErr_NoMemory(); |
| 452 | return -1; |
| 453 | } |
| 454 | |
| 455 | /* D is a Pdata*. Pop the topmost element and store it into V, which |
| 456 | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
| 457 | * is raised and V is set to NULL. |
| 458 | */ |
| 459 | static PyObject * |
| 460 | Pdata_pop(Pdata *self) |
| 461 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 462 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 463 | if (Py_SIZE(self) == 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 464 | PyErr_SetString(st->UnpicklingError, "bad pickle data"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 465 | return NULL; |
| 466 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 467 | return self->data[--Py_SIZE(self)]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 468 | } |
| 469 | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
| 470 | |
| 471 | static int |
| 472 | Pdata_push(Pdata *self, PyObject *obj) |
| 473 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 474 | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 475 | return -1; |
| 476 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 477 | self->data[Py_SIZE(self)++] = obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 478 | return 0; |
| 479 | } |
| 480 | |
| 481 | /* Push an object on stack, transferring its ownership to the stack. */ |
| 482 | #define PDATA_PUSH(D, O, ER) do { \ |
| 483 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 484 | |
| 485 | /* Push an object on stack, adding a new reference to the object. */ |
| 486 | #define PDATA_APPEND(D, O, ER) do { \ |
| 487 | Py_INCREF((O)); \ |
| 488 | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
| 489 | |
| 490 | static PyObject * |
| 491 | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
| 492 | { |
| 493 | PyObject *tuple; |
| 494 | Py_ssize_t len, i, j; |
| 495 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 496 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 497 | tuple = PyTuple_New(len); |
| 498 | if (tuple == NULL) |
| 499 | return NULL; |
| 500 | for (i = start, j = 0; j < len; i++, j++) |
| 501 | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
| 502 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 503 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 504 | return tuple; |
| 505 | } |
| 506 | |
| 507 | static PyObject * |
| 508 | Pdata_poplist(Pdata *self, Py_ssize_t start) |
| 509 | { |
| 510 | PyObject *list; |
| 511 | Py_ssize_t len, i, j; |
| 512 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 513 | len = Py_SIZE(self) - start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 514 | list = PyList_New(len); |
| 515 | if (list == NULL) |
| 516 | return NULL; |
| 517 | for (i = start, j = 0; j < len; i++, j++) |
| 518 | PyList_SET_ITEM(list, j, self->data[i]); |
| 519 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 520 | Py_SIZE(self) = start; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 521 | return list; |
| 522 | } |
| 523 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 524 | typedef struct { |
| 525 | PyObject *me_key; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 526 | Py_ssize_t me_value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 527 | } PyMemoEntry; |
| 528 | |
| 529 | typedef struct { |
| 530 | Py_ssize_t mt_mask; |
| 531 | Py_ssize_t mt_used; |
| 532 | Py_ssize_t mt_allocated; |
| 533 | PyMemoEntry *mt_table; |
| 534 | } PyMemoTable; |
| 535 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 536 | typedef struct PicklerObject { |
| 537 | PyObject_HEAD |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 538 | PyMemoTable *memo; /* Memo table, keep track of the seen |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 539 | objects to support self-referential objects |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 540 | pickling. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 541 | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 542 | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 543 | |
| 544 | PyObject *write; /* write() method of the output stream. */ |
| 545 | PyObject *output_buffer; /* Write into a local bytearray buffer before |
| 546 | flushing to the stream. */ |
| 547 | Py_ssize_t output_len; /* Length of output_buffer. */ |
| 548 | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 549 | int proto; /* Pickle protocol number, >= 0 */ |
| 550 | int bin; /* Boolean, true if proto > 0 */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 551 | int framing; /* True when framing is enabled, proto >= 4 */ |
| 552 | Py_ssize_t frame_start; /* Position in output_buffer where the |
| 553 | where the current frame begins. -1 if there |
| 554 | is no frame currently open. */ |
| 555 | |
| 556 | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 557 | int fast; /* Enable fast mode if set to a true value. |
| 558 | The fast mode disable the usage of memo, |
| 559 | therefore speeding the pickling process by |
| 560 | not generating superfluous PUT opcodes. It |
| 561 | should not be used if with self-referential |
| 562 | objects. */ |
| 563 | int fast_nesting; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 564 | int fix_imports; /* Indicate whether Pickler should fix |
| 565 | the name of globals for Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 566 | PyObject *fast_memo; |
| 567 | } PicklerObject; |
| 568 | |
| 569 | typedef struct UnpicklerObject { |
| 570 | PyObject_HEAD |
| 571 | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 572 | |
| 573 | /* The unpickler memo is just an array of PyObject *s. Using a dict |
| 574 | is unnecessary, since the keys are contiguous ints. */ |
| 575 | PyObject **memo; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 576 | Py_ssize_t memo_size; /* Capacity of the memo array */ |
| 577 | Py_ssize_t memo_len; /* Number of objects in the memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 578 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 579 | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 580 | |
| 581 | Py_buffer buffer; |
| 582 | char *input_buffer; |
| 583 | char *input_line; |
| 584 | Py_ssize_t input_len; |
| 585 | Py_ssize_t next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 586 | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 587 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 588 | PyObject *read; /* read() method of the input stream. */ |
| 589 | PyObject *readline; /* readline() method of the input stream. */ |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 590 | PyObject *peek; /* peek() method of the input stream, or NULL */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 591 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 592 | char *encoding; /* Name of the encoding to be used for |
| 593 | decoding strings pickled using Python |
| 594 | 2.x. The default value is "ASCII" */ |
| 595 | char *errors; /* Name of errors handling scheme to used when |
| 596 | decoding strings. The default value is |
| 597 | "strict". */ |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 598 | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 599 | objects. */ |
| 600 | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
| 601 | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 602 | int proto; /* Protocol of the pickle loaded. */ |
| 603 | int fix_imports; /* Indicate whether Unpickler should fix |
| 604 | the name of globals pickled by Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 605 | } UnpicklerObject; |
| 606 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 607 | typedef struct { |
| 608 | PyObject_HEAD |
| 609 | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
| 610 | } PicklerMemoProxyObject; |
| 611 | |
| 612 | typedef struct { |
| 613 | PyObject_HEAD |
| 614 | UnpicklerObject *unpickler; |
| 615 | } UnpicklerMemoProxyObject; |
| 616 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 617 | /* Forward declarations */ |
| 618 | static int save(PicklerObject *, PyObject *, int); |
| 619 | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
| 620 | static PyTypeObject Pickler_Type; |
| 621 | static PyTypeObject Unpickler_Type; |
| 622 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 623 | #include "clinic/_pickle.c.h" |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 624 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 625 | /************************************************************************* |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 626 | A custom hashtable mapping void* to Python ints. This is used by the pickler |
| 627 | 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] | 628 | a bunch of unnecessary object creation. This makes a huge performance |
| 629 | difference. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 631 | #define MT_MINSIZE 8 |
| 632 | #define PERTURB_SHIFT 5 |
| 633 | |
| 634 | |
| 635 | static PyMemoTable * |
| 636 | PyMemoTable_New(void) |
| 637 | { |
| 638 | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
| 639 | if (memo == NULL) { |
| 640 | PyErr_NoMemory(); |
| 641 | return NULL; |
| 642 | } |
| 643 | |
| 644 | memo->mt_used = 0; |
| 645 | memo->mt_allocated = MT_MINSIZE; |
| 646 | memo->mt_mask = MT_MINSIZE - 1; |
| 647 | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
| 648 | if (memo->mt_table == NULL) { |
| 649 | PyMem_FREE(memo); |
| 650 | PyErr_NoMemory(); |
| 651 | return NULL; |
| 652 | } |
| 653 | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
| 654 | |
| 655 | return memo; |
| 656 | } |
| 657 | |
| 658 | static PyMemoTable * |
| 659 | PyMemoTable_Copy(PyMemoTable *self) |
| 660 | { |
| 661 | Py_ssize_t i; |
| 662 | PyMemoTable *new = PyMemoTable_New(); |
| 663 | if (new == NULL) |
| 664 | return NULL; |
| 665 | |
| 666 | new->mt_used = self->mt_used; |
| 667 | new->mt_allocated = self->mt_allocated; |
| 668 | new->mt_mask = self->mt_mask; |
| 669 | /* The table we get from _New() is probably smaller than we wanted. |
| 670 | Free it and allocate one that's the right size. */ |
| 671 | PyMem_FREE(new->mt_table); |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 672 | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 673 | if (new->mt_table == NULL) { |
| 674 | PyMem_FREE(new); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 675 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 676 | return NULL; |
| 677 | } |
| 678 | for (i = 0; i < self->mt_allocated; i++) { |
| 679 | Py_XINCREF(self->mt_table[i].me_key); |
| 680 | } |
| 681 | memcpy(new->mt_table, self->mt_table, |
| 682 | sizeof(PyMemoEntry) * self->mt_allocated); |
| 683 | |
| 684 | return new; |
| 685 | } |
| 686 | |
| 687 | static Py_ssize_t |
| 688 | PyMemoTable_Size(PyMemoTable *self) |
| 689 | { |
| 690 | return self->mt_used; |
| 691 | } |
| 692 | |
| 693 | static int |
| 694 | PyMemoTable_Clear(PyMemoTable *self) |
| 695 | { |
| 696 | Py_ssize_t i = self->mt_allocated; |
| 697 | |
| 698 | while (--i >= 0) { |
| 699 | Py_XDECREF(self->mt_table[i].me_key); |
| 700 | } |
| 701 | self->mt_used = 0; |
| 702 | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
| 703 | return 0; |
| 704 | } |
| 705 | |
| 706 | static void |
| 707 | PyMemoTable_Del(PyMemoTable *self) |
| 708 | { |
| 709 | if (self == NULL) |
| 710 | return; |
| 711 | PyMemoTable_Clear(self); |
| 712 | |
| 713 | PyMem_FREE(self->mt_table); |
| 714 | PyMem_FREE(self); |
| 715 | } |
| 716 | |
| 717 | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
| 718 | can be considerably simpler than dictobject.c's lookdict(). */ |
| 719 | static PyMemoEntry * |
| 720 | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
| 721 | { |
| 722 | size_t i; |
| 723 | size_t perturb; |
| 724 | size_t mask = (size_t)self->mt_mask; |
| 725 | PyMemoEntry *table = self->mt_table; |
| 726 | PyMemoEntry *entry; |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 727 | Py_hash_t hash = (Py_hash_t)key >> 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 728 | |
| 729 | i = hash & mask; |
| 730 | entry = &table[i]; |
| 731 | if (entry->me_key == NULL || entry->me_key == key) |
| 732 | return entry; |
| 733 | |
| 734 | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
| 735 | i = (i << 2) + i + perturb + 1; |
| 736 | entry = &table[i & mask]; |
| 737 | if (entry->me_key == NULL || entry->me_key == key) |
| 738 | return entry; |
| 739 | } |
| 740 | assert(0); /* Never reached */ |
| 741 | return NULL; |
| 742 | } |
| 743 | |
| 744 | /* Returns -1 on failure, 0 on success. */ |
| 745 | static int |
| 746 | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
| 747 | { |
| 748 | PyMemoEntry *oldtable = NULL; |
| 749 | PyMemoEntry *oldentry, *newentry; |
| 750 | Py_ssize_t new_size = MT_MINSIZE; |
| 751 | Py_ssize_t to_process; |
| 752 | |
| 753 | assert(min_size > 0); |
| 754 | |
| 755 | /* Find the smallest valid table size >= min_size. */ |
| 756 | while (new_size < min_size && new_size > 0) |
| 757 | new_size <<= 1; |
| 758 | if (new_size <= 0) { |
| 759 | PyErr_NoMemory(); |
| 760 | return -1; |
| 761 | } |
| 762 | /* new_size needs to be a power of two. */ |
| 763 | assert((new_size & (new_size - 1)) == 0); |
| 764 | |
| 765 | /* Allocate new table. */ |
| 766 | oldtable = self->mt_table; |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 767 | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 768 | if (self->mt_table == NULL) { |
Victor Stinner | 8ca72e2 | 2013-07-12 00:53:26 +0200 | [diff] [blame] | 769 | self->mt_table = oldtable; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 770 | PyErr_NoMemory(); |
| 771 | return -1; |
| 772 | } |
| 773 | self->mt_allocated = new_size; |
| 774 | self->mt_mask = new_size - 1; |
| 775 | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
| 776 | |
| 777 | /* Copy entries from the old table. */ |
| 778 | to_process = self->mt_used; |
| 779 | for (oldentry = oldtable; to_process > 0; oldentry++) { |
| 780 | if (oldentry->me_key != NULL) { |
| 781 | to_process--; |
| 782 | /* newentry is a pointer to a chunk of the new |
| 783 | mt_table, so we're setting the key:value pair |
| 784 | in-place. */ |
| 785 | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
| 786 | newentry->me_key = oldentry->me_key; |
| 787 | newentry->me_value = oldentry->me_value; |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | /* Deallocate the old table. */ |
| 792 | PyMem_FREE(oldtable); |
| 793 | return 0; |
| 794 | } |
| 795 | |
| 796 | /* Returns NULL on failure, a pointer to the value otherwise. */ |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 797 | static Py_ssize_t * |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 798 | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
| 799 | { |
| 800 | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
| 801 | if (entry->me_key == NULL) |
| 802 | return NULL; |
| 803 | return &entry->me_value; |
| 804 | } |
| 805 | |
| 806 | /* Returns -1 on failure, 0 on success. */ |
| 807 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 808 | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 809 | { |
| 810 | PyMemoEntry *entry; |
| 811 | |
| 812 | assert(key != NULL); |
| 813 | |
| 814 | entry = _PyMemoTable_Lookup(self, key); |
| 815 | if (entry->me_key != NULL) { |
| 816 | entry->me_value = value; |
| 817 | return 0; |
| 818 | } |
| 819 | Py_INCREF(key); |
| 820 | entry->me_key = key; |
| 821 | entry->me_value = value; |
| 822 | self->mt_used++; |
| 823 | |
| 824 | /* If we added a key, we can safely resize. Otherwise just return! |
| 825 | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
| 826 | * |
| 827 | * Quadrupling the size improves average table sparseness |
| 828 | * (reducing collisions) at the cost of some memory. It also halves |
| 829 | * the number of expensive resize operations in a growing memo table. |
| 830 | * |
| 831 | * Very large memo tables (over 50K items) use doubling instead. |
| 832 | * This may help applications with severe memory constraints. |
| 833 | */ |
| 834 | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
| 835 | return 0; |
| 836 | return _PyMemoTable_ResizeTable(self, |
| 837 | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
| 838 | } |
| 839 | |
| 840 | #undef MT_MINSIZE |
| 841 | #undef PERTURB_SHIFT |
| 842 | |
| 843 | /*************************************************************************/ |
| 844 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 845 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 846 | static int |
| 847 | _Pickler_ClearBuffer(PicklerObject *self) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 848 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 849 | Py_CLEAR(self->output_buffer); |
| 850 | self->output_buffer = |
| 851 | PyBytes_FromStringAndSize(NULL, self->max_output_len); |
| 852 | if (self->output_buffer == NULL) |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 853 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 854 | self->output_len = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 855 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 856 | return 0; |
| 857 | } |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 858 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 859 | static void |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 860 | _write_size64(char *out, size_t value) |
| 861 | { |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 862 | size_t i; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 863 | |
| 864 | assert(sizeof(size_t) <= 8); |
| 865 | |
| 866 | for (i = 0; i < sizeof(size_t); i++) { |
| 867 | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
| 868 | } |
| 869 | for (i = sizeof(size_t); i < 8; i++) { |
| 870 | out[i] = 0; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 871 | } |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | static void |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 875 | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
| 876 | { |
Antoine Pitrou | 8f2ee6e | 2013-11-23 21:05:08 +0100 | [diff] [blame] | 877 | qdata[0] = FRAME; |
| 878 | _write_size64(qdata + 1, frame_len); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | static int |
| 882 | _Pickler_CommitFrame(PicklerObject *self) |
| 883 | { |
| 884 | size_t frame_len; |
| 885 | char *qdata; |
| 886 | |
| 887 | if (!self->framing || self->frame_start == -1) |
| 888 | return 0; |
| 889 | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
| 890 | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
| 891 | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
| 892 | self->frame_start = -1; |
| 893 | return 0; |
| 894 | } |
| 895 | |
| 896 | static int |
| 897 | _Pickler_OpcodeBoundary(PicklerObject *self) |
| 898 | { |
| 899 | Py_ssize_t frame_len; |
| 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 | if (frame_len >= FRAME_SIZE_TARGET) |
| 905 | return _Pickler_CommitFrame(self); |
| 906 | else |
| 907 | return 0; |
| 908 | } |
| 909 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 910 | static PyObject * |
| 911 | _Pickler_GetString(PicklerObject *self) |
| 912 | { |
| 913 | PyObject *output_buffer = self->output_buffer; |
| 914 | |
| 915 | assert(self->output_buffer != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 916 | |
| 917 | if (_Pickler_CommitFrame(self)) |
| 918 | return NULL; |
| 919 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 920 | self->output_buffer = NULL; |
| 921 | /* Resize down to exact size */ |
| 922 | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
| 923 | return NULL; |
| 924 | return output_buffer; |
| 925 | } |
| 926 | |
| 927 | static int |
| 928 | _Pickler_FlushToFile(PicklerObject *self) |
| 929 | { |
| 930 | PyObject *output, *result; |
| 931 | |
| 932 | assert(self->write != NULL); |
| 933 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 934 | /* This will commit the frame first */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 935 | output = _Pickler_GetString(self); |
| 936 | if (output == NULL) |
| 937 | return -1; |
| 938 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 939 | result = _Pickle_FastCall(self->write, output); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 940 | Py_XDECREF(result); |
| 941 | return (result == NULL) ? -1 : 0; |
| 942 | } |
| 943 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 944 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 945 | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 946 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 947 | Py_ssize_t i, n, required; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 948 | char *buffer; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 949 | int need_new_frame; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 950 | |
| 951 | assert(s != NULL); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 952 | need_new_frame = (self->framing && self->frame_start == -1); |
| 953 | |
| 954 | if (need_new_frame) |
| 955 | n = data_len + FRAME_HEADER_SIZE; |
| 956 | else |
| 957 | n = data_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 958 | |
| 959 | required = self->output_len + n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 960 | if (required > self->max_output_len) { |
| 961 | /* Make place in buffer for the pickle chunk */ |
| 962 | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
| 963 | PyErr_NoMemory(); |
| 964 | return -1; |
| 965 | } |
| 966 | self->max_output_len = (self->output_len + n) / 2 * 3; |
| 967 | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
| 968 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 969 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 970 | buffer = PyBytes_AS_STRING(self->output_buffer); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 971 | if (need_new_frame) { |
| 972 | /* Setup new frame */ |
| 973 | Py_ssize_t frame_start = self->output_len; |
| 974 | self->frame_start = frame_start; |
| 975 | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
| 976 | /* Write an invalid value, for debugging */ |
| 977 | buffer[frame_start + i] = 0xFE; |
| 978 | } |
| 979 | self->output_len += FRAME_HEADER_SIZE; |
| 980 | } |
| 981 | if (data_len < 8) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 982 | /* This is faster than memcpy when the string is short. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 983 | for (i = 0; i < data_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 984 | buffer[self->output_len + i] = s[i]; |
| 985 | } |
| 986 | } |
| 987 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 988 | memcpy(buffer + self->output_len, s, data_len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 989 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 990 | self->output_len += data_len; |
| 991 | return data_len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 992 | } |
| 993 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 994 | static PicklerObject * |
| 995 | _Pickler_New(void) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 996 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 997 | PicklerObject *self; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 998 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 999 | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
| 1000 | if (self == NULL) |
| 1001 | return NULL; |
| 1002 | |
| 1003 | self->pers_func = NULL; |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 1004 | self->dispatch_table = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1005 | self->write = NULL; |
| 1006 | self->proto = 0; |
| 1007 | self->bin = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1008 | self->framing = 0; |
| 1009 | self->frame_start = -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1010 | self->fast = 0; |
| 1011 | self->fast_nesting = 0; |
| 1012 | self->fix_imports = 0; |
| 1013 | self->fast_memo = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1014 | self->max_output_len = WRITE_BUF_SIZE; |
| 1015 | self->output_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1016 | |
| 1017 | self->memo = PyMemoTable_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1018 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 1019 | self->max_output_len); |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1020 | |
| 1021 | if (self->memo == NULL || self->output_buffer == NULL) { |
Victor Stinner | c31df04 | 2013-07-12 00:08:59 +0200 | [diff] [blame] | 1022 | Py_DECREF(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1023 | return NULL; |
| 1024 | } |
| 1025 | return self; |
| 1026 | } |
| 1027 | |
| 1028 | static int |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1029 | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1030 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1031 | long proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1032 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1033 | if (protocol == NULL || protocol == Py_None) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1034 | proto = DEFAULT_PROTOCOL; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1035 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1036 | else { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1037 | proto = PyLong_AsLong(protocol); |
| 1038 | if (proto < 0) { |
| 1039 | if (proto == -1 && PyErr_Occurred()) |
| 1040 | return -1; |
| 1041 | proto = HIGHEST_PROTOCOL; |
| 1042 | } |
| 1043 | else if (proto > HIGHEST_PROTOCOL) { |
| 1044 | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
| 1045 | HIGHEST_PROTOCOL); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1046 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1047 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1048 | } |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 1049 | self->proto = (int)proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1050 | self->bin = proto > 0; |
| 1051 | self->fix_imports = fix_imports && proto < 3; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1052 | return 0; |
| 1053 | } |
| 1054 | |
| 1055 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1056 | be called once on a freshly created Pickler. */ |
| 1057 | static int |
| 1058 | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
| 1059 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1060 | _Py_IDENTIFIER(write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1061 | assert(file != NULL); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1062 | self->write = _PyObject_GetAttrId(file, &PyId_write); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1063 | if (self->write == NULL) { |
| 1064 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1065 | PyErr_SetString(PyExc_TypeError, |
| 1066 | "file must have a 'write' attribute"); |
| 1067 | return -1; |
| 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | } |
| 1072 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1073 | /* Returns the size of the input on success, -1 on failure. This takes its |
| 1074 | own reference to `input`. */ |
| 1075 | static Py_ssize_t |
| 1076 | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
| 1077 | { |
| 1078 | if (self->buffer.buf != NULL) |
| 1079 | PyBuffer_Release(&self->buffer); |
| 1080 | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
| 1081 | return -1; |
| 1082 | self->input_buffer = self->buffer.buf; |
| 1083 | self->input_len = self->buffer.len; |
| 1084 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1085 | self->prefetched_idx = self->input_len; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1086 | return self->input_len; |
| 1087 | } |
| 1088 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1089 | static int |
| 1090 | _Unpickler_SkipConsumed(UnpicklerObject *self) |
| 1091 | { |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1092 | Py_ssize_t consumed; |
| 1093 | PyObject *r; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1094 | |
Victor Stinner | b43ad1d | 2013-10-31 13:38:42 +0100 | [diff] [blame] | 1095 | consumed = self->next_read_idx - self->prefetched_idx; |
| 1096 | if (consumed <= 0) |
| 1097 | return 0; |
| 1098 | |
| 1099 | assert(self->peek); /* otherwise we did something wrong */ |
| 1100 | /* This makes an useless copy... */ |
| 1101 | r = PyObject_CallFunction(self->read, "n", consumed); |
| 1102 | if (r == NULL) |
| 1103 | return -1; |
| 1104 | Py_DECREF(r); |
| 1105 | |
| 1106 | self->prefetched_idx = self->next_read_idx; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1107 | return 0; |
| 1108 | } |
| 1109 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1110 | static const Py_ssize_t READ_WHOLE_LINE = -1; |
| 1111 | |
| 1112 | /* If reading from a file, we need to only pull the bytes we need, since there |
| 1113 | may be multiple pickle objects arranged contiguously in the same input |
| 1114 | buffer. |
| 1115 | |
| 1116 | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
| 1117 | bytes from the input stream/buffer. |
| 1118 | |
| 1119 | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
| 1120 | failure; on success, returns the number of bytes read from the file. |
| 1121 | |
| 1122 | On success, self->input_len will be 0; this is intentional so that when |
| 1123 | unpickling from a file, the "we've run out of data" code paths will trigger, |
| 1124 | causing the Unpickler to go back to the file for more data. Use the returned |
| 1125 | size to tell you how much data you can process. */ |
| 1126 | static Py_ssize_t |
| 1127 | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
| 1128 | { |
| 1129 | PyObject *data; |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1130 | Py_ssize_t read_size; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1131 | |
| 1132 | assert(self->read != NULL); |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1133 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1134 | if (_Unpickler_SkipConsumed(self) < 0) |
| 1135 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1136 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1137 | if (n == READ_WHOLE_LINE) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 1138 | PyObject *empty_tuple = PyTuple_New(0); |
| 1139 | data = PyObject_Call(self->readline, empty_tuple, NULL); |
| 1140 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1141 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1142 | else { |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1143 | PyObject *len; |
| 1144 | /* Prefetch some data without advancing the file pointer, if possible */ |
| 1145 | if (self->peek && n < PREFETCH) { |
| 1146 | len = PyLong_FromSsize_t(PREFETCH); |
| 1147 | if (len == NULL) |
| 1148 | return -1; |
| 1149 | data = _Pickle_FastCall(self->peek, len); |
| 1150 | if (data == NULL) { |
| 1151 | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
| 1152 | return -1; |
| 1153 | /* peek() is probably not supported by the given file object */ |
| 1154 | PyErr_Clear(); |
| 1155 | Py_CLEAR(self->peek); |
| 1156 | } |
| 1157 | else { |
| 1158 | read_size = _Unpickler_SetStringInput(self, data); |
| 1159 | Py_DECREF(data); |
| 1160 | self->prefetched_idx = 0; |
| 1161 | if (n <= read_size) |
| 1162 | return n; |
| 1163 | } |
| 1164 | } |
| 1165 | len = PyLong_FromSsize_t(n); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1166 | if (len == NULL) |
| 1167 | return -1; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 1168 | data = _Pickle_FastCall(self->read, len); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1169 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1170 | if (data == NULL) |
| 1171 | return -1; |
| 1172 | |
Serhiy Storchaka | 6fe39b7 | 2013-11-30 23:15:38 +0200 | [diff] [blame] | 1173 | read_size = _Unpickler_SetStringInput(self, data); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1174 | Py_DECREF(data); |
| 1175 | return read_size; |
| 1176 | } |
| 1177 | |
| 1178 | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
| 1179 | |
| 1180 | This should be used for all data reads, rather than accessing the unpickler's |
| 1181 | input buffer directly. This method deals correctly with reading from input |
| 1182 | streams, which the input buffer doesn't deal with. |
| 1183 | |
| 1184 | Note that when reading from a file-like object, self->next_read_idx won't |
| 1185 | be updated (it should remain at 0 for the entire unpickling process). You |
| 1186 | should use this function's return value to know how many bytes you can |
| 1187 | consume. |
| 1188 | |
| 1189 | Returns -1 (with an exception set) on failure. On success, return the |
| 1190 | number of chars read. */ |
| 1191 | static Py_ssize_t |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 1192 | _Unpickler_Read(UnpicklerObject *self, char **s, Py_ssize_t n) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1193 | { |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1194 | Py_ssize_t num_read; |
| 1195 | |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1196 | if (self->next_read_idx + n <= self->input_len) { |
| 1197 | *s = self->input_buffer + self->next_read_idx; |
| 1198 | self->next_read_idx += n; |
| 1199 | return n; |
| 1200 | } |
| 1201 | if (!self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1202 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
Amaury Forgeot d'Arc | 3e4e72f | 2008-11-11 20:05:06 +0000 | [diff] [blame] | 1203 | return -1; |
| 1204 | } |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1205 | num_read = _Unpickler_ReadFromFile(self, n); |
| 1206 | if (num_read < 0) |
| 1207 | return -1; |
| 1208 | if (num_read < n) { |
| 1209 | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
| 1210 | return -1; |
| 1211 | } |
| 1212 | *s = self->input_buffer; |
| 1213 | self->next_read_idx = n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1214 | return n; |
| 1215 | } |
| 1216 | |
| 1217 | static Py_ssize_t |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1218 | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
| 1219 | char **result) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1220 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1221 | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1222 | if (input_line == NULL) { |
| 1223 | PyErr_NoMemory(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1224 | return -1; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1225 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1226 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1227 | memcpy(input_line, line, len); |
| 1228 | input_line[len] = '\0'; |
| 1229 | self->input_line = input_line; |
| 1230 | *result = self->input_line; |
| 1231 | return len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1234 | /* Read a line from the input stream/buffer. If we run off the end of the input |
| 1235 | before hitting \n, return the data we found. |
| 1236 | |
| 1237 | Returns the number of chars read, or -1 on failure. */ |
| 1238 | static Py_ssize_t |
| 1239 | _Unpickler_Readline(UnpicklerObject *self, char **result) |
| 1240 | { |
| 1241 | Py_ssize_t i, num_read; |
| 1242 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1243 | for (i = self->next_read_idx; i < self->input_len; i++) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1244 | if (self->input_buffer[i] == '\n') { |
| 1245 | char *line_start = self->input_buffer + self->next_read_idx; |
| 1246 | num_read = i - self->next_read_idx + 1; |
| 1247 | self->next_read_idx = i + 1; |
| 1248 | return _Unpickler_CopyLine(self, line_start, num_read, result); |
| 1249 | } |
| 1250 | } |
| 1251 | if (self->read) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1252 | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
| 1253 | if (num_read < 0) |
| 1254 | return -1; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1255 | self->next_read_idx = num_read; |
Antoine Pitrou | f6c7a85 | 2011-08-11 21:04:02 +0200 | [diff] [blame] | 1256 | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1257 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1258 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1259 | /* If we get here, we've run off the end of the input string. Return the |
| 1260 | remaining string and let the caller figure it out. */ |
| 1261 | *result = self->input_buffer + self->next_read_idx; |
| 1262 | num_read = i - self->next_read_idx; |
| 1263 | self->next_read_idx = i; |
| 1264 | return num_read; |
| 1265 | } |
| 1266 | |
| 1267 | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
| 1268 | will be modified in place. */ |
| 1269 | static int |
| 1270 | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
| 1271 | { |
| 1272 | Py_ssize_t i; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1273 | |
| 1274 | assert(new_size > self->memo_size); |
| 1275 | |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1276 | PyMem_RESIZE(self->memo, PyObject *, new_size); |
| 1277 | if (self->memo == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1278 | PyErr_NoMemory(); |
| 1279 | return -1; |
| 1280 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1281 | for (i = self->memo_size; i < new_size; i++) |
| 1282 | self->memo[i] = NULL; |
| 1283 | self->memo_size = new_size; |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* Returns NULL if idx is out of bounds. */ |
| 1288 | static PyObject * |
| 1289 | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
| 1290 | { |
| 1291 | if (idx < 0 || idx >= self->memo_size) |
| 1292 | return NULL; |
| 1293 | |
| 1294 | return self->memo[idx]; |
| 1295 | } |
| 1296 | |
| 1297 | /* Returns -1 (with an exception set) on failure, 0 on success. |
| 1298 | This takes its own reference to `value`. */ |
| 1299 | static int |
| 1300 | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
| 1301 | { |
| 1302 | PyObject *old_item; |
| 1303 | |
| 1304 | if (idx >= self->memo_size) { |
| 1305 | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
| 1306 | return -1; |
| 1307 | assert(idx < self->memo_size); |
| 1308 | } |
| 1309 | Py_INCREF(value); |
| 1310 | old_item = self->memo[idx]; |
| 1311 | self->memo[idx] = value; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1312 | if (old_item != NULL) { |
| 1313 | Py_DECREF(old_item); |
| 1314 | } |
| 1315 | else { |
| 1316 | self->memo_len++; |
| 1317 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1318 | return 0; |
| 1319 | } |
| 1320 | |
| 1321 | static PyObject ** |
| 1322 | _Unpickler_NewMemo(Py_ssize_t new_size) |
| 1323 | { |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 1324 | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1325 | if (memo == NULL) { |
| 1326 | PyErr_NoMemory(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1327 | return NULL; |
Victor Stinner | 4202456 | 2013-07-12 00:53:57 +0200 | [diff] [blame] | 1328 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1329 | memset(memo, 0, new_size * sizeof(PyObject *)); |
| 1330 | return memo; |
| 1331 | } |
| 1332 | |
| 1333 | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
| 1334 | static void |
| 1335 | _Unpickler_MemoCleanup(UnpicklerObject *self) |
| 1336 | { |
| 1337 | Py_ssize_t i; |
| 1338 | PyObject **memo = self->memo; |
| 1339 | |
| 1340 | if (self->memo == NULL) |
| 1341 | return; |
| 1342 | self->memo = NULL; |
| 1343 | i = self->memo_size; |
| 1344 | while (--i >= 0) { |
| 1345 | Py_XDECREF(memo[i]); |
| 1346 | } |
| 1347 | PyMem_FREE(memo); |
| 1348 | } |
| 1349 | |
| 1350 | static UnpicklerObject * |
| 1351 | _Unpickler_New(void) |
| 1352 | { |
| 1353 | UnpicklerObject *self; |
| 1354 | |
| 1355 | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
| 1356 | if (self == NULL) |
| 1357 | return NULL; |
| 1358 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1359 | self->pers_func = NULL; |
| 1360 | self->input_buffer = NULL; |
| 1361 | self->input_line = NULL; |
| 1362 | self->input_len = 0; |
| 1363 | self->next_read_idx = 0; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1364 | self->prefetched_idx = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1365 | self->read = NULL; |
| 1366 | self->readline = NULL; |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1367 | self->peek = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1368 | self->encoding = NULL; |
| 1369 | self->errors = NULL; |
| 1370 | self->marks = NULL; |
| 1371 | self->num_marks = 0; |
| 1372 | self->marks_size = 0; |
| 1373 | self->proto = 0; |
| 1374 | self->fix_imports = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1375 | memset(&self->buffer, 0, sizeof(Py_buffer)); |
| 1376 | self->memo_size = 32; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1377 | self->memo_len = 0; |
Victor Stinner | 68c8ea2 | 2013-07-11 22:56:25 +0200 | [diff] [blame] | 1378 | self->memo = _Unpickler_NewMemo(self->memo_size); |
| 1379 | self->stack = (Pdata *)Pdata_New(); |
| 1380 | |
| 1381 | if (self->memo == NULL || self->stack == NULL) { |
| 1382 | Py_DECREF(self); |
| 1383 | return NULL; |
| 1384 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1385 | |
| 1386 | return self; |
| 1387 | } |
| 1388 | |
| 1389 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1390 | be called once on a freshly created Pickler. */ |
| 1391 | static int |
| 1392 | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
| 1393 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 1394 | _Py_IDENTIFIER(peek); |
| 1395 | _Py_IDENTIFIER(read); |
| 1396 | _Py_IDENTIFIER(readline); |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1397 | |
| 1398 | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1399 | if (self->peek == NULL) { |
| 1400 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1401 | PyErr_Clear(); |
| 1402 | else |
| 1403 | return -1; |
| 1404 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 1405 | self->read = _PyObject_GetAttrId(file, &PyId_read); |
| 1406 | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1407 | if (self->readline == NULL || self->read == NULL) { |
| 1408 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1409 | PyErr_SetString(PyExc_TypeError, |
| 1410 | "file must have 'read' and 'readline' attributes"); |
| 1411 | Py_CLEAR(self->read); |
| 1412 | Py_CLEAR(self->readline); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 1413 | Py_CLEAR(self->peek); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1414 | return -1; |
| 1415 | } |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
| 1420 | be called once on a freshly created Pickler. */ |
| 1421 | static int |
| 1422 | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
| 1423 | const char *encoding, |
| 1424 | const char *errors) |
| 1425 | { |
| 1426 | if (encoding == NULL) |
| 1427 | encoding = "ASCII"; |
| 1428 | if (errors == NULL) |
| 1429 | errors = "strict"; |
| 1430 | |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 1431 | self->encoding = _PyMem_Strdup(encoding); |
| 1432 | self->errors = _PyMem_Strdup(errors); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1433 | if (self->encoding == NULL || self->errors == NULL) { |
| 1434 | PyErr_NoMemory(); |
| 1435 | return -1; |
| 1436 | } |
| 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | /* Generate a GET opcode for an object stored in the memo. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1441 | static int |
| 1442 | memo_get(PicklerObject *self, PyObject *key) |
| 1443 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1444 | Py_ssize_t *value; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1445 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1446 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1447 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1448 | value = PyMemoTable_Get(self->memo, key); |
| 1449 | if (value == NULL) { |
| 1450 | PyErr_SetObject(PyExc_KeyError, key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1451 | return -1; |
| 1452 | } |
| 1453 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1454 | if (!self->bin) { |
| 1455 | pdata[0] = GET; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1456 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
| 1457 | "%" PY_FORMAT_SIZE_T "d\n", *value); |
| 1458 | len = strlen(pdata); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1459 | } |
| 1460 | else { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1461 | if (*value < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1462 | pdata[0] = BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1463 | pdata[1] = (unsigned char)(*value & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1464 | len = 2; |
| 1465 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1466 | else if ((size_t)*value <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1467 | pdata[0] = LONG_BINGET; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1468 | pdata[1] = (unsigned char)(*value & 0xff); |
| 1469 | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
| 1470 | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
| 1471 | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1472 | len = 5; |
| 1473 | } |
| 1474 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1475 | PickleState *st = _Pickle_GetGlobalState(); |
| 1476 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1477 | "memo id too large for LONG_BINGET"); |
| 1478 | return -1; |
| 1479 | } |
| 1480 | } |
| 1481 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1482 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1483 | return -1; |
| 1484 | |
| 1485 | return 0; |
| 1486 | } |
| 1487 | |
| 1488 | /* Store an object in the memo, assign it a new unique ID based on the number |
| 1489 | of objects currently stored in the memo and generate a PUT opcode. */ |
| 1490 | static int |
| 1491 | memo_put(PicklerObject *self, PyObject *obj) |
| 1492 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1493 | char pdata[30]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1494 | Py_ssize_t len; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1495 | Py_ssize_t idx; |
| 1496 | |
| 1497 | const char memoize_op = MEMOIZE; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1498 | |
| 1499 | if (self->fast) |
| 1500 | return 0; |
| 1501 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1502 | idx = PyMemoTable_Size(self->memo); |
| 1503 | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
| 1504 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1505 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1506 | if (self->proto >= 4) { |
| 1507 | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
| 1508 | return -1; |
| 1509 | return 0; |
| 1510 | } |
| 1511 | else if (!self->bin) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1512 | pdata[0] = PUT; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1513 | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1514 | "%" PY_FORMAT_SIZE_T "d\n", idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1515 | len = strlen(pdata); |
| 1516 | } |
| 1517 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1518 | if (idx < 256) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1519 | pdata[0] = BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1520 | pdata[1] = (unsigned char)idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1521 | len = 2; |
| 1522 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 1523 | else if ((size_t)idx <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1524 | pdata[0] = LONG_BINPUT; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1525 | pdata[1] = (unsigned char)(idx & 0xff); |
| 1526 | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
| 1527 | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
| 1528 | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1529 | len = 5; |
| 1530 | } |
| 1531 | else { /* unlikely */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 1532 | PickleState *st = _Pickle_GetGlobalState(); |
| 1533 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1534 | "memo id too large for LONG_BINPUT"); |
| 1535 | return -1; |
| 1536 | } |
| 1537 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1538 | if (_Pickler_Write(self, pdata, len) < 0) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1539 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1540 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1541 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
| 1544 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1545 | get_dotted_path(PyObject *obj, PyObject *name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1546 | _Py_static_string(PyId_dot, "."); |
| 1547 | _Py_static_string(PyId_locals, "<locals>"); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1548 | PyObject *dotted_path; |
| 1549 | Py_ssize_t i, n; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1550 | |
| 1551 | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1552 | if (dotted_path == NULL) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1553 | return NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1554 | n = PyList_GET_SIZE(dotted_path); |
| 1555 | assert(n >= 1); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1556 | for (i = 0; i < n; i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1557 | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1558 | PyObject *result = PyUnicode_RichCompare( |
| 1559 | subpath, _PyUnicode_FromId(&PyId_locals), Py_EQ); |
| 1560 | int is_equal = (result == Py_True); |
| 1561 | assert(PyBool_Check(result)); |
| 1562 | Py_DECREF(result); |
| 1563 | if (is_equal) { |
Antoine Pitrou | 6cd5eda | 2014-12-02 00:20:03 +0100 | [diff] [blame] | 1564 | if (obj == NULL) |
| 1565 | PyErr_Format(PyExc_AttributeError, |
| 1566 | "Can't pickle local object %R", name); |
| 1567 | else |
| 1568 | PyErr_Format(PyExc_AttributeError, |
| 1569 | "Can't pickle local attribute %R on %R", name, obj); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1570 | Py_DECREF(dotted_path); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1571 | return NULL; |
| 1572 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1573 | } |
| 1574 | return dotted_path; |
| 1575 | } |
| 1576 | |
| 1577 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1578 | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1579 | { |
| 1580 | Py_ssize_t i, n; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1581 | PyObject *parent = NULL; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1582 | |
| 1583 | assert(PyList_CheckExact(names)); |
| 1584 | Py_INCREF(obj); |
| 1585 | n = PyList_GET_SIZE(names); |
| 1586 | for (i = 0; i < n; i++) { |
| 1587 | PyObject *name = PyList_GET_ITEM(names, i); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1588 | Py_XDECREF(parent); |
| 1589 | parent = obj; |
| 1590 | obj = PyObject_GetAttr(parent, name); |
| 1591 | if (obj == NULL) { |
| 1592 | Py_DECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1593 | return NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1594 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1595 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1596 | if (pparent != NULL) |
| 1597 | *pparent = parent; |
| 1598 | else |
| 1599 | Py_XDECREF(parent); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1600 | return obj; |
| 1601 | } |
| 1602 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1603 | static void |
| 1604 | reformat_attribute_error(PyObject *obj, PyObject *name) |
| 1605 | { |
| 1606 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1607 | PyErr_Clear(); |
| 1608 | PyErr_Format(PyExc_AttributeError, |
| 1609 | "Can't get attribute %R on %R", name, obj); |
| 1610 | } |
| 1611 | } |
| 1612 | |
| 1613 | |
| 1614 | static PyObject * |
| 1615 | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
| 1616 | { |
| 1617 | PyObject *dotted_path, *attr; |
| 1618 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1619 | if (allow_qualname) { |
| 1620 | dotted_path = get_dotted_path(obj, name); |
| 1621 | if (dotted_path == NULL) |
| 1622 | return NULL; |
| 1623 | attr = get_deep_attribute(obj, dotted_path, NULL); |
| 1624 | Py_DECREF(dotted_path); |
| 1625 | } |
| 1626 | else |
| 1627 | attr = PyObject_GetAttr(obj, name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1628 | if (attr == NULL) |
| 1629 | reformat_attribute_error(obj, name); |
| 1630 | return attr; |
| 1631 | } |
| 1632 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1633 | static PyObject * |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1634 | whichmodule(PyObject *global, PyObject *dotted_path) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1635 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1636 | PyObject *module_name; |
| 1637 | PyObject *modules_dict; |
| 1638 | PyObject *module; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1639 | Py_ssize_t i; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1640 | _Py_IDENTIFIER(__module__); |
| 1641 | _Py_IDENTIFIER(modules); |
| 1642 | _Py_IDENTIFIER(__main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1643 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1644 | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
| 1645 | |
| 1646 | if (module_name == NULL) { |
| 1647 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1648 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1649 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1650 | } |
| 1651 | else { |
| 1652 | /* In some rare cases (e.g., bound methods of extension types), |
| 1653 | __module__ can be None. If it is so, then search sys.modules for |
| 1654 | the module of global. */ |
| 1655 | if (module_name != Py_None) |
| 1656 | return module_name; |
| 1657 | Py_CLEAR(module_name); |
| 1658 | } |
| 1659 | assert(module_name == NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1660 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1661 | /* Fallback on walking sys.modules */ |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 1662 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1663 | if (modules_dict == NULL) { |
| 1664 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1665 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 1666 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1667 | |
| 1668 | i = 0; |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1669 | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
| 1670 | PyObject *candidate; |
| 1671 | if (PyUnicode_Check(module_name) && |
| 1672 | !PyUnicode_CompareWithASCIIString(module_name, "__main__")) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1673 | continue; |
| 1674 | if (module == Py_None) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1675 | continue; |
| 1676 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1677 | candidate = get_deep_attribute(module, dotted_path, NULL); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1678 | if (candidate == NULL) { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 1679 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1680 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1681 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1682 | continue; |
| 1683 | } |
| 1684 | |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1685 | if (candidate == global) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1686 | Py_INCREF(module_name); |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1687 | Py_DECREF(candidate); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1688 | return module_name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1689 | } |
Antoine Pitrou | fce60ea | 2014-10-23 22:47:50 +0200 | [diff] [blame] | 1690 | Py_DECREF(candidate); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1691 | } |
| 1692 | |
| 1693 | /* If no module is found, use __main__. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 1694 | module_name = _PyUnicode_FromId(&PyId___main__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1695 | Py_INCREF(module_name); |
| 1696 | return module_name; |
| 1697 | } |
| 1698 | |
| 1699 | /* fast_save_enter() and fast_save_leave() are guards against recursive |
| 1700 | objects when Pickler is used with the "fast mode" (i.e., with object |
| 1701 | memoization disabled). If the nesting of a list or dict object exceed |
| 1702 | FAST_NESTING_LIMIT, these guards will start keeping an internal |
| 1703 | reference to the seen list or dict objects and check whether these objects |
| 1704 | are recursive. These are not strictly necessary, since save() has a |
| 1705 | hard-coded recursion limit, but they give a nicer error message than the |
| 1706 | typical RuntimeError. */ |
| 1707 | static int |
| 1708 | fast_save_enter(PicklerObject *self, PyObject *obj) |
| 1709 | { |
| 1710 | /* if fast_nesting < 0, we're doing an error exit. */ |
| 1711 | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
| 1712 | PyObject *key = NULL; |
| 1713 | if (self->fast_memo == NULL) { |
| 1714 | self->fast_memo = PyDict_New(); |
| 1715 | if (self->fast_memo == NULL) { |
| 1716 | self->fast_nesting = -1; |
| 1717 | return 0; |
| 1718 | } |
| 1719 | } |
| 1720 | key = PyLong_FromVoidPtr(obj); |
| 1721 | if (key == NULL) |
| 1722 | return 0; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1723 | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1724 | Py_DECREF(key); |
| 1725 | PyErr_Format(PyExc_ValueError, |
| 1726 | "fast mode: can't pickle cyclic objects " |
| 1727 | "including object type %.200s at %p", |
| 1728 | obj->ob_type->tp_name, obj); |
| 1729 | self->fast_nesting = -1; |
| 1730 | return 0; |
| 1731 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 1732 | if (PyErr_Occurred()) { |
| 1733 | return 0; |
| 1734 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1735 | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
| 1736 | Py_DECREF(key); |
| 1737 | self->fast_nesting = -1; |
| 1738 | return 0; |
| 1739 | } |
| 1740 | Py_DECREF(key); |
| 1741 | } |
| 1742 | return 1; |
| 1743 | } |
| 1744 | |
| 1745 | static int |
| 1746 | fast_save_leave(PicklerObject *self, PyObject *obj) |
| 1747 | { |
| 1748 | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
| 1749 | PyObject *key = PyLong_FromVoidPtr(obj); |
| 1750 | if (key == NULL) |
| 1751 | return 0; |
| 1752 | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
| 1753 | Py_DECREF(key); |
| 1754 | return 0; |
| 1755 | } |
| 1756 | Py_DECREF(key); |
| 1757 | } |
| 1758 | return 1; |
| 1759 | } |
| 1760 | |
| 1761 | static int |
| 1762 | save_none(PicklerObject *self, PyObject *obj) |
| 1763 | { |
| 1764 | const char none_op = NONE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1765 | if (_Pickler_Write(self, &none_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1766 | return -1; |
| 1767 | |
| 1768 | return 0; |
| 1769 | } |
| 1770 | |
| 1771 | static int |
| 1772 | save_bool(PicklerObject *self, PyObject *obj) |
| 1773 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1774 | if (self->proto >= 2) { |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1775 | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1776 | if (_Pickler_Write(self, &bool_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1777 | return -1; |
| 1778 | } |
Alexandre Vassalotti | 8a67f52 | 2013-11-24 21:40:18 -0800 | [diff] [blame] | 1779 | else { |
| 1780 | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
| 1781 | * so that unpicklers written before bools were introduced unpickle them |
| 1782 | * as ints, but unpicklers after can recognize that bools were intended. |
| 1783 | * Note that protocol 2 added direct ways to pickle bools. |
| 1784 | */ |
| 1785 | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
| 1786 | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
| 1787 | return -1; |
| 1788 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1789 | return 0; |
| 1790 | } |
| 1791 | |
| 1792 | static int |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1793 | save_long(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1794 | { |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1795 | PyObject *repr = NULL; |
| 1796 | Py_ssize_t size; |
| 1797 | long val; |
| 1798 | int status = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1799 | |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1800 | const char long_op = LONG; |
| 1801 | |
| 1802 | val= PyLong_AsLong(obj); |
| 1803 | if (val == -1 && PyErr_Occurred()) { |
| 1804 | /* out of range for int pickling */ |
| 1805 | PyErr_Clear(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1806 | } |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1807 | else if (self->bin && |
| 1808 | (sizeof(long) <= 4 || |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1809 | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 1810 | /* result fits in a signed 4-byte integer. |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 1811 | |
| 1812 | Note: we can't use -0x80000000L in the above condition because some |
| 1813 | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
| 1814 | before applying the unary minus when sizeof(long) <= 4. The |
| 1815 | resulting value stays unsigned which is commonly not what we want, |
| 1816 | so MSVC happily warns us about it. However, that result would have |
| 1817 | been fine because we guard for sizeof(long) <= 4 which turns the |
| 1818 | condition true in that particular case. */ |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1819 | char pdata[32]; |
| 1820 | Py_ssize_t len = 0; |
| 1821 | |
| 1822 | pdata[1] = (unsigned char)(val & 0xff); |
| 1823 | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
| 1824 | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
| 1825 | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1826 | |
| 1827 | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
| 1828 | if (pdata[2] == 0) { |
| 1829 | pdata[0] = BININT1; |
| 1830 | len = 2; |
| 1831 | } |
| 1832 | else { |
| 1833 | pdata[0] = BININT2; |
| 1834 | len = 3; |
| 1835 | } |
| 1836 | } |
| 1837 | else { |
| 1838 | pdata[0] = BININT; |
| 1839 | len = 5; |
| 1840 | } |
| 1841 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1842 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1843 | return -1; |
Alexandre Vassalotti | ded929b | 2013-11-24 22:41:13 -0800 | [diff] [blame] | 1844 | |
| 1845 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1846 | } |
| 1847 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1848 | if (self->proto >= 2) { |
| 1849 | /* Linear-time pickling. */ |
| 1850 | size_t nbits; |
| 1851 | size_t nbytes; |
| 1852 | unsigned char *pdata; |
| 1853 | char header[5]; |
| 1854 | int i; |
| 1855 | int sign = _PyLong_Sign(obj); |
| 1856 | |
| 1857 | if (sign == 0) { |
| 1858 | header[0] = LONG1; |
| 1859 | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1860 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1861 | goto error; |
| 1862 | return 0; |
| 1863 | } |
| 1864 | nbits = _PyLong_NumBits(obj); |
| 1865 | if (nbits == (size_t)-1 && PyErr_Occurred()) |
| 1866 | goto error; |
| 1867 | /* How many bytes do we need? There are nbits >> 3 full |
| 1868 | * bytes of data, and nbits & 7 leftover bits. If there |
| 1869 | * are any leftover bits, then we clearly need another |
| 1870 | * byte. Wnat's not so obvious is that we *probably* |
| 1871 | * need another byte even if there aren't any leftovers: |
| 1872 | * the most-significant bit of the most-significant byte |
| 1873 | * acts like a sign bit, and it's usually got a sense |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1874 | * opposite of the one we need. The exception is ints |
| 1875 | * 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] | 1876 | * its own 256's-complement, so has the right sign bit |
| 1877 | * even without the extra byte. That's a pain to check |
| 1878 | * for in advance, though, so we always grab an extra |
| 1879 | * byte at the start, and cut it back later if possible. |
| 1880 | */ |
| 1881 | nbytes = (nbits >> 3) + 1; |
Antoine Pitrou | bf6ecf9 | 2012-11-24 20:40:21 +0100 | [diff] [blame] | 1882 | if (nbytes > 0x7fffffffL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1883 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1884 | "int too large to pickle"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1885 | goto error; |
| 1886 | } |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1887 | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1888 | if (repr == NULL) |
| 1889 | goto error; |
Neal Norwitz | 6ae2eb2 | 2008-08-24 23:50:08 +0000 | [diff] [blame] | 1890 | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1891 | i = _PyLong_AsByteArray((PyLongObject *)obj, |
| 1892 | pdata, nbytes, |
| 1893 | 1 /* little endian */ , 1 /* signed */ ); |
| 1894 | if (i < 0) |
| 1895 | goto error; |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 1896 | /* If the int is negative, this may be a byte more than |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1897 | * needed. This is so iff the MSB is all redundant sign |
| 1898 | * bits. |
| 1899 | */ |
| 1900 | if (sign < 0 && |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1901 | nbytes > 1 && |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1902 | pdata[nbytes - 1] == 0xff && |
| 1903 | (pdata[nbytes - 2] & 0x80) != 0) { |
| 1904 | nbytes--; |
| 1905 | } |
| 1906 | |
| 1907 | if (nbytes < 256) { |
| 1908 | header[0] = LONG1; |
| 1909 | header[1] = (unsigned char)nbytes; |
| 1910 | size = 2; |
| 1911 | } |
| 1912 | else { |
| 1913 | header[0] = LONG4; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 1914 | size = (Py_ssize_t) nbytes; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1915 | for (i = 1; i < 5; i++) { |
| 1916 | header[i] = (unsigned char)(size & 0xff); |
| 1917 | size >>= 8; |
| 1918 | } |
| 1919 | size = 5; |
| 1920 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1921 | if (_Pickler_Write(self, header, size) < 0 || |
| 1922 | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1923 | goto error; |
| 1924 | } |
| 1925 | else { |
| 1926 | char *string; |
| 1927 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 1928 | /* proto < 2: write the repr and newline. This is quadratic-time (in |
| 1929 | the number of digits), in both directions. We add a trailing 'L' |
| 1930 | to the repr, for compatibility with Python 2.x. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1931 | |
| 1932 | repr = PyObject_Repr(obj); |
| 1933 | if (repr == NULL) |
| 1934 | goto error; |
| 1935 | |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 1936 | string = _PyUnicode_AsStringAndSize(repr, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1937 | if (string == NULL) |
| 1938 | goto error; |
| 1939 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1940 | if (_Pickler_Write(self, &long_op, 1) < 0 || |
| 1941 | _Pickler_Write(self, string, size) < 0 || |
| 1942 | _Pickler_Write(self, "L\n", 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1943 | goto error; |
| 1944 | } |
| 1945 | |
| 1946 | if (0) { |
| 1947 | error: |
| 1948 | status = -1; |
| 1949 | } |
| 1950 | Py_XDECREF(repr); |
| 1951 | |
| 1952 | return status; |
| 1953 | } |
| 1954 | |
| 1955 | static int |
| 1956 | save_float(PicklerObject *self, PyObject *obj) |
| 1957 | { |
| 1958 | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
| 1959 | |
| 1960 | if (self->bin) { |
| 1961 | char pdata[9]; |
| 1962 | pdata[0] = BINFLOAT; |
| 1963 | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
| 1964 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1965 | if (_Pickler_Write(self, pdata, 9) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1966 | return -1; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 1967 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1968 | else { |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1969 | int result = -1; |
| 1970 | char *buf = NULL; |
| 1971 | char op = FLOAT; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1972 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1973 | if (_Pickler_Write(self, &op, 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1974 | goto done; |
| 1975 | |
Serhiy Storchaka | c86ca26 | 2015-02-15 14:18:32 +0200 | [diff] [blame] | 1976 | 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] | 1977 | if (!buf) { |
| 1978 | PyErr_NoMemory(); |
| 1979 | goto done; |
| 1980 | } |
| 1981 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1982 | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1983 | goto done; |
| 1984 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 1985 | if (_Pickler_Write(self, "\n", 1) < 0) |
Eric Smith | 0923d1d | 2009-04-16 20:16:10 +0000 | [diff] [blame] | 1986 | goto done; |
| 1987 | |
| 1988 | result = 0; |
| 1989 | done: |
| 1990 | PyMem_Free(buf); |
| 1991 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 1992 | } |
| 1993 | |
| 1994 | return 0; |
| 1995 | } |
| 1996 | |
| 1997 | static int |
| 1998 | save_bytes(PicklerObject *self, PyObject *obj) |
| 1999 | { |
| 2000 | if (self->proto < 3) { |
| 2001 | /* Older pickle protocols do not have an opcode for pickling bytes |
| 2002 | objects. Therefore, we need to fake the copy protocol (i.e., |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2003 | the __reduce__ method) to permit bytes object unpickling. |
| 2004 | |
| 2005 | Here we use a hack to be compatible with Python 2. Since in Python |
| 2006 | 2 'bytes' is just an alias for 'str' (which has different |
| 2007 | parameters than the actual bytes object), we use codecs.encode |
| 2008 | to create the appropriate 'str' object when unpickled using |
| 2009 | Python 2 *and* the appropriate 'bytes' object when unpickled |
| 2010 | using Python 3. Again this is a hack and we don't need to do this |
| 2011 | with newer protocols. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2012 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2013 | int status; |
| 2014 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2015 | if (PyBytes_GET_SIZE(obj) == 0) { |
| 2016 | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
| 2017 | } |
| 2018 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2019 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2020 | PyObject *unicode_str = |
| 2021 | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
| 2022 | PyBytes_GET_SIZE(obj), |
| 2023 | "strict"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2024 | _Py_IDENTIFIER(latin1); |
| 2025 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2026 | if (unicode_str == NULL) |
| 2027 | return -1; |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2028 | reduce_value = Py_BuildValue("(O(OO))", |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 2029 | st->codecs_encode, unicode_str, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2030 | _PyUnicode_FromId(&PyId_latin1)); |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2031 | Py_DECREF(unicode_str); |
| 2032 | } |
| 2033 | |
| 2034 | if (reduce_value == NULL) |
| 2035 | return -1; |
| 2036 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2037 | /* save_reduce() will memoize the object automatically. */ |
| 2038 | status = save_reduce(self, reduce_value, obj); |
| 2039 | Py_DECREF(reduce_value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2040 | return status; |
| 2041 | } |
| 2042 | else { |
| 2043 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2044 | char header[9]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2045 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2046 | |
Alexandre Vassalotti | 3bfc65a | 2011-12-13 13:08:09 -0500 | [diff] [blame] | 2047 | size = PyBytes_GET_SIZE(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2048 | if (size < 0) |
| 2049 | return -1; |
| 2050 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2051 | if (size <= 0xff) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2052 | header[0] = SHORT_BINBYTES; |
| 2053 | header[1] = (unsigned char)size; |
| 2054 | len = 2; |
| 2055 | } |
Serhiy Storchaka | 67c719b | 2014-09-05 10:10:23 +0300 | [diff] [blame] | 2056 | else if ((size_t)size <= 0xffffffffUL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2057 | header[0] = BINBYTES; |
| 2058 | header[1] = (unsigned char)(size & 0xff); |
| 2059 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2060 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2061 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2062 | len = 5; |
| 2063 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2064 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2065 | header[0] = BINBYTES8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2066 | _write_size64(header + 1, size); |
Alexandre Vassalotti | 6e73ff1 | 2013-12-05 19:29:32 -0800 | [diff] [blame] | 2067 | len = 9; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2068 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2069 | else { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2070 | PyErr_SetString(PyExc_OverflowError, |
Serhiy Storchaka | f8def28 | 2013-02-16 17:29:56 +0200 | [diff] [blame] | 2071 | "cannot serialize a bytes object larger than 4 GiB"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2072 | return -1; /* string too large */ |
| 2073 | } |
| 2074 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2075 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2076 | return -1; |
| 2077 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2078 | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2079 | return -1; |
| 2080 | |
| 2081 | if (memo_put(self, obj) < 0) |
| 2082 | return -1; |
| 2083 | |
| 2084 | return 0; |
| 2085 | } |
| 2086 | } |
| 2087 | |
| 2088 | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
| 2089 | backslash and newline characters to \uXXXX escapes. */ |
| 2090 | static PyObject * |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2091 | raw_unicode_escape(PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2092 | { |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2093 | PyObject *repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2094 | char *p; |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2095 | Py_ssize_t i, size; |
| 2096 | size_t expandsize; |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2097 | void *data; |
| 2098 | unsigned int kind; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2099 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2100 | if (PyUnicode_READY(obj)) |
| 2101 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2102 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2103 | size = PyUnicode_GET_LENGTH(obj); |
| 2104 | data = PyUnicode_DATA(obj); |
| 2105 | kind = PyUnicode_KIND(obj); |
| 2106 | if (kind == PyUnicode_4BYTE_KIND) |
| 2107 | expandsize = 10; |
| 2108 | else |
| 2109 | expandsize = 6; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 2110 | |
Victor Stinner | 049e509 | 2014-08-17 22:20:00 +0200 | [diff] [blame] | 2111 | if ((size_t)size > (size_t)PY_SSIZE_T_MAX / expandsize) |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2112 | return PyErr_NoMemory(); |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2113 | repr = PyBytes_FromStringAndSize(NULL, expandsize * size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2114 | if (repr == NULL) |
| 2115 | return NULL; |
| 2116 | if (size == 0) |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2117 | return repr; |
| 2118 | assert(Py_REFCNT(repr) == 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2119 | |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2120 | p = PyBytes_AS_STRING(repr); |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2121 | for (i=0; i < size; i++) { |
| 2122 | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2123 | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
| 2124 | if (ch >= 0x10000) { |
| 2125 | *p++ = '\\'; |
| 2126 | *p++ = 'U'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2127 | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
| 2128 | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
| 2129 | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
| 2130 | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
| 2131 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2132 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2133 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2134 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2135 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2136 | /* Map 16-bit characters to '\uxxxx' */ |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2137 | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2138 | *p++ = '\\'; |
| 2139 | *p++ = 'u'; |
Victor Stinner | f5cff56 | 2011-10-14 02:13:11 +0200 | [diff] [blame] | 2140 | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
| 2141 | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
| 2142 | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
| 2143 | *p++ = Py_hexdigits[ch & 15]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2144 | } |
Alexandre Vassalotti | 554d878 | 2008-12-27 07:32:41 +0000 | [diff] [blame] | 2145 | /* Copy everything else as-is */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2146 | else |
| 2147 | *p++ = (char) ch; |
| 2148 | } |
Victor Stinner | 7270b7f | 2014-08-17 21:14:46 +0200 | [diff] [blame] | 2149 | size = p - PyBytes_AS_STRING(repr); |
| 2150 | if (_PyBytes_Resize(&repr, size) < 0) |
| 2151 | return NULL; |
| 2152 | return repr; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2153 | } |
| 2154 | |
| 2155 | static int |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2156 | write_utf8(PicklerObject *self, char *data, Py_ssize_t size) |
| 2157 | { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2158 | char header[9]; |
| 2159 | Py_ssize_t len; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2160 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2161 | assert(size >= 0); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2162 | if (size <= 0xff && self->proto >= 4) { |
| 2163 | header[0] = SHORT_BINUNICODE; |
| 2164 | header[1] = (unsigned char)(size & 0xff); |
| 2165 | len = 2; |
| 2166 | } |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 2167 | else if ((size_t)size <= 0xffffffffUL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2168 | header[0] = BINUNICODE; |
| 2169 | header[1] = (unsigned char)(size & 0xff); |
| 2170 | header[2] = (unsigned char)((size >> 8) & 0xff); |
| 2171 | header[3] = (unsigned char)((size >> 16) & 0xff); |
| 2172 | header[4] = (unsigned char)((size >> 24) & 0xff); |
| 2173 | len = 5; |
| 2174 | } |
| 2175 | else if (self->proto >= 4) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2176 | header[0] = BINUNICODE8; |
Alexandre Vassalotti | 1048fb5 | 2013-11-25 11:35:46 -0800 | [diff] [blame] | 2177 | _write_size64(header + 1, size); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2178 | len = 9; |
| 2179 | } |
| 2180 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2181 | PyErr_SetString(PyExc_OverflowError, |
Antoine Pitrou | 4b7b0f0 | 2013-04-07 23:46:52 +0200 | [diff] [blame] | 2182 | "cannot serialize a string larger than 4GiB"); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2183 | return -1; |
| 2184 | } |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2185 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2186 | if (_Pickler_Write(self, header, len) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2187 | return -1; |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2188 | if (_Pickler_Write(self, data, size) < 0) |
| 2189 | return -1; |
| 2190 | |
| 2191 | return 0; |
| 2192 | } |
| 2193 | |
| 2194 | static int |
| 2195 | write_unicode_binary(PicklerObject *self, PyObject *obj) |
| 2196 | { |
| 2197 | PyObject *encoded = NULL; |
| 2198 | Py_ssize_t size; |
| 2199 | char *data; |
| 2200 | int r; |
| 2201 | |
| 2202 | if (PyUnicode_READY(obj)) |
| 2203 | return -1; |
| 2204 | |
| 2205 | data = PyUnicode_AsUTF8AndSize(obj, &size); |
| 2206 | if (data != NULL) |
| 2207 | return write_utf8(self, data, size); |
| 2208 | |
| 2209 | /* Issue #8383: for strings with lone surrogates, fallback on the |
| 2210 | "surrogatepass" error handler. */ |
| 2211 | PyErr_Clear(); |
| 2212 | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
| 2213 | if (encoded == NULL) |
| 2214 | return -1; |
| 2215 | |
| 2216 | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
| 2217 | PyBytes_GET_SIZE(encoded)); |
| 2218 | Py_DECREF(encoded); |
| 2219 | return r; |
| 2220 | } |
| 2221 | |
| 2222 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2223 | save_unicode(PicklerObject *self, PyObject *obj) |
| 2224 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2225 | if (self->bin) { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2226 | if (write_unicode_binary(self, obj) < 0) |
| 2227 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2228 | } |
| 2229 | else { |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2230 | PyObject *encoded; |
| 2231 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2232 | const char unicode_op = UNICODE; |
| 2233 | |
Victor Stinner | c806fdc | 2011-09-29 23:50:23 +0200 | [diff] [blame] | 2234 | encoded = raw_unicode_escape(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2235 | if (encoded == NULL) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2236 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2237 | |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2238 | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
| 2239 | Py_DECREF(encoded); |
| 2240 | return -1; |
| 2241 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2242 | |
| 2243 | size = PyBytes_GET_SIZE(encoded); |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2244 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
| 2245 | Py_DECREF(encoded); |
| 2246 | return -1; |
| 2247 | } |
| 2248 | Py_DECREF(encoded); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2250 | if (_Pickler_Write(self, "\n", 1) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2251 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2252 | } |
| 2253 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | 299978d | 2013-04-07 17:38:11 +0200 | [diff] [blame] | 2254 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2255 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2256 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
| 2260 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2261 | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2262 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2263 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2264 | |
| 2265 | assert(PyTuple_Size(t) == len); |
| 2266 | |
| 2267 | for (i = 0; i < len; i++) { |
| 2268 | PyObject *element = PyTuple_GET_ITEM(t, i); |
| 2269 | |
| 2270 | if (element == NULL) |
| 2271 | return -1; |
| 2272 | if (save(self, element, 0) < 0) |
| 2273 | return -1; |
| 2274 | } |
| 2275 | |
| 2276 | return 0; |
| 2277 | } |
| 2278 | |
| 2279 | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
| 2280 | * used across protocols to minimize the space needed to pickle them. |
| 2281 | * Tuples are also the only builtin immutable type that can be recursive |
| 2282 | * (a tuple can be reached from itself), and that requires some subtle |
| 2283 | * magic so that it works in all cases. IOW, this is a long routine. |
| 2284 | */ |
| 2285 | static int |
| 2286 | save_tuple(PicklerObject *self, PyObject *obj) |
| 2287 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2288 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2289 | |
| 2290 | const char mark_op = MARK; |
| 2291 | const char tuple_op = TUPLE; |
| 2292 | const char pop_op = POP; |
| 2293 | const char pop_mark_op = POP_MARK; |
| 2294 | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
| 2295 | |
| 2296 | if ((len = PyTuple_Size(obj)) < 0) |
| 2297 | return -1; |
| 2298 | |
| 2299 | if (len == 0) { |
| 2300 | char pdata[2]; |
| 2301 | |
| 2302 | if (self->proto) { |
| 2303 | pdata[0] = EMPTY_TUPLE; |
| 2304 | len = 1; |
| 2305 | } |
| 2306 | else { |
| 2307 | pdata[0] = MARK; |
| 2308 | pdata[1] = TUPLE; |
| 2309 | len = 2; |
| 2310 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2311 | if (_Pickler_Write(self, pdata, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2312 | return -1; |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2316 | /* 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] | 2317 | * saving the tuple elements, the tuple must be recursive, in |
| 2318 | * which case we'll pop everything we put on the stack, and fetch |
| 2319 | * its value from the memo. |
| 2320 | */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2321 | if (len <= 3 && self->proto >= 2) { |
| 2322 | /* Use TUPLE{1,2,3} opcodes. */ |
| 2323 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2324 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2325 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2326 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2327 | /* pop the len elements */ |
| 2328 | for (i = 0; i < len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2329 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2330 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2331 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2332 | if (memo_get(self, obj) < 0) |
| 2333 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2334 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2335 | return 0; |
| 2336 | } |
| 2337 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2338 | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
| 2339 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2340 | } |
| 2341 | goto memoize; |
| 2342 | } |
| 2343 | |
| 2344 | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
| 2345 | * Generate MARK e1 e2 ... TUPLE |
| 2346 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2347 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2348 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2349 | |
| 2350 | if (store_tuple_elements(self, obj, len) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2351 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2352 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2353 | if (PyMemoTable_Get(self->memo, obj)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2354 | /* pop the stack stuff we pushed */ |
| 2355 | if (self->bin) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2356 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 2357 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2358 | } |
| 2359 | else { |
| 2360 | /* Note that we pop one more than len, to remove |
| 2361 | * the MARK too. |
| 2362 | */ |
| 2363 | for (i = 0; i <= len; i++) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2364 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 2365 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2366 | } |
| 2367 | /* fetch from memo */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2368 | if (memo_get(self, obj) < 0) |
| 2369 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2370 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2371 | return 0; |
| 2372 | } |
| 2373 | else { /* Not recursive. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2374 | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
| 2375 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2376 | } |
| 2377 | |
| 2378 | memoize: |
| 2379 | if (memo_put(self, obj) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2380 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2381 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2382 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2383 | } |
| 2384 | |
| 2385 | /* iter is an iterator giving items, and we batch up chunks of |
| 2386 | * MARK item item ... item APPENDS |
| 2387 | * opcode sequences. Calling code should have arranged to first create an |
| 2388 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2389 | * Returns 0 on success, <0 on error. |
| 2390 | */ |
| 2391 | static int |
| 2392 | batch_list(PicklerObject *self, PyObject *iter) |
| 2393 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2394 | PyObject *obj = NULL; |
| 2395 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2396 | int i, n; |
| 2397 | |
| 2398 | const char mark_op = MARK; |
| 2399 | const char append_op = APPEND; |
| 2400 | const char appends_op = APPENDS; |
| 2401 | |
| 2402 | assert(iter != NULL); |
| 2403 | |
| 2404 | /* XXX: I think this function could be made faster by avoiding the |
| 2405 | iterator interface and fetching objects directly from list using |
| 2406 | PyList_GET_ITEM. |
| 2407 | */ |
| 2408 | |
| 2409 | if (self->proto == 0) { |
| 2410 | /* APPENDS isn't available; do one at a time. */ |
| 2411 | for (;;) { |
| 2412 | obj = PyIter_Next(iter); |
| 2413 | if (obj == NULL) { |
| 2414 | if (PyErr_Occurred()) |
| 2415 | return -1; |
| 2416 | break; |
| 2417 | } |
| 2418 | i = save(self, obj, 0); |
| 2419 | Py_DECREF(obj); |
| 2420 | if (i < 0) |
| 2421 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2422 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2423 | return -1; |
| 2424 | } |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
| 2428 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2429 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2430 | /* Get first item */ |
| 2431 | firstitem = PyIter_Next(iter); |
| 2432 | if (firstitem == NULL) { |
| 2433 | if (PyErr_Occurred()) |
| 2434 | goto error; |
| 2435 | |
| 2436 | /* nothing more to add */ |
| 2437 | break; |
| 2438 | } |
| 2439 | |
| 2440 | /* Try to get a second item */ |
| 2441 | obj = PyIter_Next(iter); |
| 2442 | if (obj == NULL) { |
| 2443 | if (PyErr_Occurred()) |
| 2444 | goto error; |
| 2445 | |
| 2446 | /* Only one item to write */ |
| 2447 | if (save(self, firstitem, 0) < 0) |
| 2448 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2449 | if (_Pickler_Write(self, &append_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2450 | goto error; |
| 2451 | Py_CLEAR(firstitem); |
| 2452 | break; |
| 2453 | } |
| 2454 | |
| 2455 | /* More than one item to write */ |
| 2456 | |
| 2457 | /* Pump out MARK, items, APPENDS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2458 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2459 | goto error; |
| 2460 | |
| 2461 | if (save(self, firstitem, 0) < 0) |
| 2462 | goto error; |
| 2463 | Py_CLEAR(firstitem); |
| 2464 | n = 1; |
| 2465 | |
| 2466 | /* Fetch and save up to BATCHSIZE items */ |
| 2467 | while (obj) { |
| 2468 | if (save(self, obj, 0) < 0) |
| 2469 | goto error; |
| 2470 | Py_CLEAR(obj); |
| 2471 | n += 1; |
| 2472 | |
| 2473 | if (n == BATCHSIZE) |
| 2474 | break; |
| 2475 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2476 | obj = PyIter_Next(iter); |
| 2477 | if (obj == NULL) { |
| 2478 | if (PyErr_Occurred()) |
| 2479 | goto error; |
| 2480 | break; |
| 2481 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2482 | } |
| 2483 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2484 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2485 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2486 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2487 | } while (n == BATCHSIZE); |
| 2488 | return 0; |
| 2489 | |
| 2490 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2491 | Py_XDECREF(firstitem); |
| 2492 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2493 | return -1; |
| 2494 | } |
| 2495 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2496 | /* This is a variant of batch_list() above, specialized for lists (with no |
| 2497 | * support for list subclasses). Like batch_list(), we batch up chunks of |
| 2498 | * MARK item item ... item APPENDS |
| 2499 | * opcode sequences. Calling code should have arranged to first create an |
| 2500 | * empty list, or list-like object, for the APPENDS to operate on. |
| 2501 | * Returns 0 on success, -1 on error. |
| 2502 | * |
| 2503 | * This version is considerably faster than batch_list(), if less general. |
| 2504 | * |
| 2505 | * Note that this only works for protocols > 0. |
| 2506 | */ |
| 2507 | static int |
| 2508 | batch_list_exact(PicklerObject *self, PyObject *obj) |
| 2509 | { |
| 2510 | PyObject *item = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2511 | Py_ssize_t this_batch, total; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2512 | |
| 2513 | const char append_op = APPEND; |
| 2514 | const char appends_op = APPENDS; |
| 2515 | const char mark_op = MARK; |
| 2516 | |
| 2517 | assert(obj != NULL); |
| 2518 | assert(self->proto > 0); |
| 2519 | assert(PyList_CheckExact(obj)); |
| 2520 | |
| 2521 | if (PyList_GET_SIZE(obj) == 1) { |
| 2522 | item = PyList_GET_ITEM(obj, 0); |
| 2523 | if (save(self, item, 0) < 0) |
| 2524 | return -1; |
| 2525 | if (_Pickler_Write(self, &append_op, 1) < 0) |
| 2526 | return -1; |
| 2527 | return 0; |
| 2528 | } |
| 2529 | |
| 2530 | /* Write in batches of BATCHSIZE. */ |
| 2531 | total = 0; |
| 2532 | do { |
| 2533 | this_batch = 0; |
| 2534 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2535 | return -1; |
| 2536 | while (total < PyList_GET_SIZE(obj)) { |
| 2537 | item = PyList_GET_ITEM(obj, total); |
| 2538 | if (save(self, item, 0) < 0) |
| 2539 | return -1; |
| 2540 | total++; |
| 2541 | if (++this_batch == BATCHSIZE) |
| 2542 | break; |
| 2543 | } |
| 2544 | if (_Pickler_Write(self, &appends_op, 1) < 0) |
| 2545 | return -1; |
| 2546 | |
| 2547 | } while (total < PyList_GET_SIZE(obj)); |
| 2548 | |
| 2549 | return 0; |
| 2550 | } |
| 2551 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2552 | static int |
| 2553 | save_list(PicklerObject *self, PyObject *obj) |
| 2554 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2555 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2556 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2557 | int status = 0; |
| 2558 | |
| 2559 | if (self->fast && !fast_save_enter(self, obj)) |
| 2560 | goto error; |
| 2561 | |
| 2562 | /* Create an empty list. */ |
| 2563 | if (self->bin) { |
| 2564 | header[0] = EMPTY_LIST; |
| 2565 | len = 1; |
| 2566 | } |
| 2567 | else { |
| 2568 | header[0] = MARK; |
| 2569 | header[1] = LIST; |
| 2570 | len = 2; |
| 2571 | } |
| 2572 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2573 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2574 | goto error; |
| 2575 | |
| 2576 | /* Get list length, and bow out early if empty. */ |
| 2577 | if ((len = PyList_Size(obj)) < 0) |
| 2578 | goto error; |
| 2579 | |
| 2580 | if (memo_put(self, obj) < 0) |
| 2581 | goto error; |
| 2582 | |
| 2583 | if (len != 0) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2584 | /* Materialize the list elements. */ |
| 2585 | if (PyList_CheckExact(obj) && self->proto > 0) { |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2586 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2587 | goto error; |
| 2588 | status = batch_list_exact(self, obj); |
| 2589 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2590 | } else { |
| 2591 | PyObject *iter = PyObject_GetIter(obj); |
| 2592 | if (iter == NULL) |
| 2593 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2594 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2595 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2596 | Py_DECREF(iter); |
| 2597 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2598 | } |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2599 | status = batch_list(self, iter); |
| 2600 | Py_LeaveRecursiveCall(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2601 | Py_DECREF(iter); |
| 2602 | } |
| 2603 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2604 | if (0) { |
| 2605 | error: |
| 2606 | status = -1; |
| 2607 | } |
| 2608 | |
| 2609 | if (self->fast && !fast_save_leave(self, obj)) |
| 2610 | status = -1; |
| 2611 | |
| 2612 | return status; |
| 2613 | } |
| 2614 | |
| 2615 | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
| 2616 | * MARK key value ... key value SETITEMS |
| 2617 | * opcode sequences. Calling code should have arranged to first create an |
| 2618 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2619 | * Returns 0 on success, <0 on error. |
| 2620 | * |
| 2621 | * This is very much like batch_list(). The difference between saving |
| 2622 | * elements directly, and picking apart two-tuples, is so long-winded at |
| 2623 | * the C level, though, that attempts to combine these routines were too |
| 2624 | * ugly to bear. |
| 2625 | */ |
| 2626 | static int |
| 2627 | batch_dict(PicklerObject *self, PyObject *iter) |
| 2628 | { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2629 | PyObject *obj = NULL; |
| 2630 | PyObject *firstitem = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2631 | int i, n; |
| 2632 | |
| 2633 | const char mark_op = MARK; |
| 2634 | const char setitem_op = SETITEM; |
| 2635 | const char setitems_op = SETITEMS; |
| 2636 | |
| 2637 | assert(iter != NULL); |
| 2638 | |
| 2639 | if (self->proto == 0) { |
| 2640 | /* SETITEMS isn't available; do one at a time. */ |
| 2641 | for (;;) { |
| 2642 | obj = PyIter_Next(iter); |
| 2643 | if (obj == NULL) { |
| 2644 | if (PyErr_Occurred()) |
| 2645 | return -1; |
| 2646 | break; |
| 2647 | } |
| 2648 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2649 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2650 | "iterator must return 2-tuples"); |
| 2651 | return -1; |
| 2652 | } |
| 2653 | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
| 2654 | if (i >= 0) |
| 2655 | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
| 2656 | Py_DECREF(obj); |
| 2657 | if (i < 0) |
| 2658 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2659 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2660 | return -1; |
| 2661 | } |
| 2662 | return 0; |
| 2663 | } |
| 2664 | |
| 2665 | /* proto > 0: write in batches of BATCHSIZE. */ |
| 2666 | do { |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2667 | /* Get first item */ |
| 2668 | firstitem = PyIter_Next(iter); |
| 2669 | if (firstitem == NULL) { |
| 2670 | if (PyErr_Occurred()) |
| 2671 | goto error; |
| 2672 | |
| 2673 | /* nothing more to add */ |
| 2674 | break; |
| 2675 | } |
| 2676 | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
| 2677 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2678 | "iterator must return 2-tuples"); |
| 2679 | goto error; |
| 2680 | } |
| 2681 | |
| 2682 | /* Try to get a second item */ |
| 2683 | obj = PyIter_Next(iter); |
| 2684 | if (obj == NULL) { |
| 2685 | if (PyErr_Occurred()) |
| 2686 | goto error; |
| 2687 | |
| 2688 | /* Only one item to write */ |
| 2689 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2690 | goto error; |
| 2691 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2692 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2693 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2694 | goto error; |
| 2695 | Py_CLEAR(firstitem); |
| 2696 | break; |
| 2697 | } |
| 2698 | |
| 2699 | /* More than one item to write */ |
| 2700 | |
| 2701 | /* Pump out MARK, items, SETITEMS. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2702 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2703 | goto error; |
| 2704 | |
| 2705 | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
| 2706 | goto error; |
| 2707 | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
| 2708 | goto error; |
| 2709 | Py_CLEAR(firstitem); |
| 2710 | n = 1; |
| 2711 | |
| 2712 | /* Fetch and save up to BATCHSIZE items */ |
| 2713 | while (obj) { |
| 2714 | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
| 2715 | PyErr_SetString(PyExc_TypeError, "dict items " |
| 2716 | "iterator must return 2-tuples"); |
| 2717 | goto error; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2718 | } |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2719 | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
| 2720 | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
| 2721 | goto error; |
| 2722 | Py_CLEAR(obj); |
| 2723 | n += 1; |
| 2724 | |
| 2725 | if (n == BATCHSIZE) |
| 2726 | break; |
| 2727 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2728 | obj = PyIter_Next(iter); |
| 2729 | if (obj == NULL) { |
| 2730 | if (PyErr_Occurred()) |
| 2731 | goto error; |
| 2732 | break; |
| 2733 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2736 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2737 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2738 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2739 | } while (n == BATCHSIZE); |
| 2740 | return 0; |
| 2741 | |
| 2742 | error: |
Amaury Forgeot d'Arc | fb1a5eb | 2008-09-11 21:03:37 +0000 | [diff] [blame] | 2743 | Py_XDECREF(firstitem); |
| 2744 | Py_XDECREF(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2745 | return -1; |
| 2746 | } |
| 2747 | |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2748 | /* This is a variant of batch_dict() above that specializes for dicts, with no |
| 2749 | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
| 2750 | * MARK key value ... key value SETITEMS |
| 2751 | * opcode sequences. Calling code should have arranged to first create an |
| 2752 | * empty dict, or dict-like object, for the SETITEMS to operate on. |
| 2753 | * Returns 0 on success, -1 on error. |
| 2754 | * |
| 2755 | * Note that this currently doesn't work for protocol 0. |
| 2756 | */ |
| 2757 | static int |
| 2758 | batch_dict_exact(PicklerObject *self, PyObject *obj) |
| 2759 | { |
| 2760 | PyObject *key = NULL, *value = NULL; |
| 2761 | int i; |
| 2762 | Py_ssize_t dict_size, ppos = 0; |
| 2763 | |
Alexandre Vassalotti | f70b129 | 2009-05-25 18:00:52 +0000 | [diff] [blame] | 2764 | const char mark_op = MARK; |
| 2765 | const char setitem_op = SETITEM; |
| 2766 | const char setitems_op = SETITEMS; |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2767 | |
| 2768 | assert(obj != NULL); |
| 2769 | assert(self->proto > 0); |
| 2770 | |
| 2771 | dict_size = PyDict_Size(obj); |
| 2772 | |
| 2773 | /* Special-case len(d) == 1 to save space. */ |
| 2774 | if (dict_size == 1) { |
| 2775 | PyDict_Next(obj, &ppos, &key, &value); |
| 2776 | if (save(self, key, 0) < 0) |
| 2777 | return -1; |
| 2778 | if (save(self, value, 0) < 0) |
| 2779 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2780 | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2781 | return -1; |
| 2782 | return 0; |
| 2783 | } |
| 2784 | |
| 2785 | /* Write in batches of BATCHSIZE. */ |
| 2786 | do { |
| 2787 | i = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2788 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2789 | return -1; |
| 2790 | while (PyDict_Next(obj, &ppos, &key, &value)) { |
| 2791 | if (save(self, key, 0) < 0) |
| 2792 | return -1; |
| 2793 | if (save(self, value, 0) < 0) |
| 2794 | return -1; |
| 2795 | if (++i == BATCHSIZE) |
| 2796 | break; |
| 2797 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2798 | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2799 | return -1; |
| 2800 | if (PyDict_Size(obj) != dict_size) { |
| 2801 | PyErr_Format( |
| 2802 | PyExc_RuntimeError, |
| 2803 | "dictionary changed size during iteration"); |
| 2804 | return -1; |
| 2805 | } |
| 2806 | |
| 2807 | } while (i == BATCHSIZE); |
| 2808 | return 0; |
| 2809 | } |
| 2810 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2811 | static int |
| 2812 | save_dict(PicklerObject *self, PyObject *obj) |
| 2813 | { |
| 2814 | PyObject *items, *iter; |
| 2815 | char header[3]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 2816 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2817 | int status = 0; |
| 2818 | |
| 2819 | if (self->fast && !fast_save_enter(self, obj)) |
| 2820 | goto error; |
| 2821 | |
| 2822 | /* Create an empty dict. */ |
| 2823 | if (self->bin) { |
| 2824 | header[0] = EMPTY_DICT; |
| 2825 | len = 1; |
| 2826 | } |
| 2827 | else { |
| 2828 | header[0] = MARK; |
| 2829 | header[1] = DICT; |
| 2830 | len = 2; |
| 2831 | } |
| 2832 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 2833 | if (_Pickler_Write(self, header, len) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2834 | goto error; |
| 2835 | |
| 2836 | /* Get dict size, and bow out early if empty. */ |
| 2837 | if ((len = PyDict_Size(obj)) < 0) |
| 2838 | goto error; |
| 2839 | |
| 2840 | if (memo_put(self, obj) < 0) |
| 2841 | goto error; |
| 2842 | |
| 2843 | if (len != 0) { |
| 2844 | /* Save the dict items. */ |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2845 | if (PyDict_CheckExact(obj) && self->proto > 0) { |
| 2846 | /* We can take certain shortcuts if we know this is a dict and |
| 2847 | not a dict subclass. */ |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2848 | if (Py_EnterRecursiveCall(" while pickling an object")) |
| 2849 | goto error; |
| 2850 | status = batch_dict_exact(self, obj); |
| 2851 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2852 | } else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 2853 | _Py_IDENTIFIER(items); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 2854 | |
| 2855 | items = _PyObject_CallMethodId(obj, &PyId_items, "()"); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2856 | if (items == NULL) |
| 2857 | goto error; |
| 2858 | iter = PyObject_GetIter(items); |
| 2859 | Py_DECREF(items); |
| 2860 | if (iter == NULL) |
| 2861 | goto error; |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2862 | if (Py_EnterRecursiveCall(" while pickling an object")) { |
| 2863 | Py_DECREF(iter); |
| 2864 | goto error; |
| 2865 | } |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2866 | status = batch_dict(self, iter); |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 2867 | Py_LeaveRecursiveCall(); |
Collin Winter | 5c9b02d | 2009-05-25 05:43:30 +0000 | [diff] [blame] | 2868 | Py_DECREF(iter); |
| 2869 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 2870 | } |
| 2871 | |
| 2872 | if (0) { |
| 2873 | error: |
| 2874 | status = -1; |
| 2875 | } |
| 2876 | |
| 2877 | if (self->fast && !fast_save_leave(self, obj)) |
| 2878 | status = -1; |
| 2879 | |
| 2880 | return status; |
| 2881 | } |
| 2882 | |
| 2883 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2884 | save_set(PicklerObject *self, PyObject *obj) |
| 2885 | { |
| 2886 | PyObject *item; |
| 2887 | int i; |
| 2888 | Py_ssize_t set_size, ppos = 0; |
| 2889 | Py_hash_t hash; |
| 2890 | |
| 2891 | const char empty_set_op = EMPTY_SET; |
| 2892 | const char mark_op = MARK; |
| 2893 | const char additems_op = ADDITEMS; |
| 2894 | |
| 2895 | if (self->proto < 4) { |
| 2896 | PyObject *items; |
| 2897 | PyObject *reduce_value; |
| 2898 | int status; |
| 2899 | |
| 2900 | items = PySequence_List(obj); |
| 2901 | if (items == NULL) { |
| 2902 | return -1; |
| 2903 | } |
| 2904 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
| 2905 | Py_DECREF(items); |
| 2906 | if (reduce_value == NULL) { |
| 2907 | return -1; |
| 2908 | } |
| 2909 | /* save_reduce() will memoize the object automatically. */ |
| 2910 | status = save_reduce(self, reduce_value, obj); |
| 2911 | Py_DECREF(reduce_value); |
| 2912 | return status; |
| 2913 | } |
| 2914 | |
| 2915 | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
| 2916 | return -1; |
| 2917 | |
| 2918 | if (memo_put(self, obj) < 0) |
| 2919 | return -1; |
| 2920 | |
| 2921 | set_size = PySet_GET_SIZE(obj); |
| 2922 | if (set_size == 0) |
| 2923 | return 0; /* nothing to do */ |
| 2924 | |
| 2925 | /* Write in batches of BATCHSIZE. */ |
| 2926 | do { |
| 2927 | i = 0; |
| 2928 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2929 | return -1; |
| 2930 | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
| 2931 | if (save(self, item, 0) < 0) |
| 2932 | return -1; |
| 2933 | if (++i == BATCHSIZE) |
| 2934 | break; |
| 2935 | } |
| 2936 | if (_Pickler_Write(self, &additems_op, 1) < 0) |
| 2937 | return -1; |
| 2938 | if (PySet_GET_SIZE(obj) != set_size) { |
| 2939 | PyErr_Format( |
| 2940 | PyExc_RuntimeError, |
| 2941 | "set changed size during iteration"); |
| 2942 | return -1; |
| 2943 | } |
| 2944 | } while (i == BATCHSIZE); |
| 2945 | |
| 2946 | return 0; |
| 2947 | } |
| 2948 | |
| 2949 | static int |
| 2950 | save_frozenset(PicklerObject *self, PyObject *obj) |
| 2951 | { |
| 2952 | PyObject *iter; |
| 2953 | |
| 2954 | const char mark_op = MARK; |
| 2955 | const char frozenset_op = FROZENSET; |
| 2956 | |
| 2957 | if (self->fast && !fast_save_enter(self, obj)) |
| 2958 | return -1; |
| 2959 | |
| 2960 | if (self->proto < 4) { |
| 2961 | PyObject *items; |
| 2962 | PyObject *reduce_value; |
| 2963 | int status; |
| 2964 | |
| 2965 | items = PySequence_List(obj); |
| 2966 | if (items == NULL) { |
| 2967 | return -1; |
| 2968 | } |
| 2969 | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
| 2970 | items); |
| 2971 | Py_DECREF(items); |
| 2972 | if (reduce_value == NULL) { |
| 2973 | return -1; |
| 2974 | } |
| 2975 | /* save_reduce() will memoize the object automatically. */ |
| 2976 | status = save_reduce(self, reduce_value, obj); |
| 2977 | Py_DECREF(reduce_value); |
| 2978 | return status; |
| 2979 | } |
| 2980 | |
| 2981 | if (_Pickler_Write(self, &mark_op, 1) < 0) |
| 2982 | return -1; |
| 2983 | |
| 2984 | iter = PyObject_GetIter(obj); |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2985 | if (iter == NULL) { |
Christian Heimes | 74d8d63 | 2013-11-23 21:05:31 +0100 | [diff] [blame] | 2986 | return -1; |
Christian Heimes | b3d3ee4 | 2013-11-23 21:01:40 +0100 | [diff] [blame] | 2987 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 2988 | for (;;) { |
| 2989 | PyObject *item; |
| 2990 | |
| 2991 | item = PyIter_Next(iter); |
| 2992 | if (item == NULL) { |
| 2993 | if (PyErr_Occurred()) { |
| 2994 | Py_DECREF(iter); |
| 2995 | return -1; |
| 2996 | } |
| 2997 | break; |
| 2998 | } |
| 2999 | if (save(self, item, 0) < 0) { |
| 3000 | Py_DECREF(item); |
| 3001 | Py_DECREF(iter); |
| 3002 | return -1; |
| 3003 | } |
| 3004 | Py_DECREF(item); |
| 3005 | } |
| 3006 | Py_DECREF(iter); |
| 3007 | |
| 3008 | /* If the object is already in the memo, this means it is |
| 3009 | recursive. In this case, throw away everything we put on the |
| 3010 | stack, and fetch the object back from the memo. */ |
| 3011 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3012 | const char pop_mark_op = POP_MARK; |
| 3013 | |
| 3014 | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
| 3015 | return -1; |
| 3016 | if (memo_get(self, obj) < 0) |
| 3017 | return -1; |
| 3018 | return 0; |
| 3019 | } |
| 3020 | |
| 3021 | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
| 3022 | return -1; |
| 3023 | if (memo_put(self, obj) < 0) |
| 3024 | return -1; |
| 3025 | |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | static int |
| 3030 | fix_imports(PyObject **module_name, PyObject **global_name) |
| 3031 | { |
| 3032 | PyObject *key; |
| 3033 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3034 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3035 | |
| 3036 | key = PyTuple_Pack(2, *module_name, *global_name); |
| 3037 | if (key == NULL) |
| 3038 | return -1; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3039 | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3040 | Py_DECREF(key); |
| 3041 | if (item) { |
| 3042 | PyObject *fixed_module_name; |
| 3043 | PyObject *fixed_global_name; |
| 3044 | |
| 3045 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 3046 | PyErr_Format(PyExc_RuntimeError, |
| 3047 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3048 | "should be 2-tuples, not %.200s", |
| 3049 | Py_TYPE(item)->tp_name); |
| 3050 | return -1; |
| 3051 | } |
| 3052 | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
| 3053 | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
| 3054 | if (!PyUnicode_Check(fixed_module_name) || |
| 3055 | !PyUnicode_Check(fixed_global_name)) { |
| 3056 | PyErr_Format(PyExc_RuntimeError, |
| 3057 | "_compat_pickle.REVERSE_NAME_MAPPING values " |
| 3058 | "should be pairs of str, not (%.200s, %.200s)", |
| 3059 | Py_TYPE(fixed_module_name)->tp_name, |
| 3060 | Py_TYPE(fixed_global_name)->tp_name); |
| 3061 | return -1; |
| 3062 | } |
| 3063 | |
| 3064 | Py_CLEAR(*module_name); |
| 3065 | Py_CLEAR(*global_name); |
| 3066 | Py_INCREF(fixed_module_name); |
| 3067 | Py_INCREF(fixed_global_name); |
| 3068 | *module_name = fixed_module_name; |
| 3069 | *global_name = fixed_global_name; |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 3070 | return 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3071 | } |
| 3072 | else if (PyErr_Occurred()) { |
| 3073 | return -1; |
| 3074 | } |
| 3075 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3076 | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3077 | if (item) { |
| 3078 | if (!PyUnicode_Check(item)) { |
| 3079 | PyErr_Format(PyExc_RuntimeError, |
| 3080 | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
| 3081 | "should be strings, not %.200s", |
| 3082 | Py_TYPE(item)->tp_name); |
| 3083 | return -1; |
| 3084 | } |
| 3085 | Py_CLEAR(*module_name); |
| 3086 | Py_INCREF(item); |
| 3087 | *module_name = item; |
| 3088 | } |
| 3089 | else if (PyErr_Occurred()) { |
| 3090 | return -1; |
| 3091 | } |
| 3092 | |
| 3093 | return 0; |
| 3094 | } |
| 3095 | |
| 3096 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3097 | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
| 3098 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3099 | PyObject *global_name = NULL; |
| 3100 | PyObject *module_name = NULL; |
| 3101 | PyObject *module = NULL; |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3102 | PyObject *parent = NULL; |
| 3103 | PyObject *dotted_path = NULL; |
| 3104 | PyObject *lastname = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3105 | PyObject *cls; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3106 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3107 | int status = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3108 | _Py_IDENTIFIER(__name__); |
| 3109 | _Py_IDENTIFIER(__qualname__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3110 | |
| 3111 | const char global_op = GLOBAL; |
| 3112 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3113 | if (name) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3114 | Py_INCREF(name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3115 | global_name = name; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3116 | } |
| 3117 | else { |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3118 | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
| 3119 | if (global_name == NULL) { |
| 3120 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 3121 | goto error; |
| 3122 | PyErr_Clear(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3123 | } |
| 3124 | if (global_name == NULL) { |
| 3125 | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
| 3126 | if (global_name == NULL) |
| 3127 | goto error; |
| 3128 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3129 | } |
| 3130 | |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3131 | dotted_path = get_dotted_path(module, global_name); |
| 3132 | if (dotted_path == NULL) |
| 3133 | goto error; |
| 3134 | module_name = whichmodule(obj, dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3135 | if (module_name == NULL) |
| 3136 | goto error; |
| 3137 | |
| 3138 | /* XXX: Change to use the import C API directly with level=0 to disallow |
| 3139 | relative imports. |
| 3140 | |
| 3141 | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
| 3142 | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
| 3143 | custom import functions (IMHO, this would be a nice security |
| 3144 | feature). The import C API would need to be extended to support the |
| 3145 | extra parameters of __import__ to fix that. */ |
| 3146 | module = PyImport_Import(module_name); |
| 3147 | if (module == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3148 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3149 | "Can't pickle %R: import of module %R failed", |
| 3150 | obj, module_name); |
| 3151 | goto error; |
| 3152 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3153 | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
| 3154 | Py_INCREF(lastname); |
| 3155 | cls = get_deep_attribute(module, dotted_path, &parent); |
| 3156 | Py_CLEAR(dotted_path); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3157 | if (cls == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3158 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3159 | "Can't pickle %R: attribute lookup %S on %S failed", |
| 3160 | obj, global_name, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3161 | goto error; |
| 3162 | } |
| 3163 | if (cls != obj) { |
| 3164 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3165 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3166 | "Can't pickle %R: it's not the same object as %S.%S", |
| 3167 | obj, module_name, global_name); |
| 3168 | goto error; |
| 3169 | } |
| 3170 | Py_DECREF(cls); |
| 3171 | |
| 3172 | if (self->proto >= 2) { |
| 3173 | /* See whether this is in the extension registry, and if |
| 3174 | * so generate an EXT opcode. |
| 3175 | */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3176 | PyObject *extension_key; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3177 | PyObject *code_obj; /* extension code as Python object */ |
| 3178 | long code; /* extension code as C value */ |
| 3179 | char pdata[5]; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3180 | Py_ssize_t n; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3181 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3182 | extension_key = PyTuple_Pack(2, module_name, global_name); |
| 3183 | if (extension_key == NULL) { |
| 3184 | goto error; |
| 3185 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3186 | code_obj = PyDict_GetItemWithError(st->extension_registry, |
| 3187 | extension_key); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3188 | Py_DECREF(extension_key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3189 | /* The object is not registered in the extension registry. |
| 3190 | This is the most likely code path. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3191 | if (code_obj == NULL) { |
| 3192 | if (PyErr_Occurred()) { |
| 3193 | goto error; |
| 3194 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3195 | goto gen_global; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3196 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3197 | |
| 3198 | /* XXX: pickle.py doesn't check neither the type, nor the range |
| 3199 | of the value returned by the extension_registry. It should for |
| 3200 | consistency. */ |
| 3201 | |
| 3202 | /* Verify code_obj has the right type and value. */ |
| 3203 | if (!PyLong_Check(code_obj)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3204 | PyErr_Format(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3205 | "Can't pickle %R: extension code %R isn't an integer", |
| 3206 | obj, code_obj); |
| 3207 | goto error; |
| 3208 | } |
| 3209 | code = PyLong_AS_LONG(code_obj); |
| 3210 | if (code <= 0 || code > 0x7fffffffL) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 3211 | if (!PyErr_Occurred()) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3212 | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
| 3213 | "code %ld is out of range", obj, code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3214 | goto error; |
| 3215 | } |
| 3216 | |
| 3217 | /* Generate an EXT opcode. */ |
| 3218 | if (code <= 0xff) { |
| 3219 | pdata[0] = EXT1; |
| 3220 | pdata[1] = (unsigned char)code; |
| 3221 | n = 2; |
| 3222 | } |
| 3223 | else if (code <= 0xffff) { |
| 3224 | pdata[0] = EXT2; |
| 3225 | pdata[1] = (unsigned char)(code & 0xff); |
| 3226 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3227 | n = 3; |
| 3228 | } |
| 3229 | else { |
| 3230 | pdata[0] = EXT4; |
| 3231 | pdata[1] = (unsigned char)(code & 0xff); |
| 3232 | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
| 3233 | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
| 3234 | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
| 3235 | n = 5; |
| 3236 | } |
| 3237 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3238 | if (_Pickler_Write(self, pdata, n) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3239 | goto error; |
| 3240 | } |
| 3241 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3242 | gen_global: |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3243 | if (parent == module) { |
| 3244 | Py_INCREF(lastname); |
| 3245 | Py_DECREF(global_name); |
| 3246 | global_name = lastname; |
| 3247 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3248 | if (self->proto >= 4) { |
| 3249 | const char stack_global_op = STACK_GLOBAL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3250 | |
Christian Heimes | e8b1ba1 | 2013-11-23 21:13:39 +0100 | [diff] [blame] | 3251 | if (save(self, module_name, 0) < 0) |
| 3252 | goto error; |
| 3253 | if (save(self, global_name, 0) < 0) |
| 3254 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3255 | |
| 3256 | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
| 3257 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3258 | } |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3259 | else if (parent != module) { |
| 3260 | PickleState *st = _Pickle_GetGlobalState(); |
| 3261 | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
| 3262 | st->getattr, parent, lastname); |
| 3263 | status = save_reduce(self, reduce_value, NULL); |
| 3264 | Py_DECREF(reduce_value); |
| 3265 | if (status < 0) |
| 3266 | goto error; |
| 3267 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3268 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3269 | /* Generate a normal global opcode if we are using a pickle |
| 3270 | protocol < 4, or if the object is not registered in the |
| 3271 | extension registry. */ |
| 3272 | PyObject *encoded; |
| 3273 | PyObject *(*unicode_encoder)(PyObject *); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3274 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3275 | if (_Pickler_Write(self, &global_op, 1) < 0) |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3276 | goto error; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3277 | |
| 3278 | /* For protocol < 3 and if the user didn't request against doing |
| 3279 | so, we convert module names to the old 2.x module names. */ |
| 3280 | if (self->proto < 3 && self->fix_imports) { |
| 3281 | if (fix_imports(&module_name, &global_name) < 0) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3282 | goto error; |
| 3283 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3286 | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
| 3287 | both the module name and the global name using UTF-8. We do so |
| 3288 | only when we are using the pickle protocol newer than version |
| 3289 | 3. This is to ensure compatibility with older Unpickler running |
| 3290 | on Python 2.x. */ |
| 3291 | if (self->proto == 3) { |
| 3292 | unicode_encoder = PyUnicode_AsUTF8String; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3293 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3294 | else { |
| 3295 | unicode_encoder = PyUnicode_AsASCIIString; |
| 3296 | } |
| 3297 | encoded = unicode_encoder(module_name); |
| 3298 | if (encoded == NULL) { |
| 3299 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3300 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3301 | "can't pickle module identifier '%S' using " |
| 3302 | "pickle protocol %i", |
| 3303 | module_name, self->proto); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 3304 | goto error; |
| 3305 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3306 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3307 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3308 | Py_DECREF(encoded); |
| 3309 | goto error; |
| 3310 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3311 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3312 | if(_Pickler_Write(self, "\n", 1) < 0) |
| 3313 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3314 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3315 | /* Save the name of the module. */ |
| 3316 | encoded = unicode_encoder(global_name); |
| 3317 | if (encoded == NULL) { |
| 3318 | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3319 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3320 | "can't pickle global identifier '%S' using " |
| 3321 | "pickle protocol %i", |
| 3322 | global_name, self->proto); |
| 3323 | goto error; |
| 3324 | } |
| 3325 | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
| 3326 | PyBytes_GET_SIZE(encoded)) < 0) { |
| 3327 | Py_DECREF(encoded); |
| 3328 | goto error; |
| 3329 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3330 | Py_DECREF(encoded); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3331 | if (_Pickler_Write(self, "\n", 1) < 0) |
| 3332 | goto error; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3333 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3334 | /* Memoize the object. */ |
| 3335 | if (memo_put(self, obj) < 0) |
| 3336 | goto error; |
| 3337 | } |
| 3338 | |
| 3339 | if (0) { |
| 3340 | error: |
| 3341 | status = -1; |
| 3342 | } |
| 3343 | Py_XDECREF(module_name); |
| 3344 | Py_XDECREF(global_name); |
| 3345 | Py_XDECREF(module); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 3346 | Py_XDECREF(parent); |
| 3347 | Py_XDECREF(dotted_path); |
| 3348 | Py_XDECREF(lastname); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3349 | |
| 3350 | return status; |
| 3351 | } |
| 3352 | |
| 3353 | static int |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3354 | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
| 3355 | { |
| 3356 | PyObject *reduce_value; |
| 3357 | int status; |
| 3358 | |
| 3359 | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
| 3360 | if (reduce_value == NULL) { |
| 3361 | return -1; |
| 3362 | } |
| 3363 | status = save_reduce(self, reduce_value, obj); |
| 3364 | Py_DECREF(reduce_value); |
| 3365 | return status; |
| 3366 | } |
| 3367 | |
| 3368 | static int |
| 3369 | save_type(PicklerObject *self, PyObject *obj) |
| 3370 | { |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3371 | if (obj == (PyObject *)&_PyNone_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3372 | return save_singleton_type(self, obj, Py_None); |
| 3373 | } |
| 3374 | else if (obj == (PyObject *)&PyEllipsis_Type) { |
| 3375 | return save_singleton_type(self, obj, Py_Ellipsis); |
| 3376 | } |
Alexandre Vassalotti | 65846c6 | 2013-11-30 17:55:48 -0800 | [diff] [blame] | 3377 | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3378 | return save_singleton_type(self, obj, Py_NotImplemented); |
| 3379 | } |
| 3380 | return save_global(self, obj, NULL); |
| 3381 | } |
| 3382 | |
| 3383 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3384 | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
| 3385 | { |
| 3386 | PyObject *pid = NULL; |
| 3387 | int status = 0; |
| 3388 | |
| 3389 | const char persid_op = PERSID; |
| 3390 | const char binpersid_op = BINPERSID; |
| 3391 | |
| 3392 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3393 | pid = _Pickle_FastCall(func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3394 | if (pid == NULL) |
| 3395 | return -1; |
| 3396 | |
| 3397 | if (pid != Py_None) { |
| 3398 | if (self->bin) { |
| 3399 | if (save(self, pid, 1) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3400 | _Pickler_Write(self, &binpersid_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3401 | goto error; |
| 3402 | } |
| 3403 | else { |
| 3404 | PyObject *pid_str = NULL; |
| 3405 | char *pid_ascii_bytes; |
| 3406 | Py_ssize_t size; |
| 3407 | |
| 3408 | pid_str = PyObject_Str(pid); |
| 3409 | if (pid_str == NULL) |
| 3410 | goto error; |
| 3411 | |
| 3412 | /* XXX: Should it check whether the persistent id only contains |
| 3413 | ASCII characters? And what if the pid contains embedded |
| 3414 | newlines? */ |
Marc-André Lemburg | 4cc0f24 | 2008-08-07 18:54:33 +0000 | [diff] [blame] | 3415 | pid_ascii_bytes = _PyUnicode_AsStringAndSize(pid_str, &size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3416 | Py_DECREF(pid_str); |
| 3417 | if (pid_ascii_bytes == NULL) |
| 3418 | goto error; |
| 3419 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3420 | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
| 3421 | _Pickler_Write(self, pid_ascii_bytes, size) < 0 || |
| 3422 | _Pickler_Write(self, "\n", 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3423 | goto error; |
| 3424 | } |
| 3425 | status = 1; |
| 3426 | } |
| 3427 | |
| 3428 | if (0) { |
| 3429 | error: |
| 3430 | status = -1; |
| 3431 | } |
| 3432 | Py_XDECREF(pid); |
| 3433 | |
| 3434 | return status; |
| 3435 | } |
| 3436 | |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3437 | static PyObject * |
| 3438 | get_class(PyObject *obj) |
| 3439 | { |
| 3440 | PyObject *cls; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3441 | _Py_IDENTIFIER(__class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3442 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3443 | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3444 | if (cls == NULL) { |
| 3445 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 3446 | PyErr_Clear(); |
| 3447 | cls = (PyObject *) Py_TYPE(obj); |
| 3448 | Py_INCREF(cls); |
| 3449 | } |
| 3450 | } |
| 3451 | return cls; |
| 3452 | } |
| 3453 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3454 | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
| 3455 | * appropriate __reduce__ method for obj. |
| 3456 | */ |
| 3457 | static int |
| 3458 | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
| 3459 | { |
| 3460 | PyObject *callable; |
| 3461 | PyObject *argtup; |
| 3462 | PyObject *state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3463 | PyObject *listitems = Py_None; |
| 3464 | PyObject *dictitems = Py_None; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3465 | PickleState *st = _Pickle_GetGlobalState(); |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3466 | Py_ssize_t size; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3467 | int use_newobj = 0, use_newobj_ex = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3468 | |
| 3469 | const char reduce_op = REDUCE; |
| 3470 | const char build_op = BUILD; |
| 3471 | const char newobj_op = NEWOBJ; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3472 | const char newobj_ex_op = NEWOBJ_EX; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3473 | |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3474 | size = PyTuple_Size(args); |
| 3475 | if (size < 2 || size > 5) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3476 | PyErr_SetString(st->PicklingError, "tuple returned by " |
Hirokazu Yamamoto | b46a633 | 2008-11-04 00:35:10 +0000 | [diff] [blame] | 3477 | "__reduce__ must contain 2 through 5 elements"); |
| 3478 | return -1; |
| 3479 | } |
| 3480 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3481 | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
| 3482 | &callable, &argtup, &state, &listitems, &dictitems)) |
| 3483 | return -1; |
| 3484 | |
| 3485 | if (!PyCallable_Check(callable)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3486 | PyErr_SetString(st->PicklingError, "first item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3487 | "returned by __reduce__ must be callable"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3488 | return -1; |
| 3489 | } |
| 3490 | if (!PyTuple_Check(argtup)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3491 | PyErr_SetString(st->PicklingError, "second item of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3492 | "returned by __reduce__ must be a tuple"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3493 | return -1; |
| 3494 | } |
| 3495 | |
| 3496 | if (state == Py_None) |
| 3497 | state = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3498 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3499 | if (listitems == Py_None) |
| 3500 | listitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3501 | else if (!PyIter_Check(listitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3502 | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3503 | "returned by __reduce__ must be an iterator, not %s", |
| 3504 | Py_TYPE(listitems)->tp_name); |
| 3505 | return -1; |
| 3506 | } |
| 3507 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3508 | if (dictitems == Py_None) |
| 3509 | dictitems = NULL; |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3510 | else if (!PyIter_Check(dictitems)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3511 | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
Amaury Forgeot d'Arc | 424b481 | 2008-10-30 22:25:31 +0000 | [diff] [blame] | 3512 | "returned by __reduce__ must be an iterator, not %s", |
| 3513 | Py_TYPE(dictitems)->tp_name); |
| 3514 | return -1; |
| 3515 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3516 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3517 | if (self->proto >= 2) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3518 | PyObject *name; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3519 | _Py_IDENTIFIER(__name__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3520 | |
Victor Stinner | 804e05e | 2013-11-14 01:26:17 +0100 | [diff] [blame] | 3521 | name = _PyObject_GetAttrId(callable, &PyId___name__); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3522 | if (name == NULL) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3523 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3524 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3525 | } |
| 3526 | PyErr_Clear(); |
| 3527 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3528 | else if (PyUnicode_Check(name)) { |
| 3529 | if (self->proto >= 4) { |
| 3530 | _Py_IDENTIFIER(__newobj_ex__); |
| 3531 | use_newobj_ex = PyUnicode_Compare( |
| 3532 | name, _PyUnicode_FromId(&PyId___newobj_ex__)) == 0; |
| 3533 | } |
| 3534 | if (!use_newobj_ex) { |
| 3535 | _Py_IDENTIFIER(__newobj__); |
| 3536 | use_newobj = PyUnicode_Compare( |
| 3537 | name, _PyUnicode_FromId(&PyId___newobj__)) == 0; |
| 3538 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3539 | } |
Serhiy Storchaka | 707b5cc | 2014-12-16 19:43:46 +0200 | [diff] [blame] | 3540 | Py_XDECREF(name); |
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 | |
| 3543 | if (use_newobj_ex) { |
| 3544 | PyObject *cls; |
| 3545 | PyObject *args; |
| 3546 | PyObject *kwargs; |
| 3547 | |
| 3548 | if (Py_SIZE(argtup) != 3) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3549 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3550 | "length of the NEWOBJ_EX argument tuple must be " |
| 3551 | "exactly 3, not %zd", Py_SIZE(argtup)); |
| 3552 | return -1; |
| 3553 | } |
| 3554 | |
| 3555 | cls = PyTuple_GET_ITEM(argtup, 0); |
| 3556 | if (!PyType_Check(cls)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3557 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3558 | "first item from NEWOBJ_EX argument tuple must " |
| 3559 | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
| 3560 | return -1; |
| 3561 | } |
| 3562 | args = PyTuple_GET_ITEM(argtup, 1); |
| 3563 | if (!PyTuple_Check(args)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3564 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3565 | "second item from NEWOBJ_EX argument tuple must " |
| 3566 | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
| 3567 | return -1; |
| 3568 | } |
| 3569 | kwargs = PyTuple_GET_ITEM(argtup, 2); |
| 3570 | if (!PyDict_Check(kwargs)) { |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3571 | PyErr_Format(st->PicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3572 | "third item from NEWOBJ_EX argument tuple must " |
| 3573 | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
| 3574 | return -1; |
| 3575 | } |
| 3576 | |
| 3577 | if (save(self, cls, 0) < 0 || |
| 3578 | save(self, args, 0) < 0 || |
| 3579 | save(self, kwargs, 0) < 0 || |
| 3580 | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
| 3581 | return -1; |
| 3582 | } |
| 3583 | } |
| 3584 | else if (use_newobj) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3585 | PyObject *cls; |
| 3586 | PyObject *newargtup; |
| 3587 | PyObject *obj_class; |
| 3588 | int p; |
| 3589 | |
| 3590 | /* Sanity checks. */ |
| 3591 | if (Py_SIZE(argtup) < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3592 | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3593 | return -1; |
| 3594 | } |
| 3595 | |
| 3596 | cls = PyTuple_GET_ITEM(argtup, 0); |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3597 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3598 | PyErr_SetString(st->PicklingError, "args[0] from " |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3599 | "__newobj__ args is not a type"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3600 | return -1; |
| 3601 | } |
| 3602 | |
| 3603 | if (obj != NULL) { |
Antoine Pitrou | 16c4ce1 | 2011-03-11 21:30:43 +0100 | [diff] [blame] | 3604 | obj_class = get_class(obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3605 | p = obj_class != cls; /* true iff a problem */ |
| 3606 | Py_DECREF(obj_class); |
| 3607 | if (p) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3608 | PyErr_SetString(st->PicklingError, "args[0] from " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3609 | "__newobj__ args has the wrong class"); |
| 3610 | return -1; |
| 3611 | } |
| 3612 | } |
| 3613 | /* XXX: These calls save() are prone to infinite recursion. Imagine |
| 3614 | what happen if the value returned by the __reduce__() method of |
| 3615 | some extension type contains another object of the same type. Ouch! |
| 3616 | |
| 3617 | Here is a quick example, that I ran into, to illustrate what I |
| 3618 | mean: |
| 3619 | |
| 3620 | >>> import pickle, copyreg |
| 3621 | >>> copyreg.dispatch_table.pop(complex) |
| 3622 | >>> pickle.dumps(1+2j) |
| 3623 | Traceback (most recent call last): |
| 3624 | ... |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame^] | 3625 | RecursionError: maximum recursion depth exceeded |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3626 | |
| 3627 | Removing the complex class from copyreg.dispatch_table made the |
| 3628 | __reduce_ex__() method emit another complex object: |
| 3629 | |
| 3630 | >>> (1+1j).__reduce_ex__(2) |
| 3631 | (<function __newobj__ at 0xb7b71c3c>, |
| 3632 | (<class 'complex'>, (1+1j)), None, None, None) |
| 3633 | |
| 3634 | Thus when save() was called on newargstup (the 2nd item) recursion |
| 3635 | ensued. Of course, the bug was in the complex class which had a |
| 3636 | broken __getnewargs__() that emitted another complex object. But, |
| 3637 | the point, here, is it is quite easy to end up with a broken reduce |
| 3638 | function. */ |
| 3639 | |
| 3640 | /* Save the class and its __new__ arguments. */ |
| 3641 | if (save(self, cls, 0) < 0) |
| 3642 | return -1; |
| 3643 | |
| 3644 | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
| 3645 | if (newargtup == NULL) |
| 3646 | return -1; |
| 3647 | |
| 3648 | p = save(self, newargtup, 0); |
| 3649 | Py_DECREF(newargtup); |
| 3650 | if (p < 0) |
| 3651 | return -1; |
| 3652 | |
| 3653 | /* Add NEWOBJ opcode. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3654 | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3655 | return -1; |
| 3656 | } |
| 3657 | else { /* Not using NEWOBJ. */ |
| 3658 | if (save(self, callable, 0) < 0 || |
| 3659 | save(self, argtup, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3660 | _Pickler_Write(self, &reduce_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3661 | return -1; |
| 3662 | } |
| 3663 | |
| 3664 | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
| 3665 | the caller do not want to memoize the object. Not particularly useful, |
| 3666 | but that is to mimic the behavior save_reduce() in pickle.py when |
| 3667 | obj is None. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3668 | if (obj != NULL) { |
| 3669 | /* If the object is already in the memo, this means it is |
| 3670 | recursive. In this case, throw away everything we put on the |
| 3671 | stack, and fetch the object back from the memo. */ |
| 3672 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3673 | const char pop_op = POP; |
| 3674 | |
| 3675 | if (_Pickler_Write(self, &pop_op, 1) < 0) |
| 3676 | return -1; |
| 3677 | if (memo_get(self, obj) < 0) |
| 3678 | return -1; |
| 3679 | |
| 3680 | return 0; |
| 3681 | } |
| 3682 | else if (memo_put(self, obj) < 0) |
| 3683 | return -1; |
| 3684 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3685 | |
| 3686 | if (listitems && batch_list(self, listitems) < 0) |
| 3687 | return -1; |
| 3688 | |
| 3689 | if (dictitems && batch_dict(self, dictitems) < 0) |
| 3690 | return -1; |
| 3691 | |
| 3692 | if (state) { |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 3693 | if (save(self, state, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3694 | _Pickler_Write(self, &build_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3695 | return -1; |
| 3696 | } |
| 3697 | |
| 3698 | return 0; |
| 3699 | } |
| 3700 | |
| 3701 | static int |
| 3702 | save(PicklerObject *self, PyObject *obj, int pers_save) |
| 3703 | { |
| 3704 | PyTypeObject *type; |
| 3705 | PyObject *reduce_func = NULL; |
| 3706 | PyObject *reduce_value = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3707 | int status = 0; |
| 3708 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3709 | if (_Pickler_OpcodeBoundary(self) < 0) |
| 3710 | return -1; |
| 3711 | |
Antoine Pitrou | e6d4c5b | 2011-01-23 17:12:25 +0000 | [diff] [blame] | 3712 | if (Py_EnterRecursiveCall(" while pickling an object")) |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3713 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3714 | |
| 3715 | /* The extra pers_save argument is necessary to avoid calling save_pers() |
| 3716 | on its returned object. */ |
| 3717 | if (!pers_save && self->pers_func) { |
| 3718 | /* save_pers() returns: |
| 3719 | -1 to signal an error; |
| 3720 | 0 if it did nothing successfully; |
| 3721 | 1 if a persistent id was saved. |
| 3722 | */ |
| 3723 | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
| 3724 | goto done; |
| 3725 | } |
| 3726 | |
| 3727 | type = Py_TYPE(obj); |
| 3728 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3729 | /* The old cPickle had an optimization that used switch-case statement |
| 3730 | dispatching on the first letter of the type name. This has was removed |
| 3731 | since benchmarks shown that this optimization was actually slowing |
| 3732 | things down. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3733 | |
| 3734 | /* Atom types; these aren't memoized, so don't check the memo. */ |
| 3735 | |
| 3736 | if (obj == Py_None) { |
| 3737 | status = save_none(self, obj); |
| 3738 | goto done; |
| 3739 | } |
| 3740 | else if (obj == Py_False || obj == Py_True) { |
| 3741 | status = save_bool(self, obj); |
| 3742 | goto done; |
| 3743 | } |
| 3744 | else if (type == &PyLong_Type) { |
| 3745 | status = save_long(self, obj); |
| 3746 | goto done; |
| 3747 | } |
| 3748 | else if (type == &PyFloat_Type) { |
| 3749 | status = save_float(self, obj); |
| 3750 | goto done; |
| 3751 | } |
| 3752 | |
| 3753 | /* Check the memo to see if it has the object. If so, generate |
| 3754 | a GET (or BINGET) opcode, instead of pickling the object |
| 3755 | once again. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3756 | if (PyMemoTable_Get(self->memo, obj)) { |
| 3757 | if (memo_get(self, obj) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3758 | goto error; |
| 3759 | goto done; |
| 3760 | } |
| 3761 | |
| 3762 | if (type == &PyBytes_Type) { |
| 3763 | status = save_bytes(self, obj); |
| 3764 | goto done; |
| 3765 | } |
| 3766 | else if (type == &PyUnicode_Type) { |
| 3767 | status = save_unicode(self, obj); |
| 3768 | goto done; |
| 3769 | } |
| 3770 | else if (type == &PyDict_Type) { |
| 3771 | status = save_dict(self, obj); |
| 3772 | goto done; |
| 3773 | } |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3774 | else if (type == &PySet_Type) { |
| 3775 | status = save_set(self, obj); |
| 3776 | goto done; |
| 3777 | } |
| 3778 | else if (type == &PyFrozenSet_Type) { |
| 3779 | status = save_frozenset(self, obj); |
| 3780 | goto done; |
| 3781 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3782 | else if (type == &PyList_Type) { |
| 3783 | status = save_list(self, obj); |
| 3784 | goto done; |
| 3785 | } |
| 3786 | else if (type == &PyTuple_Type) { |
| 3787 | status = save_tuple(self, obj); |
| 3788 | goto done; |
| 3789 | } |
| 3790 | else if (type == &PyType_Type) { |
Alexandre Vassalotti | 19b6fa6 | 2013-11-30 16:06:39 -0800 | [diff] [blame] | 3791 | status = save_type(self, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3792 | goto done; |
| 3793 | } |
| 3794 | else if (type == &PyFunction_Type) { |
| 3795 | status = save_global(self, obj, NULL); |
Alexandre Vassalotti | fc91285 | 2013-11-24 03:07:35 -0800 | [diff] [blame] | 3796 | goto done; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3797 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3798 | |
| 3799 | /* XXX: This part needs some unit tests. */ |
| 3800 | |
| 3801 | /* Get a reduction callable, and call it. This may come from |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3802 | * self.dispatch_table, copyreg.dispatch_table, the object's |
| 3803 | * __reduce_ex__ method, or the object's __reduce__ method. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3804 | */ |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3805 | if (self->dispatch_table == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3806 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3807 | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
| 3808 | (PyObject *)type); |
| 3809 | if (reduce_func == NULL) { |
| 3810 | if (PyErr_Occurred()) { |
| 3811 | goto error; |
| 3812 | } |
| 3813 | } else { |
| 3814 | /* PyDict_GetItemWithError() returns a borrowed reference. |
| 3815 | Increase the reference count to be consistent with |
| 3816 | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
| 3817 | Py_INCREF(reduce_func); |
| 3818 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3819 | } else { |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 3820 | reduce_func = PyObject_GetItem(self->dispatch_table, |
| 3821 | (PyObject *)type); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 3822 | if (reduce_func == NULL) { |
| 3823 | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
| 3824 | PyErr_Clear(); |
| 3825 | else |
| 3826 | goto error; |
| 3827 | } |
| 3828 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3829 | if (reduce_func != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3830 | Py_INCREF(obj); |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3831 | reduce_value = _Pickle_FastCall(reduce_func, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3832 | } |
Antoine Pitrou | ffd41d9 | 2011-10-04 09:23:04 +0200 | [diff] [blame] | 3833 | else if (PyType_IsSubtype(type, &PyType_Type)) { |
| 3834 | status = save_global(self, obj, NULL); |
| 3835 | goto done; |
| 3836 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3837 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3838 | _Py_IDENTIFIER(__reduce__); |
| 3839 | _Py_IDENTIFIER(__reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3840 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3841 | |
| 3842 | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
| 3843 | automatically defined as __reduce__. While this is convenient, this |
| 3844 | make it impossible to know which method was actually called. Of |
| 3845 | course, this is not a big deal. But still, it would be nice to let |
| 3846 | the user know which method was called when something go |
| 3847 | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
| 3848 | don't actually have to check for a __reduce__ method. */ |
| 3849 | |
| 3850 | /* Check for a __reduce_ex__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3851 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3852 | if (reduce_func != NULL) { |
| 3853 | PyObject *proto; |
| 3854 | proto = PyLong_FromLong(self->proto); |
| 3855 | if (proto != NULL) { |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 3856 | reduce_value = _Pickle_FastCall(reduce_func, proto); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3857 | } |
| 3858 | } |
| 3859 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3860 | PickleState *st = _Pickle_GetGlobalState(); |
| 3861 | |
| 3862 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3863 | PyErr_Clear(); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3864 | } |
| 3865 | else { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3866 | goto error; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3867 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3868 | /* Check for a __reduce__ method. */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3869 | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3870 | if (reduce_func != NULL) { |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3871 | PyObject *empty_tuple = PyTuple_New(0); |
| 3872 | reduce_value = PyObject_Call(reduce_func, empty_tuple, |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3873 | NULL); |
Alexandre Vassalotti | 6bf41e5 | 2013-11-28 15:17:29 -0800 | [diff] [blame] | 3874 | Py_DECREF(empty_tuple); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3875 | } |
| 3876 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3877 | PyErr_Format(st->PicklingError, |
| 3878 | "can't pickle '%.200s' object: %R", |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3879 | type->tp_name, obj); |
| 3880 | goto error; |
| 3881 | } |
| 3882 | } |
| 3883 | } |
| 3884 | |
| 3885 | if (reduce_value == NULL) |
| 3886 | goto error; |
| 3887 | |
| 3888 | if (PyUnicode_Check(reduce_value)) { |
| 3889 | status = save_global(self, obj, reduce_value); |
| 3890 | goto done; |
| 3891 | } |
| 3892 | |
| 3893 | if (!PyTuple_Check(reduce_value)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3894 | PickleState *st = _Pickle_GetGlobalState(); |
| 3895 | PyErr_SetString(st->PicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3896 | "__reduce__ must return a string or tuple"); |
| 3897 | goto error; |
| 3898 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3899 | |
| 3900 | status = save_reduce(self, reduce_value, obj); |
| 3901 | |
| 3902 | if (0) { |
| 3903 | error: |
| 3904 | status = -1; |
| 3905 | } |
| 3906 | done: |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 3907 | |
Alexandre Vassalotti | dff1834 | 2008-07-13 18:48:30 +0000 | [diff] [blame] | 3908 | Py_LeaveRecursiveCall(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3909 | Py_XDECREF(reduce_func); |
| 3910 | Py_XDECREF(reduce_value); |
| 3911 | |
| 3912 | return status; |
| 3913 | } |
| 3914 | |
| 3915 | static int |
| 3916 | dump(PicklerObject *self, PyObject *obj) |
| 3917 | { |
| 3918 | const char stop_op = STOP; |
| 3919 | |
| 3920 | if (self->proto >= 2) { |
| 3921 | char header[2]; |
| 3922 | |
| 3923 | header[0] = PROTO; |
| 3924 | assert(self->proto >= 0 && self->proto < 256); |
| 3925 | header[1] = (unsigned char)self->proto; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3926 | if (_Pickler_Write(self, header, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3927 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 3928 | if (self->proto >= 4) |
| 3929 | self->framing = 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3930 | } |
| 3931 | |
| 3932 | if (save(self, obj, 0) < 0 || |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3933 | _Pickler_Write(self, &stop_op, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3934 | return -1; |
| 3935 | |
| 3936 | return 0; |
| 3937 | } |
| 3938 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3939 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3940 | |
| 3941 | _pickle.Pickler.clear_memo |
| 3942 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3943 | Clears the pickler's "memo". |
| 3944 | |
| 3945 | The memo is the data structure that remembers which objects the |
| 3946 | pickler has already seen, so that shared or recursive objects are |
| 3947 | pickled by reference and not by value. This method is useful when |
| 3948 | re-using picklers. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3949 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3950 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 3951 | static PyObject * |
| 3952 | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3953 | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3954 | { |
| 3955 | if (self->memo) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3956 | PyMemoTable_Clear(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3957 | |
| 3958 | Py_RETURN_NONE; |
| 3959 | } |
| 3960 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3961 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3962 | |
| 3963 | _pickle.Pickler.dump |
| 3964 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3965 | obj: object |
| 3966 | / |
| 3967 | |
| 3968 | Write a pickled representation of the given object to the open file. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 3969 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3970 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3971 | static PyObject * |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 3972 | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 3973 | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3974 | { |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3975 | /* Check whether the Pickler was initialized correctly (issue3664). |
| 3976 | Developers often forget to call __init__() in their subclasses, which |
| 3977 | would trigger a segfault without this check. */ |
| 3978 | if (self->write == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 3979 | PickleState *st = _Pickle_GetGlobalState(); |
| 3980 | PyErr_Format(st->PicklingError, |
Amaury Forgeot d'Arc | 87eee63 | 2008-10-17 20:15:53 +0000 | [diff] [blame] | 3981 | "Pickler.__init__() was not called by %s.__init__()", |
| 3982 | Py_TYPE(self)->tp_name); |
| 3983 | return NULL; |
| 3984 | } |
| 3985 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3986 | if (_Pickler_ClearBuffer(self) < 0) |
| 3987 | return NULL; |
| 3988 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3989 | if (dump(self, obj) < 0) |
| 3990 | return NULL; |
| 3991 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 3992 | if (_Pickler_FlushToFile(self) < 0) |
| 3993 | return NULL; |
| 3994 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 3995 | Py_RETURN_NONE; |
| 3996 | } |
| 3997 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 3998 | /*[clinic input] |
| 3999 | |
| 4000 | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
| 4001 | |
| 4002 | Returns size in memory, in bytes. |
| 4003 | [clinic start generated code]*/ |
| 4004 | |
| 4005 | static Py_ssize_t |
| 4006 | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
| 4007 | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
| 4008 | { |
| 4009 | Py_ssize_t res, s; |
| 4010 | |
| 4011 | res = sizeof(PicklerObject); |
| 4012 | if (self->memo != NULL) { |
| 4013 | res += sizeof(PyMemoTable); |
| 4014 | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
| 4015 | } |
| 4016 | if (self->output_buffer != NULL) { |
| 4017 | s = _PySys_GetSizeOf(self->output_buffer); |
| 4018 | if (s == -1) |
| 4019 | return -1; |
| 4020 | res += s; |
| 4021 | } |
| 4022 | return res; |
| 4023 | } |
| 4024 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4025 | static struct PyMethodDef Pickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4026 | _PICKLE_PICKLER_DUMP_METHODDEF |
| 4027 | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 4028 | _PICKLE_PICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4029 | {NULL, NULL} /* sentinel */ |
| 4030 | }; |
| 4031 | |
| 4032 | static void |
| 4033 | Pickler_dealloc(PicklerObject *self) |
| 4034 | { |
| 4035 | PyObject_GC_UnTrack(self); |
| 4036 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4037 | Py_XDECREF(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4038 | Py_XDECREF(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4039 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4040 | Py_XDECREF(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4041 | Py_XDECREF(self->fast_memo); |
| 4042 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4043 | PyMemoTable_Del(self->memo); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4044 | |
| 4045 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 4046 | } |
| 4047 | |
| 4048 | static int |
| 4049 | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
| 4050 | { |
| 4051 | Py_VISIT(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4052 | Py_VISIT(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4053 | Py_VISIT(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4054 | Py_VISIT(self->fast_memo); |
| 4055 | return 0; |
| 4056 | } |
| 4057 | |
| 4058 | static int |
| 4059 | Pickler_clear(PicklerObject *self) |
| 4060 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4061 | Py_CLEAR(self->output_buffer); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4062 | Py_CLEAR(self->write); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4063 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4064 | Py_CLEAR(self->dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4065 | Py_CLEAR(self->fast_memo); |
| 4066 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4067 | if (self->memo != NULL) { |
| 4068 | PyMemoTable *memo = self->memo; |
| 4069 | self->memo = NULL; |
| 4070 | PyMemoTable_Del(memo); |
| 4071 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4072 | return 0; |
| 4073 | } |
| 4074 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4075 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4076 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4077 | |
| 4078 | _pickle.Pickler.__init__ |
| 4079 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4080 | file: object |
| 4081 | protocol: object = NULL |
| 4082 | fix_imports: bool = True |
| 4083 | |
| 4084 | This takes a binary file for writing a pickle data stream. |
| 4085 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4086 | The optional *protocol* argument tells the pickler to use the given |
| 4087 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 4088 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4089 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4090 | Specifying a negative protocol version selects the highest protocol |
| 4091 | version supported. The higher the protocol used, the more recent the |
| 4092 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4093 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4094 | The *file* argument must have a write() method that accepts a single |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4095 | bytes argument. It can thus be a file object opened for binary |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4096 | writing, a io.BytesIO instance, or any other custom object that meets |
| 4097 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4098 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4099 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 4100 | to map the new Python 3 names to the old module names used in Python |
| 4101 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4102 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4103 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4104 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 4105 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 4106 | PyObject *protocol, int fix_imports) |
| 4107 | /*[clinic end generated code: output=b5f31078dab17fb0 input=b8cdeb7e3f5ee674]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4108 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4109 | _Py_IDENTIFIER(persistent_id); |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4110 | _Py_IDENTIFIER(dispatch_table); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4111 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4112 | /* In case of multiple __init__() calls, clear previous content. */ |
| 4113 | if (self->write != NULL) |
| 4114 | (void)Pickler_clear(self); |
| 4115 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4116 | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4117 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4118 | |
| 4119 | if (_Pickler_SetOutputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4120 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4121 | |
| 4122 | /* memo and output_buffer may have already been created in _Pickler_New */ |
| 4123 | if (self->memo == NULL) { |
| 4124 | self->memo = PyMemoTable_New(); |
| 4125 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4126 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4127 | } |
| 4128 | self->output_len = 0; |
| 4129 | if (self->output_buffer == NULL) { |
| 4130 | self->max_output_len = WRITE_BUF_SIZE; |
| 4131 | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
| 4132 | self->max_output_len); |
| 4133 | if (self->output_buffer == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4134 | return -1; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4135 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4136 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 4137 | self->fast = 0; |
| 4138 | self->fast_nesting = 0; |
| 4139 | self->fast_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4140 | self->pers_func = NULL; |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 4141 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
| 4142 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 4143 | &PyId_persistent_id); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4144 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4145 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4146 | } |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4147 | self->dispatch_table = NULL; |
| 4148 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
| 4149 | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
| 4150 | &PyId_dispatch_table); |
| 4151 | if (self->dispatch_table == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4152 | return -1; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4153 | } |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4154 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4155 | return 0; |
| 4156 | } |
| 4157 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4158 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4159 | /* Define a proxy object for the Pickler's internal memo object. This is to |
| 4160 | * avoid breaking code like: |
| 4161 | * pickler.memo.clear() |
| 4162 | * and |
| 4163 | * pickler.memo = saved_memo |
| 4164 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 4165 | * it. Note that we don't implement the entire mapping API here. This is |
| 4166 | * intentional, as these should be treated as black-box implementation details. |
| 4167 | */ |
| 4168 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4169 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4170 | _pickle.PicklerMemoProxy.clear |
| 4171 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4172 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4173 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4174 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4175 | static PyObject * |
| 4176 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4177 | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4178 | { |
| 4179 | if (self->pickler->memo) |
| 4180 | PyMemoTable_Clear(self->pickler->memo); |
| 4181 | Py_RETURN_NONE; |
| 4182 | } |
| 4183 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4184 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4185 | _pickle.PicklerMemoProxy.copy |
| 4186 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4187 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4188 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4189 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4190 | static PyObject * |
| 4191 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4192 | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4193 | { |
| 4194 | Py_ssize_t i; |
| 4195 | PyMemoTable *memo; |
| 4196 | PyObject *new_memo = PyDict_New(); |
| 4197 | if (new_memo == NULL) |
| 4198 | return NULL; |
| 4199 | |
| 4200 | memo = self->pickler->memo; |
| 4201 | for (i = 0; i < memo->mt_allocated; ++i) { |
| 4202 | PyMemoEntry entry = memo->mt_table[i]; |
| 4203 | if (entry.me_key != NULL) { |
| 4204 | int status; |
| 4205 | PyObject *key, *value; |
| 4206 | |
| 4207 | key = PyLong_FromVoidPtr(entry.me_key); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4208 | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4209 | |
| 4210 | if (key == NULL || value == NULL) { |
| 4211 | Py_XDECREF(key); |
| 4212 | Py_XDECREF(value); |
| 4213 | goto error; |
| 4214 | } |
| 4215 | status = PyDict_SetItem(new_memo, key, value); |
| 4216 | Py_DECREF(key); |
| 4217 | Py_DECREF(value); |
| 4218 | if (status < 0) |
| 4219 | goto error; |
| 4220 | } |
| 4221 | } |
| 4222 | return new_memo; |
| 4223 | |
| 4224 | error: |
| 4225 | Py_XDECREF(new_memo); |
| 4226 | return NULL; |
| 4227 | } |
| 4228 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4229 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4230 | _pickle.PicklerMemoProxy.__reduce__ |
| 4231 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4232 | Implement pickle support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 4233 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4234 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4235 | static PyObject * |
| 4236 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 4237 | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4238 | { |
| 4239 | PyObject *reduce_value, *dict_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 4240 | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4241 | if (contents == NULL) |
| 4242 | return NULL; |
| 4243 | |
| 4244 | reduce_value = PyTuple_New(2); |
| 4245 | if (reduce_value == NULL) { |
| 4246 | Py_DECREF(contents); |
| 4247 | return NULL; |
| 4248 | } |
| 4249 | dict_args = PyTuple_New(1); |
| 4250 | if (dict_args == NULL) { |
| 4251 | Py_DECREF(contents); |
| 4252 | Py_DECREF(reduce_value); |
| 4253 | return NULL; |
| 4254 | } |
| 4255 | PyTuple_SET_ITEM(dict_args, 0, contents); |
| 4256 | Py_INCREF((PyObject *)&PyDict_Type); |
| 4257 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 4258 | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
| 4259 | return reduce_value; |
| 4260 | } |
| 4261 | |
| 4262 | static PyMethodDef picklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4263 | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
| 4264 | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
| 4265 | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4266 | {NULL, NULL} /* sentinel */ |
| 4267 | }; |
| 4268 | |
| 4269 | static void |
| 4270 | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
| 4271 | { |
| 4272 | PyObject_GC_UnTrack(self); |
| 4273 | Py_XDECREF(self->pickler); |
| 4274 | PyObject_GC_Del((PyObject *)self); |
| 4275 | } |
| 4276 | |
| 4277 | static int |
| 4278 | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
| 4279 | visitproc visit, void *arg) |
| 4280 | { |
| 4281 | Py_VISIT(self->pickler); |
| 4282 | return 0; |
| 4283 | } |
| 4284 | |
| 4285 | static int |
| 4286 | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
| 4287 | { |
| 4288 | Py_CLEAR(self->pickler); |
| 4289 | return 0; |
| 4290 | } |
| 4291 | |
| 4292 | static PyTypeObject PicklerMemoProxyType = { |
| 4293 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4294 | "_pickle.PicklerMemoProxy", /*tp_name*/ |
| 4295 | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
| 4296 | 0, |
| 4297 | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 4298 | 0, /* tp_print */ |
| 4299 | 0, /* tp_getattr */ |
| 4300 | 0, /* tp_setattr */ |
| 4301 | 0, /* tp_compare */ |
| 4302 | 0, /* tp_repr */ |
| 4303 | 0, /* tp_as_number */ |
| 4304 | 0, /* tp_as_sequence */ |
| 4305 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 4306 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4307 | 0, /* tp_call */ |
| 4308 | 0, /* tp_str */ |
| 4309 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 4310 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 4311 | 0, /* tp_as_buffer */ |
| 4312 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 4313 | 0, /* tp_doc */ |
| 4314 | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
| 4315 | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
| 4316 | 0, /* tp_richcompare */ |
| 4317 | 0, /* tp_weaklistoffset */ |
| 4318 | 0, /* tp_iter */ |
| 4319 | 0, /* tp_iternext */ |
| 4320 | picklerproxy_methods, /* tp_methods */ |
| 4321 | }; |
| 4322 | |
| 4323 | static PyObject * |
| 4324 | PicklerMemoProxy_New(PicklerObject *pickler) |
| 4325 | { |
| 4326 | PicklerMemoProxyObject *self; |
| 4327 | |
| 4328 | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
| 4329 | if (self == NULL) |
| 4330 | return NULL; |
| 4331 | Py_INCREF(pickler); |
| 4332 | self->pickler = pickler; |
| 4333 | PyObject_GC_Track(self); |
| 4334 | return (PyObject *)self; |
| 4335 | } |
| 4336 | |
| 4337 | /*****************************************************************************/ |
| 4338 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4339 | static PyObject * |
| 4340 | Pickler_get_memo(PicklerObject *self) |
| 4341 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4342 | return PicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4346 | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4347 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4348 | PyMemoTable *new_memo = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4349 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4350 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4351 | PyErr_SetString(PyExc_TypeError, |
| 4352 | "attribute deletion is not supported"); |
| 4353 | return -1; |
| 4354 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4355 | |
| 4356 | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
| 4357 | PicklerObject *pickler = |
| 4358 | ((PicklerMemoProxyObject *)obj)->pickler; |
| 4359 | |
| 4360 | new_memo = PyMemoTable_Copy(pickler->memo); |
| 4361 | if (new_memo == NULL) |
| 4362 | return -1; |
| 4363 | } |
| 4364 | else if (PyDict_Check(obj)) { |
| 4365 | Py_ssize_t i = 0; |
| 4366 | PyObject *key, *value; |
| 4367 | |
| 4368 | new_memo = PyMemoTable_New(); |
| 4369 | if (new_memo == NULL) |
| 4370 | return -1; |
| 4371 | |
| 4372 | while (PyDict_Next(obj, &i, &key, &value)) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4373 | Py_ssize_t memo_id; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4374 | PyObject *memo_obj; |
| 4375 | |
| 4376 | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
| 4377 | PyErr_SetString(PyExc_TypeError, |
| 4378 | "'memo' values must be 2-item tuples"); |
| 4379 | goto error; |
| 4380 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4381 | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4382 | if (memo_id == -1 && PyErr_Occurred()) |
| 4383 | goto error; |
| 4384 | memo_obj = PyTuple_GET_ITEM(value, 1); |
| 4385 | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
| 4386 | goto error; |
| 4387 | } |
| 4388 | } |
| 4389 | else { |
| 4390 | PyErr_Format(PyExc_TypeError, |
| 4391 | "'memo' attribute must be an PicklerMemoProxy object" |
| 4392 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4393 | return -1; |
| 4394 | } |
| 4395 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4396 | PyMemoTable_Del(self->memo); |
| 4397 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4398 | |
| 4399 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4400 | |
| 4401 | error: |
| 4402 | if (new_memo) |
| 4403 | PyMemoTable_Del(new_memo); |
| 4404 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4405 | } |
| 4406 | |
| 4407 | static PyObject * |
| 4408 | Pickler_get_persid(PicklerObject *self) |
| 4409 | { |
| 4410 | if (self->pers_func == NULL) |
| 4411 | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
| 4412 | else |
| 4413 | Py_INCREF(self->pers_func); |
| 4414 | return self->pers_func; |
| 4415 | } |
| 4416 | |
| 4417 | static int |
| 4418 | Pickler_set_persid(PicklerObject *self, PyObject *value) |
| 4419 | { |
| 4420 | PyObject *tmp; |
| 4421 | |
| 4422 | if (value == NULL) { |
| 4423 | PyErr_SetString(PyExc_TypeError, |
| 4424 | "attribute deletion is not supported"); |
| 4425 | return -1; |
| 4426 | } |
| 4427 | if (!PyCallable_Check(value)) { |
| 4428 | PyErr_SetString(PyExc_TypeError, |
| 4429 | "persistent_id must be a callable taking one argument"); |
| 4430 | return -1; |
| 4431 | } |
| 4432 | |
| 4433 | tmp = self->pers_func; |
| 4434 | Py_INCREF(value); |
| 4435 | self->pers_func = value; |
| 4436 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 4437 | |
| 4438 | return 0; |
| 4439 | } |
| 4440 | |
| 4441 | static PyMemberDef Pickler_members[] = { |
| 4442 | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
| 4443 | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
Antoine Pitrou | 8d3c290 | 2012-03-04 18:31:48 +0100 | [diff] [blame] | 4444 | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4445 | {NULL} |
| 4446 | }; |
| 4447 | |
| 4448 | static PyGetSetDef Pickler_getsets[] = { |
| 4449 | {"memo", (getter)Pickler_get_memo, |
| 4450 | (setter)Pickler_set_memo}, |
| 4451 | {"persistent_id", (getter)Pickler_get_persid, |
| 4452 | (setter)Pickler_set_persid}, |
| 4453 | {NULL} |
| 4454 | }; |
| 4455 | |
| 4456 | static PyTypeObject Pickler_Type = { |
| 4457 | PyVarObject_HEAD_INIT(NULL, 0) |
| 4458 | "_pickle.Pickler" , /*tp_name*/ |
| 4459 | sizeof(PicklerObject), /*tp_basicsize*/ |
| 4460 | 0, /*tp_itemsize*/ |
| 4461 | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
| 4462 | 0, /*tp_print*/ |
| 4463 | 0, /*tp_getattr*/ |
| 4464 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 4465 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4466 | 0, /*tp_repr*/ |
| 4467 | 0, /*tp_as_number*/ |
| 4468 | 0, /*tp_as_sequence*/ |
| 4469 | 0, /*tp_as_mapping*/ |
| 4470 | 0, /*tp_hash*/ |
| 4471 | 0, /*tp_call*/ |
| 4472 | 0, /*tp_str*/ |
| 4473 | 0, /*tp_getattro*/ |
| 4474 | 0, /*tp_setattro*/ |
| 4475 | 0, /*tp_as_buffer*/ |
| 4476 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 4477 | _pickle_Pickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4478 | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
| 4479 | (inquiry)Pickler_clear, /*tp_clear*/ |
| 4480 | 0, /*tp_richcompare*/ |
| 4481 | 0, /*tp_weaklistoffset*/ |
| 4482 | 0, /*tp_iter*/ |
| 4483 | 0, /*tp_iternext*/ |
| 4484 | Pickler_methods, /*tp_methods*/ |
| 4485 | Pickler_members, /*tp_members*/ |
| 4486 | Pickler_getsets, /*tp_getset*/ |
| 4487 | 0, /*tp_base*/ |
| 4488 | 0, /*tp_dict*/ |
| 4489 | 0, /*tp_descr_get*/ |
| 4490 | 0, /*tp_descr_set*/ |
| 4491 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 4492 | _pickle_Pickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4493 | PyType_GenericAlloc, /*tp_alloc*/ |
| 4494 | PyType_GenericNew, /*tp_new*/ |
| 4495 | PyObject_GC_Del, /*tp_free*/ |
| 4496 | 0, /*tp_is_gc*/ |
| 4497 | }; |
| 4498 | |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4499 | /* Temporary helper for calling self.find_class(). |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4500 | |
| 4501 | XXX: It would be nice to able to avoid Python function call overhead, by |
| 4502 | using directly the C version of find_class(), when find_class() is not |
| 4503 | overridden by a subclass. Although, this could become rather hackish. A |
| 4504 | simpler optimization would be to call the C function when self is not a |
| 4505 | subclass instance. */ |
| 4506 | static PyObject * |
| 4507 | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
| 4508 | { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 4509 | _Py_IDENTIFIER(find_class); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 4510 | |
| 4511 | return _PyObject_CallMethodId((PyObject *)self, &PyId_find_class, "OO", |
| 4512 | module_name, global_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4513 | } |
| 4514 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4515 | static Py_ssize_t |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4516 | marker(UnpicklerObject *self) |
| 4517 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4518 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4519 | if (self->num_marks < 1) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4520 | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4521 | return -1; |
| 4522 | } |
| 4523 | |
| 4524 | return self->marks[--self->num_marks]; |
| 4525 | } |
| 4526 | |
| 4527 | static int |
| 4528 | load_none(UnpicklerObject *self) |
| 4529 | { |
| 4530 | PDATA_APPEND(self->stack, Py_None, -1); |
| 4531 | return 0; |
| 4532 | } |
| 4533 | |
| 4534 | static int |
| 4535 | bad_readline(void) |
| 4536 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4537 | PickleState *st = _Pickle_GetGlobalState(); |
| 4538 | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4539 | return -1; |
| 4540 | } |
| 4541 | |
| 4542 | static int |
| 4543 | load_int(UnpicklerObject *self) |
| 4544 | { |
| 4545 | PyObject *value; |
| 4546 | char *endptr, *s; |
| 4547 | Py_ssize_t len; |
| 4548 | long x; |
| 4549 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4550 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4551 | return -1; |
| 4552 | if (len < 2) |
| 4553 | return bad_readline(); |
| 4554 | |
| 4555 | errno = 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 4556 | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4557 | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4558 | x = strtol(s, &endptr, 0); |
| 4559 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4560 | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4561 | /* Hm, maybe we've got something long. Let's try reading |
Serhiy Storchaka | 9594942 | 2013-08-27 19:40:23 +0300 | [diff] [blame] | 4562 | * it as a Python int object. */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4563 | errno = 0; |
| 4564 | /* XXX: Same thing about the base here. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4565 | value = PyLong_FromString(s, NULL, 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4566 | if (value == NULL) { |
| 4567 | PyErr_SetString(PyExc_ValueError, |
| 4568 | "could not convert string to int"); |
| 4569 | return -1; |
| 4570 | } |
| 4571 | } |
| 4572 | else { |
| 4573 | if (len == 3 && (x == 0 || x == 1)) { |
| 4574 | if ((value = PyBool_FromLong(x)) == NULL) |
| 4575 | return -1; |
| 4576 | } |
| 4577 | else { |
| 4578 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4579 | return -1; |
| 4580 | } |
| 4581 | } |
| 4582 | |
| 4583 | PDATA_PUSH(self->stack, value, -1); |
| 4584 | return 0; |
| 4585 | } |
| 4586 | |
| 4587 | static int |
| 4588 | load_bool(UnpicklerObject *self, PyObject *boolean) |
| 4589 | { |
| 4590 | assert(boolean == Py_True || boolean == Py_False); |
| 4591 | PDATA_APPEND(self->stack, boolean, -1); |
| 4592 | return 0; |
| 4593 | } |
| 4594 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4595 | /* s contains x bytes of an unsigned little-endian integer. Return its value |
| 4596 | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
| 4597 | */ |
| 4598 | static Py_ssize_t |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4599 | calc_binsize(char *bytes, int nbytes) |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4600 | { |
| 4601 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4602 | Py_ssize_t i; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4603 | size_t x = 0; |
| 4604 | |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4605 | for (i = 0; i < nbytes && (size_t)i < sizeof(size_t); i++) { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4606 | x |= (size_t) s[i] << (8 * i); |
| 4607 | } |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4608 | |
| 4609 | if (x > PY_SSIZE_T_MAX) |
| 4610 | return -1; |
| 4611 | else |
| 4612 | return (Py_ssize_t) x; |
| 4613 | } |
| 4614 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4615 | /* s contains x bytes of a little-endian integer. Return its value as a |
| 4616 | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
| 4617 | * int, but when x is 4 it's a signed one. This is an historical source |
| 4618 | * of x-platform bugs. |
| 4619 | */ |
| 4620 | static long |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4621 | calc_binint(char *bytes, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4622 | { |
| 4623 | unsigned char *s = (unsigned char *)bytes; |
Victor Stinner | f13c46c | 2014-08-17 21:05:55 +0200 | [diff] [blame] | 4624 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4625 | long x = 0; |
| 4626 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4627 | for (i = 0; i < nbytes; i++) { |
| 4628 | x |= (long)s[i] << (8 * i); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4629 | } |
| 4630 | |
| 4631 | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
| 4632 | * is signed, so on a box with longs bigger than 4 bytes we need |
| 4633 | * to extend a BININT's sign bit to the full width. |
| 4634 | */ |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4635 | if (SIZEOF_LONG > 4 && nbytes == 4) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4636 | x |= -(x & (1L << 31)); |
| 4637 | } |
| 4638 | |
| 4639 | return x; |
| 4640 | } |
| 4641 | |
| 4642 | static int |
| 4643 | load_binintx(UnpicklerObject *self, char *s, int size) |
| 4644 | { |
| 4645 | PyObject *value; |
| 4646 | long x; |
| 4647 | |
| 4648 | x = calc_binint(s, size); |
| 4649 | |
| 4650 | if ((value = PyLong_FromLong(x)) == NULL) |
| 4651 | return -1; |
| 4652 | |
| 4653 | PDATA_PUSH(self->stack, value, -1); |
| 4654 | return 0; |
| 4655 | } |
| 4656 | |
| 4657 | static int |
| 4658 | load_binint(UnpicklerObject *self) |
| 4659 | { |
| 4660 | char *s; |
| 4661 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4662 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4663 | return -1; |
| 4664 | |
| 4665 | return load_binintx(self, s, 4); |
| 4666 | } |
| 4667 | |
| 4668 | static int |
| 4669 | load_binint1(UnpicklerObject *self) |
| 4670 | { |
| 4671 | char *s; |
| 4672 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4673 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4674 | return -1; |
| 4675 | |
| 4676 | return load_binintx(self, s, 1); |
| 4677 | } |
| 4678 | |
| 4679 | static int |
| 4680 | load_binint2(UnpicklerObject *self) |
| 4681 | { |
| 4682 | char *s; |
| 4683 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4684 | if (_Unpickler_Read(self, &s, 2) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4685 | return -1; |
| 4686 | |
| 4687 | return load_binintx(self, s, 2); |
| 4688 | } |
| 4689 | |
| 4690 | static int |
| 4691 | load_long(UnpicklerObject *self) |
| 4692 | { |
| 4693 | PyObject *value; |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4694 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4695 | Py_ssize_t len; |
| 4696 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4697 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4698 | return -1; |
| 4699 | if (len < 2) |
| 4700 | return bad_readline(); |
| 4701 | |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4702 | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
| 4703 | the 'L' before calling PyLong_FromString. In order to maintain |
| 4704 | compatibility with Python 3.0.0, we don't actually *require* |
| 4705 | the 'L' to be present. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4706 | if (s[len-2] == 'L') |
Alexandre Vassalotti | 446f7ff | 2009-01-23 04:43:46 +0000 | [diff] [blame] | 4707 | s[len-2] = '\0'; |
Alexandre Vassalotti | e4bccb7 | 2009-01-24 01:47:57 +0000 | [diff] [blame] | 4708 | /* XXX: Should the base argument explicitly set to 10? */ |
| 4709 | value = PyLong_FromString(s, NULL, 0); |
Mark Dickinson | 8dd0514 | 2009-01-20 20:43:58 +0000 | [diff] [blame] | 4710 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4711 | return -1; |
| 4712 | |
| 4713 | PDATA_PUSH(self->stack, value, -1); |
| 4714 | return 0; |
| 4715 | } |
| 4716 | |
| 4717 | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
| 4718 | * data following. |
| 4719 | */ |
| 4720 | static int |
| 4721 | load_counted_long(UnpicklerObject *self, int size) |
| 4722 | { |
| 4723 | PyObject *value; |
| 4724 | char *nbytes; |
| 4725 | char *pdata; |
| 4726 | |
| 4727 | assert(size == 1 || size == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4728 | if (_Unpickler_Read(self, &nbytes, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4729 | return -1; |
| 4730 | |
| 4731 | size = calc_binint(nbytes, size); |
| 4732 | if (size < 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4733 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4734 | /* Corrupt or hostile pickle -- we never write one like this */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4735 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4736 | "LONG pickle has negative byte count"); |
| 4737 | return -1; |
| 4738 | } |
| 4739 | |
| 4740 | if (size == 0) |
| 4741 | value = PyLong_FromLong(0L); |
| 4742 | else { |
| 4743 | /* Read the raw little-endian bytes and convert. */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4744 | if (_Unpickler_Read(self, &pdata, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4745 | return -1; |
| 4746 | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
| 4747 | 1 /* little endian */ , 1 /* signed */ ); |
| 4748 | } |
| 4749 | if (value == NULL) |
| 4750 | return -1; |
| 4751 | PDATA_PUSH(self->stack, value, -1); |
| 4752 | return 0; |
| 4753 | } |
| 4754 | |
| 4755 | static int |
| 4756 | load_float(UnpicklerObject *self) |
| 4757 | { |
| 4758 | PyObject *value; |
| 4759 | char *endptr, *s; |
| 4760 | Py_ssize_t len; |
| 4761 | double d; |
| 4762 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4763 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4764 | return -1; |
| 4765 | if (len < 2) |
| 4766 | return bad_readline(); |
| 4767 | |
| 4768 | errno = 0; |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4769 | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
| 4770 | if (d == -1.0 && PyErr_Occurred()) |
| 4771 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4772 | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4773 | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
| 4774 | return -1; |
| 4775 | } |
Mark Dickinson | 725bfd8 | 2009-05-03 20:33:40 +0000 | [diff] [blame] | 4776 | value = PyFloat_FromDouble(d); |
| 4777 | if (value == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4778 | return -1; |
| 4779 | |
| 4780 | PDATA_PUSH(self->stack, value, -1); |
| 4781 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4782 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4783 | |
| 4784 | static int |
| 4785 | load_binfloat(UnpicklerObject *self) |
| 4786 | { |
| 4787 | PyObject *value; |
| 4788 | double x; |
| 4789 | char *s; |
| 4790 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4791 | if (_Unpickler_Read(self, &s, 8) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4792 | return -1; |
| 4793 | |
| 4794 | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
| 4795 | if (x == -1.0 && PyErr_Occurred()) |
| 4796 | return -1; |
| 4797 | |
| 4798 | if ((value = PyFloat_FromDouble(x)) == NULL) |
| 4799 | return -1; |
| 4800 | |
| 4801 | PDATA_PUSH(self->stack, value, -1); |
| 4802 | return 0; |
| 4803 | } |
| 4804 | |
| 4805 | static int |
| 4806 | load_string(UnpicklerObject *self) |
| 4807 | { |
| 4808 | PyObject *bytes; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4809 | PyObject *obj; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4810 | Py_ssize_t len; |
| 4811 | char *s, *p; |
| 4812 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4813 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4814 | return -1; |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4815 | /* Strip the newline */ |
| 4816 | len--; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4817 | /* Strip outermost quotes */ |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4818 | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4819 | p = s + 1; |
| 4820 | len -= 2; |
| 4821 | } |
| 4822 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 4823 | PickleState *st = _Pickle_GetGlobalState(); |
| 4824 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4825 | "the STRING opcode argument must be quoted"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4826 | return -1; |
| 4827 | } |
Alexandre Vassalotti | 7c5e094 | 2013-04-15 23:14:55 -0700 | [diff] [blame] | 4828 | assert(len >= 0); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4829 | |
| 4830 | /* Use the PyBytes API to decode the string, since that is what is used |
| 4831 | to encode, and then coerce the result to Unicode. */ |
| 4832 | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4833 | if (bytes == NULL) |
| 4834 | return -1; |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4835 | |
| 4836 | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
| 4837 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4838 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4839 | obj = bytes; |
| 4840 | } |
| 4841 | else { |
| 4842 | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
| 4843 | Py_DECREF(bytes); |
| 4844 | if (obj == NULL) { |
| 4845 | return -1; |
| 4846 | } |
| 4847 | } |
| 4848 | |
| 4849 | PDATA_PUSH(self->stack, obj, -1); |
| 4850 | return 0; |
| 4851 | } |
| 4852 | |
| 4853 | static int |
| 4854 | load_counted_binstring(UnpicklerObject *self, int nbytes) |
| 4855 | { |
| 4856 | PyObject *obj; |
| 4857 | Py_ssize_t size; |
| 4858 | char *s; |
| 4859 | |
| 4860 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4861 | return -1; |
| 4862 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 4863 | size = calc_binsize(s, nbytes); |
| 4864 | if (size < 0) { |
| 4865 | PickleState *st = _Pickle_GetGlobalState(); |
| 4866 | PyErr_Format(st->UnpicklingError, |
| 4867 | "BINSTRING exceeds system's maximum size of %zd bytes", |
| 4868 | PY_SSIZE_T_MAX); |
| 4869 | return -1; |
| 4870 | } |
| 4871 | |
| 4872 | if (_Unpickler_Read(self, &s, size) < 0) |
| 4873 | return -1; |
| 4874 | |
| 4875 | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
| 4876 | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
| 4877 | if (strcmp(self->encoding, "bytes") == 0) { |
| 4878 | obj = PyBytes_FromStringAndSize(s, size); |
| 4879 | } |
| 4880 | else { |
| 4881 | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
| 4882 | } |
| 4883 | if (obj == NULL) { |
| 4884 | return -1; |
| 4885 | } |
| 4886 | |
| 4887 | PDATA_PUSH(self->stack, obj, -1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4888 | return 0; |
| 4889 | } |
| 4890 | |
| 4891 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4892 | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4893 | { |
| 4894 | PyObject *bytes; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4895 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4896 | char *s; |
| 4897 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4898 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4899 | return -1; |
| 4900 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4901 | size = calc_binsize(s, nbytes); |
| 4902 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4903 | PyErr_Format(PyExc_OverflowError, |
| 4904 | "BINBYTES exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4905 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4906 | return -1; |
| 4907 | } |
| 4908 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4909 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4910 | return -1; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4911 | |
| 4912 | bytes = PyBytes_FromStringAndSize(s, size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4913 | if (bytes == NULL) |
| 4914 | return -1; |
| 4915 | |
| 4916 | PDATA_PUSH(self->stack, bytes, -1); |
| 4917 | return 0; |
| 4918 | } |
| 4919 | |
| 4920 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4921 | load_unicode(UnpicklerObject *self) |
| 4922 | { |
| 4923 | PyObject *str; |
| 4924 | Py_ssize_t len; |
| 4925 | char *s; |
| 4926 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4927 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4928 | return -1; |
| 4929 | if (len < 1) |
| 4930 | return bad_readline(); |
| 4931 | |
| 4932 | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
| 4933 | if (str == NULL) |
| 4934 | return -1; |
| 4935 | |
| 4936 | PDATA_PUSH(self->stack, str, -1); |
| 4937 | return 0; |
| 4938 | } |
| 4939 | |
| 4940 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4941 | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4942 | { |
| 4943 | PyObject *str; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4944 | Py_ssize_t size; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4945 | char *s; |
| 4946 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4947 | if (_Unpickler_Read(self, &s, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4948 | return -1; |
| 4949 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 4950 | size = calc_binsize(s, nbytes); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4951 | if (size < 0) { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4952 | PyErr_Format(PyExc_OverflowError, |
| 4953 | "BINUNICODE exceeds system's maximum size of %zd bytes", |
Alexandre Vassalotti | cc75717 | 2013-04-14 02:25:10 -0700 | [diff] [blame] | 4954 | PY_SSIZE_T_MAX); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4955 | return -1; |
| 4956 | } |
| 4957 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 4958 | if (_Unpickler_Read(self, &s, size) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4959 | return -1; |
| 4960 | |
Victor Stinner | 485fb56 | 2010-04-13 11:07:24 +0000 | [diff] [blame] | 4961 | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4962 | if (str == NULL) |
| 4963 | return -1; |
| 4964 | |
| 4965 | PDATA_PUSH(self->stack, str, -1); |
| 4966 | return 0; |
| 4967 | } |
| 4968 | |
| 4969 | static int |
| 4970 | load_tuple(UnpicklerObject *self) |
| 4971 | { |
| 4972 | PyObject *tuple; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 4973 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 4974 | |
| 4975 | if ((i = marker(self)) < 0) |
| 4976 | return -1; |
| 4977 | |
| 4978 | tuple = Pdata_poptuple(self->stack, i); |
| 4979 | if (tuple == NULL) |
| 4980 | return -1; |
| 4981 | PDATA_PUSH(self->stack, tuple, -1); |
| 4982 | return 0; |
| 4983 | } |
| 4984 | |
| 4985 | static int |
| 4986 | load_counted_tuple(UnpicklerObject *self, int len) |
| 4987 | { |
| 4988 | PyObject *tuple; |
| 4989 | |
| 4990 | tuple = PyTuple_New(len); |
| 4991 | if (tuple == NULL) |
| 4992 | return -1; |
| 4993 | |
| 4994 | while (--len >= 0) { |
| 4995 | PyObject *item; |
| 4996 | |
| 4997 | PDATA_POP(self->stack, item); |
| 4998 | if (item == NULL) |
| 4999 | return -1; |
| 5000 | PyTuple_SET_ITEM(tuple, len, item); |
| 5001 | } |
| 5002 | PDATA_PUSH(self->stack, tuple, -1); |
| 5003 | return 0; |
| 5004 | } |
| 5005 | |
| 5006 | static int |
| 5007 | load_empty_list(UnpicklerObject *self) |
| 5008 | { |
| 5009 | PyObject *list; |
| 5010 | |
| 5011 | if ((list = PyList_New(0)) == NULL) |
| 5012 | return -1; |
| 5013 | PDATA_PUSH(self->stack, list, -1); |
| 5014 | return 0; |
| 5015 | } |
| 5016 | |
| 5017 | static int |
| 5018 | load_empty_dict(UnpicklerObject *self) |
| 5019 | { |
| 5020 | PyObject *dict; |
| 5021 | |
| 5022 | if ((dict = PyDict_New()) == NULL) |
| 5023 | return -1; |
| 5024 | PDATA_PUSH(self->stack, dict, -1); |
| 5025 | return 0; |
| 5026 | } |
| 5027 | |
| 5028 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5029 | load_empty_set(UnpicklerObject *self) |
| 5030 | { |
| 5031 | PyObject *set; |
| 5032 | |
| 5033 | if ((set = PySet_New(NULL)) == NULL) |
| 5034 | return -1; |
| 5035 | PDATA_PUSH(self->stack, set, -1); |
| 5036 | return 0; |
| 5037 | } |
| 5038 | |
| 5039 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5040 | load_list(UnpicklerObject *self) |
| 5041 | { |
| 5042 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5043 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5044 | |
| 5045 | if ((i = marker(self)) < 0) |
| 5046 | return -1; |
| 5047 | |
| 5048 | list = Pdata_poplist(self->stack, i); |
| 5049 | if (list == NULL) |
| 5050 | return -1; |
| 5051 | PDATA_PUSH(self->stack, list, -1); |
| 5052 | return 0; |
| 5053 | } |
| 5054 | |
| 5055 | static int |
| 5056 | load_dict(UnpicklerObject *self) |
| 5057 | { |
| 5058 | PyObject *dict, *key, *value; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5059 | Py_ssize_t i, j, k; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5060 | |
| 5061 | if ((i = marker(self)) < 0) |
| 5062 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5063 | j = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5064 | |
| 5065 | if ((dict = PyDict_New()) == NULL) |
| 5066 | return -1; |
| 5067 | |
| 5068 | for (k = i + 1; k < j; k += 2) { |
| 5069 | key = self->stack->data[k - 1]; |
| 5070 | value = self->stack->data[k]; |
| 5071 | if (PyDict_SetItem(dict, key, value) < 0) { |
| 5072 | Py_DECREF(dict); |
| 5073 | return -1; |
| 5074 | } |
| 5075 | } |
| 5076 | Pdata_clear(self->stack, i); |
| 5077 | PDATA_PUSH(self->stack, dict, -1); |
| 5078 | return 0; |
| 5079 | } |
| 5080 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5081 | static int |
| 5082 | load_frozenset(UnpicklerObject *self) |
| 5083 | { |
| 5084 | PyObject *items; |
| 5085 | PyObject *frozenset; |
| 5086 | Py_ssize_t i; |
| 5087 | |
| 5088 | if ((i = marker(self)) < 0) |
| 5089 | return -1; |
| 5090 | |
| 5091 | items = Pdata_poptuple(self->stack, i); |
| 5092 | if (items == NULL) |
| 5093 | return -1; |
| 5094 | |
| 5095 | frozenset = PyFrozenSet_New(items); |
| 5096 | Py_DECREF(items); |
| 5097 | if (frozenset == NULL) |
| 5098 | return -1; |
| 5099 | |
| 5100 | PDATA_PUSH(self->stack, frozenset, -1); |
| 5101 | return 0; |
| 5102 | } |
| 5103 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5104 | static PyObject * |
| 5105 | instantiate(PyObject *cls, PyObject *args) |
| 5106 | { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5107 | PyObject *result = NULL; |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5108 | _Py_IDENTIFIER(__getinitargs__); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5109 | /* Caller must assure args are a tuple. Normally, args come from |
| 5110 | Pdata_poptuple which packs objects from the top of the stack |
| 5111 | into a newly created tuple. */ |
| 5112 | assert(PyTuple_Check(args)); |
| 5113 | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 5114 | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5115 | result = PyObject_CallObject(cls, args); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5116 | } |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5117 | else { |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5118 | _Py_IDENTIFIER(__new__); |
Martin v. Löwis | afe55bb | 2011-10-09 10:38:36 +0200 | [diff] [blame] | 5119 | |
| 5120 | result = _PyObject_CallMethodId(cls, &PyId___new__, "O", cls); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5121 | } |
| 5122 | return result; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5123 | } |
| 5124 | |
| 5125 | static int |
| 5126 | load_obj(UnpicklerObject *self) |
| 5127 | { |
| 5128 | PyObject *cls, *args, *obj = NULL; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5129 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5130 | |
| 5131 | if ((i = marker(self)) < 0) |
| 5132 | return -1; |
| 5133 | |
| 5134 | args = Pdata_poptuple(self->stack, i + 1); |
| 5135 | if (args == NULL) |
| 5136 | return -1; |
| 5137 | |
| 5138 | PDATA_POP(self->stack, cls); |
| 5139 | if (cls) { |
| 5140 | obj = instantiate(cls, args); |
| 5141 | Py_DECREF(cls); |
| 5142 | } |
| 5143 | Py_DECREF(args); |
| 5144 | if (obj == NULL) |
| 5145 | return -1; |
| 5146 | |
| 5147 | PDATA_PUSH(self->stack, obj, -1); |
| 5148 | return 0; |
| 5149 | } |
| 5150 | |
| 5151 | static int |
| 5152 | load_inst(UnpicklerObject *self) |
| 5153 | { |
| 5154 | PyObject *cls = NULL; |
| 5155 | PyObject *args = NULL; |
| 5156 | PyObject *obj = NULL; |
| 5157 | PyObject *module_name; |
| 5158 | PyObject *class_name; |
| 5159 | Py_ssize_t len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5160 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5161 | char *s; |
| 5162 | |
| 5163 | if ((i = marker(self)) < 0) |
| 5164 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5165 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5166 | return -1; |
| 5167 | if (len < 2) |
| 5168 | return bad_readline(); |
| 5169 | |
| 5170 | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
| 5171 | identifiers are permitted in Python 3.0, since the INST opcode is only |
| 5172 | supported by older protocols on Python 2.x. */ |
| 5173 | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
| 5174 | if (module_name == NULL) |
| 5175 | return -1; |
| 5176 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5177 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5178 | if (len < 2) |
| 5179 | return bad_readline(); |
| 5180 | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 5181 | if (class_name != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5182 | cls = find_class(self, module_name, class_name); |
| 5183 | Py_DECREF(class_name); |
| 5184 | } |
| 5185 | } |
| 5186 | Py_DECREF(module_name); |
| 5187 | |
| 5188 | if (cls == NULL) |
| 5189 | return -1; |
| 5190 | |
| 5191 | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
| 5192 | obj = instantiate(cls, args); |
| 5193 | Py_DECREF(args); |
| 5194 | } |
| 5195 | Py_DECREF(cls); |
| 5196 | |
| 5197 | if (obj == NULL) |
| 5198 | return -1; |
| 5199 | |
| 5200 | PDATA_PUSH(self->stack, obj, -1); |
| 5201 | return 0; |
| 5202 | } |
| 5203 | |
| 5204 | static int |
| 5205 | load_newobj(UnpicklerObject *self) |
| 5206 | { |
| 5207 | PyObject *args = NULL; |
| 5208 | PyObject *clsraw = NULL; |
| 5209 | PyTypeObject *cls; /* clsraw cast to its true type */ |
| 5210 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5211 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5212 | |
| 5213 | /* Stack is ... cls argtuple, and we want to call |
| 5214 | * cls.__new__(cls, *argtuple). |
| 5215 | */ |
| 5216 | PDATA_POP(self->stack, args); |
| 5217 | if (args == NULL) |
| 5218 | goto error; |
| 5219 | if (!PyTuple_Check(args)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5220 | PyErr_SetString(st->UnpicklingError, |
| 5221 | "NEWOBJ expected an arg " "tuple."); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5222 | goto error; |
| 5223 | } |
| 5224 | |
| 5225 | PDATA_POP(self->stack, clsraw); |
| 5226 | cls = (PyTypeObject *)clsraw; |
| 5227 | if (cls == NULL) |
| 5228 | goto error; |
| 5229 | if (!PyType_Check(cls)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5230 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5231 | "isn't a type object"); |
| 5232 | goto error; |
| 5233 | } |
| 5234 | if (cls->tp_new == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5235 | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5236 | "has NULL tp_new"); |
| 5237 | goto error; |
| 5238 | } |
| 5239 | |
| 5240 | /* Call __new__. */ |
| 5241 | obj = cls->tp_new(cls, args, NULL); |
| 5242 | if (obj == NULL) |
| 5243 | goto error; |
| 5244 | |
| 5245 | Py_DECREF(args); |
| 5246 | Py_DECREF(clsraw); |
| 5247 | PDATA_PUSH(self->stack, obj, -1); |
| 5248 | return 0; |
| 5249 | |
| 5250 | error: |
| 5251 | Py_XDECREF(args); |
| 5252 | Py_XDECREF(clsraw); |
| 5253 | return -1; |
| 5254 | } |
| 5255 | |
| 5256 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5257 | load_newobj_ex(UnpicklerObject *self) |
| 5258 | { |
| 5259 | PyObject *cls, *args, *kwargs; |
| 5260 | PyObject *obj; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5261 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5262 | |
| 5263 | PDATA_POP(self->stack, kwargs); |
| 5264 | if (kwargs == NULL) { |
| 5265 | return -1; |
| 5266 | } |
| 5267 | PDATA_POP(self->stack, args); |
| 5268 | if (args == NULL) { |
| 5269 | Py_DECREF(kwargs); |
| 5270 | return -1; |
| 5271 | } |
| 5272 | PDATA_POP(self->stack, cls); |
| 5273 | if (cls == NULL) { |
| 5274 | Py_DECREF(kwargs); |
| 5275 | Py_DECREF(args); |
| 5276 | return -1; |
| 5277 | } |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5278 | |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5279 | if (!PyType_Check(cls)) { |
| 5280 | Py_DECREF(kwargs); |
| 5281 | Py_DECREF(args); |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 5282 | PyErr_Format(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5283 | "NEWOBJ_EX class argument must be a type, not %.200s", |
| 5284 | Py_TYPE(cls)->tp_name); |
Benjamin Peterson | 80f78a3 | 2015-07-02 16:18:38 -0500 | [diff] [blame] | 5285 | Py_DECREF(cls); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5286 | return -1; |
| 5287 | } |
| 5288 | |
| 5289 | if (((PyTypeObject *)cls)->tp_new == NULL) { |
| 5290 | Py_DECREF(kwargs); |
| 5291 | Py_DECREF(args); |
| 5292 | Py_DECREF(cls); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5293 | PyErr_SetString(st->UnpicklingError, |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5294 | "NEWOBJ_EX class argument doesn't have __new__"); |
| 5295 | return -1; |
| 5296 | } |
| 5297 | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
| 5298 | Py_DECREF(kwargs); |
| 5299 | Py_DECREF(args); |
| 5300 | Py_DECREF(cls); |
| 5301 | if (obj == NULL) { |
| 5302 | return -1; |
| 5303 | } |
| 5304 | PDATA_PUSH(self->stack, obj, -1); |
| 5305 | return 0; |
| 5306 | } |
| 5307 | |
| 5308 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5309 | load_global(UnpicklerObject *self) |
| 5310 | { |
| 5311 | PyObject *global = NULL; |
| 5312 | PyObject *module_name; |
| 5313 | PyObject *global_name; |
| 5314 | Py_ssize_t len; |
| 5315 | char *s; |
| 5316 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5317 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5318 | return -1; |
| 5319 | if (len < 2) |
| 5320 | return bad_readline(); |
| 5321 | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5322 | if (!module_name) |
| 5323 | return -1; |
| 5324 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5325 | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5326 | if (len < 2) { |
| 5327 | Py_DECREF(module_name); |
| 5328 | return bad_readline(); |
| 5329 | } |
| 5330 | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
| 5331 | if (global_name) { |
| 5332 | global = find_class(self, module_name, global_name); |
| 5333 | Py_DECREF(global_name); |
| 5334 | } |
| 5335 | } |
| 5336 | Py_DECREF(module_name); |
| 5337 | |
| 5338 | if (global == NULL) |
| 5339 | return -1; |
| 5340 | PDATA_PUSH(self->stack, global, -1); |
| 5341 | return 0; |
| 5342 | } |
| 5343 | |
| 5344 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5345 | load_stack_global(UnpicklerObject *self) |
| 5346 | { |
| 5347 | PyObject *global; |
| 5348 | PyObject *module_name; |
| 5349 | PyObject *global_name; |
| 5350 | |
| 5351 | PDATA_POP(self->stack, global_name); |
| 5352 | PDATA_POP(self->stack, module_name); |
| 5353 | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
| 5354 | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5355 | PickleState *st = _Pickle_GetGlobalState(); |
| 5356 | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5357 | Py_XDECREF(global_name); |
| 5358 | Py_XDECREF(module_name); |
| 5359 | return -1; |
| 5360 | } |
| 5361 | global = find_class(self, module_name, global_name); |
| 5362 | Py_DECREF(global_name); |
| 5363 | Py_DECREF(module_name); |
| 5364 | if (global == NULL) |
| 5365 | return -1; |
| 5366 | PDATA_PUSH(self->stack, global, -1); |
| 5367 | return 0; |
| 5368 | } |
| 5369 | |
| 5370 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5371 | load_persid(UnpicklerObject *self) |
| 5372 | { |
| 5373 | PyObject *pid; |
| 5374 | Py_ssize_t len; |
| 5375 | char *s; |
| 5376 | |
| 5377 | if (self->pers_func) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5378 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5379 | return -1; |
Alexandre Vassalotti | 896414f | 2013-11-30 13:52:35 -0800 | [diff] [blame] | 5380 | if (len < 1) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5381 | return bad_readline(); |
| 5382 | |
| 5383 | pid = PyBytes_FromStringAndSize(s, len - 1); |
| 5384 | if (pid == NULL) |
| 5385 | return -1; |
| 5386 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5387 | /* This does not leak since _Pickle_FastCall() steals the reference |
| 5388 | to pid first. */ |
| 5389 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5390 | if (pid == NULL) |
| 5391 | return -1; |
| 5392 | |
| 5393 | PDATA_PUSH(self->stack, pid, -1); |
| 5394 | return 0; |
| 5395 | } |
| 5396 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5397 | PickleState *st = _Pickle_GetGlobalState(); |
| 5398 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5399 | "A load persistent id instruction was encountered,\n" |
| 5400 | "but no persistent_load function was specified."); |
| 5401 | return -1; |
| 5402 | } |
| 5403 | } |
| 5404 | |
| 5405 | static int |
| 5406 | load_binpersid(UnpicklerObject *self) |
| 5407 | { |
| 5408 | PyObject *pid; |
| 5409 | |
| 5410 | if (self->pers_func) { |
| 5411 | PDATA_POP(self->stack, pid); |
| 5412 | if (pid == NULL) |
| 5413 | return -1; |
| 5414 | |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5415 | /* This does not leak since _Pickle_FastCall() steals the |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5416 | reference to pid first. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5417 | pid = _Pickle_FastCall(self->pers_func, pid); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5418 | if (pid == NULL) |
| 5419 | return -1; |
| 5420 | |
| 5421 | PDATA_PUSH(self->stack, pid, -1); |
| 5422 | return 0; |
| 5423 | } |
| 5424 | else { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5425 | PickleState *st = _Pickle_GetGlobalState(); |
| 5426 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5427 | "A load persistent id instruction was encountered,\n" |
| 5428 | "but no persistent_load function was specified."); |
| 5429 | return -1; |
| 5430 | } |
| 5431 | } |
| 5432 | |
| 5433 | static int |
| 5434 | load_pop(UnpicklerObject *self) |
| 5435 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5436 | Py_ssize_t len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5437 | |
| 5438 | /* Note that we split the (pickle.py) stack into two stacks, |
| 5439 | * an object stack and a mark stack. We have to be clever and |
| 5440 | * 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] | 5441 | * mark stack first, and only signalling a stack underflow if |
| 5442 | * the object stack is empty and the mark stack doesn't match |
| 5443 | * our expectations. |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5444 | */ |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5445 | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5446 | self->num_marks--; |
Antoine Pitrou | 01a15ea | 2010-01-07 17:57:31 +0000 | [diff] [blame] | 5447 | } else if (len > 0) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5448 | len--; |
| 5449 | Py_DECREF(self->stack->data[len]); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5450 | Py_SIZE(self->stack) = len; |
Collin Winter | 8ca69de | 2009-05-26 16:53:41 +0000 | [diff] [blame] | 5451 | } else { |
| 5452 | return stack_underflow(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5453 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5454 | return 0; |
| 5455 | } |
| 5456 | |
| 5457 | static int |
| 5458 | load_pop_mark(UnpicklerObject *self) |
| 5459 | { |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5460 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5461 | |
| 5462 | if ((i = marker(self)) < 0) |
| 5463 | return -1; |
| 5464 | |
| 5465 | Pdata_clear(self->stack, i); |
| 5466 | |
| 5467 | return 0; |
| 5468 | } |
| 5469 | |
| 5470 | static int |
| 5471 | load_dup(UnpicklerObject *self) |
| 5472 | { |
| 5473 | PyObject *last; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5474 | Py_ssize_t len; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5475 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5476 | if ((len = Py_SIZE(self->stack)) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5477 | return stack_underflow(); |
| 5478 | last = self->stack->data[len - 1]; |
| 5479 | PDATA_APPEND(self->stack, last, -1); |
| 5480 | return 0; |
| 5481 | } |
| 5482 | |
| 5483 | static int |
| 5484 | load_get(UnpicklerObject *self) |
| 5485 | { |
| 5486 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5487 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5488 | Py_ssize_t len; |
| 5489 | char *s; |
| 5490 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5491 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5492 | return -1; |
| 5493 | if (len < 2) |
| 5494 | return bad_readline(); |
| 5495 | |
| 5496 | key = PyLong_FromString(s, NULL, 10); |
| 5497 | if (key == NULL) |
| 5498 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5499 | idx = PyLong_AsSsize_t(key); |
| 5500 | if (idx == -1 && PyErr_Occurred()) { |
| 5501 | Py_DECREF(key); |
| 5502 | return -1; |
| 5503 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5504 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5505 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5506 | if (value == NULL) { |
| 5507 | if (!PyErr_Occurred()) |
| 5508 | PyErr_SetObject(PyExc_KeyError, key); |
| 5509 | Py_DECREF(key); |
| 5510 | return -1; |
| 5511 | } |
| 5512 | Py_DECREF(key); |
| 5513 | |
| 5514 | PDATA_APPEND(self->stack, value, -1); |
| 5515 | return 0; |
| 5516 | } |
| 5517 | |
| 5518 | static int |
| 5519 | load_binget(UnpicklerObject *self) |
| 5520 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5521 | PyObject *value; |
| 5522 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5523 | char *s; |
| 5524 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5525 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5526 | return -1; |
| 5527 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5528 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5529 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5530 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5531 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5532 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5533 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5534 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5535 | Py_DECREF(key); |
| 5536 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5537 | return -1; |
| 5538 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5539 | |
| 5540 | PDATA_APPEND(self->stack, value, -1); |
| 5541 | return 0; |
| 5542 | } |
| 5543 | |
| 5544 | static int |
| 5545 | load_long_binget(UnpicklerObject *self) |
| 5546 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5547 | PyObject *value; |
| 5548 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5549 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5550 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5551 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5552 | return -1; |
| 5553 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5554 | idx = calc_binsize(s, 4); |
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 | value = _Unpickler_MemoGet(self, idx); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5557 | if (value == NULL) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5558 | PyObject *key = PyLong_FromSsize_t(idx); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5559 | if (key != NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5560 | PyErr_SetObject(PyExc_KeyError, key); |
Christian Heimes | 9ee5c37 | 2013-07-26 22:45:00 +0200 | [diff] [blame] | 5561 | Py_DECREF(key); |
| 5562 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5563 | return -1; |
| 5564 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5565 | |
| 5566 | PDATA_APPEND(self->stack, value, -1); |
| 5567 | return 0; |
| 5568 | } |
| 5569 | |
| 5570 | /* Push an object from the extension registry (EXT[124]). nbytes is |
| 5571 | * the number of bytes following the opcode, holding the index (code) value. |
| 5572 | */ |
| 5573 | static int |
| 5574 | load_extension(UnpicklerObject *self, int nbytes) |
| 5575 | { |
| 5576 | char *codebytes; /* the nbytes bytes after the opcode */ |
| 5577 | long code; /* calc_binint returns long */ |
| 5578 | PyObject *py_code; /* code as a Python int */ |
| 5579 | PyObject *obj; /* the object to push */ |
| 5580 | PyObject *pair; /* (module_name, class_name) */ |
| 5581 | PyObject *module_name, *class_name; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5582 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5583 | |
| 5584 | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5585 | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5586 | return -1; |
| 5587 | code = calc_binint(codebytes, nbytes); |
| 5588 | if (code <= 0) { /* note that 0 is forbidden */ |
| 5589 | /* Corrupt or hostile pickle. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5590 | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5591 | return -1; |
| 5592 | } |
| 5593 | |
| 5594 | /* Look for the code in the cache. */ |
| 5595 | py_code = PyLong_FromLong(code); |
| 5596 | if (py_code == NULL) |
| 5597 | return -1; |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5598 | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5599 | if (obj != NULL) { |
| 5600 | /* Bingo. */ |
| 5601 | Py_DECREF(py_code); |
| 5602 | PDATA_APPEND(self->stack, obj, -1); |
| 5603 | return 0; |
| 5604 | } |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5605 | if (PyErr_Occurred()) { |
| 5606 | Py_DECREF(py_code); |
| 5607 | return -1; |
| 5608 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5609 | |
| 5610 | /* Look up the (module_name, class_name) pair. */ |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5611 | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5612 | if (pair == NULL) { |
| 5613 | Py_DECREF(py_code); |
Alexandre Vassalotti | 567eba1 | 2013-11-28 17:09:16 -0800 | [diff] [blame] | 5614 | if (!PyErr_Occurred()) { |
| 5615 | PyErr_Format(PyExc_ValueError, "unregistered extension " |
| 5616 | "code %ld", code); |
| 5617 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5618 | return -1; |
| 5619 | } |
| 5620 | /* Since the extension registry is manipulable via Python code, |
| 5621 | * confirm that pair is really a 2-tuple of strings. |
| 5622 | */ |
| 5623 | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
| 5624 | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
| 5625 | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
| 5626 | Py_DECREF(py_code); |
| 5627 | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
| 5628 | "isn't a 2-tuple of strings", code); |
| 5629 | return -1; |
| 5630 | } |
| 5631 | /* Load the object. */ |
| 5632 | obj = find_class(self, module_name, class_name); |
| 5633 | if (obj == NULL) { |
| 5634 | Py_DECREF(py_code); |
| 5635 | return -1; |
| 5636 | } |
| 5637 | /* Cache code -> obj. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5638 | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5639 | Py_DECREF(py_code); |
| 5640 | if (code < 0) { |
| 5641 | Py_DECREF(obj); |
| 5642 | return -1; |
| 5643 | } |
| 5644 | PDATA_PUSH(self->stack, obj, -1); |
| 5645 | return 0; |
| 5646 | } |
| 5647 | |
| 5648 | static int |
| 5649 | load_put(UnpicklerObject *self) |
| 5650 | { |
| 5651 | PyObject *key, *value; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5652 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5653 | Py_ssize_t len; |
| 5654 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5655 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5656 | if ((len = _Unpickler_Readline(self, &s)) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5657 | return -1; |
| 5658 | if (len < 2) |
| 5659 | return bad_readline(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5660 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5661 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5662 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5663 | |
| 5664 | key = PyLong_FromString(s, NULL, 10); |
| 5665 | if (key == NULL) |
| 5666 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5667 | idx = PyLong_AsSsize_t(key); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5668 | Py_DECREF(key); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5669 | if (idx < 0) { |
| 5670 | if (!PyErr_Occurred()) |
| 5671 | PyErr_SetString(PyExc_ValueError, |
| 5672 | "negative PUT argument"); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5673 | return -1; |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5674 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5675 | |
| 5676 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5677 | } |
| 5678 | |
| 5679 | static int |
| 5680 | load_binput(UnpicklerObject *self) |
| 5681 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5682 | PyObject *value; |
| 5683 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5684 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5685 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5686 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5687 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5688 | |
| 5689 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5690 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5691 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5692 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5693 | idx = Py_CHARMASK(s[0]); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5694 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5695 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5696 | } |
| 5697 | |
| 5698 | static int |
| 5699 | load_long_binput(UnpicklerObject *self) |
| 5700 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5701 | PyObject *value; |
| 5702 | Py_ssize_t idx; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5703 | char *s; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5704 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5705 | if (_Unpickler_Read(self, &s, 4) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5706 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5707 | |
| 5708 | if (Py_SIZE(self->stack) <= 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5709 | return stack_underflow(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5710 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5711 | |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5712 | idx = calc_binsize(s, 4); |
Antoine Pitrou | 55549ec | 2011-08-30 00:27:10 +0200 | [diff] [blame] | 5713 | if (idx < 0) { |
| 5714 | PyErr_SetString(PyExc_ValueError, |
| 5715 | "negative LONG_BINPUT argument"); |
| 5716 | return -1; |
| 5717 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5718 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5719 | return _Unpickler_MemoPut(self, idx, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5720 | } |
| 5721 | |
| 5722 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5723 | load_memoize(UnpicklerObject *self) |
| 5724 | { |
| 5725 | PyObject *value; |
| 5726 | |
| 5727 | if (Py_SIZE(self->stack) <= 0) |
| 5728 | return stack_underflow(); |
| 5729 | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
| 5730 | |
| 5731 | return _Unpickler_MemoPut(self, self->memo_len, value); |
| 5732 | } |
| 5733 | |
| 5734 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5735 | do_append(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5736 | { |
| 5737 | PyObject *value; |
| 5738 | PyObject *list; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5739 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5740 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5741 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5742 | if (x > len || x <= 0) |
| 5743 | return stack_underflow(); |
| 5744 | if (len == x) /* nothing to do */ |
| 5745 | return 0; |
| 5746 | |
| 5747 | list = self->stack->data[x - 1]; |
| 5748 | |
| 5749 | if (PyList_Check(list)) { |
| 5750 | PyObject *slice; |
| 5751 | Py_ssize_t list_len; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5752 | int ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5753 | |
| 5754 | slice = Pdata_poplist(self->stack, x); |
| 5755 | if (!slice) |
| 5756 | return -1; |
| 5757 | list_len = PyList_GET_SIZE(list); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5758 | ret = PyList_SetSlice(list, list_len, list_len, slice); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5759 | Py_DECREF(slice); |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5760 | return ret; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5761 | } |
| 5762 | else { |
| 5763 | PyObject *append_func; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5764 | _Py_IDENTIFIER(append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5765 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5766 | append_func = _PyObject_GetAttrId(list, &PyId_append); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5767 | if (append_func == NULL) |
| 5768 | return -1; |
| 5769 | for (i = x; i < len; i++) { |
| 5770 | PyObject *result; |
| 5771 | |
| 5772 | value = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5773 | result = _Pickle_FastCall(append_func, value); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5774 | if (result == NULL) { |
| 5775 | Pdata_clear(self->stack, i + 1); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5776 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5777 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5778 | return -1; |
| 5779 | } |
| 5780 | Py_DECREF(result); |
| 5781 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5782 | Py_SIZE(self->stack) = x; |
Alexandre Vassalotti | 637c7c4 | 2013-04-20 21:28:21 -0700 | [diff] [blame] | 5783 | Py_DECREF(append_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
| 5786 | return 0; |
| 5787 | } |
| 5788 | |
| 5789 | static int |
| 5790 | load_append(UnpicklerObject *self) |
| 5791 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5792 | return do_append(self, Py_SIZE(self->stack) - 1); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5793 | } |
| 5794 | |
| 5795 | static int |
| 5796 | load_appends(UnpicklerObject *self) |
| 5797 | { |
| 5798 | return do_append(self, marker(self)); |
| 5799 | } |
| 5800 | |
| 5801 | static int |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5802 | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5803 | { |
| 5804 | PyObject *value, *key; |
| 5805 | PyObject *dict; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 5806 | Py_ssize_t len, i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5807 | int status = 0; |
| 5808 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5809 | len = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5810 | if (x > len || x <= 0) |
| 5811 | return stack_underflow(); |
| 5812 | if (len == x) /* nothing to do */ |
| 5813 | return 0; |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 5814 | if ((len - x) % 2 != 0) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5815 | PickleState *st = _Pickle_GetGlobalState(); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5816 | /* Currupt or hostile pickle -- we never write one like this. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5817 | PyErr_SetString(st->UnpicklingError, |
| 5818 | "odd number of items for SETITEMS"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5819 | return -1; |
| 5820 | } |
| 5821 | |
| 5822 | /* Here, dict does not actually need to be a PyDict; it could be anything |
| 5823 | that supports the __setitem__ attribute. */ |
| 5824 | dict = self->stack->data[x - 1]; |
| 5825 | |
| 5826 | for (i = x + 1; i < len; i += 2) { |
| 5827 | key = self->stack->data[i - 1]; |
| 5828 | value = self->stack->data[i]; |
| 5829 | if (PyObject_SetItem(dict, key, value) < 0) { |
| 5830 | status = -1; |
| 5831 | break; |
| 5832 | } |
| 5833 | } |
| 5834 | |
| 5835 | Pdata_clear(self->stack, x); |
| 5836 | return status; |
| 5837 | } |
| 5838 | |
| 5839 | static int |
| 5840 | load_setitem(UnpicklerObject *self) |
| 5841 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5842 | return do_setitems(self, Py_SIZE(self->stack) - 2); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5843 | } |
| 5844 | |
| 5845 | static int |
| 5846 | load_setitems(UnpicklerObject *self) |
| 5847 | { |
| 5848 | return do_setitems(self, marker(self)); |
| 5849 | } |
| 5850 | |
| 5851 | static int |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5852 | load_additems(UnpicklerObject *self) |
| 5853 | { |
| 5854 | PyObject *set; |
| 5855 | Py_ssize_t mark, len, i; |
| 5856 | |
| 5857 | mark = marker(self); |
| 5858 | len = Py_SIZE(self->stack); |
| 5859 | if (mark > len || mark <= 0) |
| 5860 | return stack_underflow(); |
| 5861 | if (len == mark) /* nothing to do */ |
| 5862 | return 0; |
| 5863 | |
| 5864 | set = self->stack->data[mark - 1]; |
| 5865 | |
| 5866 | if (PySet_Check(set)) { |
| 5867 | PyObject *items; |
| 5868 | int status; |
| 5869 | |
| 5870 | items = Pdata_poptuple(self->stack, mark); |
| 5871 | if (items == NULL) |
| 5872 | return -1; |
| 5873 | |
| 5874 | status = _PySet_Update(set, items); |
| 5875 | Py_DECREF(items); |
| 5876 | return status; |
| 5877 | } |
| 5878 | else { |
| 5879 | PyObject *add_func; |
| 5880 | _Py_IDENTIFIER(add); |
| 5881 | |
| 5882 | add_func = _PyObject_GetAttrId(set, &PyId_add); |
| 5883 | if (add_func == NULL) |
| 5884 | return -1; |
| 5885 | for (i = mark; i < len; i++) { |
| 5886 | PyObject *result; |
| 5887 | PyObject *item; |
| 5888 | |
| 5889 | item = self->stack->data[i]; |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5890 | result = _Pickle_FastCall(add_func, item); |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 5891 | if (result == NULL) { |
| 5892 | Pdata_clear(self->stack, i + 1); |
| 5893 | Py_SIZE(self->stack) = mark; |
| 5894 | return -1; |
| 5895 | } |
| 5896 | Py_DECREF(result); |
| 5897 | } |
| 5898 | Py_SIZE(self->stack) = mark; |
| 5899 | } |
| 5900 | |
| 5901 | return 0; |
| 5902 | } |
| 5903 | |
| 5904 | static int |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5905 | load_build(UnpicklerObject *self) |
| 5906 | { |
| 5907 | PyObject *state, *inst, *slotstate; |
| 5908 | PyObject *setstate; |
| 5909 | int status = 0; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5910 | _Py_IDENTIFIER(__setstate__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5911 | |
| 5912 | /* Stack is ... instance, state. We want to leave instance at |
| 5913 | * the stack top, possibly mutated via instance.__setstate__(state). |
| 5914 | */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5915 | if (Py_SIZE(self->stack) < 2) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5916 | return stack_underflow(); |
| 5917 | |
| 5918 | PDATA_POP(self->stack, state); |
| 5919 | if (state == NULL) |
| 5920 | return -1; |
| 5921 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 5922 | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5923 | |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5924 | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5925 | if (setstate == NULL) { |
| 5926 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 5927 | PyErr_Clear(); |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5928 | else { |
| 5929 | Py_DECREF(state); |
Alexandre Vassalotti | 1f9d907 | 2008-08-15 03:07:47 +0000 | [diff] [blame] | 5930 | return -1; |
Antoine Pitrou | d79dc62 | 2008-09-05 00:03:33 +0000 | [diff] [blame] | 5931 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5932 | } |
| 5933 | else { |
| 5934 | PyObject *result; |
| 5935 | |
| 5936 | /* The explicit __setstate__ is responsible for everything. */ |
Alexandre Vassalotti | 20c28c1 | 2013-11-27 02:26:54 -0800 | [diff] [blame] | 5937 | result = _Pickle_FastCall(setstate, state); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5938 | Py_DECREF(setstate); |
| 5939 | if (result == NULL) |
| 5940 | return -1; |
| 5941 | Py_DECREF(result); |
| 5942 | return 0; |
| 5943 | } |
| 5944 | |
| 5945 | /* A default __setstate__. First see whether state embeds a |
| 5946 | * slot state dict too (a proto 2 addition). |
| 5947 | */ |
| 5948 | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
| 5949 | PyObject *tmp = state; |
| 5950 | |
| 5951 | state = PyTuple_GET_ITEM(tmp, 0); |
| 5952 | slotstate = PyTuple_GET_ITEM(tmp, 1); |
| 5953 | Py_INCREF(state); |
| 5954 | Py_INCREF(slotstate); |
| 5955 | Py_DECREF(tmp); |
| 5956 | } |
| 5957 | else |
| 5958 | slotstate = NULL; |
| 5959 | |
| 5960 | /* Set inst.__dict__ from the state dict (if any). */ |
| 5961 | if (state != Py_None) { |
| 5962 | PyObject *dict; |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5963 | PyObject *d_key, *d_value; |
| 5964 | Py_ssize_t i; |
Martin v. Löwis | bd928fe | 2011-10-14 10:20:37 +0200 | [diff] [blame] | 5965 | _Py_IDENTIFIER(__dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5966 | |
| 5967 | if (!PyDict_Check(state)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5968 | PickleState *st = _Pickle_GetGlobalState(); |
| 5969 | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5970 | goto error; |
| 5971 | } |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 5972 | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5973 | if (dict == NULL) |
| 5974 | goto error; |
| 5975 | |
Antoine Pitrou | a9f48a0 | 2009-05-02 21:41:14 +0000 | [diff] [blame] | 5976 | i = 0; |
| 5977 | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
| 5978 | /* normally the keys for instance attributes are |
| 5979 | interned. we should try to do that here. */ |
| 5980 | Py_INCREF(d_key); |
| 5981 | if (PyUnicode_CheckExact(d_key)) |
| 5982 | PyUnicode_InternInPlace(&d_key); |
| 5983 | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
| 5984 | Py_DECREF(d_key); |
| 5985 | goto error; |
| 5986 | } |
| 5987 | Py_DECREF(d_key); |
| 5988 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 5989 | Py_DECREF(dict); |
| 5990 | } |
| 5991 | |
| 5992 | /* Also set instance attributes from the slotstate dict (if any). */ |
| 5993 | if (slotstate != NULL) { |
| 5994 | PyObject *d_key, *d_value; |
| 5995 | Py_ssize_t i; |
| 5996 | |
| 5997 | if (!PyDict_Check(slotstate)) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 5998 | PickleState *st = _Pickle_GetGlobalState(); |
| 5999 | PyErr_SetString(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6000 | "slot state is not a dictionary"); |
| 6001 | goto error; |
| 6002 | } |
| 6003 | i = 0; |
| 6004 | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
| 6005 | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
| 6006 | goto error; |
| 6007 | } |
| 6008 | } |
| 6009 | |
| 6010 | if (0) { |
| 6011 | error: |
| 6012 | status = -1; |
| 6013 | } |
| 6014 | |
| 6015 | Py_DECREF(state); |
| 6016 | Py_XDECREF(slotstate); |
| 6017 | return status; |
| 6018 | } |
| 6019 | |
| 6020 | static int |
| 6021 | load_mark(UnpicklerObject *self) |
| 6022 | { |
| 6023 | |
| 6024 | /* Note that we split the (pickle.py) stack into two stacks, an |
| 6025 | * object stack and a mark stack. Here we push a mark onto the |
| 6026 | * mark stack. |
| 6027 | */ |
| 6028 | |
| 6029 | if ((self->num_marks + 1) >= self->marks_size) { |
| 6030 | size_t alloc; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6031 | |
| 6032 | /* Use the size_t type to check for overflow. */ |
| 6033 | alloc = ((size_t)self->num_marks << 1) + 20; |
Antoine Pitrou | 82be19f | 2011-08-29 23:09:33 +0200 | [diff] [blame] | 6034 | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
Alexandre Vassalotti | 7634ff5 | 2008-06-13 02:16:06 +0000 | [diff] [blame] | 6035 | alloc <= ((size_t)self->num_marks + 1)) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6036 | PyErr_NoMemory(); |
| 6037 | return -1; |
| 6038 | } |
| 6039 | |
| 6040 | if (self->marks == NULL) |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6041 | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6042 | else |
Benjamin Peterson | 59b08c1 | 2015-06-27 13:41:33 -0500 | [diff] [blame] | 6043 | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
| 6044 | if (self->marks == NULL) { |
| 6045 | self->marks_size = 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6046 | PyErr_NoMemory(); |
| 6047 | return -1; |
| 6048 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6049 | self->marks_size = (Py_ssize_t)alloc; |
| 6050 | } |
| 6051 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6052 | self->marks[self->num_marks++] = Py_SIZE(self->stack); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6053 | |
| 6054 | return 0; |
| 6055 | } |
| 6056 | |
| 6057 | static int |
| 6058 | load_reduce(UnpicklerObject *self) |
| 6059 | { |
| 6060 | PyObject *callable = NULL; |
| 6061 | PyObject *argtup = NULL; |
| 6062 | PyObject *obj = NULL; |
| 6063 | |
| 6064 | PDATA_POP(self->stack, argtup); |
| 6065 | if (argtup == NULL) |
| 6066 | return -1; |
| 6067 | PDATA_POP(self->stack, callable); |
| 6068 | if (callable) { |
Alexander Belopolsky | d92f040 | 2010-07-17 22:50:45 +0000 | [diff] [blame] | 6069 | obj = PyObject_CallObject(callable, argtup); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6070 | Py_DECREF(callable); |
| 6071 | } |
| 6072 | Py_DECREF(argtup); |
| 6073 | |
| 6074 | if (obj == NULL) |
| 6075 | return -1; |
| 6076 | |
| 6077 | PDATA_PUSH(self->stack, obj, -1); |
| 6078 | return 0; |
| 6079 | } |
| 6080 | |
| 6081 | /* Just raises an error if we don't know the protocol specified. PROTO |
| 6082 | * is the first opcode for protocols >= 2. |
| 6083 | */ |
| 6084 | static int |
| 6085 | load_proto(UnpicklerObject *self) |
| 6086 | { |
| 6087 | char *s; |
| 6088 | int i; |
| 6089 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6090 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6091 | return -1; |
| 6092 | |
| 6093 | i = (unsigned char)s[0]; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6094 | if (i <= HIGHEST_PROTOCOL) { |
| 6095 | self->proto = i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6096 | return 0; |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6097 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6098 | |
| 6099 | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
| 6100 | return -1; |
| 6101 | } |
| 6102 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6103 | static int |
| 6104 | load_frame(UnpicklerObject *self) |
| 6105 | { |
| 6106 | char *s; |
| 6107 | Py_ssize_t frame_len; |
| 6108 | |
| 6109 | if (_Unpickler_Read(self, &s, 8) < 0) |
| 6110 | return -1; |
| 6111 | |
| 6112 | frame_len = calc_binsize(s, 8); |
| 6113 | if (frame_len < 0) { |
| 6114 | PyErr_Format(PyExc_OverflowError, |
| 6115 | "FRAME length exceeds system's maximum of %zd bytes", |
| 6116 | PY_SSIZE_T_MAX); |
| 6117 | return -1; |
| 6118 | } |
| 6119 | |
| 6120 | if (_Unpickler_Read(self, &s, frame_len) < 0) |
| 6121 | return -1; |
| 6122 | |
| 6123 | /* Rewind to start of frame */ |
| 6124 | self->next_read_idx -= frame_len; |
| 6125 | return 0; |
| 6126 | } |
| 6127 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6128 | static PyObject * |
| 6129 | load(UnpicklerObject *self) |
| 6130 | { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6131 | PyObject *value = NULL; |
Christian Heimes | 27ea78b | 2014-01-27 01:03:53 +0100 | [diff] [blame] | 6132 | char *s = NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6133 | |
| 6134 | self->num_marks = 0; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6135 | self->proto = 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6136 | if (Py_SIZE(self->stack)) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6137 | Pdata_clear(self->stack, 0); |
| 6138 | |
| 6139 | /* Convenient macros for the dispatch while-switch loop just below. */ |
| 6140 | #define OP(opcode, load_func) \ |
| 6141 | case opcode: if (load_func(self) < 0) break; continue; |
| 6142 | |
| 6143 | #define OP_ARG(opcode, load_func, arg) \ |
| 6144 | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
| 6145 | |
| 6146 | while (1) { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6147 | if (_Unpickler_Read(self, &s, 1) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6148 | break; |
| 6149 | |
| 6150 | switch ((enum opcode)s[0]) { |
| 6151 | OP(NONE, load_none) |
| 6152 | OP(BININT, load_binint) |
| 6153 | OP(BININT1, load_binint1) |
| 6154 | OP(BININT2, load_binint2) |
| 6155 | OP(INT, load_int) |
| 6156 | OP(LONG, load_long) |
| 6157 | OP_ARG(LONG1, load_counted_long, 1) |
| 6158 | OP_ARG(LONG4, load_counted_long, 4) |
| 6159 | OP(FLOAT, load_float) |
| 6160 | OP(BINFLOAT, load_binfloat) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6161 | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
| 6162 | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
| 6163 | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
| 6164 | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
| 6165 | OP_ARG(BINSTRING, load_counted_binstring, 4) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6166 | OP(STRING, load_string) |
| 6167 | OP(UNICODE, load_unicode) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6168 | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
| 6169 | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
| 6170 | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6171 | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
| 6172 | OP_ARG(TUPLE1, load_counted_tuple, 1) |
| 6173 | OP_ARG(TUPLE2, load_counted_tuple, 2) |
| 6174 | OP_ARG(TUPLE3, load_counted_tuple, 3) |
| 6175 | OP(TUPLE, load_tuple) |
| 6176 | OP(EMPTY_LIST, load_empty_list) |
| 6177 | OP(LIST, load_list) |
| 6178 | OP(EMPTY_DICT, load_empty_dict) |
| 6179 | OP(DICT, load_dict) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6180 | OP(EMPTY_SET, load_empty_set) |
| 6181 | OP(ADDITEMS, load_additems) |
| 6182 | OP(FROZENSET, load_frozenset) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6183 | OP(OBJ, load_obj) |
| 6184 | OP(INST, load_inst) |
| 6185 | OP(NEWOBJ, load_newobj) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6186 | OP(NEWOBJ_EX, load_newobj_ex) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6187 | OP(GLOBAL, load_global) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6188 | OP(STACK_GLOBAL, load_stack_global) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6189 | OP(APPEND, load_append) |
| 6190 | OP(APPENDS, load_appends) |
| 6191 | OP(BUILD, load_build) |
| 6192 | OP(DUP, load_dup) |
| 6193 | OP(BINGET, load_binget) |
| 6194 | OP(LONG_BINGET, load_long_binget) |
| 6195 | OP(GET, load_get) |
| 6196 | OP(MARK, load_mark) |
| 6197 | OP(BINPUT, load_binput) |
| 6198 | OP(LONG_BINPUT, load_long_binput) |
| 6199 | OP(PUT, load_put) |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6200 | OP(MEMOIZE, load_memoize) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6201 | OP(POP, load_pop) |
| 6202 | OP(POP_MARK, load_pop_mark) |
| 6203 | OP(SETITEM, load_setitem) |
| 6204 | OP(SETITEMS, load_setitems) |
| 6205 | OP(PERSID, load_persid) |
| 6206 | OP(BINPERSID, load_binpersid) |
| 6207 | OP(REDUCE, load_reduce) |
| 6208 | OP(PROTO, load_proto) |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6209 | OP(FRAME, load_frame) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6210 | OP_ARG(EXT1, load_extension, 1) |
| 6211 | OP_ARG(EXT2, load_extension, 2) |
| 6212 | OP_ARG(EXT4, load_extension, 4) |
| 6213 | OP_ARG(NEWTRUE, load_bool, Py_True) |
| 6214 | OP_ARG(NEWFALSE, load_bool, Py_False) |
| 6215 | |
| 6216 | case STOP: |
| 6217 | break; |
| 6218 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6219 | default: |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6220 | if (s[0] == '\0') { |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6221 | PyErr_SetNone(PyExc_EOFError); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6222 | } |
| 6223 | else { |
| 6224 | PickleState *st = _Pickle_GetGlobalState(); |
| 6225 | PyErr_Format(st->UnpicklingError, |
Benjamin Peterson | adde86d | 2011-09-23 13:41:41 -0400 | [diff] [blame] | 6226 | "invalid load key, '%c'.", s[0]); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6227 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6228 | return NULL; |
| 6229 | } |
| 6230 | |
| 6231 | break; /* and we are done! */ |
| 6232 | } |
| 6233 | |
Alexandre Vassalotti | b6a2f2a | 2013-11-23 20:30:03 -0800 | [diff] [blame] | 6234 | if (PyErr_Occurred()) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6235 | return NULL; |
| 6236 | } |
| 6237 | |
Victor Stinner | 2ae57e3 | 2013-10-31 13:39:23 +0100 | [diff] [blame] | 6238 | if (_Unpickler_SkipConsumed(self) < 0) |
| 6239 | return NULL; |
| 6240 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6241 | PDATA_POP(self->stack, value); |
| 6242 | return value; |
| 6243 | } |
| 6244 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6245 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6246 | |
| 6247 | _pickle.Unpickler.load |
| 6248 | |
| 6249 | Load a pickle. |
| 6250 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6251 | Read a pickled object representation from the open file object given |
| 6252 | in the constructor, and return the reconstituted object hierarchy |
| 6253 | specified therein. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6254 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6255 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6256 | static PyObject * |
Larry Hastings | c204726 | 2014-01-25 20:43:29 -0800 | [diff] [blame] | 6257 | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6258 | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6259 | { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6260 | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6261 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6262 | /* Check whether the Unpickler was initialized correctly. This prevents |
| 6263 | segfaulting if a subclass overridden __init__ with a function that does |
| 6264 | not call Unpickler.__init__(). Here, we simply ensure that self->read |
| 6265 | is not NULL. */ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6266 | if (unpickler->read == NULL) { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6267 | PickleState *st = _Pickle_GetGlobalState(); |
| 6268 | PyErr_Format(st->UnpicklingError, |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6269 | "Unpickler.__init__() was not called by %s.__init__()", |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6270 | Py_TYPE(unpickler)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6271 | return NULL; |
| 6272 | } |
| 6273 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6274 | return load(unpickler); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6275 | } |
| 6276 | |
| 6277 | /* The name of find_class() is misleading. In newer pickle protocols, this |
| 6278 | function is used for loading any global (i.e., functions), not just |
| 6279 | classes. The name is kept only for backward compatibility. */ |
| 6280 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6281 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6282 | |
| 6283 | _pickle.Unpickler.find_class |
| 6284 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6285 | module_name: object |
| 6286 | global_name: object |
| 6287 | / |
| 6288 | |
| 6289 | Return an object from a specified module. |
| 6290 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6291 | If necessary, the module will be imported. Subclasses may override |
| 6292 | this method (e.g. to restrict unpickling of arbitrary classes and |
| 6293 | functions). |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6294 | |
| 6295 | This method is called whenever a class or a function object is |
| 6296 | needed. Both arguments passed are str objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6297 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6298 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6299 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6300 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 6301 | PyObject *module_name, |
| 6302 | PyObject *global_name) |
| 6303 | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6304 | { |
| 6305 | PyObject *global; |
| 6306 | PyObject *modules_dict; |
| 6307 | PyObject *module; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6308 | _Py_IDENTIFIER(modules); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6309 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6310 | /* Try to map the old names used in Python 2.x to the new ones used in |
| 6311 | Python 3.x. We do this only with old pickle protocols and when the |
| 6312 | user has not disabled the feature. */ |
| 6313 | if (self->proto < 3 && self->fix_imports) { |
| 6314 | PyObject *key; |
| 6315 | PyObject *item; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6316 | PickleState *st = _Pickle_GetGlobalState(); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6317 | |
| 6318 | /* Check if the global (i.e., a function or a class) was renamed |
| 6319 | or moved to another module. */ |
| 6320 | key = PyTuple_Pack(2, module_name, global_name); |
| 6321 | if (key == NULL) |
| 6322 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 6323 | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6324 | Py_DECREF(key); |
| 6325 | if (item) { |
| 6326 | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
| 6327 | PyErr_Format(PyExc_RuntimeError, |
| 6328 | "_compat_pickle.NAME_MAPPING values should be " |
| 6329 | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
| 6330 | return NULL; |
| 6331 | } |
| 6332 | module_name = PyTuple_GET_ITEM(item, 0); |
| 6333 | global_name = PyTuple_GET_ITEM(item, 1); |
| 6334 | if (!PyUnicode_Check(module_name) || |
| 6335 | !PyUnicode_Check(global_name)) { |
| 6336 | PyErr_Format(PyExc_RuntimeError, |
| 6337 | "_compat_pickle.NAME_MAPPING values should be " |
| 6338 | "pairs of str, not (%.200s, %.200s)", |
| 6339 | Py_TYPE(module_name)->tp_name, |
| 6340 | Py_TYPE(global_name)->tp_name); |
| 6341 | return NULL; |
| 6342 | } |
| 6343 | } |
| 6344 | else if (PyErr_Occurred()) { |
| 6345 | return NULL; |
| 6346 | } |
Serhiy Storchaka | bfe1824 | 2015-03-31 13:12:37 +0300 | [diff] [blame] | 6347 | else { |
| 6348 | /* Check if the module was renamed. */ |
| 6349 | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
| 6350 | if (item) { |
| 6351 | if (!PyUnicode_Check(item)) { |
| 6352 | PyErr_Format(PyExc_RuntimeError, |
| 6353 | "_compat_pickle.IMPORT_MAPPING values should be " |
| 6354 | "strings, not %.200s", Py_TYPE(item)->tp_name); |
| 6355 | return NULL; |
| 6356 | } |
| 6357 | module_name = item; |
| 6358 | } |
| 6359 | else if (PyErr_Occurred()) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6360 | return NULL; |
| 6361 | } |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6362 | } |
| 6363 | } |
| 6364 | |
Victor Stinner | bb52020 | 2013-11-06 22:40:41 +0100 | [diff] [blame] | 6365 | modules_dict = _PySys_GetObjectId(&PyId_modules); |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6366 | if (modules_dict == NULL) { |
| 6367 | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6368 | return NULL; |
Victor Stinner | 1e53bba | 2013-07-16 22:26:05 +0200 | [diff] [blame] | 6369 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6370 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6371 | module = PyDict_GetItemWithError(modules_dict, module_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6372 | if (module == NULL) { |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6373 | if (PyErr_Occurred()) |
| 6374 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6375 | module = PyImport_Import(module_name); |
| 6376 | if (module == NULL) |
| 6377 | return NULL; |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6378 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6379 | Py_DECREF(module); |
| 6380 | } |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6381 | else { |
Antoine Pitrou | c9dc4a2 | 2013-11-23 18:59:12 +0100 | [diff] [blame] | 6382 | global = getattribute(module, global_name, self->proto >= 4); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6383 | } |
| 6384 | return global; |
| 6385 | } |
| 6386 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6387 | /*[clinic input] |
| 6388 | |
| 6389 | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
| 6390 | |
| 6391 | Returns size in memory, in bytes. |
| 6392 | [clinic start generated code]*/ |
| 6393 | |
| 6394 | static Py_ssize_t |
| 6395 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
| 6396 | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
| 6397 | { |
| 6398 | Py_ssize_t res; |
| 6399 | |
| 6400 | res = sizeof(UnpicklerObject); |
| 6401 | if (self->memo != NULL) |
| 6402 | res += self->memo_size * sizeof(PyObject *); |
| 6403 | if (self->marks != NULL) |
| 6404 | res += self->marks_size * sizeof(Py_ssize_t); |
| 6405 | if (self->input_line != NULL) |
| 6406 | res += strlen(self->input_line) + 1; |
| 6407 | if (self->encoding != NULL) |
| 6408 | res += strlen(self->encoding) + 1; |
| 6409 | if (self->errors != NULL) |
| 6410 | res += strlen(self->errors) + 1; |
| 6411 | return res; |
| 6412 | } |
| 6413 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6414 | static struct PyMethodDef Unpickler_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6415 | _PICKLE_UNPICKLER_LOAD_METHODDEF |
| 6416 | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 6417 | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6418 | {NULL, NULL} /* sentinel */ |
| 6419 | }; |
| 6420 | |
| 6421 | static void |
| 6422 | Unpickler_dealloc(UnpicklerObject *self) |
| 6423 | { |
| 6424 | PyObject_GC_UnTrack((PyObject *)self); |
| 6425 | Py_XDECREF(self->readline); |
| 6426 | Py_XDECREF(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6427 | Py_XDECREF(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6428 | Py_XDECREF(self->stack); |
| 6429 | Py_XDECREF(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6430 | if (self->buffer.buf != NULL) { |
| 6431 | PyBuffer_Release(&self->buffer); |
| 6432 | self->buffer.buf = NULL; |
| 6433 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6434 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6435 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6436 | PyMem_Free(self->marks); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6437 | PyMem_Free(self->input_line); |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6438 | PyMem_Free(self->encoding); |
| 6439 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6440 | |
| 6441 | Py_TYPE(self)->tp_free((PyObject *)self); |
| 6442 | } |
| 6443 | |
| 6444 | static int |
| 6445 | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
| 6446 | { |
| 6447 | Py_VISIT(self->readline); |
| 6448 | Py_VISIT(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6449 | Py_VISIT(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6450 | Py_VISIT(self->stack); |
| 6451 | Py_VISIT(self->pers_func); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6452 | return 0; |
| 6453 | } |
| 6454 | |
| 6455 | static int |
| 6456 | Unpickler_clear(UnpicklerObject *self) |
| 6457 | { |
| 6458 | Py_CLEAR(self->readline); |
| 6459 | Py_CLEAR(self->read); |
Antoine Pitrou | 04248a8 | 2010-10-12 20:51:21 +0000 | [diff] [blame] | 6460 | Py_CLEAR(self->peek); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6461 | Py_CLEAR(self->stack); |
| 6462 | Py_CLEAR(self->pers_func); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6463 | if (self->buffer.buf != NULL) { |
| 6464 | PyBuffer_Release(&self->buffer); |
| 6465 | self->buffer.buf = NULL; |
| 6466 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6467 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6468 | _Unpickler_MemoCleanup(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6469 | PyMem_Free(self->marks); |
| 6470 | self->marks = NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6471 | PyMem_Free(self->input_line); |
| 6472 | self->input_line = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6473 | PyMem_Free(self->encoding); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6474 | self->encoding = NULL; |
Victor Stinner | 49fc8ec | 2013-07-07 23:30:24 +0200 | [diff] [blame] | 6475 | PyMem_Free(self->errors); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6476 | self->errors = NULL; |
| 6477 | |
| 6478 | return 0; |
| 6479 | } |
| 6480 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6481 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6482 | |
| 6483 | _pickle.Unpickler.__init__ |
| 6484 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6485 | file: object |
| 6486 | * |
| 6487 | fix_imports: bool = True |
| 6488 | encoding: str = 'ASCII' |
| 6489 | errors: str = 'strict' |
| 6490 | |
| 6491 | This takes a binary file for reading a pickle data stream. |
| 6492 | |
| 6493 | The protocol version of the pickle is detected automatically, so no |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6494 | protocol argument is needed. Bytes past the pickled object's |
| 6495 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6496 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6497 | The argument *file* must have two methods, a read() method that takes |
| 6498 | an integer argument, and a readline() method that requires no |
| 6499 | arguments. Both methods should return bytes. Thus *file* can be a |
| 6500 | binary file object opened for reading, a io.BytesIO object, or any |
| 6501 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6502 | |
| 6503 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 6504 | which are used to control compatiblity support for pickle stream |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6505 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 6506 | 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] | 6507 | *encoding* and *errors* tell pickle how to decode 8-bit string |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6508 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 6509 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 6510 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6511 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6512 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6513 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6514 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 6515 | int fix_imports, const char *encoding, |
| 6516 | const char *errors) |
| 6517 | /*[clinic end generated code: output=e2c8ce748edc57b0 input=30b4dc9e976b890c]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6518 | { |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6519 | _Py_IDENTIFIER(persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6520 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6521 | /* In case of multiple __init__() calls, clear previous content. */ |
| 6522 | if (self->read != NULL) |
| 6523 | (void)Unpickler_clear(self); |
| 6524 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6525 | if (_Unpickler_SetInputStream(self, file) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6526 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6527 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6528 | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6529 | return -1; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6530 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6531 | self->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6532 | if (self->fix_imports == -1) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6533 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6534 | |
Martin v. Löwis | 1c67dd9 | 2011-10-14 15:16:45 +0200 | [diff] [blame] | 6535 | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
Martin v. Löwis | 1ee1b6f | 2011-10-10 18:11:30 +0200 | [diff] [blame] | 6536 | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
| 6537 | &PyId_persistent_load); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6538 | if (self->pers_func == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6539 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6540 | } |
| 6541 | else { |
| 6542 | self->pers_func = NULL; |
| 6543 | } |
| 6544 | |
| 6545 | self->stack = (Pdata *)Pdata_New(); |
| 6546 | if (self->stack == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6547 | return 1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6548 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6549 | self->memo_size = 32; |
| 6550 | self->memo = _Unpickler_NewMemo(self->memo_size); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6551 | if (self->memo == NULL) |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6552 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6553 | |
Antoine Pitrou | d9dfaa9 | 2009-06-04 20:32:06 +0000 | [diff] [blame] | 6554 | self->proto = 0; |
Alexandre Vassalotti | 0e7aa8c | 2009-04-03 04:17:41 +0000 | [diff] [blame] | 6555 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6556 | return 0; |
| 6557 | } |
| 6558 | |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6559 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6560 | /* Define a proxy object for the Unpickler's internal memo object. This is to |
| 6561 | * avoid breaking code like: |
| 6562 | * unpickler.memo.clear() |
| 6563 | * and |
| 6564 | * unpickler.memo = saved_memo |
| 6565 | * Is this a good idea? Not really, but we don't want to break code that uses |
| 6566 | * it. Note that we don't implement the entire mapping API here. This is |
| 6567 | * intentional, as these should be treated as black-box implementation details. |
| 6568 | * |
| 6569 | * We do, however, have to implement pickling/unpickling support because of |
Victor Stinner | 121aab4 | 2011-09-29 23:40:53 +0200 | [diff] [blame] | 6570 | * real-world code like cvs2svn. |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6571 | */ |
| 6572 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6573 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6574 | _pickle.UnpicklerMemoProxy.clear |
| 6575 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6576 | Remove all items from memo. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6577 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6578 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6579 | static PyObject * |
| 6580 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6581 | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6582 | { |
| 6583 | _Unpickler_MemoCleanup(self->unpickler); |
| 6584 | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
| 6585 | if (self->unpickler->memo == NULL) |
| 6586 | return NULL; |
| 6587 | Py_RETURN_NONE; |
| 6588 | } |
| 6589 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6590 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6591 | _pickle.UnpicklerMemoProxy.copy |
| 6592 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6593 | Copy the memo to a new object. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6594 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6595 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6596 | static PyObject * |
| 6597 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6598 | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6599 | { |
| 6600 | Py_ssize_t i; |
| 6601 | PyObject *new_memo = PyDict_New(); |
| 6602 | if (new_memo == NULL) |
| 6603 | return NULL; |
| 6604 | |
| 6605 | for (i = 0; i < self->unpickler->memo_size; i++) { |
| 6606 | int status; |
| 6607 | PyObject *key, *value; |
| 6608 | |
| 6609 | value = self->unpickler->memo[i]; |
| 6610 | if (value == NULL) |
| 6611 | continue; |
| 6612 | |
| 6613 | key = PyLong_FromSsize_t(i); |
| 6614 | if (key == NULL) |
| 6615 | goto error; |
| 6616 | status = PyDict_SetItem(new_memo, key, value); |
| 6617 | Py_DECREF(key); |
| 6618 | if (status < 0) |
| 6619 | goto error; |
| 6620 | } |
| 6621 | return new_memo; |
| 6622 | |
| 6623 | error: |
| 6624 | Py_DECREF(new_memo); |
| 6625 | return NULL; |
| 6626 | } |
| 6627 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6628 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6629 | _pickle.UnpicklerMemoProxy.__reduce__ |
| 6630 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6631 | Implement pickling support. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6632 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6633 | |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6634 | static PyObject * |
| 6635 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
Larry Hastings | 581ee36 | 2014-01-28 05:00:08 -0800 | [diff] [blame] | 6636 | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6637 | { |
| 6638 | PyObject *reduce_value; |
| 6639 | PyObject *constructor_args; |
Larry Hastings | 3cceb38 | 2014-01-04 11:09:09 -0800 | [diff] [blame] | 6640 | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6641 | if (contents == NULL) |
| 6642 | return NULL; |
| 6643 | |
| 6644 | reduce_value = PyTuple_New(2); |
| 6645 | if (reduce_value == NULL) { |
| 6646 | Py_DECREF(contents); |
| 6647 | return NULL; |
| 6648 | } |
| 6649 | constructor_args = PyTuple_New(1); |
| 6650 | if (constructor_args == NULL) { |
| 6651 | Py_DECREF(contents); |
| 6652 | Py_DECREF(reduce_value); |
| 6653 | return NULL; |
| 6654 | } |
| 6655 | PyTuple_SET_ITEM(constructor_args, 0, contents); |
| 6656 | Py_INCREF((PyObject *)&PyDict_Type); |
| 6657 | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
| 6658 | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
| 6659 | return reduce_value; |
| 6660 | } |
| 6661 | |
| 6662 | static PyMethodDef unpicklerproxy_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6663 | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
| 6664 | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
| 6665 | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6666 | {NULL, NULL} /* sentinel */ |
| 6667 | }; |
| 6668 | |
| 6669 | static void |
| 6670 | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
| 6671 | { |
| 6672 | PyObject_GC_UnTrack(self); |
| 6673 | Py_XDECREF(self->unpickler); |
| 6674 | PyObject_GC_Del((PyObject *)self); |
| 6675 | } |
| 6676 | |
| 6677 | static int |
| 6678 | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
| 6679 | visitproc visit, void *arg) |
| 6680 | { |
| 6681 | Py_VISIT(self->unpickler); |
| 6682 | return 0; |
| 6683 | } |
| 6684 | |
| 6685 | static int |
| 6686 | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
| 6687 | { |
| 6688 | Py_CLEAR(self->unpickler); |
| 6689 | return 0; |
| 6690 | } |
| 6691 | |
| 6692 | static PyTypeObject UnpicklerMemoProxyType = { |
| 6693 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6694 | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
| 6695 | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
| 6696 | 0, |
| 6697 | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
| 6698 | 0, /* tp_print */ |
| 6699 | 0, /* tp_getattr */ |
| 6700 | 0, /* tp_setattr */ |
| 6701 | 0, /* tp_compare */ |
| 6702 | 0, /* tp_repr */ |
| 6703 | 0, /* tp_as_number */ |
| 6704 | 0, /* tp_as_sequence */ |
| 6705 | 0, /* tp_as_mapping */ |
Georg Brandl | f038b32 | 2010-10-18 07:35:09 +0000 | [diff] [blame] | 6706 | PyObject_HashNotImplemented, /* tp_hash */ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6707 | 0, /* tp_call */ |
| 6708 | 0, /* tp_str */ |
| 6709 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 6710 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 6711 | 0, /* tp_as_buffer */ |
| 6712 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
| 6713 | 0, /* tp_doc */ |
| 6714 | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
| 6715 | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
| 6716 | 0, /* tp_richcompare */ |
| 6717 | 0, /* tp_weaklistoffset */ |
| 6718 | 0, /* tp_iter */ |
| 6719 | 0, /* tp_iternext */ |
| 6720 | unpicklerproxy_methods, /* tp_methods */ |
| 6721 | }; |
| 6722 | |
| 6723 | static PyObject * |
| 6724 | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
| 6725 | { |
| 6726 | UnpicklerMemoProxyObject *self; |
| 6727 | |
| 6728 | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
| 6729 | &UnpicklerMemoProxyType); |
| 6730 | if (self == NULL) |
| 6731 | return NULL; |
| 6732 | Py_INCREF(unpickler); |
| 6733 | self->unpickler = unpickler; |
| 6734 | PyObject_GC_Track(self); |
| 6735 | return (PyObject *)self; |
| 6736 | } |
| 6737 | |
| 6738 | /*****************************************************************************/ |
| 6739 | |
| 6740 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6741 | static PyObject * |
| 6742 | Unpickler_get_memo(UnpicklerObject *self) |
| 6743 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6744 | return UnpicklerMemoProxy_New(self); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6745 | } |
| 6746 | |
| 6747 | static int |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6748 | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6749 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6750 | PyObject **new_memo; |
| 6751 | Py_ssize_t new_memo_size = 0; |
| 6752 | Py_ssize_t i; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6753 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6754 | if (obj == NULL) { |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6755 | PyErr_SetString(PyExc_TypeError, |
| 6756 | "attribute deletion is not supported"); |
| 6757 | return -1; |
| 6758 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6759 | |
| 6760 | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
| 6761 | UnpicklerObject *unpickler = |
| 6762 | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
| 6763 | |
| 6764 | new_memo_size = unpickler->memo_size; |
| 6765 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6766 | if (new_memo == NULL) |
| 6767 | return -1; |
| 6768 | |
| 6769 | for (i = 0; i < new_memo_size; i++) { |
| 6770 | Py_XINCREF(unpickler->memo[i]); |
| 6771 | new_memo[i] = unpickler->memo[i]; |
| 6772 | } |
| 6773 | } |
| 6774 | else if (PyDict_Check(obj)) { |
| 6775 | Py_ssize_t i = 0; |
| 6776 | PyObject *key, *value; |
| 6777 | |
| 6778 | new_memo_size = PyDict_Size(obj); |
| 6779 | new_memo = _Unpickler_NewMemo(new_memo_size); |
| 6780 | if (new_memo == NULL) |
| 6781 | return -1; |
| 6782 | |
| 6783 | while (PyDict_Next(obj, &i, &key, &value)) { |
| 6784 | Py_ssize_t idx; |
| 6785 | if (!PyLong_Check(key)) { |
| 6786 | PyErr_SetString(PyExc_TypeError, |
| 6787 | "memo key must be integers"); |
| 6788 | goto error; |
| 6789 | } |
| 6790 | idx = PyLong_AsSsize_t(key); |
| 6791 | if (idx == -1 && PyErr_Occurred()) |
| 6792 | goto error; |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6793 | if (idx < 0) { |
| 6794 | PyErr_SetString(PyExc_ValueError, |
Christian Heimes | 8087879 | 2013-07-01 15:23:39 +0200 | [diff] [blame] | 6795 | "memo key must be positive integers."); |
Christian Heimes | a24b4d2 | 2013-07-01 15:17:45 +0200 | [diff] [blame] | 6796 | goto error; |
| 6797 | } |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6798 | if (_Unpickler_MemoPut(self, idx, value) < 0) |
| 6799 | goto error; |
| 6800 | } |
| 6801 | } |
| 6802 | else { |
| 6803 | PyErr_Format(PyExc_TypeError, |
| 6804 | "'memo' attribute must be an UnpicklerMemoProxy object" |
| 6805 | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6806 | return -1; |
| 6807 | } |
| 6808 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6809 | _Unpickler_MemoCleanup(self); |
| 6810 | self->memo_size = new_memo_size; |
| 6811 | self->memo = new_memo; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6812 | |
| 6813 | return 0; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6814 | |
| 6815 | error: |
| 6816 | if (new_memo_size) { |
| 6817 | i = new_memo_size; |
| 6818 | while (--i >= 0) { |
| 6819 | Py_XDECREF(new_memo[i]); |
| 6820 | } |
| 6821 | PyMem_FREE(new_memo); |
| 6822 | } |
| 6823 | return -1; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6824 | } |
| 6825 | |
| 6826 | static PyObject * |
| 6827 | Unpickler_get_persload(UnpicklerObject *self) |
| 6828 | { |
| 6829 | if (self->pers_func == NULL) |
| 6830 | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
| 6831 | else |
| 6832 | Py_INCREF(self->pers_func); |
| 6833 | return self->pers_func; |
| 6834 | } |
| 6835 | |
| 6836 | static int |
| 6837 | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
| 6838 | { |
| 6839 | PyObject *tmp; |
| 6840 | |
| 6841 | if (value == NULL) { |
| 6842 | PyErr_SetString(PyExc_TypeError, |
| 6843 | "attribute deletion is not supported"); |
| 6844 | return -1; |
| 6845 | } |
| 6846 | if (!PyCallable_Check(value)) { |
| 6847 | PyErr_SetString(PyExc_TypeError, |
| 6848 | "persistent_load must be a callable taking " |
| 6849 | "one argument"); |
| 6850 | return -1; |
| 6851 | } |
| 6852 | |
| 6853 | tmp = self->pers_func; |
| 6854 | Py_INCREF(value); |
| 6855 | self->pers_func = value; |
| 6856 | Py_XDECREF(tmp); /* self->pers_func can be NULL, so be careful. */ |
| 6857 | |
| 6858 | return 0; |
| 6859 | } |
| 6860 | |
| 6861 | static PyGetSetDef Unpickler_getsets[] = { |
| 6862 | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
| 6863 | {"persistent_load", (getter)Unpickler_get_persload, |
| 6864 | (setter)Unpickler_set_persload}, |
| 6865 | {NULL} |
| 6866 | }; |
| 6867 | |
| 6868 | static PyTypeObject Unpickler_Type = { |
| 6869 | PyVarObject_HEAD_INIT(NULL, 0) |
| 6870 | "_pickle.Unpickler", /*tp_name*/ |
| 6871 | sizeof(UnpicklerObject), /*tp_basicsize*/ |
| 6872 | 0, /*tp_itemsize*/ |
| 6873 | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
| 6874 | 0, /*tp_print*/ |
| 6875 | 0, /*tp_getattr*/ |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6876 | 0, /*tp_setattr*/ |
Mark Dickinson | e94c679 | 2009-02-02 20:36:42 +0000 | [diff] [blame] | 6877 | 0, /*tp_reserved*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6878 | 0, /*tp_repr*/ |
| 6879 | 0, /*tp_as_number*/ |
| 6880 | 0, /*tp_as_sequence*/ |
| 6881 | 0, /*tp_as_mapping*/ |
| 6882 | 0, /*tp_hash*/ |
| 6883 | 0, /*tp_call*/ |
| 6884 | 0, /*tp_str*/ |
| 6885 | 0, /*tp_getattro*/ |
| 6886 | 0, /*tp_setattro*/ |
| 6887 | 0, /*tp_as_buffer*/ |
| 6888 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6889 | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6890 | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
| 6891 | (inquiry)Unpickler_clear, /*tp_clear*/ |
| 6892 | 0, /*tp_richcompare*/ |
| 6893 | 0, /*tp_weaklistoffset*/ |
| 6894 | 0, /*tp_iter*/ |
| 6895 | 0, /*tp_iternext*/ |
| 6896 | Unpickler_methods, /*tp_methods*/ |
| 6897 | 0, /*tp_members*/ |
| 6898 | Unpickler_getsets, /*tp_getset*/ |
| 6899 | 0, /*tp_base*/ |
| 6900 | 0, /*tp_dict*/ |
| 6901 | 0, /*tp_descr_get*/ |
| 6902 | 0, /*tp_descr_set*/ |
| 6903 | 0, /*tp_dictoffset*/ |
Larry Hastings | b7ccb20 | 2014-01-18 23:50:21 -0800 | [diff] [blame] | 6904 | _pickle_Unpickler___init__, /*tp_init*/ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 6905 | PyType_GenericAlloc, /*tp_alloc*/ |
| 6906 | PyType_GenericNew, /*tp_new*/ |
| 6907 | PyObject_GC_Del, /*tp_free*/ |
| 6908 | 0, /*tp_is_gc*/ |
| 6909 | }; |
| 6910 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6911 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6912 | |
| 6913 | _pickle.dump |
| 6914 | |
| 6915 | obj: object |
| 6916 | file: object |
| 6917 | protocol: object = NULL |
| 6918 | * |
| 6919 | fix_imports: bool = True |
| 6920 | |
| 6921 | Write a pickled representation of obj to the open file object file. |
| 6922 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6923 | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
| 6924 | be more efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6925 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6926 | The optional *protocol* argument tells the pickler to use the given |
| 6927 | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
| 6928 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6929 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6930 | Specifying a negative protocol version selects the highest protocol |
| 6931 | version supported. The higher the protocol used, the more recent the |
| 6932 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6933 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6934 | The *file* argument must have a write() method that accepts a single |
| 6935 | bytes argument. It can thus be a file object opened for binary |
| 6936 | writing, a io.BytesIO instance, or any other custom object that meets |
| 6937 | this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6938 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6939 | If *fix_imports* is True and protocol is less than 3, pickle will try |
| 6940 | to map the new Python 3 names to the old module names used in Python |
| 6941 | 2, so that the pickle data stream is readable with Python 2. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6942 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6943 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6944 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6945 | _pickle_dump_impl(PyModuleDef *module, PyObject *obj, PyObject *file, |
| 6946 | PyObject *protocol, int fix_imports) |
| 6947 | /*[clinic end generated code: output=0de7dff89c406816 input=e9e5fdd48de92eae]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6948 | { |
| 6949 | PicklerObject *pickler = _Pickler_New(); |
| 6950 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6951 | if (pickler == NULL) |
| 6952 | return NULL; |
| 6953 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6954 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 6955 | goto error; |
| 6956 | |
| 6957 | if (_Pickler_SetOutputStream(pickler, file) < 0) |
| 6958 | goto error; |
| 6959 | |
| 6960 | if (dump(pickler, obj) < 0) |
| 6961 | goto error; |
| 6962 | |
| 6963 | if (_Pickler_FlushToFile(pickler) < 0) |
| 6964 | goto error; |
| 6965 | |
| 6966 | Py_DECREF(pickler); |
| 6967 | Py_RETURN_NONE; |
| 6968 | |
| 6969 | error: |
| 6970 | Py_XDECREF(pickler); |
| 6971 | return NULL; |
| 6972 | } |
| 6973 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 6974 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6975 | |
| 6976 | _pickle.dumps |
| 6977 | |
| 6978 | obj: object |
| 6979 | protocol: object = NULL |
| 6980 | * |
| 6981 | fix_imports: bool = True |
| 6982 | |
| 6983 | Return the pickled representation of the object as a bytes object. |
| 6984 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6985 | The optional *protocol* argument tells the pickler to use the given |
| 6986 | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
| 6987 | protocol is 3; a backward-incompatible protocol designed for Python 3. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6988 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6989 | Specifying a negative protocol version selects the highest protocol |
| 6990 | version supported. The higher the protocol used, the more recent the |
| 6991 | version of Python needed to read the pickle produced. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6992 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 6993 | If *fix_imports* is True and *protocol* is less than 3, pickle will |
| 6994 | try to map the new Python 3 names to the old module names used in |
| 6995 | 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] | 6996 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6997 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 6998 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 6999 | _pickle_dumps_impl(PyModuleDef *module, PyObject *obj, PyObject *protocol, |
| 7000 | int fix_imports) |
| 7001 | /*[clinic end generated code: output=daa380db56fe07b9 input=293dbeda181580b7]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7002 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7003 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7004 | PicklerObject *pickler = _Pickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7005 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7006 | if (pickler == NULL) |
| 7007 | return NULL; |
| 7008 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7009 | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7010 | goto error; |
| 7011 | |
| 7012 | if (dump(pickler, obj) < 0) |
| 7013 | goto error; |
| 7014 | |
| 7015 | result = _Pickler_GetString(pickler); |
| 7016 | Py_DECREF(pickler); |
| 7017 | return result; |
| 7018 | |
| 7019 | error: |
| 7020 | Py_XDECREF(pickler); |
| 7021 | return NULL; |
| 7022 | } |
| 7023 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7024 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7025 | |
| 7026 | _pickle.load |
| 7027 | |
| 7028 | file: object |
| 7029 | * |
| 7030 | fix_imports: bool = True |
| 7031 | encoding: str = 'ASCII' |
| 7032 | errors: str = 'strict' |
| 7033 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7034 | 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] | 7035 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7036 | This is equivalent to ``Unpickler(file).load()``, but may be more |
| 7037 | efficient. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7038 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7039 | The protocol version of the pickle is detected automatically, so no |
| 7040 | protocol argument is needed. Bytes past the pickled object's |
| 7041 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7042 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7043 | The argument *file* must have two methods, a read() method that takes |
| 7044 | an integer argument, and a readline() method that requires no |
| 7045 | arguments. Both methods should return bytes. Thus *file* can be a |
| 7046 | binary file object opened for reading, a io.BytesIO object, or any |
| 7047 | other custom object that meets this interface. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7048 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7049 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7050 | which are used to control compatiblity support for pickle stream |
| 7051 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7052 | map the old Python 2 names to the new names used in Python 3. The |
| 7053 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7054 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7055 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7056 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7057 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7058 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7059 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7060 | _pickle_load_impl(PyModuleDef *module, PyObject *file, int fix_imports, |
| 7061 | const char *encoding, const char *errors) |
| 7062 | /*[clinic end generated code: output=798f1c57cb2b4eb1 input=da97372e38e510a6]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7063 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7064 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7065 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7066 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7067 | if (unpickler == NULL) |
| 7068 | return NULL; |
| 7069 | |
| 7070 | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
| 7071 | goto error; |
| 7072 | |
| 7073 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7074 | goto error; |
| 7075 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7076 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7077 | |
| 7078 | result = load(unpickler); |
| 7079 | Py_DECREF(unpickler); |
| 7080 | return result; |
| 7081 | |
| 7082 | error: |
| 7083 | Py_XDECREF(unpickler); |
| 7084 | return NULL; |
| 7085 | } |
| 7086 | |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7087 | /*[clinic input] |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7088 | |
| 7089 | _pickle.loads |
| 7090 | |
| 7091 | data: object |
| 7092 | * |
| 7093 | fix_imports: bool = True |
| 7094 | encoding: str = 'ASCII' |
| 7095 | errors: str = 'strict' |
| 7096 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7097 | Read and return an object from the given pickle data. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7098 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7099 | The protocol version of the pickle is detected automatically, so no |
| 7100 | protocol argument is needed. Bytes past the pickled object's |
| 7101 | representation are ignored. |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7102 | |
Alexandre Vassalotti | d05c9ff | 2013-12-07 01:09:27 -0800 | [diff] [blame] | 7103 | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
| 7104 | which are used to control compatiblity support for pickle stream |
| 7105 | generated by Python 2. If *fix_imports* is True, pickle will try to |
| 7106 | map the old Python 2 names to the new names used in Python 3. The |
| 7107 | *encoding* and *errors* tell pickle how to decode 8-bit string |
| 7108 | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
| 7109 | respectively. The *encoding* can be 'bytes' to read these 8-bit |
| 7110 | string instances as bytes objects. |
Larry Hastings | 61272b7 | 2014-01-07 12:41:53 -0800 | [diff] [blame] | 7111 | [clinic start generated code]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7112 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7113 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 7114 | _pickle_loads_impl(PyModuleDef *module, PyObject *data, int fix_imports, |
| 7115 | const char *encoding, const char *errors) |
| 7116 | /*[clinic end generated code: output=61e9cdb01e36a736 input=f57f0fdaa2b4cb8b]*/ |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7117 | { |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7118 | PyObject *result; |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7119 | UnpicklerObject *unpickler = _Unpickler_New(); |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7120 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7121 | if (unpickler == NULL) |
| 7122 | return NULL; |
| 7123 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7124 | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7125 | goto error; |
| 7126 | |
| 7127 | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
| 7128 | goto error; |
| 7129 | |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7130 | unpickler->fix_imports = fix_imports; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7131 | |
| 7132 | result = load(unpickler); |
| 7133 | Py_DECREF(unpickler); |
| 7134 | return result; |
| 7135 | |
| 7136 | error: |
| 7137 | Py_XDECREF(unpickler); |
| 7138 | return NULL; |
| 7139 | } |
| 7140 | |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7141 | static struct PyMethodDef pickle_methods[] = { |
Alexandre Vassalotti | ed8c906 | 2013-11-24 12:25:48 -0800 | [diff] [blame] | 7142 | _PICKLE_DUMP_METHODDEF |
| 7143 | _PICKLE_DUMPS_METHODDEF |
| 7144 | _PICKLE_LOAD_METHODDEF |
| 7145 | _PICKLE_LOADS_METHODDEF |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7146 | {NULL, NULL} /* sentinel */ |
| 7147 | }; |
| 7148 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7149 | static int |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7150 | pickle_clear(PyObject *m) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7151 | { |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7152 | _Pickle_ClearState(_Pickle_GetState(m)); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7153 | return 0; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7154 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7155 | |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7156 | static void |
| 7157 | pickle_free(PyObject *m) |
| 7158 | { |
| 7159 | _Pickle_ClearState(_Pickle_GetState(m)); |
| 7160 | } |
| 7161 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7162 | static int |
| 7163 | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
| 7164 | { |
| 7165 | PickleState *st = _Pickle_GetState(m); |
| 7166 | Py_VISIT(st->PickleError); |
| 7167 | Py_VISIT(st->PicklingError); |
| 7168 | Py_VISIT(st->UnpicklingError); |
| 7169 | Py_VISIT(st->dispatch_table); |
| 7170 | Py_VISIT(st->extension_registry); |
| 7171 | Py_VISIT(st->extension_cache); |
| 7172 | Py_VISIT(st->inverted_registry); |
| 7173 | Py_VISIT(st->name_mapping_2to3); |
| 7174 | Py_VISIT(st->import_mapping_2to3); |
| 7175 | Py_VISIT(st->name_mapping_3to2); |
| 7176 | Py_VISIT(st->import_mapping_3to2); |
| 7177 | Py_VISIT(st->codecs_encode); |
Serhiy Storchaka | 58e4134 | 2015-03-31 14:07:24 +0300 | [diff] [blame] | 7178 | Py_VISIT(st->getattr); |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7179 | return 0; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7180 | } |
| 7181 | |
| 7182 | static struct PyModuleDef _picklemodule = { |
| 7183 | PyModuleDef_HEAD_INIT, |
Stefan Krah | f483b0f | 2013-12-14 13:43:10 +0100 | [diff] [blame] | 7184 | "_pickle", /* m_name */ |
| 7185 | pickle_module_doc, /* m_doc */ |
| 7186 | sizeof(PickleState), /* m_size */ |
| 7187 | pickle_methods, /* m_methods */ |
| 7188 | NULL, /* m_reload */ |
| 7189 | pickle_traverse, /* m_traverse */ |
| 7190 | pickle_clear, /* m_clear */ |
| 7191 | (freefunc)pickle_free /* m_free */ |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7192 | }; |
| 7193 | |
| 7194 | PyMODINIT_FUNC |
| 7195 | PyInit__pickle(void) |
| 7196 | { |
| 7197 | PyObject *m; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7198 | PickleState *st; |
| 7199 | |
| 7200 | m = PyState_FindModule(&_picklemodule); |
| 7201 | if (m) { |
| 7202 | Py_INCREF(m); |
| 7203 | return m; |
| 7204 | } |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7205 | |
| 7206 | if (PyType_Ready(&Unpickler_Type) < 0) |
| 7207 | return NULL; |
| 7208 | if (PyType_Ready(&Pickler_Type) < 0) |
| 7209 | return NULL; |
| 7210 | if (PyType_Ready(&Pdata_Type) < 0) |
| 7211 | return NULL; |
Antoine Pitrou | ea99c5c | 2010-09-09 18:33:21 +0000 | [diff] [blame] | 7212 | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
| 7213 | return NULL; |
| 7214 | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
| 7215 | return NULL; |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7216 | |
| 7217 | /* Create the module and add the functions. */ |
| 7218 | m = PyModule_Create(&_picklemodule); |
| 7219 | if (m == NULL) |
| 7220 | return NULL; |
| 7221 | |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7222 | Py_INCREF(&Pickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7223 | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
| 7224 | return NULL; |
Antoine Pitrou | 8391cf4 | 2011-07-15 21:01:21 +0200 | [diff] [blame] | 7225 | Py_INCREF(&Unpickler_Type); |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7226 | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
| 7227 | return NULL; |
| 7228 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7229 | st = _Pickle_GetState(m); |
| 7230 | |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7231 | /* Initialize the exceptions. */ |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7232 | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
| 7233 | if (st->PickleError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7234 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7235 | st->PicklingError = \ |
| 7236 | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
| 7237 | if (st->PicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7238 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7239 | st->UnpicklingError = \ |
| 7240 | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
| 7241 | if (st->UnpicklingError == NULL) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7242 | return NULL; |
| 7243 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7244 | Py_INCREF(st->PickleError); |
| 7245 | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7246 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7247 | Py_INCREF(st->PicklingError); |
| 7248 | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7249 | return NULL; |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7250 | Py_INCREF(st->UnpicklingError); |
| 7251 | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7252 | return NULL; |
| 7253 | |
Alexandre Vassalotti | 23bdd83 | 2013-11-27 19:36:52 -0800 | [diff] [blame] | 7254 | if (_Pickle_InitState(st) < 0) |
Alexandre Vassalotti | ca2d610 | 2008-06-12 18:26:05 +0000 | [diff] [blame] | 7255 | return NULL; |
| 7256 | |
| 7257 | return m; |
| 7258 | } |