| 1 | n/a | #include "Python.h" |
|---|
| 2 | n/a | #include "structmember.h" |
|---|
| 3 | n/a | |
|---|
| 4 | n/a | PyDoc_STRVAR(pickle_module_doc, |
|---|
| 5 | n/a | "Optimized C implementation for the Python pickle module."); |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | /*[clinic input] |
|---|
| 8 | n/a | module _pickle |
|---|
| 9 | n/a | class _pickle.Pickler "PicklerObject *" "&Pickler_Type" |
|---|
| 10 | n/a | class _pickle.PicklerMemoProxy "PicklerMemoProxyObject *" "&PicklerMemoProxyType" |
|---|
| 11 | n/a | class _pickle.Unpickler "UnpicklerObject *" "&Unpickler_Type" |
|---|
| 12 | n/a | class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoProxyType" |
|---|
| 13 | n/a | [clinic start generated code]*/ |
|---|
| 14 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/ |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | /* Bump this when new opcodes are added to the pickle protocol. */ |
|---|
| 17 | n/a | enum { |
|---|
| 18 | n/a | HIGHEST_PROTOCOL = 4, |
|---|
| 19 | n/a | DEFAULT_PROTOCOL = 3 |
|---|
| 20 | n/a | }; |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | /* Pickle opcodes. These must be kept updated with pickle.py. |
|---|
| 23 | n/a | Extensive docs are in pickletools.py. */ |
|---|
| 24 | n/a | enum opcode { |
|---|
| 25 | n/a | MARK = '(', |
|---|
| 26 | n/a | STOP = '.', |
|---|
| 27 | n/a | POP = '0', |
|---|
| 28 | n/a | POP_MARK = '1', |
|---|
| 29 | n/a | DUP = '2', |
|---|
| 30 | n/a | FLOAT = 'F', |
|---|
| 31 | n/a | INT = 'I', |
|---|
| 32 | n/a | BININT = 'J', |
|---|
| 33 | n/a | BININT1 = 'K', |
|---|
| 34 | n/a | LONG = 'L', |
|---|
| 35 | n/a | BININT2 = 'M', |
|---|
| 36 | n/a | NONE = 'N', |
|---|
| 37 | n/a | PERSID = 'P', |
|---|
| 38 | n/a | BINPERSID = 'Q', |
|---|
| 39 | n/a | REDUCE = 'R', |
|---|
| 40 | n/a | STRING = 'S', |
|---|
| 41 | n/a | BINSTRING = 'T', |
|---|
| 42 | n/a | SHORT_BINSTRING = 'U', |
|---|
| 43 | n/a | UNICODE = 'V', |
|---|
| 44 | n/a | BINUNICODE = 'X', |
|---|
| 45 | n/a | APPEND = 'a', |
|---|
| 46 | n/a | BUILD = 'b', |
|---|
| 47 | n/a | GLOBAL = 'c', |
|---|
| 48 | n/a | DICT = 'd', |
|---|
| 49 | n/a | EMPTY_DICT = '}', |
|---|
| 50 | n/a | APPENDS = 'e', |
|---|
| 51 | n/a | GET = 'g', |
|---|
| 52 | n/a | BINGET = 'h', |
|---|
| 53 | n/a | INST = 'i', |
|---|
| 54 | n/a | LONG_BINGET = 'j', |
|---|
| 55 | n/a | LIST = 'l', |
|---|
| 56 | n/a | EMPTY_LIST = ']', |
|---|
| 57 | n/a | OBJ = 'o', |
|---|
| 58 | n/a | PUT = 'p', |
|---|
| 59 | n/a | BINPUT = 'q', |
|---|
| 60 | n/a | LONG_BINPUT = 'r', |
|---|
| 61 | n/a | SETITEM = 's', |
|---|
| 62 | n/a | TUPLE = 't', |
|---|
| 63 | n/a | EMPTY_TUPLE = ')', |
|---|
| 64 | n/a | SETITEMS = 'u', |
|---|
| 65 | n/a | BINFLOAT = 'G', |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | /* Protocol 2. */ |
|---|
| 68 | n/a | PROTO = '\x80', |
|---|
| 69 | n/a | NEWOBJ = '\x81', |
|---|
| 70 | n/a | EXT1 = '\x82', |
|---|
| 71 | n/a | EXT2 = '\x83', |
|---|
| 72 | n/a | EXT4 = '\x84', |
|---|
| 73 | n/a | TUPLE1 = '\x85', |
|---|
| 74 | n/a | TUPLE2 = '\x86', |
|---|
| 75 | n/a | TUPLE3 = '\x87', |
|---|
| 76 | n/a | NEWTRUE = '\x88', |
|---|
| 77 | n/a | NEWFALSE = '\x89', |
|---|
| 78 | n/a | LONG1 = '\x8a', |
|---|
| 79 | n/a | LONG4 = '\x8b', |
|---|
| 80 | n/a | |
|---|
| 81 | n/a | /* Protocol 3 (Python 3.x) */ |
|---|
| 82 | n/a | BINBYTES = 'B', |
|---|
| 83 | n/a | SHORT_BINBYTES = 'C', |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | /* Protocol 4 */ |
|---|
| 86 | n/a | SHORT_BINUNICODE = '\x8c', |
|---|
| 87 | n/a | BINUNICODE8 = '\x8d', |
|---|
| 88 | n/a | BINBYTES8 = '\x8e', |
|---|
| 89 | n/a | EMPTY_SET = '\x8f', |
|---|
| 90 | n/a | ADDITEMS = '\x90', |
|---|
| 91 | n/a | FROZENSET = '\x91', |
|---|
| 92 | n/a | NEWOBJ_EX = '\x92', |
|---|
| 93 | n/a | STACK_GLOBAL = '\x93', |
|---|
| 94 | n/a | MEMOIZE = '\x94', |
|---|
| 95 | n/a | FRAME = '\x95' |
|---|
| 96 | n/a | }; |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | enum { |
|---|
| 99 | n/a | /* Keep in synch with pickle.Pickler._BATCHSIZE. This is how many elements |
|---|
| 100 | n/a | batch_list/dict() pumps out before doing APPENDS/SETITEMS. Nothing will |
|---|
| 101 | n/a | break if this gets out of synch with pickle.py, but it's unclear that would |
|---|
| 102 | n/a | help anything either. */ |
|---|
| 103 | n/a | BATCHSIZE = 1000, |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | /* Nesting limit until Pickler, when running in "fast mode", starts |
|---|
| 106 | n/a | checking for self-referential data-structures. */ |
|---|
| 107 | n/a | FAST_NESTING_LIMIT = 50, |
|---|
| 108 | n/a | |
|---|
| 109 | n/a | /* Initial size of the write buffer of Pickler. */ |
|---|
| 110 | n/a | WRITE_BUF_SIZE = 4096, |
|---|
| 111 | n/a | |
|---|
| 112 | n/a | /* Prefetch size when unpickling (disabled on unpeekable streams) */ |
|---|
| 113 | n/a | PREFETCH = 8192 * 16, |
|---|
| 114 | n/a | |
|---|
| 115 | n/a | FRAME_SIZE_TARGET = 64 * 1024, |
|---|
| 116 | n/a | |
|---|
| 117 | n/a | FRAME_HEADER_SIZE = 9 |
|---|
| 118 | n/a | }; |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | /*************************************************************************/ |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | /* State of the pickle module, per PEP 3121. */ |
|---|
| 123 | n/a | typedef struct { |
|---|
| 124 | n/a | /* Exception classes for pickle. */ |
|---|
| 125 | n/a | PyObject *PickleError; |
|---|
| 126 | n/a | PyObject *PicklingError; |
|---|
| 127 | n/a | PyObject *UnpicklingError; |
|---|
| 128 | n/a | |
|---|
| 129 | n/a | /* copyreg.dispatch_table, {type_object: pickling_function} */ |
|---|
| 130 | n/a | PyObject *dispatch_table; |
|---|
| 131 | n/a | |
|---|
| 132 | n/a | /* For the extension opcodes EXT1, EXT2 and EXT4. */ |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | /* copyreg._extension_registry, {(module_name, function_name): code} */ |
|---|
| 135 | n/a | PyObject *extension_registry; |
|---|
| 136 | n/a | /* copyreg._extension_cache, {code: object} */ |
|---|
| 137 | n/a | PyObject *extension_cache; |
|---|
| 138 | n/a | /* copyreg._inverted_registry, {code: (module_name, function_name)} */ |
|---|
| 139 | n/a | PyObject *inverted_registry; |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | /* Import mappings for compatibility with Python 2.x */ |
|---|
| 142 | n/a | |
|---|
| 143 | n/a | /* _compat_pickle.NAME_MAPPING, |
|---|
| 144 | n/a | {(oldmodule, oldname): (newmodule, newname)} */ |
|---|
| 145 | n/a | PyObject *name_mapping_2to3; |
|---|
| 146 | n/a | /* _compat_pickle.IMPORT_MAPPING, {oldmodule: newmodule} */ |
|---|
| 147 | n/a | PyObject *import_mapping_2to3; |
|---|
| 148 | n/a | /* Same, but with REVERSE_NAME_MAPPING / REVERSE_IMPORT_MAPPING */ |
|---|
| 149 | n/a | PyObject *name_mapping_3to2; |
|---|
| 150 | n/a | PyObject *import_mapping_3to2; |
|---|
| 151 | n/a | |
|---|
| 152 | n/a | /* codecs.encode, used for saving bytes in older protocols */ |
|---|
| 153 | n/a | PyObject *codecs_encode; |
|---|
| 154 | n/a | /* builtins.getattr, used for saving nested names with protocol < 4 */ |
|---|
| 155 | n/a | PyObject *getattr; |
|---|
| 156 | n/a | /* functools.partial, used for implementing __newobj_ex__ with protocols |
|---|
| 157 | n/a | 2 and 3 */ |
|---|
| 158 | n/a | PyObject *partial; |
|---|
| 159 | n/a | } PickleState; |
|---|
| 160 | n/a | |
|---|
| 161 | n/a | /* Forward declaration of the _pickle module definition. */ |
|---|
| 162 | n/a | static struct PyModuleDef _picklemodule; |
|---|
| 163 | n/a | |
|---|
| 164 | n/a | /* Given a module object, get its per-module state. */ |
|---|
| 165 | n/a | static PickleState * |
|---|
| 166 | n/a | _Pickle_GetState(PyObject *module) |
|---|
| 167 | n/a | { |
|---|
| 168 | n/a | return (PickleState *)PyModule_GetState(module); |
|---|
| 169 | n/a | } |
|---|
| 170 | n/a | |
|---|
| 171 | n/a | /* Find the module instance imported in the currently running sub-interpreter |
|---|
| 172 | n/a | and get its state. */ |
|---|
| 173 | n/a | static PickleState * |
|---|
| 174 | n/a | _Pickle_GetGlobalState(void) |
|---|
| 175 | n/a | { |
|---|
| 176 | n/a | return _Pickle_GetState(PyState_FindModule(&_picklemodule)); |
|---|
| 177 | n/a | } |
|---|
| 178 | n/a | |
|---|
| 179 | n/a | /* Clear the given pickle module state. */ |
|---|
| 180 | n/a | static void |
|---|
| 181 | n/a | _Pickle_ClearState(PickleState *st) |
|---|
| 182 | n/a | { |
|---|
| 183 | n/a | Py_CLEAR(st->PickleError); |
|---|
| 184 | n/a | Py_CLEAR(st->PicklingError); |
|---|
| 185 | n/a | Py_CLEAR(st->UnpicklingError); |
|---|
| 186 | n/a | Py_CLEAR(st->dispatch_table); |
|---|
| 187 | n/a | Py_CLEAR(st->extension_registry); |
|---|
| 188 | n/a | Py_CLEAR(st->extension_cache); |
|---|
| 189 | n/a | Py_CLEAR(st->inverted_registry); |
|---|
| 190 | n/a | Py_CLEAR(st->name_mapping_2to3); |
|---|
| 191 | n/a | Py_CLEAR(st->import_mapping_2to3); |
|---|
| 192 | n/a | Py_CLEAR(st->name_mapping_3to2); |
|---|
| 193 | n/a | Py_CLEAR(st->import_mapping_3to2); |
|---|
| 194 | n/a | Py_CLEAR(st->codecs_encode); |
|---|
| 195 | n/a | Py_CLEAR(st->getattr); |
|---|
| 196 | n/a | Py_CLEAR(st->partial); |
|---|
| 197 | n/a | } |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | /* Initialize the given pickle module state. */ |
|---|
| 200 | n/a | static int |
|---|
| 201 | n/a | _Pickle_InitState(PickleState *st) |
|---|
| 202 | n/a | { |
|---|
| 203 | n/a | PyObject *builtins; |
|---|
| 204 | n/a | PyObject *copyreg = NULL; |
|---|
| 205 | n/a | PyObject *compat_pickle = NULL; |
|---|
| 206 | n/a | PyObject *codecs = NULL; |
|---|
| 207 | n/a | PyObject *functools = NULL; |
|---|
| 208 | n/a | |
|---|
| 209 | n/a | builtins = PyEval_GetBuiltins(); |
|---|
| 210 | n/a | if (builtins == NULL) |
|---|
| 211 | n/a | goto error; |
|---|
| 212 | n/a | st->getattr = PyDict_GetItemString(builtins, "getattr"); |
|---|
| 213 | n/a | if (st->getattr == NULL) |
|---|
| 214 | n/a | goto error; |
|---|
| 215 | n/a | Py_INCREF(st->getattr); |
|---|
| 216 | n/a | |
|---|
| 217 | n/a | copyreg = PyImport_ImportModule("copyreg"); |
|---|
| 218 | n/a | if (!copyreg) |
|---|
| 219 | n/a | goto error; |
|---|
| 220 | n/a | st->dispatch_table = PyObject_GetAttrString(copyreg, "dispatch_table"); |
|---|
| 221 | n/a | if (!st->dispatch_table) |
|---|
| 222 | n/a | goto error; |
|---|
| 223 | n/a | if (!PyDict_CheckExact(st->dispatch_table)) { |
|---|
| 224 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 225 | n/a | "copyreg.dispatch_table should be a dict, not %.200s", |
|---|
| 226 | n/a | Py_TYPE(st->dispatch_table)->tp_name); |
|---|
| 227 | n/a | goto error; |
|---|
| 228 | n/a | } |
|---|
| 229 | n/a | st->extension_registry = \ |
|---|
| 230 | n/a | PyObject_GetAttrString(copyreg, "_extension_registry"); |
|---|
| 231 | n/a | if (!st->extension_registry) |
|---|
| 232 | n/a | goto error; |
|---|
| 233 | n/a | if (!PyDict_CheckExact(st->extension_registry)) { |
|---|
| 234 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 235 | n/a | "copyreg._extension_registry should be a dict, " |
|---|
| 236 | n/a | "not %.200s", Py_TYPE(st->extension_registry)->tp_name); |
|---|
| 237 | n/a | goto error; |
|---|
| 238 | n/a | } |
|---|
| 239 | n/a | st->inverted_registry = \ |
|---|
| 240 | n/a | PyObject_GetAttrString(copyreg, "_inverted_registry"); |
|---|
| 241 | n/a | if (!st->inverted_registry) |
|---|
| 242 | n/a | goto error; |
|---|
| 243 | n/a | if (!PyDict_CheckExact(st->inverted_registry)) { |
|---|
| 244 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 245 | n/a | "copyreg._inverted_registry should be a dict, " |
|---|
| 246 | n/a | "not %.200s", Py_TYPE(st->inverted_registry)->tp_name); |
|---|
| 247 | n/a | goto error; |
|---|
| 248 | n/a | } |
|---|
| 249 | n/a | st->extension_cache = PyObject_GetAttrString(copyreg, "_extension_cache"); |
|---|
| 250 | n/a | if (!st->extension_cache) |
|---|
| 251 | n/a | goto error; |
|---|
| 252 | n/a | if (!PyDict_CheckExact(st->extension_cache)) { |
|---|
| 253 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 254 | n/a | "copyreg._extension_cache should be a dict, " |
|---|
| 255 | n/a | "not %.200s", Py_TYPE(st->extension_cache)->tp_name); |
|---|
| 256 | n/a | goto error; |
|---|
| 257 | n/a | } |
|---|
| 258 | n/a | Py_CLEAR(copyreg); |
|---|
| 259 | n/a | |
|---|
| 260 | n/a | /* Load the 2.x -> 3.x stdlib module mapping tables */ |
|---|
| 261 | n/a | compat_pickle = PyImport_ImportModule("_compat_pickle"); |
|---|
| 262 | n/a | if (!compat_pickle) |
|---|
| 263 | n/a | goto error; |
|---|
| 264 | n/a | st->name_mapping_2to3 = \ |
|---|
| 265 | n/a | PyObject_GetAttrString(compat_pickle, "NAME_MAPPING"); |
|---|
| 266 | n/a | if (!st->name_mapping_2to3) |
|---|
| 267 | n/a | goto error; |
|---|
| 268 | n/a | if (!PyDict_CheckExact(st->name_mapping_2to3)) { |
|---|
| 269 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 270 | n/a | "_compat_pickle.NAME_MAPPING should be a dict, not %.200s", |
|---|
| 271 | n/a | Py_TYPE(st->name_mapping_2to3)->tp_name); |
|---|
| 272 | n/a | goto error; |
|---|
| 273 | n/a | } |
|---|
| 274 | n/a | st->import_mapping_2to3 = \ |
|---|
| 275 | n/a | PyObject_GetAttrString(compat_pickle, "IMPORT_MAPPING"); |
|---|
| 276 | n/a | if (!st->import_mapping_2to3) |
|---|
| 277 | n/a | goto error; |
|---|
| 278 | n/a | if (!PyDict_CheckExact(st->import_mapping_2to3)) { |
|---|
| 279 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 280 | n/a | "_compat_pickle.IMPORT_MAPPING should be a dict, " |
|---|
| 281 | n/a | "not %.200s", Py_TYPE(st->import_mapping_2to3)->tp_name); |
|---|
| 282 | n/a | goto error; |
|---|
| 283 | n/a | } |
|---|
| 284 | n/a | /* ... and the 3.x -> 2.x mapping tables */ |
|---|
| 285 | n/a | st->name_mapping_3to2 = \ |
|---|
| 286 | n/a | PyObject_GetAttrString(compat_pickle, "REVERSE_NAME_MAPPING"); |
|---|
| 287 | n/a | if (!st->name_mapping_3to2) |
|---|
| 288 | n/a | goto error; |
|---|
| 289 | n/a | if (!PyDict_CheckExact(st->name_mapping_3to2)) { |
|---|
| 290 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 291 | n/a | "_compat_pickle.REVERSE_NAME_MAPPING should be a dict, " |
|---|
| 292 | n/a | "not %.200s", Py_TYPE(st->name_mapping_3to2)->tp_name); |
|---|
| 293 | n/a | goto error; |
|---|
| 294 | n/a | } |
|---|
| 295 | n/a | st->import_mapping_3to2 = \ |
|---|
| 296 | n/a | PyObject_GetAttrString(compat_pickle, "REVERSE_IMPORT_MAPPING"); |
|---|
| 297 | n/a | if (!st->import_mapping_3to2) |
|---|
| 298 | n/a | goto error; |
|---|
| 299 | n/a | if (!PyDict_CheckExact(st->import_mapping_3to2)) { |
|---|
| 300 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 301 | n/a | "_compat_pickle.REVERSE_IMPORT_MAPPING should be a dict, " |
|---|
| 302 | n/a | "not %.200s", Py_TYPE(st->import_mapping_3to2)->tp_name); |
|---|
| 303 | n/a | goto error; |
|---|
| 304 | n/a | } |
|---|
| 305 | n/a | Py_CLEAR(compat_pickle); |
|---|
| 306 | n/a | |
|---|
| 307 | n/a | codecs = PyImport_ImportModule("codecs"); |
|---|
| 308 | n/a | if (codecs == NULL) |
|---|
| 309 | n/a | goto error; |
|---|
| 310 | n/a | st->codecs_encode = PyObject_GetAttrString(codecs, "encode"); |
|---|
| 311 | n/a | if (st->codecs_encode == NULL) { |
|---|
| 312 | n/a | goto error; |
|---|
| 313 | n/a | } |
|---|
| 314 | n/a | if (!PyCallable_Check(st->codecs_encode)) { |
|---|
| 315 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 316 | n/a | "codecs.encode should be a callable, not %.200s", |
|---|
| 317 | n/a | Py_TYPE(st->codecs_encode)->tp_name); |
|---|
| 318 | n/a | goto error; |
|---|
| 319 | n/a | } |
|---|
| 320 | n/a | Py_CLEAR(codecs); |
|---|
| 321 | n/a | |
|---|
| 322 | n/a | functools = PyImport_ImportModule("functools"); |
|---|
| 323 | n/a | if (!functools) |
|---|
| 324 | n/a | goto error; |
|---|
| 325 | n/a | st->partial = PyObject_GetAttrString(functools, "partial"); |
|---|
| 326 | n/a | if (!st->partial) |
|---|
| 327 | n/a | goto error; |
|---|
| 328 | n/a | Py_CLEAR(functools); |
|---|
| 329 | n/a | |
|---|
| 330 | n/a | return 0; |
|---|
| 331 | n/a | |
|---|
| 332 | n/a | error: |
|---|
| 333 | n/a | Py_CLEAR(copyreg); |
|---|
| 334 | n/a | Py_CLEAR(compat_pickle); |
|---|
| 335 | n/a | Py_CLEAR(codecs); |
|---|
| 336 | n/a | Py_CLEAR(functools); |
|---|
| 337 | n/a | _Pickle_ClearState(st); |
|---|
| 338 | n/a | return -1; |
|---|
| 339 | n/a | } |
|---|
| 340 | n/a | |
|---|
| 341 | n/a | /* Helper for calling a function with a single argument quickly. |
|---|
| 342 | n/a | |
|---|
| 343 | n/a | This function steals the reference of the given argument. */ |
|---|
| 344 | n/a | static PyObject * |
|---|
| 345 | n/a | _Pickle_FastCall(PyObject *func, PyObject *obj) |
|---|
| 346 | n/a | { |
|---|
| 347 | n/a | PyObject *result; |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | result = PyObject_CallFunctionObjArgs(func, obj, NULL); |
|---|
| 350 | n/a | Py_DECREF(obj); |
|---|
| 351 | n/a | return result; |
|---|
| 352 | n/a | } |
|---|
| 353 | n/a | |
|---|
| 354 | n/a | /*************************************************************************/ |
|---|
| 355 | n/a | |
|---|
| 356 | n/a | /* Internal data type used as the unpickling stack. */ |
|---|
| 357 | n/a | typedef struct { |
|---|
| 358 | n/a | PyObject_VAR_HEAD |
|---|
| 359 | n/a | PyObject **data; |
|---|
| 360 | n/a | int mark_set; /* is MARK set? */ |
|---|
| 361 | n/a | Py_ssize_t fence; /* position of top MARK or 0 */ |
|---|
| 362 | n/a | Py_ssize_t allocated; /* number of slots in data allocated */ |
|---|
| 363 | n/a | } Pdata; |
|---|
| 364 | n/a | |
|---|
| 365 | n/a | static void |
|---|
| 366 | n/a | Pdata_dealloc(Pdata *self) |
|---|
| 367 | n/a | { |
|---|
| 368 | n/a | Py_ssize_t i = Py_SIZE(self); |
|---|
| 369 | n/a | while (--i >= 0) { |
|---|
| 370 | n/a | Py_DECREF(self->data[i]); |
|---|
| 371 | n/a | } |
|---|
| 372 | n/a | PyMem_FREE(self->data); |
|---|
| 373 | n/a | PyObject_Del(self); |
|---|
| 374 | n/a | } |
|---|
| 375 | n/a | |
|---|
| 376 | n/a | static PyTypeObject Pdata_Type = { |
|---|
| 377 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 378 | n/a | "_pickle.Pdata", /*tp_name*/ |
|---|
| 379 | n/a | sizeof(Pdata), /*tp_basicsize*/ |
|---|
| 380 | n/a | sizeof(PyObject *), /*tp_itemsize*/ |
|---|
| 381 | n/a | (destructor)Pdata_dealloc, /*tp_dealloc*/ |
|---|
| 382 | n/a | }; |
|---|
| 383 | n/a | |
|---|
| 384 | n/a | static PyObject * |
|---|
| 385 | n/a | Pdata_New(void) |
|---|
| 386 | n/a | { |
|---|
| 387 | n/a | Pdata *self; |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | if (!(self = PyObject_New(Pdata, &Pdata_Type))) |
|---|
| 390 | n/a | return NULL; |
|---|
| 391 | n/a | Py_SIZE(self) = 0; |
|---|
| 392 | n/a | self->mark_set = 0; |
|---|
| 393 | n/a | self->fence = 0; |
|---|
| 394 | n/a | self->allocated = 8; |
|---|
| 395 | n/a | self->data = PyMem_MALLOC(self->allocated * sizeof(PyObject *)); |
|---|
| 396 | n/a | if (self->data) |
|---|
| 397 | n/a | return (PyObject *)self; |
|---|
| 398 | n/a | Py_DECREF(self); |
|---|
| 399 | n/a | return PyErr_NoMemory(); |
|---|
| 400 | n/a | } |
|---|
| 401 | n/a | |
|---|
| 402 | n/a | |
|---|
| 403 | n/a | /* Retain only the initial clearto items. If clearto >= the current |
|---|
| 404 | n/a | * number of items, this is a (non-erroneous) NOP. |
|---|
| 405 | n/a | */ |
|---|
| 406 | n/a | static int |
|---|
| 407 | n/a | Pdata_clear(Pdata *self, Py_ssize_t clearto) |
|---|
| 408 | n/a | { |
|---|
| 409 | n/a | Py_ssize_t i = Py_SIZE(self); |
|---|
| 410 | n/a | |
|---|
| 411 | n/a | assert(clearto >= self->fence); |
|---|
| 412 | n/a | if (clearto >= i) |
|---|
| 413 | n/a | return 0; |
|---|
| 414 | n/a | |
|---|
| 415 | n/a | while (--i >= clearto) { |
|---|
| 416 | n/a | Py_CLEAR(self->data[i]); |
|---|
| 417 | n/a | } |
|---|
| 418 | n/a | Py_SIZE(self) = clearto; |
|---|
| 419 | n/a | return 0; |
|---|
| 420 | n/a | } |
|---|
| 421 | n/a | |
|---|
| 422 | n/a | static int |
|---|
| 423 | n/a | Pdata_grow(Pdata *self) |
|---|
| 424 | n/a | { |
|---|
| 425 | n/a | PyObject **data = self->data; |
|---|
| 426 | n/a | size_t allocated = (size_t)self->allocated; |
|---|
| 427 | n/a | size_t new_allocated; |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | new_allocated = (allocated >> 3) + 6; |
|---|
| 430 | n/a | /* check for integer overflow */ |
|---|
| 431 | n/a | if (new_allocated > (size_t)PY_SSIZE_T_MAX - allocated) |
|---|
| 432 | n/a | goto nomemory; |
|---|
| 433 | n/a | new_allocated += allocated; |
|---|
| 434 | n/a | PyMem_RESIZE(data, PyObject *, new_allocated); |
|---|
| 435 | n/a | if (data == NULL) |
|---|
| 436 | n/a | goto nomemory; |
|---|
| 437 | n/a | |
|---|
| 438 | n/a | self->data = data; |
|---|
| 439 | n/a | self->allocated = (Py_ssize_t)new_allocated; |
|---|
| 440 | n/a | return 0; |
|---|
| 441 | n/a | |
|---|
| 442 | n/a | nomemory: |
|---|
| 443 | n/a | PyErr_NoMemory(); |
|---|
| 444 | n/a | return -1; |
|---|
| 445 | n/a | } |
|---|
| 446 | n/a | |
|---|
| 447 | n/a | static int |
|---|
| 448 | n/a | Pdata_stack_underflow(Pdata *self) |
|---|
| 449 | n/a | { |
|---|
| 450 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 451 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 452 | n/a | self->mark_set ? |
|---|
| 453 | n/a | "unexpected MARK found" : |
|---|
| 454 | n/a | "unpickling stack underflow"); |
|---|
| 455 | n/a | return -1; |
|---|
| 456 | n/a | } |
|---|
| 457 | n/a | |
|---|
| 458 | n/a | /* D is a Pdata*. Pop the topmost element and store it into V, which |
|---|
| 459 | n/a | * must be an lvalue holding PyObject*. On stack underflow, UnpicklingError |
|---|
| 460 | n/a | * is raised and V is set to NULL. |
|---|
| 461 | n/a | */ |
|---|
| 462 | n/a | static PyObject * |
|---|
| 463 | n/a | Pdata_pop(Pdata *self) |
|---|
| 464 | n/a | { |
|---|
| 465 | n/a | if (Py_SIZE(self) <= self->fence) { |
|---|
| 466 | n/a | Pdata_stack_underflow(self); |
|---|
| 467 | n/a | return NULL; |
|---|
| 468 | n/a | } |
|---|
| 469 | n/a | return self->data[--Py_SIZE(self)]; |
|---|
| 470 | n/a | } |
|---|
| 471 | n/a | #define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0) |
|---|
| 472 | n/a | |
|---|
| 473 | n/a | static int |
|---|
| 474 | n/a | Pdata_push(Pdata *self, PyObject *obj) |
|---|
| 475 | n/a | { |
|---|
| 476 | n/a | if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) { |
|---|
| 477 | n/a | return -1; |
|---|
| 478 | n/a | } |
|---|
| 479 | n/a | self->data[Py_SIZE(self)++] = obj; |
|---|
| 480 | n/a | return 0; |
|---|
| 481 | n/a | } |
|---|
| 482 | n/a | |
|---|
| 483 | n/a | /* Push an object on stack, transferring its ownership to the stack. */ |
|---|
| 484 | n/a | #define PDATA_PUSH(D, O, ER) do { \ |
|---|
| 485 | n/a | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
|---|
| 486 | n/a | |
|---|
| 487 | n/a | /* Push an object on stack, adding a new reference to the object. */ |
|---|
| 488 | n/a | #define PDATA_APPEND(D, O, ER) do { \ |
|---|
| 489 | n/a | Py_INCREF((O)); \ |
|---|
| 490 | n/a | if (Pdata_push((D), (O)) < 0) return (ER); } while(0) |
|---|
| 491 | n/a | |
|---|
| 492 | n/a | static PyObject * |
|---|
| 493 | n/a | Pdata_poptuple(Pdata *self, Py_ssize_t start) |
|---|
| 494 | n/a | { |
|---|
| 495 | n/a | PyObject *tuple; |
|---|
| 496 | n/a | Py_ssize_t len, i, j; |
|---|
| 497 | n/a | |
|---|
| 498 | n/a | if (start < self->fence) { |
|---|
| 499 | n/a | Pdata_stack_underflow(self); |
|---|
| 500 | n/a | return NULL; |
|---|
| 501 | n/a | } |
|---|
| 502 | n/a | len = Py_SIZE(self) - start; |
|---|
| 503 | n/a | tuple = PyTuple_New(len); |
|---|
| 504 | n/a | if (tuple == NULL) |
|---|
| 505 | n/a | return NULL; |
|---|
| 506 | n/a | for (i = start, j = 0; j < len; i++, j++) |
|---|
| 507 | n/a | PyTuple_SET_ITEM(tuple, j, self->data[i]); |
|---|
| 508 | n/a | |
|---|
| 509 | n/a | Py_SIZE(self) = start; |
|---|
| 510 | n/a | return tuple; |
|---|
| 511 | n/a | } |
|---|
| 512 | n/a | |
|---|
| 513 | n/a | static PyObject * |
|---|
| 514 | n/a | Pdata_poplist(Pdata *self, Py_ssize_t start) |
|---|
| 515 | n/a | { |
|---|
| 516 | n/a | PyObject *list; |
|---|
| 517 | n/a | Py_ssize_t len, i, j; |
|---|
| 518 | n/a | |
|---|
| 519 | n/a | len = Py_SIZE(self) - start; |
|---|
| 520 | n/a | list = PyList_New(len); |
|---|
| 521 | n/a | if (list == NULL) |
|---|
| 522 | n/a | return NULL; |
|---|
| 523 | n/a | for (i = start, j = 0; j < len; i++, j++) |
|---|
| 524 | n/a | PyList_SET_ITEM(list, j, self->data[i]); |
|---|
| 525 | n/a | |
|---|
| 526 | n/a | Py_SIZE(self) = start; |
|---|
| 527 | n/a | return list; |
|---|
| 528 | n/a | } |
|---|
| 529 | n/a | |
|---|
| 530 | n/a | typedef struct { |
|---|
| 531 | n/a | PyObject *me_key; |
|---|
| 532 | n/a | Py_ssize_t me_value; |
|---|
| 533 | n/a | } PyMemoEntry; |
|---|
| 534 | n/a | |
|---|
| 535 | n/a | typedef struct { |
|---|
| 536 | n/a | Py_ssize_t mt_mask; |
|---|
| 537 | n/a | Py_ssize_t mt_used; |
|---|
| 538 | n/a | Py_ssize_t mt_allocated; |
|---|
| 539 | n/a | PyMemoEntry *mt_table; |
|---|
| 540 | n/a | } PyMemoTable; |
|---|
| 541 | n/a | |
|---|
| 542 | n/a | typedef struct PicklerObject { |
|---|
| 543 | n/a | PyObject_HEAD |
|---|
| 544 | n/a | PyMemoTable *memo; /* Memo table, keep track of the seen |
|---|
| 545 | n/a | objects to support self-referential objects |
|---|
| 546 | n/a | pickling. */ |
|---|
| 547 | n/a | PyObject *pers_func; /* persistent_id() method, can be NULL */ |
|---|
| 548 | n/a | PyObject *dispatch_table; /* private dispatch_table, can be NULL */ |
|---|
| 549 | n/a | |
|---|
| 550 | n/a | PyObject *write; /* write() method of the output stream. */ |
|---|
| 551 | n/a | PyObject *output_buffer; /* Write into a local bytearray buffer before |
|---|
| 552 | n/a | flushing to the stream. */ |
|---|
| 553 | n/a | Py_ssize_t output_len; /* Length of output_buffer. */ |
|---|
| 554 | n/a | Py_ssize_t max_output_len; /* Allocation size of output_buffer. */ |
|---|
| 555 | n/a | int proto; /* Pickle protocol number, >= 0 */ |
|---|
| 556 | n/a | int bin; /* Boolean, true if proto > 0 */ |
|---|
| 557 | n/a | int framing; /* True when framing is enabled, proto >= 4 */ |
|---|
| 558 | n/a | Py_ssize_t frame_start; /* Position in output_buffer where the |
|---|
| 559 | n/a | current frame begins. -1 if there |
|---|
| 560 | n/a | is no frame currently open. */ |
|---|
| 561 | n/a | |
|---|
| 562 | n/a | Py_ssize_t buf_size; /* Size of the current buffered pickle data */ |
|---|
| 563 | n/a | int fast; /* Enable fast mode if set to a true value. |
|---|
| 564 | n/a | The fast mode disable the usage of memo, |
|---|
| 565 | n/a | therefore speeding the pickling process by |
|---|
| 566 | n/a | not generating superfluous PUT opcodes. It |
|---|
| 567 | n/a | should not be used if with self-referential |
|---|
| 568 | n/a | objects. */ |
|---|
| 569 | n/a | int fast_nesting; |
|---|
| 570 | n/a | int fix_imports; /* Indicate whether Pickler should fix |
|---|
| 571 | n/a | the name of globals for Python 2.x. */ |
|---|
| 572 | n/a | PyObject *fast_memo; |
|---|
| 573 | n/a | } PicklerObject; |
|---|
| 574 | n/a | |
|---|
| 575 | n/a | typedef struct UnpicklerObject { |
|---|
| 576 | n/a | PyObject_HEAD |
|---|
| 577 | n/a | Pdata *stack; /* Pickle data stack, store unpickled objects. */ |
|---|
| 578 | n/a | |
|---|
| 579 | n/a | /* The unpickler memo is just an array of PyObject *s. Using a dict |
|---|
| 580 | n/a | is unnecessary, since the keys are contiguous ints. */ |
|---|
| 581 | n/a | PyObject **memo; |
|---|
| 582 | n/a | Py_ssize_t memo_size; /* Capacity of the memo array */ |
|---|
| 583 | n/a | Py_ssize_t memo_len; /* Number of objects in the memo */ |
|---|
| 584 | n/a | |
|---|
| 585 | n/a | PyObject *pers_func; /* persistent_load() method, can be NULL. */ |
|---|
| 586 | n/a | |
|---|
| 587 | n/a | Py_buffer buffer; |
|---|
| 588 | n/a | char *input_buffer; |
|---|
| 589 | n/a | char *input_line; |
|---|
| 590 | n/a | Py_ssize_t input_len; |
|---|
| 591 | n/a | Py_ssize_t next_read_idx; |
|---|
| 592 | n/a | Py_ssize_t prefetched_idx; /* index of first prefetched byte */ |
|---|
| 593 | n/a | |
|---|
| 594 | n/a | PyObject *read; /* read() method of the input stream. */ |
|---|
| 595 | n/a | PyObject *readline; /* readline() method of the input stream. */ |
|---|
| 596 | n/a | PyObject *peek; /* peek() method of the input stream, or NULL */ |
|---|
| 597 | n/a | |
|---|
| 598 | n/a | char *encoding; /* Name of the encoding to be used for |
|---|
| 599 | n/a | decoding strings pickled using Python |
|---|
| 600 | n/a | 2.x. The default value is "ASCII" */ |
|---|
| 601 | n/a | char *errors; /* Name of errors handling scheme to used when |
|---|
| 602 | n/a | decoding strings. The default value is |
|---|
| 603 | n/a | "strict". */ |
|---|
| 604 | n/a | Py_ssize_t *marks; /* Mark stack, used for unpickling container |
|---|
| 605 | n/a | objects. */ |
|---|
| 606 | n/a | Py_ssize_t num_marks; /* Number of marks in the mark stack. */ |
|---|
| 607 | n/a | Py_ssize_t marks_size; /* Current allocated size of the mark stack. */ |
|---|
| 608 | n/a | int proto; /* Protocol of the pickle loaded. */ |
|---|
| 609 | n/a | int fix_imports; /* Indicate whether Unpickler should fix |
|---|
| 610 | n/a | the name of globals pickled by Python 2.x. */ |
|---|
| 611 | n/a | } UnpicklerObject; |
|---|
| 612 | n/a | |
|---|
| 613 | n/a | typedef struct { |
|---|
| 614 | n/a | PyObject_HEAD |
|---|
| 615 | n/a | PicklerObject *pickler; /* Pickler whose memo table we're proxying. */ |
|---|
| 616 | n/a | } PicklerMemoProxyObject; |
|---|
| 617 | n/a | |
|---|
| 618 | n/a | typedef struct { |
|---|
| 619 | n/a | PyObject_HEAD |
|---|
| 620 | n/a | UnpicklerObject *unpickler; |
|---|
| 621 | n/a | } UnpicklerMemoProxyObject; |
|---|
| 622 | n/a | |
|---|
| 623 | n/a | /* Forward declarations */ |
|---|
| 624 | n/a | static int save(PicklerObject *, PyObject *, int); |
|---|
| 625 | n/a | static int save_reduce(PicklerObject *, PyObject *, PyObject *); |
|---|
| 626 | n/a | static PyTypeObject Pickler_Type; |
|---|
| 627 | n/a | static PyTypeObject Unpickler_Type; |
|---|
| 628 | n/a | |
|---|
| 629 | n/a | #include "clinic/_pickle.c.h" |
|---|
| 630 | n/a | |
|---|
| 631 | n/a | /************************************************************************* |
|---|
| 632 | n/a | A custom hashtable mapping void* to Python ints. This is used by the pickler |
|---|
| 633 | n/a | for memoization. Using a custom hashtable rather than PyDict allows us to skip |
|---|
| 634 | n/a | a bunch of unnecessary object creation. This makes a huge performance |
|---|
| 635 | n/a | difference. */ |
|---|
| 636 | n/a | |
|---|
| 637 | n/a | #define MT_MINSIZE 8 |
|---|
| 638 | n/a | #define PERTURB_SHIFT 5 |
|---|
| 639 | n/a | |
|---|
| 640 | n/a | |
|---|
| 641 | n/a | static PyMemoTable * |
|---|
| 642 | n/a | PyMemoTable_New(void) |
|---|
| 643 | n/a | { |
|---|
| 644 | n/a | PyMemoTable *memo = PyMem_MALLOC(sizeof(PyMemoTable)); |
|---|
| 645 | n/a | if (memo == NULL) { |
|---|
| 646 | n/a | PyErr_NoMemory(); |
|---|
| 647 | n/a | return NULL; |
|---|
| 648 | n/a | } |
|---|
| 649 | n/a | |
|---|
| 650 | n/a | memo->mt_used = 0; |
|---|
| 651 | n/a | memo->mt_allocated = MT_MINSIZE; |
|---|
| 652 | n/a | memo->mt_mask = MT_MINSIZE - 1; |
|---|
| 653 | n/a | memo->mt_table = PyMem_MALLOC(MT_MINSIZE * sizeof(PyMemoEntry)); |
|---|
| 654 | n/a | if (memo->mt_table == NULL) { |
|---|
| 655 | n/a | PyMem_FREE(memo); |
|---|
| 656 | n/a | PyErr_NoMemory(); |
|---|
| 657 | n/a | return NULL; |
|---|
| 658 | n/a | } |
|---|
| 659 | n/a | memset(memo->mt_table, 0, MT_MINSIZE * sizeof(PyMemoEntry)); |
|---|
| 660 | n/a | |
|---|
| 661 | n/a | return memo; |
|---|
| 662 | n/a | } |
|---|
| 663 | n/a | |
|---|
| 664 | n/a | static PyMemoTable * |
|---|
| 665 | n/a | PyMemoTable_Copy(PyMemoTable *self) |
|---|
| 666 | n/a | { |
|---|
| 667 | n/a | Py_ssize_t i; |
|---|
| 668 | n/a | PyMemoTable *new = PyMemoTable_New(); |
|---|
| 669 | n/a | if (new == NULL) |
|---|
| 670 | n/a | return NULL; |
|---|
| 671 | n/a | |
|---|
| 672 | n/a | new->mt_used = self->mt_used; |
|---|
| 673 | n/a | new->mt_allocated = self->mt_allocated; |
|---|
| 674 | n/a | new->mt_mask = self->mt_mask; |
|---|
| 675 | n/a | /* The table we get from _New() is probably smaller than we wanted. |
|---|
| 676 | n/a | Free it and allocate one that's the right size. */ |
|---|
| 677 | n/a | PyMem_FREE(new->mt_table); |
|---|
| 678 | n/a | new->mt_table = PyMem_NEW(PyMemoEntry, self->mt_allocated); |
|---|
| 679 | n/a | if (new->mt_table == NULL) { |
|---|
| 680 | n/a | PyMem_FREE(new); |
|---|
| 681 | n/a | PyErr_NoMemory(); |
|---|
| 682 | n/a | return NULL; |
|---|
| 683 | n/a | } |
|---|
| 684 | n/a | for (i = 0; i < self->mt_allocated; i++) { |
|---|
| 685 | n/a | Py_XINCREF(self->mt_table[i].me_key); |
|---|
| 686 | n/a | } |
|---|
| 687 | n/a | memcpy(new->mt_table, self->mt_table, |
|---|
| 688 | n/a | sizeof(PyMemoEntry) * self->mt_allocated); |
|---|
| 689 | n/a | |
|---|
| 690 | n/a | return new; |
|---|
| 691 | n/a | } |
|---|
| 692 | n/a | |
|---|
| 693 | n/a | static Py_ssize_t |
|---|
| 694 | n/a | PyMemoTable_Size(PyMemoTable *self) |
|---|
| 695 | n/a | { |
|---|
| 696 | n/a | return self->mt_used; |
|---|
| 697 | n/a | } |
|---|
| 698 | n/a | |
|---|
| 699 | n/a | static int |
|---|
| 700 | n/a | PyMemoTable_Clear(PyMemoTable *self) |
|---|
| 701 | n/a | { |
|---|
| 702 | n/a | Py_ssize_t i = self->mt_allocated; |
|---|
| 703 | n/a | |
|---|
| 704 | n/a | while (--i >= 0) { |
|---|
| 705 | n/a | Py_XDECREF(self->mt_table[i].me_key); |
|---|
| 706 | n/a | } |
|---|
| 707 | n/a | self->mt_used = 0; |
|---|
| 708 | n/a | memset(self->mt_table, 0, self->mt_allocated * sizeof(PyMemoEntry)); |
|---|
| 709 | n/a | return 0; |
|---|
| 710 | n/a | } |
|---|
| 711 | n/a | |
|---|
| 712 | n/a | static void |
|---|
| 713 | n/a | PyMemoTable_Del(PyMemoTable *self) |
|---|
| 714 | n/a | { |
|---|
| 715 | n/a | if (self == NULL) |
|---|
| 716 | n/a | return; |
|---|
| 717 | n/a | PyMemoTable_Clear(self); |
|---|
| 718 | n/a | |
|---|
| 719 | n/a | PyMem_FREE(self->mt_table); |
|---|
| 720 | n/a | PyMem_FREE(self); |
|---|
| 721 | n/a | } |
|---|
| 722 | n/a | |
|---|
| 723 | n/a | /* Since entries cannot be deleted from this hashtable, _PyMemoTable_Lookup() |
|---|
| 724 | n/a | can be considerably simpler than dictobject.c's lookdict(). */ |
|---|
| 725 | n/a | static PyMemoEntry * |
|---|
| 726 | n/a | _PyMemoTable_Lookup(PyMemoTable *self, PyObject *key) |
|---|
| 727 | n/a | { |
|---|
| 728 | n/a | size_t i; |
|---|
| 729 | n/a | size_t perturb; |
|---|
| 730 | n/a | size_t mask = (size_t)self->mt_mask; |
|---|
| 731 | n/a | PyMemoEntry *table = self->mt_table; |
|---|
| 732 | n/a | PyMemoEntry *entry; |
|---|
| 733 | n/a | Py_hash_t hash = (Py_hash_t)key >> 3; |
|---|
| 734 | n/a | |
|---|
| 735 | n/a | i = hash & mask; |
|---|
| 736 | n/a | entry = &table[i]; |
|---|
| 737 | n/a | if (entry->me_key == NULL || entry->me_key == key) |
|---|
| 738 | n/a | return entry; |
|---|
| 739 | n/a | |
|---|
| 740 | n/a | for (perturb = hash; ; perturb >>= PERTURB_SHIFT) { |
|---|
| 741 | n/a | i = (i << 2) + i + perturb + 1; |
|---|
| 742 | n/a | entry = &table[i & mask]; |
|---|
| 743 | n/a | if (entry->me_key == NULL || entry->me_key == key) |
|---|
| 744 | n/a | return entry; |
|---|
| 745 | n/a | } |
|---|
| 746 | n/a | assert(0); /* Never reached */ |
|---|
| 747 | n/a | return NULL; |
|---|
| 748 | n/a | } |
|---|
| 749 | n/a | |
|---|
| 750 | n/a | /* Returns -1 on failure, 0 on success. */ |
|---|
| 751 | n/a | static int |
|---|
| 752 | n/a | _PyMemoTable_ResizeTable(PyMemoTable *self, Py_ssize_t min_size) |
|---|
| 753 | n/a | { |
|---|
| 754 | n/a | PyMemoEntry *oldtable = NULL; |
|---|
| 755 | n/a | PyMemoEntry *oldentry, *newentry; |
|---|
| 756 | n/a | Py_ssize_t new_size = MT_MINSIZE; |
|---|
| 757 | n/a | Py_ssize_t to_process; |
|---|
| 758 | n/a | |
|---|
| 759 | n/a | assert(min_size > 0); |
|---|
| 760 | n/a | |
|---|
| 761 | n/a | /* Find the smallest valid table size >= min_size. */ |
|---|
| 762 | n/a | while (new_size < min_size && new_size > 0) |
|---|
| 763 | n/a | new_size <<= 1; |
|---|
| 764 | n/a | if (new_size <= 0) { |
|---|
| 765 | n/a | PyErr_NoMemory(); |
|---|
| 766 | n/a | return -1; |
|---|
| 767 | n/a | } |
|---|
| 768 | n/a | /* new_size needs to be a power of two. */ |
|---|
| 769 | n/a | assert((new_size & (new_size - 1)) == 0); |
|---|
| 770 | n/a | |
|---|
| 771 | n/a | /* Allocate new table. */ |
|---|
| 772 | n/a | oldtable = self->mt_table; |
|---|
| 773 | n/a | self->mt_table = PyMem_NEW(PyMemoEntry, new_size); |
|---|
| 774 | n/a | if (self->mt_table == NULL) { |
|---|
| 775 | n/a | self->mt_table = oldtable; |
|---|
| 776 | n/a | PyErr_NoMemory(); |
|---|
| 777 | n/a | return -1; |
|---|
| 778 | n/a | } |
|---|
| 779 | n/a | self->mt_allocated = new_size; |
|---|
| 780 | n/a | self->mt_mask = new_size - 1; |
|---|
| 781 | n/a | memset(self->mt_table, 0, sizeof(PyMemoEntry) * new_size); |
|---|
| 782 | n/a | |
|---|
| 783 | n/a | /* Copy entries from the old table. */ |
|---|
| 784 | n/a | to_process = self->mt_used; |
|---|
| 785 | n/a | for (oldentry = oldtable; to_process > 0; oldentry++) { |
|---|
| 786 | n/a | if (oldentry->me_key != NULL) { |
|---|
| 787 | n/a | to_process--; |
|---|
| 788 | n/a | /* newentry is a pointer to a chunk of the new |
|---|
| 789 | n/a | mt_table, so we're setting the key:value pair |
|---|
| 790 | n/a | in-place. */ |
|---|
| 791 | n/a | newentry = _PyMemoTable_Lookup(self, oldentry->me_key); |
|---|
| 792 | n/a | newentry->me_key = oldentry->me_key; |
|---|
| 793 | n/a | newentry->me_value = oldentry->me_value; |
|---|
| 794 | n/a | } |
|---|
| 795 | n/a | } |
|---|
| 796 | n/a | |
|---|
| 797 | n/a | /* Deallocate the old table. */ |
|---|
| 798 | n/a | PyMem_FREE(oldtable); |
|---|
| 799 | n/a | return 0; |
|---|
| 800 | n/a | } |
|---|
| 801 | n/a | |
|---|
| 802 | n/a | /* Returns NULL on failure, a pointer to the value otherwise. */ |
|---|
| 803 | n/a | static Py_ssize_t * |
|---|
| 804 | n/a | PyMemoTable_Get(PyMemoTable *self, PyObject *key) |
|---|
| 805 | n/a | { |
|---|
| 806 | n/a | PyMemoEntry *entry = _PyMemoTable_Lookup(self, key); |
|---|
| 807 | n/a | if (entry->me_key == NULL) |
|---|
| 808 | n/a | return NULL; |
|---|
| 809 | n/a | return &entry->me_value; |
|---|
| 810 | n/a | } |
|---|
| 811 | n/a | |
|---|
| 812 | n/a | /* Returns -1 on failure, 0 on success. */ |
|---|
| 813 | n/a | static int |
|---|
| 814 | n/a | PyMemoTable_Set(PyMemoTable *self, PyObject *key, Py_ssize_t value) |
|---|
| 815 | n/a | { |
|---|
| 816 | n/a | PyMemoEntry *entry; |
|---|
| 817 | n/a | |
|---|
| 818 | n/a | assert(key != NULL); |
|---|
| 819 | n/a | |
|---|
| 820 | n/a | entry = _PyMemoTable_Lookup(self, key); |
|---|
| 821 | n/a | if (entry->me_key != NULL) { |
|---|
| 822 | n/a | entry->me_value = value; |
|---|
| 823 | n/a | return 0; |
|---|
| 824 | n/a | } |
|---|
| 825 | n/a | Py_INCREF(key); |
|---|
| 826 | n/a | entry->me_key = key; |
|---|
| 827 | n/a | entry->me_value = value; |
|---|
| 828 | n/a | self->mt_used++; |
|---|
| 829 | n/a | |
|---|
| 830 | n/a | /* If we added a key, we can safely resize. Otherwise just return! |
|---|
| 831 | n/a | * If used >= 2/3 size, adjust size. Normally, this quaduples the size. |
|---|
| 832 | n/a | * |
|---|
| 833 | n/a | * Quadrupling the size improves average table sparseness |
|---|
| 834 | n/a | * (reducing collisions) at the cost of some memory. It also halves |
|---|
| 835 | n/a | * the number of expensive resize operations in a growing memo table. |
|---|
| 836 | n/a | * |
|---|
| 837 | n/a | * Very large memo tables (over 50K items) use doubling instead. |
|---|
| 838 | n/a | * This may help applications with severe memory constraints. |
|---|
| 839 | n/a | */ |
|---|
| 840 | n/a | if (!(self->mt_used * 3 >= (self->mt_mask + 1) * 2)) |
|---|
| 841 | n/a | return 0; |
|---|
| 842 | n/a | return _PyMemoTable_ResizeTable(self, |
|---|
| 843 | n/a | (self->mt_used > 50000 ? 2 : 4) * self->mt_used); |
|---|
| 844 | n/a | } |
|---|
| 845 | n/a | |
|---|
| 846 | n/a | #undef MT_MINSIZE |
|---|
| 847 | n/a | #undef PERTURB_SHIFT |
|---|
| 848 | n/a | |
|---|
| 849 | n/a | /*************************************************************************/ |
|---|
| 850 | n/a | |
|---|
| 851 | n/a | |
|---|
| 852 | n/a | static int |
|---|
| 853 | n/a | _Pickler_ClearBuffer(PicklerObject *self) |
|---|
| 854 | n/a | { |
|---|
| 855 | n/a | Py_XSETREF(self->output_buffer, |
|---|
| 856 | n/a | PyBytes_FromStringAndSize(NULL, self->max_output_len)); |
|---|
| 857 | n/a | if (self->output_buffer == NULL) |
|---|
| 858 | n/a | return -1; |
|---|
| 859 | n/a | self->output_len = 0; |
|---|
| 860 | n/a | self->frame_start = -1; |
|---|
| 861 | n/a | return 0; |
|---|
| 862 | n/a | } |
|---|
| 863 | n/a | |
|---|
| 864 | n/a | static void |
|---|
| 865 | n/a | _write_size64(char *out, size_t value) |
|---|
| 866 | n/a | { |
|---|
| 867 | n/a | size_t i; |
|---|
| 868 | n/a | |
|---|
| 869 | n/a | Py_BUILD_ASSERT(sizeof(size_t) <= 8); |
|---|
| 870 | n/a | |
|---|
| 871 | n/a | for (i = 0; i < sizeof(size_t); i++) { |
|---|
| 872 | n/a | out[i] = (unsigned char)((value >> (8 * i)) & 0xff); |
|---|
| 873 | n/a | } |
|---|
| 874 | n/a | for (i = sizeof(size_t); i < 8; i++) { |
|---|
| 875 | n/a | out[i] = 0; |
|---|
| 876 | n/a | } |
|---|
| 877 | n/a | } |
|---|
| 878 | n/a | |
|---|
| 879 | n/a | static void |
|---|
| 880 | n/a | _Pickler_WriteFrameHeader(PicklerObject *self, char *qdata, size_t frame_len) |
|---|
| 881 | n/a | { |
|---|
| 882 | n/a | qdata[0] = FRAME; |
|---|
| 883 | n/a | _write_size64(qdata + 1, frame_len); |
|---|
| 884 | n/a | } |
|---|
| 885 | n/a | |
|---|
| 886 | n/a | static int |
|---|
| 887 | n/a | _Pickler_CommitFrame(PicklerObject *self) |
|---|
| 888 | n/a | { |
|---|
| 889 | n/a | size_t frame_len; |
|---|
| 890 | n/a | char *qdata; |
|---|
| 891 | n/a | |
|---|
| 892 | n/a | if (!self->framing || self->frame_start == -1) |
|---|
| 893 | n/a | return 0; |
|---|
| 894 | n/a | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
|---|
| 895 | n/a | qdata = PyBytes_AS_STRING(self->output_buffer) + self->frame_start; |
|---|
| 896 | n/a | _Pickler_WriteFrameHeader(self, qdata, frame_len); |
|---|
| 897 | n/a | self->frame_start = -1; |
|---|
| 898 | n/a | return 0; |
|---|
| 899 | n/a | } |
|---|
| 900 | n/a | |
|---|
| 901 | n/a | static int |
|---|
| 902 | n/a | _Pickler_OpcodeBoundary(PicklerObject *self) |
|---|
| 903 | n/a | { |
|---|
| 904 | n/a | Py_ssize_t frame_len; |
|---|
| 905 | n/a | |
|---|
| 906 | n/a | if (!self->framing || self->frame_start == -1) |
|---|
| 907 | n/a | return 0; |
|---|
| 908 | n/a | frame_len = self->output_len - self->frame_start - FRAME_HEADER_SIZE; |
|---|
| 909 | n/a | if (frame_len >= FRAME_SIZE_TARGET) |
|---|
| 910 | n/a | return _Pickler_CommitFrame(self); |
|---|
| 911 | n/a | else |
|---|
| 912 | n/a | return 0; |
|---|
| 913 | n/a | } |
|---|
| 914 | n/a | |
|---|
| 915 | n/a | static PyObject * |
|---|
| 916 | n/a | _Pickler_GetString(PicklerObject *self) |
|---|
| 917 | n/a | { |
|---|
| 918 | n/a | PyObject *output_buffer = self->output_buffer; |
|---|
| 919 | n/a | |
|---|
| 920 | n/a | assert(self->output_buffer != NULL); |
|---|
| 921 | n/a | |
|---|
| 922 | n/a | if (_Pickler_CommitFrame(self)) |
|---|
| 923 | n/a | return NULL; |
|---|
| 924 | n/a | |
|---|
| 925 | n/a | self->output_buffer = NULL; |
|---|
| 926 | n/a | /* Resize down to exact size */ |
|---|
| 927 | n/a | if (_PyBytes_Resize(&output_buffer, self->output_len) < 0) |
|---|
| 928 | n/a | return NULL; |
|---|
| 929 | n/a | return output_buffer; |
|---|
| 930 | n/a | } |
|---|
| 931 | n/a | |
|---|
| 932 | n/a | static int |
|---|
| 933 | n/a | _Pickler_FlushToFile(PicklerObject *self) |
|---|
| 934 | n/a | { |
|---|
| 935 | n/a | PyObject *output, *result; |
|---|
| 936 | n/a | |
|---|
| 937 | n/a | assert(self->write != NULL); |
|---|
| 938 | n/a | |
|---|
| 939 | n/a | /* This will commit the frame first */ |
|---|
| 940 | n/a | output = _Pickler_GetString(self); |
|---|
| 941 | n/a | if (output == NULL) |
|---|
| 942 | n/a | return -1; |
|---|
| 943 | n/a | |
|---|
| 944 | n/a | result = _Pickle_FastCall(self->write, output); |
|---|
| 945 | n/a | Py_XDECREF(result); |
|---|
| 946 | n/a | return (result == NULL) ? -1 : 0; |
|---|
| 947 | n/a | } |
|---|
| 948 | n/a | |
|---|
| 949 | n/a | static Py_ssize_t |
|---|
| 950 | n/a | _Pickler_Write(PicklerObject *self, const char *s, Py_ssize_t data_len) |
|---|
| 951 | n/a | { |
|---|
| 952 | n/a | Py_ssize_t i, n, required; |
|---|
| 953 | n/a | char *buffer; |
|---|
| 954 | n/a | int need_new_frame; |
|---|
| 955 | n/a | |
|---|
| 956 | n/a | assert(s != NULL); |
|---|
| 957 | n/a | need_new_frame = (self->framing && self->frame_start == -1); |
|---|
| 958 | n/a | |
|---|
| 959 | n/a | if (need_new_frame) |
|---|
| 960 | n/a | n = data_len + FRAME_HEADER_SIZE; |
|---|
| 961 | n/a | else |
|---|
| 962 | n/a | n = data_len; |
|---|
| 963 | n/a | |
|---|
| 964 | n/a | required = self->output_len + n; |
|---|
| 965 | n/a | if (required > self->max_output_len) { |
|---|
| 966 | n/a | /* Make place in buffer for the pickle chunk */ |
|---|
| 967 | n/a | if (self->output_len >= PY_SSIZE_T_MAX / 2 - n) { |
|---|
| 968 | n/a | PyErr_NoMemory(); |
|---|
| 969 | n/a | return -1; |
|---|
| 970 | n/a | } |
|---|
| 971 | n/a | self->max_output_len = (self->output_len + n) / 2 * 3; |
|---|
| 972 | n/a | if (_PyBytes_Resize(&self->output_buffer, self->max_output_len) < 0) |
|---|
| 973 | n/a | return -1; |
|---|
| 974 | n/a | } |
|---|
| 975 | n/a | buffer = PyBytes_AS_STRING(self->output_buffer); |
|---|
| 976 | n/a | if (need_new_frame) { |
|---|
| 977 | n/a | /* Setup new frame */ |
|---|
| 978 | n/a | Py_ssize_t frame_start = self->output_len; |
|---|
| 979 | n/a | self->frame_start = frame_start; |
|---|
| 980 | n/a | for (i = 0; i < FRAME_HEADER_SIZE; i++) { |
|---|
| 981 | n/a | /* Write an invalid value, for debugging */ |
|---|
| 982 | n/a | buffer[frame_start + i] = 0xFE; |
|---|
| 983 | n/a | } |
|---|
| 984 | n/a | self->output_len += FRAME_HEADER_SIZE; |
|---|
| 985 | n/a | } |
|---|
| 986 | n/a | if (data_len < 8) { |
|---|
| 987 | n/a | /* This is faster than memcpy when the string is short. */ |
|---|
| 988 | n/a | for (i = 0; i < data_len; i++) { |
|---|
| 989 | n/a | buffer[self->output_len + i] = s[i]; |
|---|
| 990 | n/a | } |
|---|
| 991 | n/a | } |
|---|
| 992 | n/a | else { |
|---|
| 993 | n/a | memcpy(buffer + self->output_len, s, data_len); |
|---|
| 994 | n/a | } |
|---|
| 995 | n/a | self->output_len += data_len; |
|---|
| 996 | n/a | return data_len; |
|---|
| 997 | n/a | } |
|---|
| 998 | n/a | |
|---|
| 999 | n/a | static PicklerObject * |
|---|
| 1000 | n/a | _Pickler_New(void) |
|---|
| 1001 | n/a | { |
|---|
| 1002 | n/a | PicklerObject *self; |
|---|
| 1003 | n/a | |
|---|
| 1004 | n/a | self = PyObject_GC_New(PicklerObject, &Pickler_Type); |
|---|
| 1005 | n/a | if (self == NULL) |
|---|
| 1006 | n/a | return NULL; |
|---|
| 1007 | n/a | |
|---|
| 1008 | n/a | self->pers_func = NULL; |
|---|
| 1009 | n/a | self->dispatch_table = NULL; |
|---|
| 1010 | n/a | self->write = NULL; |
|---|
| 1011 | n/a | self->proto = 0; |
|---|
| 1012 | n/a | self->bin = 0; |
|---|
| 1013 | n/a | self->framing = 0; |
|---|
| 1014 | n/a | self->frame_start = -1; |
|---|
| 1015 | n/a | self->fast = 0; |
|---|
| 1016 | n/a | self->fast_nesting = 0; |
|---|
| 1017 | n/a | self->fix_imports = 0; |
|---|
| 1018 | n/a | self->fast_memo = NULL; |
|---|
| 1019 | n/a | self->max_output_len = WRITE_BUF_SIZE; |
|---|
| 1020 | n/a | self->output_len = 0; |
|---|
| 1021 | n/a | |
|---|
| 1022 | n/a | self->memo = PyMemoTable_New(); |
|---|
| 1023 | n/a | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
|---|
| 1024 | n/a | self->max_output_len); |
|---|
| 1025 | n/a | |
|---|
| 1026 | n/a | if (self->memo == NULL || self->output_buffer == NULL) { |
|---|
| 1027 | n/a | Py_DECREF(self); |
|---|
| 1028 | n/a | return NULL; |
|---|
| 1029 | n/a | } |
|---|
| 1030 | n/a | return self; |
|---|
| 1031 | n/a | } |
|---|
| 1032 | n/a | |
|---|
| 1033 | n/a | static int |
|---|
| 1034 | n/a | _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports) |
|---|
| 1035 | n/a | { |
|---|
| 1036 | n/a | long proto; |
|---|
| 1037 | n/a | |
|---|
| 1038 | n/a | if (protocol == NULL || protocol == Py_None) { |
|---|
| 1039 | n/a | proto = DEFAULT_PROTOCOL; |
|---|
| 1040 | n/a | } |
|---|
| 1041 | n/a | else { |
|---|
| 1042 | n/a | proto = PyLong_AsLong(protocol); |
|---|
| 1043 | n/a | if (proto < 0) { |
|---|
| 1044 | n/a | if (proto == -1 && PyErr_Occurred()) |
|---|
| 1045 | n/a | return -1; |
|---|
| 1046 | n/a | proto = HIGHEST_PROTOCOL; |
|---|
| 1047 | n/a | } |
|---|
| 1048 | n/a | else if (proto > HIGHEST_PROTOCOL) { |
|---|
| 1049 | n/a | PyErr_Format(PyExc_ValueError, "pickle protocol must be <= %d", |
|---|
| 1050 | n/a | HIGHEST_PROTOCOL); |
|---|
| 1051 | n/a | return -1; |
|---|
| 1052 | n/a | } |
|---|
| 1053 | n/a | } |
|---|
| 1054 | n/a | self->proto = (int)proto; |
|---|
| 1055 | n/a | self->bin = proto > 0; |
|---|
| 1056 | n/a | self->fix_imports = fix_imports && proto < 3; |
|---|
| 1057 | n/a | return 0; |
|---|
| 1058 | n/a | } |
|---|
| 1059 | n/a | |
|---|
| 1060 | n/a | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
|---|
| 1061 | n/a | be called once on a freshly created Pickler. */ |
|---|
| 1062 | n/a | static int |
|---|
| 1063 | n/a | _Pickler_SetOutputStream(PicklerObject *self, PyObject *file) |
|---|
| 1064 | n/a | { |
|---|
| 1065 | n/a | _Py_IDENTIFIER(write); |
|---|
| 1066 | n/a | assert(file != NULL); |
|---|
| 1067 | n/a | self->write = _PyObject_GetAttrId(file, &PyId_write); |
|---|
| 1068 | n/a | if (self->write == NULL) { |
|---|
| 1069 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 1070 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1071 | n/a | "file must have a 'write' attribute"); |
|---|
| 1072 | n/a | return -1; |
|---|
| 1073 | n/a | } |
|---|
| 1074 | n/a | |
|---|
| 1075 | n/a | return 0; |
|---|
| 1076 | n/a | } |
|---|
| 1077 | n/a | |
|---|
| 1078 | n/a | /* Returns the size of the input on success, -1 on failure. This takes its |
|---|
| 1079 | n/a | own reference to `input`. */ |
|---|
| 1080 | n/a | static Py_ssize_t |
|---|
| 1081 | n/a | _Unpickler_SetStringInput(UnpicklerObject *self, PyObject *input) |
|---|
| 1082 | n/a | { |
|---|
| 1083 | n/a | if (self->buffer.buf != NULL) |
|---|
| 1084 | n/a | PyBuffer_Release(&self->buffer); |
|---|
| 1085 | n/a | if (PyObject_GetBuffer(input, &self->buffer, PyBUF_CONTIG_RO) < 0) |
|---|
| 1086 | n/a | return -1; |
|---|
| 1087 | n/a | self->input_buffer = self->buffer.buf; |
|---|
| 1088 | n/a | self->input_len = self->buffer.len; |
|---|
| 1089 | n/a | self->next_read_idx = 0; |
|---|
| 1090 | n/a | self->prefetched_idx = self->input_len; |
|---|
| 1091 | n/a | return self->input_len; |
|---|
| 1092 | n/a | } |
|---|
| 1093 | n/a | |
|---|
| 1094 | n/a | static int |
|---|
| 1095 | n/a | bad_readline(void) |
|---|
| 1096 | n/a | { |
|---|
| 1097 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 1098 | n/a | PyErr_SetString(st->UnpicklingError, "pickle data was truncated"); |
|---|
| 1099 | n/a | return -1; |
|---|
| 1100 | n/a | } |
|---|
| 1101 | n/a | |
|---|
| 1102 | n/a | static int |
|---|
| 1103 | n/a | _Unpickler_SkipConsumed(UnpicklerObject *self) |
|---|
| 1104 | n/a | { |
|---|
| 1105 | n/a | Py_ssize_t consumed; |
|---|
| 1106 | n/a | PyObject *r; |
|---|
| 1107 | n/a | |
|---|
| 1108 | n/a | consumed = self->next_read_idx - self->prefetched_idx; |
|---|
| 1109 | n/a | if (consumed <= 0) |
|---|
| 1110 | n/a | return 0; |
|---|
| 1111 | n/a | |
|---|
| 1112 | n/a | assert(self->peek); /* otherwise we did something wrong */ |
|---|
| 1113 | n/a | /* This makes a useless copy... */ |
|---|
| 1114 | n/a | r = PyObject_CallFunction(self->read, "n", consumed); |
|---|
| 1115 | n/a | if (r == NULL) |
|---|
| 1116 | n/a | return -1; |
|---|
| 1117 | n/a | Py_DECREF(r); |
|---|
| 1118 | n/a | |
|---|
| 1119 | n/a | self->prefetched_idx = self->next_read_idx; |
|---|
| 1120 | n/a | return 0; |
|---|
| 1121 | n/a | } |
|---|
| 1122 | n/a | |
|---|
| 1123 | n/a | static const Py_ssize_t READ_WHOLE_LINE = -1; |
|---|
| 1124 | n/a | |
|---|
| 1125 | n/a | /* If reading from a file, we need to only pull the bytes we need, since there |
|---|
| 1126 | n/a | may be multiple pickle objects arranged contiguously in the same input |
|---|
| 1127 | n/a | buffer. |
|---|
| 1128 | n/a | |
|---|
| 1129 | n/a | If `n` is READ_WHOLE_LINE, read a whole line. Otherwise, read up to `n` |
|---|
| 1130 | n/a | bytes from the input stream/buffer. |
|---|
| 1131 | n/a | |
|---|
| 1132 | n/a | Update the unpickler's input buffer with the newly-read data. Returns -1 on |
|---|
| 1133 | n/a | failure; on success, returns the number of bytes read from the file. |
|---|
| 1134 | n/a | |
|---|
| 1135 | n/a | On success, self->input_len will be 0; this is intentional so that when |
|---|
| 1136 | n/a | unpickling from a file, the "we've run out of data" code paths will trigger, |
|---|
| 1137 | n/a | causing the Unpickler to go back to the file for more data. Use the returned |
|---|
| 1138 | n/a | size to tell you how much data you can process. */ |
|---|
| 1139 | n/a | static Py_ssize_t |
|---|
| 1140 | n/a | _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n) |
|---|
| 1141 | n/a | { |
|---|
| 1142 | n/a | PyObject *data; |
|---|
| 1143 | n/a | Py_ssize_t read_size; |
|---|
| 1144 | n/a | |
|---|
| 1145 | n/a | assert(self->read != NULL); |
|---|
| 1146 | n/a | |
|---|
| 1147 | n/a | if (_Unpickler_SkipConsumed(self) < 0) |
|---|
| 1148 | n/a | return -1; |
|---|
| 1149 | n/a | |
|---|
| 1150 | n/a | if (n == READ_WHOLE_LINE) { |
|---|
| 1151 | n/a | data = _PyObject_CallNoArg(self->readline); |
|---|
| 1152 | n/a | } |
|---|
| 1153 | n/a | else { |
|---|
| 1154 | n/a | PyObject *len; |
|---|
| 1155 | n/a | /* Prefetch some data without advancing the file pointer, if possible */ |
|---|
| 1156 | n/a | if (self->peek && n < PREFETCH) { |
|---|
| 1157 | n/a | len = PyLong_FromSsize_t(PREFETCH); |
|---|
| 1158 | n/a | if (len == NULL) |
|---|
| 1159 | n/a | return -1; |
|---|
| 1160 | n/a | data = _Pickle_FastCall(self->peek, len); |
|---|
| 1161 | n/a | if (data == NULL) { |
|---|
| 1162 | n/a | if (!PyErr_ExceptionMatches(PyExc_NotImplementedError)) |
|---|
| 1163 | n/a | return -1; |
|---|
| 1164 | n/a | /* peek() is probably not supported by the given file object */ |
|---|
| 1165 | n/a | PyErr_Clear(); |
|---|
| 1166 | n/a | Py_CLEAR(self->peek); |
|---|
| 1167 | n/a | } |
|---|
| 1168 | n/a | else { |
|---|
| 1169 | n/a | read_size = _Unpickler_SetStringInput(self, data); |
|---|
| 1170 | n/a | Py_DECREF(data); |
|---|
| 1171 | n/a | self->prefetched_idx = 0; |
|---|
| 1172 | n/a | if (n <= read_size) |
|---|
| 1173 | n/a | return n; |
|---|
| 1174 | n/a | } |
|---|
| 1175 | n/a | } |
|---|
| 1176 | n/a | len = PyLong_FromSsize_t(n); |
|---|
| 1177 | n/a | if (len == NULL) |
|---|
| 1178 | n/a | return -1; |
|---|
| 1179 | n/a | data = _Pickle_FastCall(self->read, len); |
|---|
| 1180 | n/a | } |
|---|
| 1181 | n/a | if (data == NULL) |
|---|
| 1182 | n/a | return -1; |
|---|
| 1183 | n/a | |
|---|
| 1184 | n/a | read_size = _Unpickler_SetStringInput(self, data); |
|---|
| 1185 | n/a | Py_DECREF(data); |
|---|
| 1186 | n/a | return read_size; |
|---|
| 1187 | n/a | } |
|---|
| 1188 | n/a | |
|---|
| 1189 | n/a | /* Don't call it directly: use _Unpickler_Read() */ |
|---|
| 1190 | n/a | static Py_ssize_t |
|---|
| 1191 | n/a | _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n) |
|---|
| 1192 | n/a | { |
|---|
| 1193 | n/a | Py_ssize_t num_read; |
|---|
| 1194 | n/a | |
|---|
| 1195 | n/a | *s = NULL; |
|---|
| 1196 | n/a | if (self->next_read_idx > PY_SSIZE_T_MAX - n) { |
|---|
| 1197 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 1198 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 1199 | n/a | "read would overflow (invalid bytecode)"); |
|---|
| 1200 | n/a | return -1; |
|---|
| 1201 | n/a | } |
|---|
| 1202 | n/a | |
|---|
| 1203 | n/a | /* This case is handled by the _Unpickler_Read() macro for efficiency */ |
|---|
| 1204 | n/a | assert(self->next_read_idx + n > self->input_len); |
|---|
| 1205 | n/a | |
|---|
| 1206 | n/a | if (!self->read) |
|---|
| 1207 | n/a | return bad_readline(); |
|---|
| 1208 | n/a | |
|---|
| 1209 | n/a | num_read = _Unpickler_ReadFromFile(self, n); |
|---|
| 1210 | n/a | if (num_read < 0) |
|---|
| 1211 | n/a | return -1; |
|---|
| 1212 | n/a | if (num_read < n) |
|---|
| 1213 | n/a | return bad_readline(); |
|---|
| 1214 | n/a | *s = self->input_buffer; |
|---|
| 1215 | n/a | self->next_read_idx = n; |
|---|
| 1216 | n/a | return n; |
|---|
| 1217 | n/a | } |
|---|
| 1218 | n/a | |
|---|
| 1219 | n/a | /* Read `n` bytes from the unpickler's data source, storing the result in `*s`. |
|---|
| 1220 | n/a | |
|---|
| 1221 | n/a | This should be used for all data reads, rather than accessing the unpickler's |
|---|
| 1222 | n/a | input buffer directly. This method deals correctly with reading from input |
|---|
| 1223 | n/a | streams, which the input buffer doesn't deal with. |
|---|
| 1224 | n/a | |
|---|
| 1225 | n/a | Note that when reading from a file-like object, self->next_read_idx won't |
|---|
| 1226 | n/a | be updated (it should remain at 0 for the entire unpickling process). You |
|---|
| 1227 | n/a | should use this function's return value to know how many bytes you can |
|---|
| 1228 | n/a | consume. |
|---|
| 1229 | n/a | |
|---|
| 1230 | n/a | Returns -1 (with an exception set) on failure. On success, return the |
|---|
| 1231 | n/a | number of chars read. */ |
|---|
| 1232 | n/a | #define _Unpickler_Read(self, s, n) \ |
|---|
| 1233 | n/a | (((n) <= (self)->input_len - (self)->next_read_idx) \ |
|---|
| 1234 | n/a | ? (*(s) = (self)->input_buffer + (self)->next_read_idx, \ |
|---|
| 1235 | n/a | (self)->next_read_idx += (n), \ |
|---|
| 1236 | n/a | (n)) \ |
|---|
| 1237 | n/a | : _Unpickler_ReadImpl(self, (s), (n))) |
|---|
| 1238 | n/a | |
|---|
| 1239 | n/a | static Py_ssize_t |
|---|
| 1240 | n/a | _Unpickler_CopyLine(UnpicklerObject *self, char *line, Py_ssize_t len, |
|---|
| 1241 | n/a | char **result) |
|---|
| 1242 | n/a | { |
|---|
| 1243 | n/a | char *input_line = PyMem_Realloc(self->input_line, len + 1); |
|---|
| 1244 | n/a | if (input_line == NULL) { |
|---|
| 1245 | n/a | PyErr_NoMemory(); |
|---|
| 1246 | n/a | return -1; |
|---|
| 1247 | n/a | } |
|---|
| 1248 | n/a | |
|---|
| 1249 | n/a | memcpy(input_line, line, len); |
|---|
| 1250 | n/a | input_line[len] = '\0'; |
|---|
| 1251 | n/a | self->input_line = input_line; |
|---|
| 1252 | n/a | *result = self->input_line; |
|---|
| 1253 | n/a | return len; |
|---|
| 1254 | n/a | } |
|---|
| 1255 | n/a | |
|---|
| 1256 | n/a | /* Read a line from the input stream/buffer. If we run off the end of the input |
|---|
| 1257 | n/a | before hitting \n, raise an error. |
|---|
| 1258 | n/a | |
|---|
| 1259 | n/a | Returns the number of chars read, or -1 on failure. */ |
|---|
| 1260 | n/a | static Py_ssize_t |
|---|
| 1261 | n/a | _Unpickler_Readline(UnpicklerObject *self, char **result) |
|---|
| 1262 | n/a | { |
|---|
| 1263 | n/a | Py_ssize_t i, num_read; |
|---|
| 1264 | n/a | |
|---|
| 1265 | n/a | for (i = self->next_read_idx; i < self->input_len; i++) { |
|---|
| 1266 | n/a | if (self->input_buffer[i] == '\n') { |
|---|
| 1267 | n/a | char *line_start = self->input_buffer + self->next_read_idx; |
|---|
| 1268 | n/a | num_read = i - self->next_read_idx + 1; |
|---|
| 1269 | n/a | self->next_read_idx = i + 1; |
|---|
| 1270 | n/a | return _Unpickler_CopyLine(self, line_start, num_read, result); |
|---|
| 1271 | n/a | } |
|---|
| 1272 | n/a | } |
|---|
| 1273 | n/a | if (!self->read) |
|---|
| 1274 | n/a | return bad_readline(); |
|---|
| 1275 | n/a | |
|---|
| 1276 | n/a | num_read = _Unpickler_ReadFromFile(self, READ_WHOLE_LINE); |
|---|
| 1277 | n/a | if (num_read < 0) |
|---|
| 1278 | n/a | return -1; |
|---|
| 1279 | n/a | if (num_read == 0 || self->input_buffer[num_read - 1] != '\n') |
|---|
| 1280 | n/a | return bad_readline(); |
|---|
| 1281 | n/a | self->next_read_idx = num_read; |
|---|
| 1282 | n/a | return _Unpickler_CopyLine(self, self->input_buffer, num_read, result); |
|---|
| 1283 | n/a | } |
|---|
| 1284 | n/a | |
|---|
| 1285 | n/a | /* Returns -1 (with an exception set) on failure, 0 on success. The memo array |
|---|
| 1286 | n/a | will be modified in place. */ |
|---|
| 1287 | n/a | static int |
|---|
| 1288 | n/a | _Unpickler_ResizeMemoList(UnpicklerObject *self, Py_ssize_t new_size) |
|---|
| 1289 | n/a | { |
|---|
| 1290 | n/a | Py_ssize_t i; |
|---|
| 1291 | n/a | |
|---|
| 1292 | n/a | assert(new_size > self->memo_size); |
|---|
| 1293 | n/a | |
|---|
| 1294 | n/a | PyMem_RESIZE(self->memo, PyObject *, new_size); |
|---|
| 1295 | n/a | if (self->memo == NULL) { |
|---|
| 1296 | n/a | PyErr_NoMemory(); |
|---|
| 1297 | n/a | return -1; |
|---|
| 1298 | n/a | } |
|---|
| 1299 | n/a | for (i = self->memo_size; i < new_size; i++) |
|---|
| 1300 | n/a | self->memo[i] = NULL; |
|---|
| 1301 | n/a | self->memo_size = new_size; |
|---|
| 1302 | n/a | return 0; |
|---|
| 1303 | n/a | } |
|---|
| 1304 | n/a | |
|---|
| 1305 | n/a | /* Returns NULL if idx is out of bounds. */ |
|---|
| 1306 | n/a | static PyObject * |
|---|
| 1307 | n/a | _Unpickler_MemoGet(UnpicklerObject *self, Py_ssize_t idx) |
|---|
| 1308 | n/a | { |
|---|
| 1309 | n/a | if (idx < 0 || idx >= self->memo_size) |
|---|
| 1310 | n/a | return NULL; |
|---|
| 1311 | n/a | |
|---|
| 1312 | n/a | return self->memo[idx]; |
|---|
| 1313 | n/a | } |
|---|
| 1314 | n/a | |
|---|
| 1315 | n/a | /* Returns -1 (with an exception set) on failure, 0 on success. |
|---|
| 1316 | n/a | This takes its own reference to `value`. */ |
|---|
| 1317 | n/a | static int |
|---|
| 1318 | n/a | _Unpickler_MemoPut(UnpicklerObject *self, Py_ssize_t idx, PyObject *value) |
|---|
| 1319 | n/a | { |
|---|
| 1320 | n/a | PyObject *old_item; |
|---|
| 1321 | n/a | |
|---|
| 1322 | n/a | if (idx >= self->memo_size) { |
|---|
| 1323 | n/a | if (_Unpickler_ResizeMemoList(self, idx * 2) < 0) |
|---|
| 1324 | n/a | return -1; |
|---|
| 1325 | n/a | assert(idx < self->memo_size); |
|---|
| 1326 | n/a | } |
|---|
| 1327 | n/a | Py_INCREF(value); |
|---|
| 1328 | n/a | old_item = self->memo[idx]; |
|---|
| 1329 | n/a | self->memo[idx] = value; |
|---|
| 1330 | n/a | if (old_item != NULL) { |
|---|
| 1331 | n/a | Py_DECREF(old_item); |
|---|
| 1332 | n/a | } |
|---|
| 1333 | n/a | else { |
|---|
| 1334 | n/a | self->memo_len++; |
|---|
| 1335 | n/a | } |
|---|
| 1336 | n/a | return 0; |
|---|
| 1337 | n/a | } |
|---|
| 1338 | n/a | |
|---|
| 1339 | n/a | static PyObject ** |
|---|
| 1340 | n/a | _Unpickler_NewMemo(Py_ssize_t new_size) |
|---|
| 1341 | n/a | { |
|---|
| 1342 | n/a | PyObject **memo = PyMem_NEW(PyObject *, new_size); |
|---|
| 1343 | n/a | if (memo == NULL) { |
|---|
| 1344 | n/a | PyErr_NoMemory(); |
|---|
| 1345 | n/a | return NULL; |
|---|
| 1346 | n/a | } |
|---|
| 1347 | n/a | memset(memo, 0, new_size * sizeof(PyObject *)); |
|---|
| 1348 | n/a | return memo; |
|---|
| 1349 | n/a | } |
|---|
| 1350 | n/a | |
|---|
| 1351 | n/a | /* Free the unpickler's memo, taking care to decref any items left in it. */ |
|---|
| 1352 | n/a | static void |
|---|
| 1353 | n/a | _Unpickler_MemoCleanup(UnpicklerObject *self) |
|---|
| 1354 | n/a | { |
|---|
| 1355 | n/a | Py_ssize_t i; |
|---|
| 1356 | n/a | PyObject **memo = self->memo; |
|---|
| 1357 | n/a | |
|---|
| 1358 | n/a | if (self->memo == NULL) |
|---|
| 1359 | n/a | return; |
|---|
| 1360 | n/a | self->memo = NULL; |
|---|
| 1361 | n/a | i = self->memo_size; |
|---|
| 1362 | n/a | while (--i >= 0) { |
|---|
| 1363 | n/a | Py_XDECREF(memo[i]); |
|---|
| 1364 | n/a | } |
|---|
| 1365 | n/a | PyMem_FREE(memo); |
|---|
| 1366 | n/a | } |
|---|
| 1367 | n/a | |
|---|
| 1368 | n/a | static UnpicklerObject * |
|---|
| 1369 | n/a | _Unpickler_New(void) |
|---|
| 1370 | n/a | { |
|---|
| 1371 | n/a | UnpicklerObject *self; |
|---|
| 1372 | n/a | |
|---|
| 1373 | n/a | self = PyObject_GC_New(UnpicklerObject, &Unpickler_Type); |
|---|
| 1374 | n/a | if (self == NULL) |
|---|
| 1375 | n/a | return NULL; |
|---|
| 1376 | n/a | |
|---|
| 1377 | n/a | self->pers_func = NULL; |
|---|
| 1378 | n/a | self->input_buffer = NULL; |
|---|
| 1379 | n/a | self->input_line = NULL; |
|---|
| 1380 | n/a | self->input_len = 0; |
|---|
| 1381 | n/a | self->next_read_idx = 0; |
|---|
| 1382 | n/a | self->prefetched_idx = 0; |
|---|
| 1383 | n/a | self->read = NULL; |
|---|
| 1384 | n/a | self->readline = NULL; |
|---|
| 1385 | n/a | self->peek = NULL; |
|---|
| 1386 | n/a | self->encoding = NULL; |
|---|
| 1387 | n/a | self->errors = NULL; |
|---|
| 1388 | n/a | self->marks = NULL; |
|---|
| 1389 | n/a | self->num_marks = 0; |
|---|
| 1390 | n/a | self->marks_size = 0; |
|---|
| 1391 | n/a | self->proto = 0; |
|---|
| 1392 | n/a | self->fix_imports = 0; |
|---|
| 1393 | n/a | memset(&self->buffer, 0, sizeof(Py_buffer)); |
|---|
| 1394 | n/a | self->memo_size = 32; |
|---|
| 1395 | n/a | self->memo_len = 0; |
|---|
| 1396 | n/a | self->memo = _Unpickler_NewMemo(self->memo_size); |
|---|
| 1397 | n/a | self->stack = (Pdata *)Pdata_New(); |
|---|
| 1398 | n/a | |
|---|
| 1399 | n/a | if (self->memo == NULL || self->stack == NULL) { |
|---|
| 1400 | n/a | Py_DECREF(self); |
|---|
| 1401 | n/a | return NULL; |
|---|
| 1402 | n/a | } |
|---|
| 1403 | n/a | |
|---|
| 1404 | n/a | return self; |
|---|
| 1405 | n/a | } |
|---|
| 1406 | n/a | |
|---|
| 1407 | n/a | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
|---|
| 1408 | n/a | be called once on a freshly created Pickler. */ |
|---|
| 1409 | n/a | static int |
|---|
| 1410 | n/a | _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file) |
|---|
| 1411 | n/a | { |
|---|
| 1412 | n/a | _Py_IDENTIFIER(peek); |
|---|
| 1413 | n/a | _Py_IDENTIFIER(read); |
|---|
| 1414 | n/a | _Py_IDENTIFIER(readline); |
|---|
| 1415 | n/a | |
|---|
| 1416 | n/a | self->peek = _PyObject_GetAttrId(file, &PyId_peek); |
|---|
| 1417 | n/a | if (self->peek == NULL) { |
|---|
| 1418 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 1419 | n/a | PyErr_Clear(); |
|---|
| 1420 | n/a | else |
|---|
| 1421 | n/a | return -1; |
|---|
| 1422 | n/a | } |
|---|
| 1423 | n/a | self->read = _PyObject_GetAttrId(file, &PyId_read); |
|---|
| 1424 | n/a | self->readline = _PyObject_GetAttrId(file, &PyId_readline); |
|---|
| 1425 | n/a | if (self->readline == NULL || self->read == NULL) { |
|---|
| 1426 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 1427 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 1428 | n/a | "file must have 'read' and 'readline' attributes"); |
|---|
| 1429 | n/a | Py_CLEAR(self->read); |
|---|
| 1430 | n/a | Py_CLEAR(self->readline); |
|---|
| 1431 | n/a | Py_CLEAR(self->peek); |
|---|
| 1432 | n/a | return -1; |
|---|
| 1433 | n/a | } |
|---|
| 1434 | n/a | return 0; |
|---|
| 1435 | n/a | } |
|---|
| 1436 | n/a | |
|---|
| 1437 | n/a | /* Returns -1 (with an exception set) on failure, 0 on success. This may |
|---|
| 1438 | n/a | be called once on a freshly created Pickler. */ |
|---|
| 1439 | n/a | static int |
|---|
| 1440 | n/a | _Unpickler_SetInputEncoding(UnpicklerObject *self, |
|---|
| 1441 | n/a | const char *encoding, |
|---|
| 1442 | n/a | const char *errors) |
|---|
| 1443 | n/a | { |
|---|
| 1444 | n/a | if (encoding == NULL) |
|---|
| 1445 | n/a | encoding = "ASCII"; |
|---|
| 1446 | n/a | if (errors == NULL) |
|---|
| 1447 | n/a | errors = "strict"; |
|---|
| 1448 | n/a | |
|---|
| 1449 | n/a | self->encoding = _PyMem_Strdup(encoding); |
|---|
| 1450 | n/a | self->errors = _PyMem_Strdup(errors); |
|---|
| 1451 | n/a | if (self->encoding == NULL || self->errors == NULL) { |
|---|
| 1452 | n/a | PyErr_NoMemory(); |
|---|
| 1453 | n/a | return -1; |
|---|
| 1454 | n/a | } |
|---|
| 1455 | n/a | return 0; |
|---|
| 1456 | n/a | } |
|---|
| 1457 | n/a | |
|---|
| 1458 | n/a | /* Generate a GET opcode for an object stored in the memo. */ |
|---|
| 1459 | n/a | static int |
|---|
| 1460 | n/a | memo_get(PicklerObject *self, PyObject *key) |
|---|
| 1461 | n/a | { |
|---|
| 1462 | n/a | Py_ssize_t *value; |
|---|
| 1463 | n/a | char pdata[30]; |
|---|
| 1464 | n/a | Py_ssize_t len; |
|---|
| 1465 | n/a | |
|---|
| 1466 | n/a | value = PyMemoTable_Get(self->memo, key); |
|---|
| 1467 | n/a | if (value == NULL) { |
|---|
| 1468 | n/a | PyErr_SetObject(PyExc_KeyError, key); |
|---|
| 1469 | n/a | return -1; |
|---|
| 1470 | n/a | } |
|---|
| 1471 | n/a | |
|---|
| 1472 | n/a | if (!self->bin) { |
|---|
| 1473 | n/a | pdata[0] = GET; |
|---|
| 1474 | n/a | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
|---|
| 1475 | n/a | "%" PY_FORMAT_SIZE_T "d\n", *value); |
|---|
| 1476 | n/a | len = strlen(pdata); |
|---|
| 1477 | n/a | } |
|---|
| 1478 | n/a | else { |
|---|
| 1479 | n/a | if (*value < 256) { |
|---|
| 1480 | n/a | pdata[0] = BINGET; |
|---|
| 1481 | n/a | pdata[1] = (unsigned char)(*value & 0xff); |
|---|
| 1482 | n/a | len = 2; |
|---|
| 1483 | n/a | } |
|---|
| 1484 | n/a | else if ((size_t)*value <= 0xffffffffUL) { |
|---|
| 1485 | n/a | pdata[0] = LONG_BINGET; |
|---|
| 1486 | n/a | pdata[1] = (unsigned char)(*value & 0xff); |
|---|
| 1487 | n/a | pdata[2] = (unsigned char)((*value >> 8) & 0xff); |
|---|
| 1488 | n/a | pdata[3] = (unsigned char)((*value >> 16) & 0xff); |
|---|
| 1489 | n/a | pdata[4] = (unsigned char)((*value >> 24) & 0xff); |
|---|
| 1490 | n/a | len = 5; |
|---|
| 1491 | n/a | } |
|---|
| 1492 | n/a | else { /* unlikely */ |
|---|
| 1493 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 1494 | n/a | PyErr_SetString(st->PicklingError, |
|---|
| 1495 | n/a | "memo id too large for LONG_BINGET"); |
|---|
| 1496 | n/a | return -1; |
|---|
| 1497 | n/a | } |
|---|
| 1498 | n/a | } |
|---|
| 1499 | n/a | |
|---|
| 1500 | n/a | if (_Pickler_Write(self, pdata, len) < 0) |
|---|
| 1501 | n/a | return -1; |
|---|
| 1502 | n/a | |
|---|
| 1503 | n/a | return 0; |
|---|
| 1504 | n/a | } |
|---|
| 1505 | n/a | |
|---|
| 1506 | n/a | /* Store an object in the memo, assign it a new unique ID based on the number |
|---|
| 1507 | n/a | of objects currently stored in the memo and generate a PUT opcode. */ |
|---|
| 1508 | n/a | static int |
|---|
| 1509 | n/a | memo_put(PicklerObject *self, PyObject *obj) |
|---|
| 1510 | n/a | { |
|---|
| 1511 | n/a | char pdata[30]; |
|---|
| 1512 | n/a | Py_ssize_t len; |
|---|
| 1513 | n/a | Py_ssize_t idx; |
|---|
| 1514 | n/a | |
|---|
| 1515 | n/a | const char memoize_op = MEMOIZE; |
|---|
| 1516 | n/a | |
|---|
| 1517 | n/a | if (self->fast) |
|---|
| 1518 | n/a | return 0; |
|---|
| 1519 | n/a | |
|---|
| 1520 | n/a | idx = PyMemoTable_Size(self->memo); |
|---|
| 1521 | n/a | if (PyMemoTable_Set(self->memo, obj, idx) < 0) |
|---|
| 1522 | n/a | return -1; |
|---|
| 1523 | n/a | |
|---|
| 1524 | n/a | if (self->proto >= 4) { |
|---|
| 1525 | n/a | if (_Pickler_Write(self, &memoize_op, 1) < 0) |
|---|
| 1526 | n/a | return -1; |
|---|
| 1527 | n/a | return 0; |
|---|
| 1528 | n/a | } |
|---|
| 1529 | n/a | else if (!self->bin) { |
|---|
| 1530 | n/a | pdata[0] = PUT; |
|---|
| 1531 | n/a | PyOS_snprintf(pdata + 1, sizeof(pdata) - 1, |
|---|
| 1532 | n/a | "%" PY_FORMAT_SIZE_T "d\n", idx); |
|---|
| 1533 | n/a | len = strlen(pdata); |
|---|
| 1534 | n/a | } |
|---|
| 1535 | n/a | else { |
|---|
| 1536 | n/a | if (idx < 256) { |
|---|
| 1537 | n/a | pdata[0] = BINPUT; |
|---|
| 1538 | n/a | pdata[1] = (unsigned char)idx; |
|---|
| 1539 | n/a | len = 2; |
|---|
| 1540 | n/a | } |
|---|
| 1541 | n/a | else if ((size_t)idx <= 0xffffffffUL) { |
|---|
| 1542 | n/a | pdata[0] = LONG_BINPUT; |
|---|
| 1543 | n/a | pdata[1] = (unsigned char)(idx & 0xff); |
|---|
| 1544 | n/a | pdata[2] = (unsigned char)((idx >> 8) & 0xff); |
|---|
| 1545 | n/a | pdata[3] = (unsigned char)((idx >> 16) & 0xff); |
|---|
| 1546 | n/a | pdata[4] = (unsigned char)((idx >> 24) & 0xff); |
|---|
| 1547 | n/a | len = 5; |
|---|
| 1548 | n/a | } |
|---|
| 1549 | n/a | else { /* unlikely */ |
|---|
| 1550 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 1551 | n/a | PyErr_SetString(st->PicklingError, |
|---|
| 1552 | n/a | "memo id too large for LONG_BINPUT"); |
|---|
| 1553 | n/a | return -1; |
|---|
| 1554 | n/a | } |
|---|
| 1555 | n/a | } |
|---|
| 1556 | n/a | if (_Pickler_Write(self, pdata, len) < 0) |
|---|
| 1557 | n/a | return -1; |
|---|
| 1558 | n/a | |
|---|
| 1559 | n/a | return 0; |
|---|
| 1560 | n/a | } |
|---|
| 1561 | n/a | |
|---|
| 1562 | n/a | static PyObject * |
|---|
| 1563 | n/a | get_dotted_path(PyObject *obj, PyObject *name) |
|---|
| 1564 | n/a | { |
|---|
| 1565 | n/a | _Py_static_string(PyId_dot, "."); |
|---|
| 1566 | n/a | PyObject *dotted_path; |
|---|
| 1567 | n/a | Py_ssize_t i, n; |
|---|
| 1568 | n/a | |
|---|
| 1569 | n/a | dotted_path = PyUnicode_Split(name, _PyUnicode_FromId(&PyId_dot), -1); |
|---|
| 1570 | n/a | if (dotted_path == NULL) |
|---|
| 1571 | n/a | return NULL; |
|---|
| 1572 | n/a | n = PyList_GET_SIZE(dotted_path); |
|---|
| 1573 | n/a | assert(n >= 1); |
|---|
| 1574 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1575 | n/a | PyObject *subpath = PyList_GET_ITEM(dotted_path, i); |
|---|
| 1576 | n/a | if (_PyUnicode_EqualToASCIIString(subpath, "<locals>")) { |
|---|
| 1577 | n/a | if (obj == NULL) |
|---|
| 1578 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 1579 | n/a | "Can't pickle local object %R", name); |
|---|
| 1580 | n/a | else |
|---|
| 1581 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 1582 | n/a | "Can't pickle local attribute %R on %R", name, obj); |
|---|
| 1583 | n/a | Py_DECREF(dotted_path); |
|---|
| 1584 | n/a | return NULL; |
|---|
| 1585 | n/a | } |
|---|
| 1586 | n/a | } |
|---|
| 1587 | n/a | return dotted_path; |
|---|
| 1588 | n/a | } |
|---|
| 1589 | n/a | |
|---|
| 1590 | n/a | static PyObject * |
|---|
| 1591 | n/a | get_deep_attribute(PyObject *obj, PyObject *names, PyObject **pparent) |
|---|
| 1592 | n/a | { |
|---|
| 1593 | n/a | Py_ssize_t i, n; |
|---|
| 1594 | n/a | PyObject *parent = NULL; |
|---|
| 1595 | n/a | |
|---|
| 1596 | n/a | assert(PyList_CheckExact(names)); |
|---|
| 1597 | n/a | Py_INCREF(obj); |
|---|
| 1598 | n/a | n = PyList_GET_SIZE(names); |
|---|
| 1599 | n/a | for (i = 0; i < n; i++) { |
|---|
| 1600 | n/a | PyObject *name = PyList_GET_ITEM(names, i); |
|---|
| 1601 | n/a | Py_XDECREF(parent); |
|---|
| 1602 | n/a | parent = obj; |
|---|
| 1603 | n/a | obj = PyObject_GetAttr(parent, name); |
|---|
| 1604 | n/a | if (obj == NULL) { |
|---|
| 1605 | n/a | Py_DECREF(parent); |
|---|
| 1606 | n/a | return NULL; |
|---|
| 1607 | n/a | } |
|---|
| 1608 | n/a | } |
|---|
| 1609 | n/a | if (pparent != NULL) |
|---|
| 1610 | n/a | *pparent = parent; |
|---|
| 1611 | n/a | else |
|---|
| 1612 | n/a | Py_XDECREF(parent); |
|---|
| 1613 | n/a | return obj; |
|---|
| 1614 | n/a | } |
|---|
| 1615 | n/a | |
|---|
| 1616 | n/a | static void |
|---|
| 1617 | n/a | reformat_attribute_error(PyObject *obj, PyObject *name) |
|---|
| 1618 | n/a | { |
|---|
| 1619 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 1620 | n/a | PyErr_Clear(); |
|---|
| 1621 | n/a | PyErr_Format(PyExc_AttributeError, |
|---|
| 1622 | n/a | "Can't get attribute %R on %R", name, obj); |
|---|
| 1623 | n/a | } |
|---|
| 1624 | n/a | } |
|---|
| 1625 | n/a | |
|---|
| 1626 | n/a | |
|---|
| 1627 | n/a | static PyObject * |
|---|
| 1628 | n/a | getattribute(PyObject *obj, PyObject *name, int allow_qualname) |
|---|
| 1629 | n/a | { |
|---|
| 1630 | n/a | PyObject *dotted_path, *attr; |
|---|
| 1631 | n/a | |
|---|
| 1632 | n/a | if (allow_qualname) { |
|---|
| 1633 | n/a | dotted_path = get_dotted_path(obj, name); |
|---|
| 1634 | n/a | if (dotted_path == NULL) |
|---|
| 1635 | n/a | return NULL; |
|---|
| 1636 | n/a | attr = get_deep_attribute(obj, dotted_path, NULL); |
|---|
| 1637 | n/a | Py_DECREF(dotted_path); |
|---|
| 1638 | n/a | } |
|---|
| 1639 | n/a | else |
|---|
| 1640 | n/a | attr = PyObject_GetAttr(obj, name); |
|---|
| 1641 | n/a | if (attr == NULL) |
|---|
| 1642 | n/a | reformat_attribute_error(obj, name); |
|---|
| 1643 | n/a | return attr; |
|---|
| 1644 | n/a | } |
|---|
| 1645 | n/a | |
|---|
| 1646 | n/a | static PyObject * |
|---|
| 1647 | n/a | whichmodule(PyObject *global, PyObject *dotted_path) |
|---|
| 1648 | n/a | { |
|---|
| 1649 | n/a | PyObject *module_name; |
|---|
| 1650 | n/a | PyObject *modules_dict; |
|---|
| 1651 | n/a | PyObject *module; |
|---|
| 1652 | n/a | Py_ssize_t i; |
|---|
| 1653 | n/a | _Py_IDENTIFIER(__module__); |
|---|
| 1654 | n/a | _Py_IDENTIFIER(modules); |
|---|
| 1655 | n/a | _Py_IDENTIFIER(__main__); |
|---|
| 1656 | n/a | |
|---|
| 1657 | n/a | module_name = _PyObject_GetAttrId(global, &PyId___module__); |
|---|
| 1658 | n/a | |
|---|
| 1659 | n/a | if (module_name == NULL) { |
|---|
| 1660 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 1661 | n/a | return NULL; |
|---|
| 1662 | n/a | PyErr_Clear(); |
|---|
| 1663 | n/a | } |
|---|
| 1664 | n/a | else { |
|---|
| 1665 | n/a | /* In some rare cases (e.g., bound methods of extension types), |
|---|
| 1666 | n/a | __module__ can be None. If it is so, then search sys.modules for |
|---|
| 1667 | n/a | the module of global. */ |
|---|
| 1668 | n/a | if (module_name != Py_None) |
|---|
| 1669 | n/a | return module_name; |
|---|
| 1670 | n/a | Py_CLEAR(module_name); |
|---|
| 1671 | n/a | } |
|---|
| 1672 | n/a | assert(module_name == NULL); |
|---|
| 1673 | n/a | |
|---|
| 1674 | n/a | /* Fallback on walking sys.modules */ |
|---|
| 1675 | n/a | modules_dict = _PySys_GetObjectId(&PyId_modules); |
|---|
| 1676 | n/a | if (modules_dict == NULL) { |
|---|
| 1677 | n/a | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
|---|
| 1678 | n/a | return NULL; |
|---|
| 1679 | n/a | } |
|---|
| 1680 | n/a | |
|---|
| 1681 | n/a | i = 0; |
|---|
| 1682 | n/a | while (PyDict_Next(modules_dict, &i, &module_name, &module)) { |
|---|
| 1683 | n/a | PyObject *candidate; |
|---|
| 1684 | n/a | if (PyUnicode_Check(module_name) && |
|---|
| 1685 | n/a | _PyUnicode_EqualToASCIIString(module_name, "__main__")) |
|---|
| 1686 | n/a | continue; |
|---|
| 1687 | n/a | if (module == Py_None) |
|---|
| 1688 | n/a | continue; |
|---|
| 1689 | n/a | |
|---|
| 1690 | n/a | candidate = get_deep_attribute(module, dotted_path, NULL); |
|---|
| 1691 | n/a | if (candidate == NULL) { |
|---|
| 1692 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 1693 | n/a | return NULL; |
|---|
| 1694 | n/a | PyErr_Clear(); |
|---|
| 1695 | n/a | continue; |
|---|
| 1696 | n/a | } |
|---|
| 1697 | n/a | |
|---|
| 1698 | n/a | if (candidate == global) { |
|---|
| 1699 | n/a | Py_INCREF(module_name); |
|---|
| 1700 | n/a | Py_DECREF(candidate); |
|---|
| 1701 | n/a | return module_name; |
|---|
| 1702 | n/a | } |
|---|
| 1703 | n/a | Py_DECREF(candidate); |
|---|
| 1704 | n/a | } |
|---|
| 1705 | n/a | |
|---|
| 1706 | n/a | /* If no module is found, use __main__. */ |
|---|
| 1707 | n/a | module_name = _PyUnicode_FromId(&PyId___main__); |
|---|
| 1708 | n/a | Py_INCREF(module_name); |
|---|
| 1709 | n/a | return module_name; |
|---|
| 1710 | n/a | } |
|---|
| 1711 | n/a | |
|---|
| 1712 | n/a | /* fast_save_enter() and fast_save_leave() are guards against recursive |
|---|
| 1713 | n/a | objects when Pickler is used with the "fast mode" (i.e., with object |
|---|
| 1714 | n/a | memoization disabled). If the nesting of a list or dict object exceed |
|---|
| 1715 | n/a | FAST_NESTING_LIMIT, these guards will start keeping an internal |
|---|
| 1716 | n/a | reference to the seen list or dict objects and check whether these objects |
|---|
| 1717 | n/a | are recursive. These are not strictly necessary, since save() has a |
|---|
| 1718 | n/a | hard-coded recursion limit, but they give a nicer error message than the |
|---|
| 1719 | n/a | typical RuntimeError. */ |
|---|
| 1720 | n/a | static int |
|---|
| 1721 | n/a | fast_save_enter(PicklerObject *self, PyObject *obj) |
|---|
| 1722 | n/a | { |
|---|
| 1723 | n/a | /* if fast_nesting < 0, we're doing an error exit. */ |
|---|
| 1724 | n/a | if (++self->fast_nesting >= FAST_NESTING_LIMIT) { |
|---|
| 1725 | n/a | PyObject *key = NULL; |
|---|
| 1726 | n/a | if (self->fast_memo == NULL) { |
|---|
| 1727 | n/a | self->fast_memo = PyDict_New(); |
|---|
| 1728 | n/a | if (self->fast_memo == NULL) { |
|---|
| 1729 | n/a | self->fast_nesting = -1; |
|---|
| 1730 | n/a | return 0; |
|---|
| 1731 | n/a | } |
|---|
| 1732 | n/a | } |
|---|
| 1733 | n/a | key = PyLong_FromVoidPtr(obj); |
|---|
| 1734 | n/a | if (key == NULL) |
|---|
| 1735 | n/a | return 0; |
|---|
| 1736 | n/a | if (PyDict_GetItemWithError(self->fast_memo, key)) { |
|---|
| 1737 | n/a | Py_DECREF(key); |
|---|
| 1738 | n/a | PyErr_Format(PyExc_ValueError, |
|---|
| 1739 | n/a | "fast mode: can't pickle cyclic objects " |
|---|
| 1740 | n/a | "including object type %.200s at %p", |
|---|
| 1741 | n/a | obj->ob_type->tp_name, obj); |
|---|
| 1742 | n/a | self->fast_nesting = -1; |
|---|
| 1743 | n/a | return 0; |
|---|
| 1744 | n/a | } |
|---|
| 1745 | n/a | if (PyErr_Occurred()) { |
|---|
| 1746 | n/a | return 0; |
|---|
| 1747 | n/a | } |
|---|
| 1748 | n/a | if (PyDict_SetItem(self->fast_memo, key, Py_None) < 0) { |
|---|
| 1749 | n/a | Py_DECREF(key); |
|---|
| 1750 | n/a | self->fast_nesting = -1; |
|---|
| 1751 | n/a | return 0; |
|---|
| 1752 | n/a | } |
|---|
| 1753 | n/a | Py_DECREF(key); |
|---|
| 1754 | n/a | } |
|---|
| 1755 | n/a | return 1; |
|---|
| 1756 | n/a | } |
|---|
| 1757 | n/a | |
|---|
| 1758 | n/a | static int |
|---|
| 1759 | n/a | fast_save_leave(PicklerObject *self, PyObject *obj) |
|---|
| 1760 | n/a | { |
|---|
| 1761 | n/a | if (self->fast_nesting-- >= FAST_NESTING_LIMIT) { |
|---|
| 1762 | n/a | PyObject *key = PyLong_FromVoidPtr(obj); |
|---|
| 1763 | n/a | if (key == NULL) |
|---|
| 1764 | n/a | return 0; |
|---|
| 1765 | n/a | if (PyDict_DelItem(self->fast_memo, key) < 0) { |
|---|
| 1766 | n/a | Py_DECREF(key); |
|---|
| 1767 | n/a | return 0; |
|---|
| 1768 | n/a | } |
|---|
| 1769 | n/a | Py_DECREF(key); |
|---|
| 1770 | n/a | } |
|---|
| 1771 | n/a | return 1; |
|---|
| 1772 | n/a | } |
|---|
| 1773 | n/a | |
|---|
| 1774 | n/a | static int |
|---|
| 1775 | n/a | save_none(PicklerObject *self, PyObject *obj) |
|---|
| 1776 | n/a | { |
|---|
| 1777 | n/a | const char none_op = NONE; |
|---|
| 1778 | n/a | if (_Pickler_Write(self, &none_op, 1) < 0) |
|---|
| 1779 | n/a | return -1; |
|---|
| 1780 | n/a | |
|---|
| 1781 | n/a | return 0; |
|---|
| 1782 | n/a | } |
|---|
| 1783 | n/a | |
|---|
| 1784 | n/a | static int |
|---|
| 1785 | n/a | save_bool(PicklerObject *self, PyObject *obj) |
|---|
| 1786 | n/a | { |
|---|
| 1787 | n/a | if (self->proto >= 2) { |
|---|
| 1788 | n/a | const char bool_op = (obj == Py_True) ? NEWTRUE : NEWFALSE; |
|---|
| 1789 | n/a | if (_Pickler_Write(self, &bool_op, 1) < 0) |
|---|
| 1790 | n/a | return -1; |
|---|
| 1791 | n/a | } |
|---|
| 1792 | n/a | else { |
|---|
| 1793 | n/a | /* These aren't opcodes -- they're ways to pickle bools before protocol 2 |
|---|
| 1794 | n/a | * so that unpicklers written before bools were introduced unpickle them |
|---|
| 1795 | n/a | * as ints, but unpicklers after can recognize that bools were intended. |
|---|
| 1796 | n/a | * Note that protocol 2 added direct ways to pickle bools. |
|---|
| 1797 | n/a | */ |
|---|
| 1798 | n/a | const char *bool_str = (obj == Py_True) ? "I01\n" : "I00\n"; |
|---|
| 1799 | n/a | if (_Pickler_Write(self, bool_str, strlen(bool_str)) < 0) |
|---|
| 1800 | n/a | return -1; |
|---|
| 1801 | n/a | } |
|---|
| 1802 | n/a | return 0; |
|---|
| 1803 | n/a | } |
|---|
| 1804 | n/a | |
|---|
| 1805 | n/a | static int |
|---|
| 1806 | n/a | save_long(PicklerObject *self, PyObject *obj) |
|---|
| 1807 | n/a | { |
|---|
| 1808 | n/a | PyObject *repr = NULL; |
|---|
| 1809 | n/a | Py_ssize_t size; |
|---|
| 1810 | n/a | long val; |
|---|
| 1811 | n/a | int status = 0; |
|---|
| 1812 | n/a | |
|---|
| 1813 | n/a | const char long_op = LONG; |
|---|
| 1814 | n/a | |
|---|
| 1815 | n/a | val= PyLong_AsLong(obj); |
|---|
| 1816 | n/a | if (val == -1 && PyErr_Occurred()) { |
|---|
| 1817 | n/a | /* out of range for int pickling */ |
|---|
| 1818 | n/a | PyErr_Clear(); |
|---|
| 1819 | n/a | } |
|---|
| 1820 | n/a | else if (self->bin && |
|---|
| 1821 | n/a | (sizeof(long) <= 4 || |
|---|
| 1822 | n/a | (val <= 0x7fffffffL && val >= (-0x7fffffffL - 1)))) { |
|---|
| 1823 | n/a | /* result fits in a signed 4-byte integer. |
|---|
| 1824 | n/a | |
|---|
| 1825 | n/a | Note: we can't use -0x80000000L in the above condition because some |
|---|
| 1826 | n/a | compilers (e.g., MSVC) will promote 0x80000000L to an unsigned type |
|---|
| 1827 | n/a | before applying the unary minus when sizeof(long) <= 4. The |
|---|
| 1828 | n/a | resulting value stays unsigned which is commonly not what we want, |
|---|
| 1829 | n/a | so MSVC happily warns us about it. However, that result would have |
|---|
| 1830 | n/a | been fine because we guard for sizeof(long) <= 4 which turns the |
|---|
| 1831 | n/a | condition true in that particular case. */ |
|---|
| 1832 | n/a | char pdata[32]; |
|---|
| 1833 | n/a | Py_ssize_t len = 0; |
|---|
| 1834 | n/a | |
|---|
| 1835 | n/a | pdata[1] = (unsigned char)(val & 0xff); |
|---|
| 1836 | n/a | pdata[2] = (unsigned char)((val >> 8) & 0xff); |
|---|
| 1837 | n/a | pdata[3] = (unsigned char)((val >> 16) & 0xff); |
|---|
| 1838 | n/a | pdata[4] = (unsigned char)((val >> 24) & 0xff); |
|---|
| 1839 | n/a | |
|---|
| 1840 | n/a | if ((pdata[4] == 0) && (pdata[3] == 0)) { |
|---|
| 1841 | n/a | if (pdata[2] == 0) { |
|---|
| 1842 | n/a | pdata[0] = BININT1; |
|---|
| 1843 | n/a | len = 2; |
|---|
| 1844 | n/a | } |
|---|
| 1845 | n/a | else { |
|---|
| 1846 | n/a | pdata[0] = BININT2; |
|---|
| 1847 | n/a | len = 3; |
|---|
| 1848 | n/a | } |
|---|
| 1849 | n/a | } |
|---|
| 1850 | n/a | else { |
|---|
| 1851 | n/a | pdata[0] = BININT; |
|---|
| 1852 | n/a | len = 5; |
|---|
| 1853 | n/a | } |
|---|
| 1854 | n/a | |
|---|
| 1855 | n/a | if (_Pickler_Write(self, pdata, len) < 0) |
|---|
| 1856 | n/a | return -1; |
|---|
| 1857 | n/a | |
|---|
| 1858 | n/a | return 0; |
|---|
| 1859 | n/a | } |
|---|
| 1860 | n/a | |
|---|
| 1861 | n/a | if (self->proto >= 2) { |
|---|
| 1862 | n/a | /* Linear-time pickling. */ |
|---|
| 1863 | n/a | size_t nbits; |
|---|
| 1864 | n/a | size_t nbytes; |
|---|
| 1865 | n/a | unsigned char *pdata; |
|---|
| 1866 | n/a | char header[5]; |
|---|
| 1867 | n/a | int i; |
|---|
| 1868 | n/a | int sign = _PyLong_Sign(obj); |
|---|
| 1869 | n/a | |
|---|
| 1870 | n/a | if (sign == 0) { |
|---|
| 1871 | n/a | header[0] = LONG1; |
|---|
| 1872 | n/a | header[1] = 0; /* It's 0 -- an empty bytestring. */ |
|---|
| 1873 | n/a | if (_Pickler_Write(self, header, 2) < 0) |
|---|
| 1874 | n/a | goto error; |
|---|
| 1875 | n/a | return 0; |
|---|
| 1876 | n/a | } |
|---|
| 1877 | n/a | nbits = _PyLong_NumBits(obj); |
|---|
| 1878 | n/a | if (nbits == (size_t)-1 && PyErr_Occurred()) |
|---|
| 1879 | n/a | goto error; |
|---|
| 1880 | n/a | /* How many bytes do we need? There are nbits >> 3 full |
|---|
| 1881 | n/a | * bytes of data, and nbits & 7 leftover bits. If there |
|---|
| 1882 | n/a | * are any leftover bits, then we clearly need another |
|---|
| 1883 | n/a | * byte. Wnat's not so obvious is that we *probably* |
|---|
| 1884 | n/a | * need another byte even if there aren't any leftovers: |
|---|
| 1885 | n/a | * the most-significant bit of the most-significant byte |
|---|
| 1886 | n/a | * acts like a sign bit, and it's usually got a sense |
|---|
| 1887 | n/a | * opposite of the one we need. The exception is ints |
|---|
| 1888 | n/a | * of the form -(2**(8*j-1)) for j > 0. Such an int is |
|---|
| 1889 | n/a | * its own 256's-complement, so has the right sign bit |
|---|
| 1890 | n/a | * even without the extra byte. That's a pain to check |
|---|
| 1891 | n/a | * for in advance, though, so we always grab an extra |
|---|
| 1892 | n/a | * byte at the start, and cut it back later if possible. |
|---|
| 1893 | n/a | */ |
|---|
| 1894 | n/a | nbytes = (nbits >> 3) + 1; |
|---|
| 1895 | n/a | if (nbytes > 0x7fffffffL) { |
|---|
| 1896 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 1897 | n/a | "int too large to pickle"); |
|---|
| 1898 | n/a | goto error; |
|---|
| 1899 | n/a | } |
|---|
| 1900 | n/a | repr = PyBytes_FromStringAndSize(NULL, (Py_ssize_t)nbytes); |
|---|
| 1901 | n/a | if (repr == NULL) |
|---|
| 1902 | n/a | goto error; |
|---|
| 1903 | n/a | pdata = (unsigned char *)PyBytes_AS_STRING(repr); |
|---|
| 1904 | n/a | i = _PyLong_AsByteArray((PyLongObject *)obj, |
|---|
| 1905 | n/a | pdata, nbytes, |
|---|
| 1906 | n/a | 1 /* little endian */ , 1 /* signed */ ); |
|---|
| 1907 | n/a | if (i < 0) |
|---|
| 1908 | n/a | goto error; |
|---|
| 1909 | n/a | /* If the int is negative, this may be a byte more than |
|---|
| 1910 | n/a | * needed. This is so iff the MSB is all redundant sign |
|---|
| 1911 | n/a | * bits. |
|---|
| 1912 | n/a | */ |
|---|
| 1913 | n/a | if (sign < 0 && |
|---|
| 1914 | n/a | nbytes > 1 && |
|---|
| 1915 | n/a | pdata[nbytes - 1] == 0xff && |
|---|
| 1916 | n/a | (pdata[nbytes - 2] & 0x80) != 0) { |
|---|
| 1917 | n/a | nbytes--; |
|---|
| 1918 | n/a | } |
|---|
| 1919 | n/a | |
|---|
| 1920 | n/a | if (nbytes < 256) { |
|---|
| 1921 | n/a | header[0] = LONG1; |
|---|
| 1922 | n/a | header[1] = (unsigned char)nbytes; |
|---|
| 1923 | n/a | size = 2; |
|---|
| 1924 | n/a | } |
|---|
| 1925 | n/a | else { |
|---|
| 1926 | n/a | header[0] = LONG4; |
|---|
| 1927 | n/a | size = (Py_ssize_t) nbytes; |
|---|
| 1928 | n/a | for (i = 1; i < 5; i++) { |
|---|
| 1929 | n/a | header[i] = (unsigned char)(size & 0xff); |
|---|
| 1930 | n/a | size >>= 8; |
|---|
| 1931 | n/a | } |
|---|
| 1932 | n/a | size = 5; |
|---|
| 1933 | n/a | } |
|---|
| 1934 | n/a | if (_Pickler_Write(self, header, size) < 0 || |
|---|
| 1935 | n/a | _Pickler_Write(self, (char *)pdata, (int)nbytes) < 0) |
|---|
| 1936 | n/a | goto error; |
|---|
| 1937 | n/a | } |
|---|
| 1938 | n/a | else { |
|---|
| 1939 | n/a | const char *string; |
|---|
| 1940 | n/a | |
|---|
| 1941 | n/a | /* proto < 2: write the repr and newline. This is quadratic-time (in |
|---|
| 1942 | n/a | the number of digits), in both directions. We add a trailing 'L' |
|---|
| 1943 | n/a | to the repr, for compatibility with Python 2.x. */ |
|---|
| 1944 | n/a | |
|---|
| 1945 | n/a | repr = PyObject_Repr(obj); |
|---|
| 1946 | n/a | if (repr == NULL) |
|---|
| 1947 | n/a | goto error; |
|---|
| 1948 | n/a | |
|---|
| 1949 | n/a | string = PyUnicode_AsUTF8AndSize(repr, &size); |
|---|
| 1950 | n/a | if (string == NULL) |
|---|
| 1951 | n/a | goto error; |
|---|
| 1952 | n/a | |
|---|
| 1953 | n/a | if (_Pickler_Write(self, &long_op, 1) < 0 || |
|---|
| 1954 | n/a | _Pickler_Write(self, string, size) < 0 || |
|---|
| 1955 | n/a | _Pickler_Write(self, "L\n", 2) < 0) |
|---|
| 1956 | n/a | goto error; |
|---|
| 1957 | n/a | } |
|---|
| 1958 | n/a | |
|---|
| 1959 | n/a | if (0) { |
|---|
| 1960 | n/a | error: |
|---|
| 1961 | n/a | status = -1; |
|---|
| 1962 | n/a | } |
|---|
| 1963 | n/a | Py_XDECREF(repr); |
|---|
| 1964 | n/a | |
|---|
| 1965 | n/a | return status; |
|---|
| 1966 | n/a | } |
|---|
| 1967 | n/a | |
|---|
| 1968 | n/a | static int |
|---|
| 1969 | n/a | save_float(PicklerObject *self, PyObject *obj) |
|---|
| 1970 | n/a | { |
|---|
| 1971 | n/a | double x = PyFloat_AS_DOUBLE((PyFloatObject *)obj); |
|---|
| 1972 | n/a | |
|---|
| 1973 | n/a | if (self->bin) { |
|---|
| 1974 | n/a | char pdata[9]; |
|---|
| 1975 | n/a | pdata[0] = BINFLOAT; |
|---|
| 1976 | n/a | if (_PyFloat_Pack8(x, (unsigned char *)&pdata[1], 0) < 0) |
|---|
| 1977 | n/a | return -1; |
|---|
| 1978 | n/a | if (_Pickler_Write(self, pdata, 9) < 0) |
|---|
| 1979 | n/a | return -1; |
|---|
| 1980 | n/a | } |
|---|
| 1981 | n/a | else { |
|---|
| 1982 | n/a | int result = -1; |
|---|
| 1983 | n/a | char *buf = NULL; |
|---|
| 1984 | n/a | char op = FLOAT; |
|---|
| 1985 | n/a | |
|---|
| 1986 | n/a | if (_Pickler_Write(self, &op, 1) < 0) |
|---|
| 1987 | n/a | goto done; |
|---|
| 1988 | n/a | |
|---|
| 1989 | n/a | buf = PyOS_double_to_string(x, 'r', 0, Py_DTSF_ADD_DOT_0, NULL); |
|---|
| 1990 | n/a | if (!buf) { |
|---|
| 1991 | n/a | PyErr_NoMemory(); |
|---|
| 1992 | n/a | goto done; |
|---|
| 1993 | n/a | } |
|---|
| 1994 | n/a | |
|---|
| 1995 | n/a | if (_Pickler_Write(self, buf, strlen(buf)) < 0) |
|---|
| 1996 | n/a | goto done; |
|---|
| 1997 | n/a | |
|---|
| 1998 | n/a | if (_Pickler_Write(self, "\n", 1) < 0) |
|---|
| 1999 | n/a | goto done; |
|---|
| 2000 | n/a | |
|---|
| 2001 | n/a | result = 0; |
|---|
| 2002 | n/a | done: |
|---|
| 2003 | n/a | PyMem_Free(buf); |
|---|
| 2004 | n/a | return result; |
|---|
| 2005 | n/a | } |
|---|
| 2006 | n/a | |
|---|
| 2007 | n/a | return 0; |
|---|
| 2008 | n/a | } |
|---|
| 2009 | n/a | |
|---|
| 2010 | n/a | static int |
|---|
| 2011 | n/a | save_bytes(PicklerObject *self, PyObject *obj) |
|---|
| 2012 | n/a | { |
|---|
| 2013 | n/a | if (self->proto < 3) { |
|---|
| 2014 | n/a | /* Older pickle protocols do not have an opcode for pickling bytes |
|---|
| 2015 | n/a | objects. Therefore, we need to fake the copy protocol (i.e., |
|---|
| 2016 | n/a | the __reduce__ method) to permit bytes object unpickling. |
|---|
| 2017 | n/a | |
|---|
| 2018 | n/a | Here we use a hack to be compatible with Python 2. Since in Python |
|---|
| 2019 | n/a | 2 'bytes' is just an alias for 'str' (which has different |
|---|
| 2020 | n/a | parameters than the actual bytes object), we use codecs.encode |
|---|
| 2021 | n/a | to create the appropriate 'str' object when unpickled using |
|---|
| 2022 | n/a | Python 2 *and* the appropriate 'bytes' object when unpickled |
|---|
| 2023 | n/a | using Python 3. Again this is a hack and we don't need to do this |
|---|
| 2024 | n/a | with newer protocols. */ |
|---|
| 2025 | n/a | PyObject *reduce_value = NULL; |
|---|
| 2026 | n/a | int status; |
|---|
| 2027 | n/a | |
|---|
| 2028 | n/a | if (PyBytes_GET_SIZE(obj) == 0) { |
|---|
| 2029 | n/a | reduce_value = Py_BuildValue("(O())", (PyObject*)&PyBytes_Type); |
|---|
| 2030 | n/a | } |
|---|
| 2031 | n/a | else { |
|---|
| 2032 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 2033 | n/a | PyObject *unicode_str = |
|---|
| 2034 | n/a | PyUnicode_DecodeLatin1(PyBytes_AS_STRING(obj), |
|---|
| 2035 | n/a | PyBytes_GET_SIZE(obj), |
|---|
| 2036 | n/a | "strict"); |
|---|
| 2037 | n/a | _Py_IDENTIFIER(latin1); |
|---|
| 2038 | n/a | |
|---|
| 2039 | n/a | if (unicode_str == NULL) |
|---|
| 2040 | n/a | return -1; |
|---|
| 2041 | n/a | reduce_value = Py_BuildValue("(O(OO))", |
|---|
| 2042 | n/a | st->codecs_encode, unicode_str, |
|---|
| 2043 | n/a | _PyUnicode_FromId(&PyId_latin1)); |
|---|
| 2044 | n/a | Py_DECREF(unicode_str); |
|---|
| 2045 | n/a | } |
|---|
| 2046 | n/a | |
|---|
| 2047 | n/a | if (reduce_value == NULL) |
|---|
| 2048 | n/a | return -1; |
|---|
| 2049 | n/a | |
|---|
| 2050 | n/a | /* save_reduce() will memoize the object automatically. */ |
|---|
| 2051 | n/a | status = save_reduce(self, reduce_value, obj); |
|---|
| 2052 | n/a | Py_DECREF(reduce_value); |
|---|
| 2053 | n/a | return status; |
|---|
| 2054 | n/a | } |
|---|
| 2055 | n/a | else { |
|---|
| 2056 | n/a | Py_ssize_t size; |
|---|
| 2057 | n/a | char header[9]; |
|---|
| 2058 | n/a | Py_ssize_t len; |
|---|
| 2059 | n/a | |
|---|
| 2060 | n/a | size = PyBytes_GET_SIZE(obj); |
|---|
| 2061 | n/a | if (size < 0) |
|---|
| 2062 | n/a | return -1; |
|---|
| 2063 | n/a | |
|---|
| 2064 | n/a | if (size <= 0xff) { |
|---|
| 2065 | n/a | header[0] = SHORT_BINBYTES; |
|---|
| 2066 | n/a | header[1] = (unsigned char)size; |
|---|
| 2067 | n/a | len = 2; |
|---|
| 2068 | n/a | } |
|---|
| 2069 | n/a | else if ((size_t)size <= 0xffffffffUL) { |
|---|
| 2070 | n/a | header[0] = BINBYTES; |
|---|
| 2071 | n/a | header[1] = (unsigned char)(size & 0xff); |
|---|
| 2072 | n/a | header[2] = (unsigned char)((size >> 8) & 0xff); |
|---|
| 2073 | n/a | header[3] = (unsigned char)((size >> 16) & 0xff); |
|---|
| 2074 | n/a | header[4] = (unsigned char)((size >> 24) & 0xff); |
|---|
| 2075 | n/a | len = 5; |
|---|
| 2076 | n/a | } |
|---|
| 2077 | n/a | else if (self->proto >= 4) { |
|---|
| 2078 | n/a | header[0] = BINBYTES8; |
|---|
| 2079 | n/a | _write_size64(header + 1, size); |
|---|
| 2080 | n/a | len = 9; |
|---|
| 2081 | n/a | } |
|---|
| 2082 | n/a | else { |
|---|
| 2083 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 2084 | n/a | "cannot serialize a bytes object larger than 4 GiB"); |
|---|
| 2085 | n/a | return -1; /* string too large */ |
|---|
| 2086 | n/a | } |
|---|
| 2087 | n/a | |
|---|
| 2088 | n/a | if (_Pickler_Write(self, header, len) < 0) |
|---|
| 2089 | n/a | return -1; |
|---|
| 2090 | n/a | |
|---|
| 2091 | n/a | if (_Pickler_Write(self, PyBytes_AS_STRING(obj), size) < 0) |
|---|
| 2092 | n/a | return -1; |
|---|
| 2093 | n/a | |
|---|
| 2094 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 2095 | n/a | return -1; |
|---|
| 2096 | n/a | |
|---|
| 2097 | n/a | return 0; |
|---|
| 2098 | n/a | } |
|---|
| 2099 | n/a | } |
|---|
| 2100 | n/a | |
|---|
| 2101 | n/a | /* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates |
|---|
| 2102 | n/a | backslash and newline characters to \uXXXX escapes. */ |
|---|
| 2103 | n/a | static PyObject * |
|---|
| 2104 | n/a | raw_unicode_escape(PyObject *obj) |
|---|
| 2105 | n/a | { |
|---|
| 2106 | n/a | char *p; |
|---|
| 2107 | n/a | Py_ssize_t i, size; |
|---|
| 2108 | n/a | void *data; |
|---|
| 2109 | n/a | unsigned int kind; |
|---|
| 2110 | n/a | _PyBytesWriter writer; |
|---|
| 2111 | n/a | |
|---|
| 2112 | n/a | if (PyUnicode_READY(obj)) |
|---|
| 2113 | n/a | return NULL; |
|---|
| 2114 | n/a | |
|---|
| 2115 | n/a | _PyBytesWriter_Init(&writer); |
|---|
| 2116 | n/a | |
|---|
| 2117 | n/a | size = PyUnicode_GET_LENGTH(obj); |
|---|
| 2118 | n/a | data = PyUnicode_DATA(obj); |
|---|
| 2119 | n/a | kind = PyUnicode_KIND(obj); |
|---|
| 2120 | n/a | |
|---|
| 2121 | n/a | p = _PyBytesWriter_Alloc(&writer, size); |
|---|
| 2122 | n/a | if (p == NULL) |
|---|
| 2123 | n/a | goto error; |
|---|
| 2124 | n/a | writer.overallocate = 1; |
|---|
| 2125 | n/a | |
|---|
| 2126 | n/a | for (i=0; i < size; i++) { |
|---|
| 2127 | n/a | Py_UCS4 ch = PyUnicode_READ(kind, data, i); |
|---|
| 2128 | n/a | /* Map 32-bit characters to '\Uxxxxxxxx' */ |
|---|
| 2129 | n/a | if (ch >= 0x10000) { |
|---|
| 2130 | n/a | /* -1: subtract 1 preallocated byte */ |
|---|
| 2131 | n/a | p = _PyBytesWriter_Prepare(&writer, p, 10-1); |
|---|
| 2132 | n/a | if (p == NULL) |
|---|
| 2133 | n/a | goto error; |
|---|
| 2134 | n/a | |
|---|
| 2135 | n/a | *p++ = '\\'; |
|---|
| 2136 | n/a | *p++ = 'U'; |
|---|
| 2137 | n/a | *p++ = Py_hexdigits[(ch >> 28) & 0xf]; |
|---|
| 2138 | n/a | *p++ = Py_hexdigits[(ch >> 24) & 0xf]; |
|---|
| 2139 | n/a | *p++ = Py_hexdigits[(ch >> 20) & 0xf]; |
|---|
| 2140 | n/a | *p++ = Py_hexdigits[(ch >> 16) & 0xf]; |
|---|
| 2141 | n/a | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
|---|
| 2142 | n/a | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
|---|
| 2143 | n/a | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
|---|
| 2144 | n/a | *p++ = Py_hexdigits[ch & 15]; |
|---|
| 2145 | n/a | } |
|---|
| 2146 | n/a | /* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */ |
|---|
| 2147 | n/a | else if (ch >= 256 || ch == '\\' || ch == '\n') { |
|---|
| 2148 | n/a | /* -1: subtract 1 preallocated byte */ |
|---|
| 2149 | n/a | p = _PyBytesWriter_Prepare(&writer, p, 6-1); |
|---|
| 2150 | n/a | if (p == NULL) |
|---|
| 2151 | n/a | goto error; |
|---|
| 2152 | n/a | |
|---|
| 2153 | n/a | *p++ = '\\'; |
|---|
| 2154 | n/a | *p++ = 'u'; |
|---|
| 2155 | n/a | *p++ = Py_hexdigits[(ch >> 12) & 0xf]; |
|---|
| 2156 | n/a | *p++ = Py_hexdigits[(ch >> 8) & 0xf]; |
|---|
| 2157 | n/a | *p++ = Py_hexdigits[(ch >> 4) & 0xf]; |
|---|
| 2158 | n/a | *p++ = Py_hexdigits[ch & 15]; |
|---|
| 2159 | n/a | } |
|---|
| 2160 | n/a | /* Copy everything else as-is */ |
|---|
| 2161 | n/a | else |
|---|
| 2162 | n/a | *p++ = (char) ch; |
|---|
| 2163 | n/a | } |
|---|
| 2164 | n/a | |
|---|
| 2165 | n/a | return _PyBytesWriter_Finish(&writer, p); |
|---|
| 2166 | n/a | |
|---|
| 2167 | n/a | error: |
|---|
| 2168 | n/a | _PyBytesWriter_Dealloc(&writer); |
|---|
| 2169 | n/a | return NULL; |
|---|
| 2170 | n/a | } |
|---|
| 2171 | n/a | |
|---|
| 2172 | n/a | static int |
|---|
| 2173 | n/a | write_utf8(PicklerObject *self, const char *data, Py_ssize_t size) |
|---|
| 2174 | n/a | { |
|---|
| 2175 | n/a | char header[9]; |
|---|
| 2176 | n/a | Py_ssize_t len; |
|---|
| 2177 | n/a | |
|---|
| 2178 | n/a | assert(size >= 0); |
|---|
| 2179 | n/a | if (size <= 0xff && self->proto >= 4) { |
|---|
| 2180 | n/a | header[0] = SHORT_BINUNICODE; |
|---|
| 2181 | n/a | header[1] = (unsigned char)(size & 0xff); |
|---|
| 2182 | n/a | len = 2; |
|---|
| 2183 | n/a | } |
|---|
| 2184 | n/a | else if ((size_t)size <= 0xffffffffUL) { |
|---|
| 2185 | n/a | header[0] = BINUNICODE; |
|---|
| 2186 | n/a | header[1] = (unsigned char)(size & 0xff); |
|---|
| 2187 | n/a | header[2] = (unsigned char)((size >> 8) & 0xff); |
|---|
| 2188 | n/a | header[3] = (unsigned char)((size >> 16) & 0xff); |
|---|
| 2189 | n/a | header[4] = (unsigned char)((size >> 24) & 0xff); |
|---|
| 2190 | n/a | len = 5; |
|---|
| 2191 | n/a | } |
|---|
| 2192 | n/a | else if (self->proto >= 4) { |
|---|
| 2193 | n/a | header[0] = BINUNICODE8; |
|---|
| 2194 | n/a | _write_size64(header + 1, size); |
|---|
| 2195 | n/a | len = 9; |
|---|
| 2196 | n/a | } |
|---|
| 2197 | n/a | else { |
|---|
| 2198 | n/a | PyErr_SetString(PyExc_OverflowError, |
|---|
| 2199 | n/a | "cannot serialize a string larger than 4GiB"); |
|---|
| 2200 | n/a | return -1; |
|---|
| 2201 | n/a | } |
|---|
| 2202 | n/a | |
|---|
| 2203 | n/a | if (_Pickler_Write(self, header, len) < 0) |
|---|
| 2204 | n/a | return -1; |
|---|
| 2205 | n/a | if (_Pickler_Write(self, data, size) < 0) |
|---|
| 2206 | n/a | return -1; |
|---|
| 2207 | n/a | |
|---|
| 2208 | n/a | return 0; |
|---|
| 2209 | n/a | } |
|---|
| 2210 | n/a | |
|---|
| 2211 | n/a | static int |
|---|
| 2212 | n/a | write_unicode_binary(PicklerObject *self, PyObject *obj) |
|---|
| 2213 | n/a | { |
|---|
| 2214 | n/a | PyObject *encoded = NULL; |
|---|
| 2215 | n/a | Py_ssize_t size; |
|---|
| 2216 | n/a | const char *data; |
|---|
| 2217 | n/a | int r; |
|---|
| 2218 | n/a | |
|---|
| 2219 | n/a | if (PyUnicode_READY(obj)) |
|---|
| 2220 | n/a | return -1; |
|---|
| 2221 | n/a | |
|---|
| 2222 | n/a | data = PyUnicode_AsUTF8AndSize(obj, &size); |
|---|
| 2223 | n/a | if (data != NULL) |
|---|
| 2224 | n/a | return write_utf8(self, data, size); |
|---|
| 2225 | n/a | |
|---|
| 2226 | n/a | /* Issue #8383: for strings with lone surrogates, fallback on the |
|---|
| 2227 | n/a | "surrogatepass" error handler. */ |
|---|
| 2228 | n/a | PyErr_Clear(); |
|---|
| 2229 | n/a | encoded = PyUnicode_AsEncodedString(obj, "utf-8", "surrogatepass"); |
|---|
| 2230 | n/a | if (encoded == NULL) |
|---|
| 2231 | n/a | return -1; |
|---|
| 2232 | n/a | |
|---|
| 2233 | n/a | r = write_utf8(self, PyBytes_AS_STRING(encoded), |
|---|
| 2234 | n/a | PyBytes_GET_SIZE(encoded)); |
|---|
| 2235 | n/a | Py_DECREF(encoded); |
|---|
| 2236 | n/a | return r; |
|---|
| 2237 | n/a | } |
|---|
| 2238 | n/a | |
|---|
| 2239 | n/a | static int |
|---|
| 2240 | n/a | save_unicode(PicklerObject *self, PyObject *obj) |
|---|
| 2241 | n/a | { |
|---|
| 2242 | n/a | if (self->bin) { |
|---|
| 2243 | n/a | if (write_unicode_binary(self, obj) < 0) |
|---|
| 2244 | n/a | return -1; |
|---|
| 2245 | n/a | } |
|---|
| 2246 | n/a | else { |
|---|
| 2247 | n/a | PyObject *encoded; |
|---|
| 2248 | n/a | Py_ssize_t size; |
|---|
| 2249 | n/a | const char unicode_op = UNICODE; |
|---|
| 2250 | n/a | |
|---|
| 2251 | n/a | encoded = raw_unicode_escape(obj); |
|---|
| 2252 | n/a | if (encoded == NULL) |
|---|
| 2253 | n/a | return -1; |
|---|
| 2254 | n/a | |
|---|
| 2255 | n/a | if (_Pickler_Write(self, &unicode_op, 1) < 0) { |
|---|
| 2256 | n/a | Py_DECREF(encoded); |
|---|
| 2257 | n/a | return -1; |
|---|
| 2258 | n/a | } |
|---|
| 2259 | n/a | |
|---|
| 2260 | n/a | size = PyBytes_GET_SIZE(encoded); |
|---|
| 2261 | n/a | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), size) < 0) { |
|---|
| 2262 | n/a | Py_DECREF(encoded); |
|---|
| 2263 | n/a | return -1; |
|---|
| 2264 | n/a | } |
|---|
| 2265 | n/a | Py_DECREF(encoded); |
|---|
| 2266 | n/a | |
|---|
| 2267 | n/a | if (_Pickler_Write(self, "\n", 1) < 0) |
|---|
| 2268 | n/a | return -1; |
|---|
| 2269 | n/a | } |
|---|
| 2270 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 2271 | n/a | return -1; |
|---|
| 2272 | n/a | |
|---|
| 2273 | n/a | return 0; |
|---|
| 2274 | n/a | } |
|---|
| 2275 | n/a | |
|---|
| 2276 | n/a | /* A helper for save_tuple. Push the len elements in tuple t on the stack. */ |
|---|
| 2277 | n/a | static int |
|---|
| 2278 | n/a | store_tuple_elements(PicklerObject *self, PyObject *t, Py_ssize_t len) |
|---|
| 2279 | n/a | { |
|---|
| 2280 | n/a | Py_ssize_t i; |
|---|
| 2281 | n/a | |
|---|
| 2282 | n/a | assert(PyTuple_Size(t) == len); |
|---|
| 2283 | n/a | |
|---|
| 2284 | n/a | for (i = 0; i < len; i++) { |
|---|
| 2285 | n/a | PyObject *element = PyTuple_GET_ITEM(t, i); |
|---|
| 2286 | n/a | |
|---|
| 2287 | n/a | if (element == NULL) |
|---|
| 2288 | n/a | return -1; |
|---|
| 2289 | n/a | if (save(self, element, 0) < 0) |
|---|
| 2290 | n/a | return -1; |
|---|
| 2291 | n/a | } |
|---|
| 2292 | n/a | |
|---|
| 2293 | n/a | return 0; |
|---|
| 2294 | n/a | } |
|---|
| 2295 | n/a | |
|---|
| 2296 | n/a | /* Tuples are ubiquitous in the pickle protocols, so many techniques are |
|---|
| 2297 | n/a | * used across protocols to minimize the space needed to pickle them. |
|---|
| 2298 | n/a | * Tuples are also the only builtin immutable type that can be recursive |
|---|
| 2299 | n/a | * (a tuple can be reached from itself), and that requires some subtle |
|---|
| 2300 | n/a | * magic so that it works in all cases. IOW, this is a long routine. |
|---|
| 2301 | n/a | */ |
|---|
| 2302 | n/a | static int |
|---|
| 2303 | n/a | save_tuple(PicklerObject *self, PyObject *obj) |
|---|
| 2304 | n/a | { |
|---|
| 2305 | n/a | Py_ssize_t len, i; |
|---|
| 2306 | n/a | |
|---|
| 2307 | n/a | const char mark_op = MARK; |
|---|
| 2308 | n/a | const char tuple_op = TUPLE; |
|---|
| 2309 | n/a | const char pop_op = POP; |
|---|
| 2310 | n/a | const char pop_mark_op = POP_MARK; |
|---|
| 2311 | n/a | const char len2opcode[] = {EMPTY_TUPLE, TUPLE1, TUPLE2, TUPLE3}; |
|---|
| 2312 | n/a | |
|---|
| 2313 | n/a | if ((len = PyTuple_Size(obj)) < 0) |
|---|
| 2314 | n/a | return -1; |
|---|
| 2315 | n/a | |
|---|
| 2316 | n/a | if (len == 0) { |
|---|
| 2317 | n/a | char pdata[2]; |
|---|
| 2318 | n/a | |
|---|
| 2319 | n/a | if (self->proto) { |
|---|
| 2320 | n/a | pdata[0] = EMPTY_TUPLE; |
|---|
| 2321 | n/a | len = 1; |
|---|
| 2322 | n/a | } |
|---|
| 2323 | n/a | else { |
|---|
| 2324 | n/a | pdata[0] = MARK; |
|---|
| 2325 | n/a | pdata[1] = TUPLE; |
|---|
| 2326 | n/a | len = 2; |
|---|
| 2327 | n/a | } |
|---|
| 2328 | n/a | if (_Pickler_Write(self, pdata, len) < 0) |
|---|
| 2329 | n/a | return -1; |
|---|
| 2330 | n/a | return 0; |
|---|
| 2331 | n/a | } |
|---|
| 2332 | n/a | |
|---|
| 2333 | n/a | /* The tuple isn't in the memo now. If it shows up there after |
|---|
| 2334 | n/a | * saving the tuple elements, the tuple must be recursive, in |
|---|
| 2335 | n/a | * which case we'll pop everything we put on the stack, and fetch |
|---|
| 2336 | n/a | * its value from the memo. |
|---|
| 2337 | n/a | */ |
|---|
| 2338 | n/a | if (len <= 3 && self->proto >= 2) { |
|---|
| 2339 | n/a | /* Use TUPLE{1,2,3} opcodes. */ |
|---|
| 2340 | n/a | if (store_tuple_elements(self, obj, len) < 0) |
|---|
| 2341 | n/a | return -1; |
|---|
| 2342 | n/a | |
|---|
| 2343 | n/a | if (PyMemoTable_Get(self->memo, obj)) { |
|---|
| 2344 | n/a | /* pop the len elements */ |
|---|
| 2345 | n/a | for (i = 0; i < len; i++) |
|---|
| 2346 | n/a | if (_Pickler_Write(self, &pop_op, 1) < 0) |
|---|
| 2347 | n/a | return -1; |
|---|
| 2348 | n/a | /* fetch from memo */ |
|---|
| 2349 | n/a | if (memo_get(self, obj) < 0) |
|---|
| 2350 | n/a | return -1; |
|---|
| 2351 | n/a | |
|---|
| 2352 | n/a | return 0; |
|---|
| 2353 | n/a | } |
|---|
| 2354 | n/a | else { /* Not recursive. */ |
|---|
| 2355 | n/a | if (_Pickler_Write(self, len2opcode + len, 1) < 0) |
|---|
| 2356 | n/a | return -1; |
|---|
| 2357 | n/a | } |
|---|
| 2358 | n/a | goto memoize; |
|---|
| 2359 | n/a | } |
|---|
| 2360 | n/a | |
|---|
| 2361 | n/a | /* proto < 2 and len > 0, or proto >= 2 and len > 3. |
|---|
| 2362 | n/a | * Generate MARK e1 e2 ... TUPLE |
|---|
| 2363 | n/a | */ |
|---|
| 2364 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2365 | n/a | return -1; |
|---|
| 2366 | n/a | |
|---|
| 2367 | n/a | if (store_tuple_elements(self, obj, len) < 0) |
|---|
| 2368 | n/a | return -1; |
|---|
| 2369 | n/a | |
|---|
| 2370 | n/a | if (PyMemoTable_Get(self->memo, obj)) { |
|---|
| 2371 | n/a | /* pop the stack stuff we pushed */ |
|---|
| 2372 | n/a | if (self->bin) { |
|---|
| 2373 | n/a | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
|---|
| 2374 | n/a | return -1; |
|---|
| 2375 | n/a | } |
|---|
| 2376 | n/a | else { |
|---|
| 2377 | n/a | /* Note that we pop one more than len, to remove |
|---|
| 2378 | n/a | * the MARK too. |
|---|
| 2379 | n/a | */ |
|---|
| 2380 | n/a | for (i = 0; i <= len; i++) |
|---|
| 2381 | n/a | if (_Pickler_Write(self, &pop_op, 1) < 0) |
|---|
| 2382 | n/a | return -1; |
|---|
| 2383 | n/a | } |
|---|
| 2384 | n/a | /* fetch from memo */ |
|---|
| 2385 | n/a | if (memo_get(self, obj) < 0) |
|---|
| 2386 | n/a | return -1; |
|---|
| 2387 | n/a | |
|---|
| 2388 | n/a | return 0; |
|---|
| 2389 | n/a | } |
|---|
| 2390 | n/a | else { /* Not recursive. */ |
|---|
| 2391 | n/a | if (_Pickler_Write(self, &tuple_op, 1) < 0) |
|---|
| 2392 | n/a | return -1; |
|---|
| 2393 | n/a | } |
|---|
| 2394 | n/a | |
|---|
| 2395 | n/a | memoize: |
|---|
| 2396 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 2397 | n/a | return -1; |
|---|
| 2398 | n/a | |
|---|
| 2399 | n/a | return 0; |
|---|
| 2400 | n/a | } |
|---|
| 2401 | n/a | |
|---|
| 2402 | n/a | /* iter is an iterator giving items, and we batch up chunks of |
|---|
| 2403 | n/a | * MARK item item ... item APPENDS |
|---|
| 2404 | n/a | * opcode sequences. Calling code should have arranged to first create an |
|---|
| 2405 | n/a | * empty list, or list-like object, for the APPENDS to operate on. |
|---|
| 2406 | n/a | * Returns 0 on success, <0 on error. |
|---|
| 2407 | n/a | */ |
|---|
| 2408 | n/a | static int |
|---|
| 2409 | n/a | batch_list(PicklerObject *self, PyObject *iter) |
|---|
| 2410 | n/a | { |
|---|
| 2411 | n/a | PyObject *obj = NULL; |
|---|
| 2412 | n/a | PyObject *firstitem = NULL; |
|---|
| 2413 | n/a | int i, n; |
|---|
| 2414 | n/a | |
|---|
| 2415 | n/a | const char mark_op = MARK; |
|---|
| 2416 | n/a | const char append_op = APPEND; |
|---|
| 2417 | n/a | const char appends_op = APPENDS; |
|---|
| 2418 | n/a | |
|---|
| 2419 | n/a | assert(iter != NULL); |
|---|
| 2420 | n/a | |
|---|
| 2421 | n/a | /* XXX: I think this function could be made faster by avoiding the |
|---|
| 2422 | n/a | iterator interface and fetching objects directly from list using |
|---|
| 2423 | n/a | PyList_GET_ITEM. |
|---|
| 2424 | n/a | */ |
|---|
| 2425 | n/a | |
|---|
| 2426 | n/a | if (self->proto == 0) { |
|---|
| 2427 | n/a | /* APPENDS isn't available; do one at a time. */ |
|---|
| 2428 | n/a | for (;;) { |
|---|
| 2429 | n/a | obj = PyIter_Next(iter); |
|---|
| 2430 | n/a | if (obj == NULL) { |
|---|
| 2431 | n/a | if (PyErr_Occurred()) |
|---|
| 2432 | n/a | return -1; |
|---|
| 2433 | n/a | break; |
|---|
| 2434 | n/a | } |
|---|
| 2435 | n/a | i = save(self, obj, 0); |
|---|
| 2436 | n/a | Py_DECREF(obj); |
|---|
| 2437 | n/a | if (i < 0) |
|---|
| 2438 | n/a | return -1; |
|---|
| 2439 | n/a | if (_Pickler_Write(self, &append_op, 1) < 0) |
|---|
| 2440 | n/a | return -1; |
|---|
| 2441 | n/a | } |
|---|
| 2442 | n/a | return 0; |
|---|
| 2443 | n/a | } |
|---|
| 2444 | n/a | |
|---|
| 2445 | n/a | /* proto > 0: write in batches of BATCHSIZE. */ |
|---|
| 2446 | n/a | do { |
|---|
| 2447 | n/a | /* Get first item */ |
|---|
| 2448 | n/a | firstitem = PyIter_Next(iter); |
|---|
| 2449 | n/a | if (firstitem == NULL) { |
|---|
| 2450 | n/a | if (PyErr_Occurred()) |
|---|
| 2451 | n/a | goto error; |
|---|
| 2452 | n/a | |
|---|
| 2453 | n/a | /* nothing more to add */ |
|---|
| 2454 | n/a | break; |
|---|
| 2455 | n/a | } |
|---|
| 2456 | n/a | |
|---|
| 2457 | n/a | /* Try to get a second item */ |
|---|
| 2458 | n/a | obj = PyIter_Next(iter); |
|---|
| 2459 | n/a | if (obj == NULL) { |
|---|
| 2460 | n/a | if (PyErr_Occurred()) |
|---|
| 2461 | n/a | goto error; |
|---|
| 2462 | n/a | |
|---|
| 2463 | n/a | /* Only one item to write */ |
|---|
| 2464 | n/a | if (save(self, firstitem, 0) < 0) |
|---|
| 2465 | n/a | goto error; |
|---|
| 2466 | n/a | if (_Pickler_Write(self, &append_op, 1) < 0) |
|---|
| 2467 | n/a | goto error; |
|---|
| 2468 | n/a | Py_CLEAR(firstitem); |
|---|
| 2469 | n/a | break; |
|---|
| 2470 | n/a | } |
|---|
| 2471 | n/a | |
|---|
| 2472 | n/a | /* More than one item to write */ |
|---|
| 2473 | n/a | |
|---|
| 2474 | n/a | /* Pump out MARK, items, APPENDS. */ |
|---|
| 2475 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2476 | n/a | goto error; |
|---|
| 2477 | n/a | |
|---|
| 2478 | n/a | if (save(self, firstitem, 0) < 0) |
|---|
| 2479 | n/a | goto error; |
|---|
| 2480 | n/a | Py_CLEAR(firstitem); |
|---|
| 2481 | n/a | n = 1; |
|---|
| 2482 | n/a | |
|---|
| 2483 | n/a | /* Fetch and save up to BATCHSIZE items */ |
|---|
| 2484 | n/a | while (obj) { |
|---|
| 2485 | n/a | if (save(self, obj, 0) < 0) |
|---|
| 2486 | n/a | goto error; |
|---|
| 2487 | n/a | Py_CLEAR(obj); |
|---|
| 2488 | n/a | n += 1; |
|---|
| 2489 | n/a | |
|---|
| 2490 | n/a | if (n == BATCHSIZE) |
|---|
| 2491 | n/a | break; |
|---|
| 2492 | n/a | |
|---|
| 2493 | n/a | obj = PyIter_Next(iter); |
|---|
| 2494 | n/a | if (obj == NULL) { |
|---|
| 2495 | n/a | if (PyErr_Occurred()) |
|---|
| 2496 | n/a | goto error; |
|---|
| 2497 | n/a | break; |
|---|
| 2498 | n/a | } |
|---|
| 2499 | n/a | } |
|---|
| 2500 | n/a | |
|---|
| 2501 | n/a | if (_Pickler_Write(self, &appends_op, 1) < 0) |
|---|
| 2502 | n/a | goto error; |
|---|
| 2503 | n/a | |
|---|
| 2504 | n/a | } while (n == BATCHSIZE); |
|---|
| 2505 | n/a | return 0; |
|---|
| 2506 | n/a | |
|---|
| 2507 | n/a | error: |
|---|
| 2508 | n/a | Py_XDECREF(firstitem); |
|---|
| 2509 | n/a | Py_XDECREF(obj); |
|---|
| 2510 | n/a | return -1; |
|---|
| 2511 | n/a | } |
|---|
| 2512 | n/a | |
|---|
| 2513 | n/a | /* This is a variant of batch_list() above, specialized for lists (with no |
|---|
| 2514 | n/a | * support for list subclasses). Like batch_list(), we batch up chunks of |
|---|
| 2515 | n/a | * MARK item item ... item APPENDS |
|---|
| 2516 | n/a | * opcode sequences. Calling code should have arranged to first create an |
|---|
| 2517 | n/a | * empty list, or list-like object, for the APPENDS to operate on. |
|---|
| 2518 | n/a | * Returns 0 on success, -1 on error. |
|---|
| 2519 | n/a | * |
|---|
| 2520 | n/a | * This version is considerably faster than batch_list(), if less general. |
|---|
| 2521 | n/a | * |
|---|
| 2522 | n/a | * Note that this only works for protocols > 0. |
|---|
| 2523 | n/a | */ |
|---|
| 2524 | n/a | static int |
|---|
| 2525 | n/a | batch_list_exact(PicklerObject *self, PyObject *obj) |
|---|
| 2526 | n/a | { |
|---|
| 2527 | n/a | PyObject *item = NULL; |
|---|
| 2528 | n/a | Py_ssize_t this_batch, total; |
|---|
| 2529 | n/a | |
|---|
| 2530 | n/a | const char append_op = APPEND; |
|---|
| 2531 | n/a | const char appends_op = APPENDS; |
|---|
| 2532 | n/a | const char mark_op = MARK; |
|---|
| 2533 | n/a | |
|---|
| 2534 | n/a | assert(obj != NULL); |
|---|
| 2535 | n/a | assert(self->proto > 0); |
|---|
| 2536 | n/a | assert(PyList_CheckExact(obj)); |
|---|
| 2537 | n/a | |
|---|
| 2538 | n/a | if (PyList_GET_SIZE(obj) == 1) { |
|---|
| 2539 | n/a | item = PyList_GET_ITEM(obj, 0); |
|---|
| 2540 | n/a | if (save(self, item, 0) < 0) |
|---|
| 2541 | n/a | return -1; |
|---|
| 2542 | n/a | if (_Pickler_Write(self, &append_op, 1) < 0) |
|---|
| 2543 | n/a | return -1; |
|---|
| 2544 | n/a | return 0; |
|---|
| 2545 | n/a | } |
|---|
| 2546 | n/a | |
|---|
| 2547 | n/a | /* Write in batches of BATCHSIZE. */ |
|---|
| 2548 | n/a | total = 0; |
|---|
| 2549 | n/a | do { |
|---|
| 2550 | n/a | this_batch = 0; |
|---|
| 2551 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2552 | n/a | return -1; |
|---|
| 2553 | n/a | while (total < PyList_GET_SIZE(obj)) { |
|---|
| 2554 | n/a | item = PyList_GET_ITEM(obj, total); |
|---|
| 2555 | n/a | if (save(self, item, 0) < 0) |
|---|
| 2556 | n/a | return -1; |
|---|
| 2557 | n/a | total++; |
|---|
| 2558 | n/a | if (++this_batch == BATCHSIZE) |
|---|
| 2559 | n/a | break; |
|---|
| 2560 | n/a | } |
|---|
| 2561 | n/a | if (_Pickler_Write(self, &appends_op, 1) < 0) |
|---|
| 2562 | n/a | return -1; |
|---|
| 2563 | n/a | |
|---|
| 2564 | n/a | } while (total < PyList_GET_SIZE(obj)); |
|---|
| 2565 | n/a | |
|---|
| 2566 | n/a | return 0; |
|---|
| 2567 | n/a | } |
|---|
| 2568 | n/a | |
|---|
| 2569 | n/a | static int |
|---|
| 2570 | n/a | save_list(PicklerObject *self, PyObject *obj) |
|---|
| 2571 | n/a | { |
|---|
| 2572 | n/a | char header[3]; |
|---|
| 2573 | n/a | Py_ssize_t len; |
|---|
| 2574 | n/a | int status = 0; |
|---|
| 2575 | n/a | |
|---|
| 2576 | n/a | if (self->fast && !fast_save_enter(self, obj)) |
|---|
| 2577 | n/a | goto error; |
|---|
| 2578 | n/a | |
|---|
| 2579 | n/a | /* Create an empty list. */ |
|---|
| 2580 | n/a | if (self->bin) { |
|---|
| 2581 | n/a | header[0] = EMPTY_LIST; |
|---|
| 2582 | n/a | len = 1; |
|---|
| 2583 | n/a | } |
|---|
| 2584 | n/a | else { |
|---|
| 2585 | n/a | header[0] = MARK; |
|---|
| 2586 | n/a | header[1] = LIST; |
|---|
| 2587 | n/a | len = 2; |
|---|
| 2588 | n/a | } |
|---|
| 2589 | n/a | |
|---|
| 2590 | n/a | if (_Pickler_Write(self, header, len) < 0) |
|---|
| 2591 | n/a | goto error; |
|---|
| 2592 | n/a | |
|---|
| 2593 | n/a | /* Get list length, and bow out early if empty. */ |
|---|
| 2594 | n/a | if ((len = PyList_Size(obj)) < 0) |
|---|
| 2595 | n/a | goto error; |
|---|
| 2596 | n/a | |
|---|
| 2597 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 2598 | n/a | goto error; |
|---|
| 2599 | n/a | |
|---|
| 2600 | n/a | if (len != 0) { |
|---|
| 2601 | n/a | /* Materialize the list elements. */ |
|---|
| 2602 | n/a | if (PyList_CheckExact(obj) && self->proto > 0) { |
|---|
| 2603 | n/a | if (Py_EnterRecursiveCall(" while pickling an object")) |
|---|
| 2604 | n/a | goto error; |
|---|
| 2605 | n/a | status = batch_list_exact(self, obj); |
|---|
| 2606 | n/a | Py_LeaveRecursiveCall(); |
|---|
| 2607 | n/a | } else { |
|---|
| 2608 | n/a | PyObject *iter = PyObject_GetIter(obj); |
|---|
| 2609 | n/a | if (iter == NULL) |
|---|
| 2610 | n/a | goto error; |
|---|
| 2611 | n/a | |
|---|
| 2612 | n/a | if (Py_EnterRecursiveCall(" while pickling an object")) { |
|---|
| 2613 | n/a | Py_DECREF(iter); |
|---|
| 2614 | n/a | goto error; |
|---|
| 2615 | n/a | } |
|---|
| 2616 | n/a | status = batch_list(self, iter); |
|---|
| 2617 | n/a | Py_LeaveRecursiveCall(); |
|---|
| 2618 | n/a | Py_DECREF(iter); |
|---|
| 2619 | n/a | } |
|---|
| 2620 | n/a | } |
|---|
| 2621 | n/a | if (0) { |
|---|
| 2622 | n/a | error: |
|---|
| 2623 | n/a | status = -1; |
|---|
| 2624 | n/a | } |
|---|
| 2625 | n/a | |
|---|
| 2626 | n/a | if (self->fast && !fast_save_leave(self, obj)) |
|---|
| 2627 | n/a | status = -1; |
|---|
| 2628 | n/a | |
|---|
| 2629 | n/a | return status; |
|---|
| 2630 | n/a | } |
|---|
| 2631 | n/a | |
|---|
| 2632 | n/a | /* iter is an iterator giving (key, value) pairs, and we batch up chunks of |
|---|
| 2633 | n/a | * MARK key value ... key value SETITEMS |
|---|
| 2634 | n/a | * opcode sequences. Calling code should have arranged to first create an |
|---|
| 2635 | n/a | * empty dict, or dict-like object, for the SETITEMS to operate on. |
|---|
| 2636 | n/a | * Returns 0 on success, <0 on error. |
|---|
| 2637 | n/a | * |
|---|
| 2638 | n/a | * This is very much like batch_list(). The difference between saving |
|---|
| 2639 | n/a | * elements directly, and picking apart two-tuples, is so long-winded at |
|---|
| 2640 | n/a | * the C level, though, that attempts to combine these routines were too |
|---|
| 2641 | n/a | * ugly to bear. |
|---|
| 2642 | n/a | */ |
|---|
| 2643 | n/a | static int |
|---|
| 2644 | n/a | batch_dict(PicklerObject *self, PyObject *iter) |
|---|
| 2645 | n/a | { |
|---|
| 2646 | n/a | PyObject *obj = NULL; |
|---|
| 2647 | n/a | PyObject *firstitem = NULL; |
|---|
| 2648 | n/a | int i, n; |
|---|
| 2649 | n/a | |
|---|
| 2650 | n/a | const char mark_op = MARK; |
|---|
| 2651 | n/a | const char setitem_op = SETITEM; |
|---|
| 2652 | n/a | const char setitems_op = SETITEMS; |
|---|
| 2653 | n/a | |
|---|
| 2654 | n/a | assert(iter != NULL); |
|---|
| 2655 | n/a | |
|---|
| 2656 | n/a | if (self->proto == 0) { |
|---|
| 2657 | n/a | /* SETITEMS isn't available; do one at a time. */ |
|---|
| 2658 | n/a | for (;;) { |
|---|
| 2659 | n/a | obj = PyIter_Next(iter); |
|---|
| 2660 | n/a | if (obj == NULL) { |
|---|
| 2661 | n/a | if (PyErr_Occurred()) |
|---|
| 2662 | n/a | return -1; |
|---|
| 2663 | n/a | break; |
|---|
| 2664 | n/a | } |
|---|
| 2665 | n/a | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
|---|
| 2666 | n/a | PyErr_SetString(PyExc_TypeError, "dict items " |
|---|
| 2667 | n/a | "iterator must return 2-tuples"); |
|---|
| 2668 | n/a | return -1; |
|---|
| 2669 | n/a | } |
|---|
| 2670 | n/a | i = save(self, PyTuple_GET_ITEM(obj, 0), 0); |
|---|
| 2671 | n/a | if (i >= 0) |
|---|
| 2672 | n/a | i = save(self, PyTuple_GET_ITEM(obj, 1), 0); |
|---|
| 2673 | n/a | Py_DECREF(obj); |
|---|
| 2674 | n/a | if (i < 0) |
|---|
| 2675 | n/a | return -1; |
|---|
| 2676 | n/a | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
|---|
| 2677 | n/a | return -1; |
|---|
| 2678 | n/a | } |
|---|
| 2679 | n/a | return 0; |
|---|
| 2680 | n/a | } |
|---|
| 2681 | n/a | |
|---|
| 2682 | n/a | /* proto > 0: write in batches of BATCHSIZE. */ |
|---|
| 2683 | n/a | do { |
|---|
| 2684 | n/a | /* Get first item */ |
|---|
| 2685 | n/a | firstitem = PyIter_Next(iter); |
|---|
| 2686 | n/a | if (firstitem == NULL) { |
|---|
| 2687 | n/a | if (PyErr_Occurred()) |
|---|
| 2688 | n/a | goto error; |
|---|
| 2689 | n/a | |
|---|
| 2690 | n/a | /* nothing more to add */ |
|---|
| 2691 | n/a | break; |
|---|
| 2692 | n/a | } |
|---|
| 2693 | n/a | if (!PyTuple_Check(firstitem) || PyTuple_Size(firstitem) != 2) { |
|---|
| 2694 | n/a | PyErr_SetString(PyExc_TypeError, "dict items " |
|---|
| 2695 | n/a | "iterator must return 2-tuples"); |
|---|
| 2696 | n/a | goto error; |
|---|
| 2697 | n/a | } |
|---|
| 2698 | n/a | |
|---|
| 2699 | n/a | /* Try to get a second item */ |
|---|
| 2700 | n/a | obj = PyIter_Next(iter); |
|---|
| 2701 | n/a | if (obj == NULL) { |
|---|
| 2702 | n/a | if (PyErr_Occurred()) |
|---|
| 2703 | n/a | goto error; |
|---|
| 2704 | n/a | |
|---|
| 2705 | n/a | /* Only one item to write */ |
|---|
| 2706 | n/a | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
|---|
| 2707 | n/a | goto error; |
|---|
| 2708 | n/a | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
|---|
| 2709 | n/a | goto error; |
|---|
| 2710 | n/a | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
|---|
| 2711 | n/a | goto error; |
|---|
| 2712 | n/a | Py_CLEAR(firstitem); |
|---|
| 2713 | n/a | break; |
|---|
| 2714 | n/a | } |
|---|
| 2715 | n/a | |
|---|
| 2716 | n/a | /* More than one item to write */ |
|---|
| 2717 | n/a | |
|---|
| 2718 | n/a | /* Pump out MARK, items, SETITEMS. */ |
|---|
| 2719 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2720 | n/a | goto error; |
|---|
| 2721 | n/a | |
|---|
| 2722 | n/a | if (save(self, PyTuple_GET_ITEM(firstitem, 0), 0) < 0) |
|---|
| 2723 | n/a | goto error; |
|---|
| 2724 | n/a | if (save(self, PyTuple_GET_ITEM(firstitem, 1), 0) < 0) |
|---|
| 2725 | n/a | goto error; |
|---|
| 2726 | n/a | Py_CLEAR(firstitem); |
|---|
| 2727 | n/a | n = 1; |
|---|
| 2728 | n/a | |
|---|
| 2729 | n/a | /* Fetch and save up to BATCHSIZE items */ |
|---|
| 2730 | n/a | while (obj) { |
|---|
| 2731 | n/a | if (!PyTuple_Check(obj) || PyTuple_Size(obj) != 2) { |
|---|
| 2732 | n/a | PyErr_SetString(PyExc_TypeError, "dict items " |
|---|
| 2733 | n/a | "iterator must return 2-tuples"); |
|---|
| 2734 | n/a | goto error; |
|---|
| 2735 | n/a | } |
|---|
| 2736 | n/a | if (save(self, PyTuple_GET_ITEM(obj, 0), 0) < 0 || |
|---|
| 2737 | n/a | save(self, PyTuple_GET_ITEM(obj, 1), 0) < 0) |
|---|
| 2738 | n/a | goto error; |
|---|
| 2739 | n/a | Py_CLEAR(obj); |
|---|
| 2740 | n/a | n += 1; |
|---|
| 2741 | n/a | |
|---|
| 2742 | n/a | if (n == BATCHSIZE) |
|---|
| 2743 | n/a | break; |
|---|
| 2744 | n/a | |
|---|
| 2745 | n/a | obj = PyIter_Next(iter); |
|---|
| 2746 | n/a | if (obj == NULL) { |
|---|
| 2747 | n/a | if (PyErr_Occurred()) |
|---|
| 2748 | n/a | goto error; |
|---|
| 2749 | n/a | break; |
|---|
| 2750 | n/a | } |
|---|
| 2751 | n/a | } |
|---|
| 2752 | n/a | |
|---|
| 2753 | n/a | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
|---|
| 2754 | n/a | goto error; |
|---|
| 2755 | n/a | |
|---|
| 2756 | n/a | } while (n == BATCHSIZE); |
|---|
| 2757 | n/a | return 0; |
|---|
| 2758 | n/a | |
|---|
| 2759 | n/a | error: |
|---|
| 2760 | n/a | Py_XDECREF(firstitem); |
|---|
| 2761 | n/a | Py_XDECREF(obj); |
|---|
| 2762 | n/a | return -1; |
|---|
| 2763 | n/a | } |
|---|
| 2764 | n/a | |
|---|
| 2765 | n/a | /* This is a variant of batch_dict() above that specializes for dicts, with no |
|---|
| 2766 | n/a | * support for dict subclasses. Like batch_dict(), we batch up chunks of |
|---|
| 2767 | n/a | * MARK key value ... key value SETITEMS |
|---|
| 2768 | n/a | * opcode sequences. Calling code should have arranged to first create an |
|---|
| 2769 | n/a | * empty dict, or dict-like object, for the SETITEMS to operate on. |
|---|
| 2770 | n/a | * Returns 0 on success, -1 on error. |
|---|
| 2771 | n/a | * |
|---|
| 2772 | n/a | * Note that this currently doesn't work for protocol 0. |
|---|
| 2773 | n/a | */ |
|---|
| 2774 | n/a | static int |
|---|
| 2775 | n/a | batch_dict_exact(PicklerObject *self, PyObject *obj) |
|---|
| 2776 | n/a | { |
|---|
| 2777 | n/a | PyObject *key = NULL, *value = NULL; |
|---|
| 2778 | n/a | int i; |
|---|
| 2779 | n/a | Py_ssize_t dict_size, ppos = 0; |
|---|
| 2780 | n/a | |
|---|
| 2781 | n/a | const char mark_op = MARK; |
|---|
| 2782 | n/a | const char setitem_op = SETITEM; |
|---|
| 2783 | n/a | const char setitems_op = SETITEMS; |
|---|
| 2784 | n/a | |
|---|
| 2785 | n/a | assert(obj != NULL && PyDict_CheckExact(obj)); |
|---|
| 2786 | n/a | assert(self->proto > 0); |
|---|
| 2787 | n/a | |
|---|
| 2788 | n/a | dict_size = PyDict_GET_SIZE(obj); |
|---|
| 2789 | n/a | |
|---|
| 2790 | n/a | /* Special-case len(d) == 1 to save space. */ |
|---|
| 2791 | n/a | if (dict_size == 1) { |
|---|
| 2792 | n/a | PyDict_Next(obj, &ppos, &key, &value); |
|---|
| 2793 | n/a | if (save(self, key, 0) < 0) |
|---|
| 2794 | n/a | return -1; |
|---|
| 2795 | n/a | if (save(self, value, 0) < 0) |
|---|
| 2796 | n/a | return -1; |
|---|
| 2797 | n/a | if (_Pickler_Write(self, &setitem_op, 1) < 0) |
|---|
| 2798 | n/a | return -1; |
|---|
| 2799 | n/a | return 0; |
|---|
| 2800 | n/a | } |
|---|
| 2801 | n/a | |
|---|
| 2802 | n/a | /* Write in batches of BATCHSIZE. */ |
|---|
| 2803 | n/a | do { |
|---|
| 2804 | n/a | i = 0; |
|---|
| 2805 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2806 | n/a | return -1; |
|---|
| 2807 | n/a | while (PyDict_Next(obj, &ppos, &key, &value)) { |
|---|
| 2808 | n/a | if (save(self, key, 0) < 0) |
|---|
| 2809 | n/a | return -1; |
|---|
| 2810 | n/a | if (save(self, value, 0) < 0) |
|---|
| 2811 | n/a | return -1; |
|---|
| 2812 | n/a | if (++i == BATCHSIZE) |
|---|
| 2813 | n/a | break; |
|---|
| 2814 | n/a | } |
|---|
| 2815 | n/a | if (_Pickler_Write(self, &setitems_op, 1) < 0) |
|---|
| 2816 | n/a | return -1; |
|---|
| 2817 | n/a | if (PyDict_GET_SIZE(obj) != dict_size) { |
|---|
| 2818 | n/a | PyErr_Format( |
|---|
| 2819 | n/a | PyExc_RuntimeError, |
|---|
| 2820 | n/a | "dictionary changed size during iteration"); |
|---|
| 2821 | n/a | return -1; |
|---|
| 2822 | n/a | } |
|---|
| 2823 | n/a | |
|---|
| 2824 | n/a | } while (i == BATCHSIZE); |
|---|
| 2825 | n/a | return 0; |
|---|
| 2826 | n/a | } |
|---|
| 2827 | n/a | |
|---|
| 2828 | n/a | static int |
|---|
| 2829 | n/a | save_dict(PicklerObject *self, PyObject *obj) |
|---|
| 2830 | n/a | { |
|---|
| 2831 | n/a | PyObject *items, *iter; |
|---|
| 2832 | n/a | char header[3]; |
|---|
| 2833 | n/a | Py_ssize_t len; |
|---|
| 2834 | n/a | int status = 0; |
|---|
| 2835 | n/a | assert(PyDict_Check(obj)); |
|---|
| 2836 | n/a | |
|---|
| 2837 | n/a | if (self->fast && !fast_save_enter(self, obj)) |
|---|
| 2838 | n/a | goto error; |
|---|
| 2839 | n/a | |
|---|
| 2840 | n/a | /* Create an empty dict. */ |
|---|
| 2841 | n/a | if (self->bin) { |
|---|
| 2842 | n/a | header[0] = EMPTY_DICT; |
|---|
| 2843 | n/a | len = 1; |
|---|
| 2844 | n/a | } |
|---|
| 2845 | n/a | else { |
|---|
| 2846 | n/a | header[0] = MARK; |
|---|
| 2847 | n/a | header[1] = DICT; |
|---|
| 2848 | n/a | len = 2; |
|---|
| 2849 | n/a | } |
|---|
| 2850 | n/a | |
|---|
| 2851 | n/a | if (_Pickler_Write(self, header, len) < 0) |
|---|
| 2852 | n/a | goto error; |
|---|
| 2853 | n/a | |
|---|
| 2854 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 2855 | n/a | goto error; |
|---|
| 2856 | n/a | |
|---|
| 2857 | n/a | if (PyDict_GET_SIZE(obj)) { |
|---|
| 2858 | n/a | /* Save the dict items. */ |
|---|
| 2859 | n/a | if (PyDict_CheckExact(obj) && self->proto > 0) { |
|---|
| 2860 | n/a | /* We can take certain shortcuts if we know this is a dict and |
|---|
| 2861 | n/a | not a dict subclass. */ |
|---|
| 2862 | n/a | if (Py_EnterRecursiveCall(" while pickling an object")) |
|---|
| 2863 | n/a | goto error; |
|---|
| 2864 | n/a | status = batch_dict_exact(self, obj); |
|---|
| 2865 | n/a | Py_LeaveRecursiveCall(); |
|---|
| 2866 | n/a | } else { |
|---|
| 2867 | n/a | _Py_IDENTIFIER(items); |
|---|
| 2868 | n/a | |
|---|
| 2869 | n/a | items = _PyObject_CallMethodId(obj, &PyId_items, NULL); |
|---|
| 2870 | n/a | if (items == NULL) |
|---|
| 2871 | n/a | goto error; |
|---|
| 2872 | n/a | iter = PyObject_GetIter(items); |
|---|
| 2873 | n/a | Py_DECREF(items); |
|---|
| 2874 | n/a | if (iter == NULL) |
|---|
| 2875 | n/a | goto error; |
|---|
| 2876 | n/a | if (Py_EnterRecursiveCall(" while pickling an object")) { |
|---|
| 2877 | n/a | Py_DECREF(iter); |
|---|
| 2878 | n/a | goto error; |
|---|
| 2879 | n/a | } |
|---|
| 2880 | n/a | status = batch_dict(self, iter); |
|---|
| 2881 | n/a | Py_LeaveRecursiveCall(); |
|---|
| 2882 | n/a | Py_DECREF(iter); |
|---|
| 2883 | n/a | } |
|---|
| 2884 | n/a | } |
|---|
| 2885 | n/a | |
|---|
| 2886 | n/a | if (0) { |
|---|
| 2887 | n/a | error: |
|---|
| 2888 | n/a | status = -1; |
|---|
| 2889 | n/a | } |
|---|
| 2890 | n/a | |
|---|
| 2891 | n/a | if (self->fast && !fast_save_leave(self, obj)) |
|---|
| 2892 | n/a | status = -1; |
|---|
| 2893 | n/a | |
|---|
| 2894 | n/a | return status; |
|---|
| 2895 | n/a | } |
|---|
| 2896 | n/a | |
|---|
| 2897 | n/a | static int |
|---|
| 2898 | n/a | save_set(PicklerObject *self, PyObject *obj) |
|---|
| 2899 | n/a | { |
|---|
| 2900 | n/a | PyObject *item; |
|---|
| 2901 | n/a | int i; |
|---|
| 2902 | n/a | Py_ssize_t set_size, ppos = 0; |
|---|
| 2903 | n/a | Py_hash_t hash; |
|---|
| 2904 | n/a | |
|---|
| 2905 | n/a | const char empty_set_op = EMPTY_SET; |
|---|
| 2906 | n/a | const char mark_op = MARK; |
|---|
| 2907 | n/a | const char additems_op = ADDITEMS; |
|---|
| 2908 | n/a | |
|---|
| 2909 | n/a | if (self->proto < 4) { |
|---|
| 2910 | n/a | PyObject *items; |
|---|
| 2911 | n/a | PyObject *reduce_value; |
|---|
| 2912 | n/a | int status; |
|---|
| 2913 | n/a | |
|---|
| 2914 | n/a | items = PySequence_List(obj); |
|---|
| 2915 | n/a | if (items == NULL) { |
|---|
| 2916 | n/a | return -1; |
|---|
| 2917 | n/a | } |
|---|
| 2918 | n/a | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PySet_Type, items); |
|---|
| 2919 | n/a | Py_DECREF(items); |
|---|
| 2920 | n/a | if (reduce_value == NULL) { |
|---|
| 2921 | n/a | return -1; |
|---|
| 2922 | n/a | } |
|---|
| 2923 | n/a | /* save_reduce() will memoize the object automatically. */ |
|---|
| 2924 | n/a | status = save_reduce(self, reduce_value, obj); |
|---|
| 2925 | n/a | Py_DECREF(reduce_value); |
|---|
| 2926 | n/a | return status; |
|---|
| 2927 | n/a | } |
|---|
| 2928 | n/a | |
|---|
| 2929 | n/a | if (_Pickler_Write(self, &empty_set_op, 1) < 0) |
|---|
| 2930 | n/a | return -1; |
|---|
| 2931 | n/a | |
|---|
| 2932 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 2933 | n/a | return -1; |
|---|
| 2934 | n/a | |
|---|
| 2935 | n/a | set_size = PySet_GET_SIZE(obj); |
|---|
| 2936 | n/a | if (set_size == 0) |
|---|
| 2937 | n/a | return 0; /* nothing to do */ |
|---|
| 2938 | n/a | |
|---|
| 2939 | n/a | /* Write in batches of BATCHSIZE. */ |
|---|
| 2940 | n/a | do { |
|---|
| 2941 | n/a | i = 0; |
|---|
| 2942 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2943 | n/a | return -1; |
|---|
| 2944 | n/a | while (_PySet_NextEntry(obj, &ppos, &item, &hash)) { |
|---|
| 2945 | n/a | if (save(self, item, 0) < 0) |
|---|
| 2946 | n/a | return -1; |
|---|
| 2947 | n/a | if (++i == BATCHSIZE) |
|---|
| 2948 | n/a | break; |
|---|
| 2949 | n/a | } |
|---|
| 2950 | n/a | if (_Pickler_Write(self, &additems_op, 1) < 0) |
|---|
| 2951 | n/a | return -1; |
|---|
| 2952 | n/a | if (PySet_GET_SIZE(obj) != set_size) { |
|---|
| 2953 | n/a | PyErr_Format( |
|---|
| 2954 | n/a | PyExc_RuntimeError, |
|---|
| 2955 | n/a | "set changed size during iteration"); |
|---|
| 2956 | n/a | return -1; |
|---|
| 2957 | n/a | } |
|---|
| 2958 | n/a | } while (i == BATCHSIZE); |
|---|
| 2959 | n/a | |
|---|
| 2960 | n/a | return 0; |
|---|
| 2961 | n/a | } |
|---|
| 2962 | n/a | |
|---|
| 2963 | n/a | static int |
|---|
| 2964 | n/a | save_frozenset(PicklerObject *self, PyObject *obj) |
|---|
| 2965 | n/a | { |
|---|
| 2966 | n/a | PyObject *iter; |
|---|
| 2967 | n/a | |
|---|
| 2968 | n/a | const char mark_op = MARK; |
|---|
| 2969 | n/a | const char frozenset_op = FROZENSET; |
|---|
| 2970 | n/a | |
|---|
| 2971 | n/a | if (self->fast && !fast_save_enter(self, obj)) |
|---|
| 2972 | n/a | return -1; |
|---|
| 2973 | n/a | |
|---|
| 2974 | n/a | if (self->proto < 4) { |
|---|
| 2975 | n/a | PyObject *items; |
|---|
| 2976 | n/a | PyObject *reduce_value; |
|---|
| 2977 | n/a | int status; |
|---|
| 2978 | n/a | |
|---|
| 2979 | n/a | items = PySequence_List(obj); |
|---|
| 2980 | n/a | if (items == NULL) { |
|---|
| 2981 | n/a | return -1; |
|---|
| 2982 | n/a | } |
|---|
| 2983 | n/a | reduce_value = Py_BuildValue("(O(O))", (PyObject*)&PyFrozenSet_Type, |
|---|
| 2984 | n/a | items); |
|---|
| 2985 | n/a | Py_DECREF(items); |
|---|
| 2986 | n/a | if (reduce_value == NULL) { |
|---|
| 2987 | n/a | return -1; |
|---|
| 2988 | n/a | } |
|---|
| 2989 | n/a | /* save_reduce() will memoize the object automatically. */ |
|---|
| 2990 | n/a | status = save_reduce(self, reduce_value, obj); |
|---|
| 2991 | n/a | Py_DECREF(reduce_value); |
|---|
| 2992 | n/a | return status; |
|---|
| 2993 | n/a | } |
|---|
| 2994 | n/a | |
|---|
| 2995 | n/a | if (_Pickler_Write(self, &mark_op, 1) < 0) |
|---|
| 2996 | n/a | return -1; |
|---|
| 2997 | n/a | |
|---|
| 2998 | n/a | iter = PyObject_GetIter(obj); |
|---|
| 2999 | n/a | if (iter == NULL) { |
|---|
| 3000 | n/a | return -1; |
|---|
| 3001 | n/a | } |
|---|
| 3002 | n/a | for (;;) { |
|---|
| 3003 | n/a | PyObject *item; |
|---|
| 3004 | n/a | |
|---|
| 3005 | n/a | item = PyIter_Next(iter); |
|---|
| 3006 | n/a | if (item == NULL) { |
|---|
| 3007 | n/a | if (PyErr_Occurred()) { |
|---|
| 3008 | n/a | Py_DECREF(iter); |
|---|
| 3009 | n/a | return -1; |
|---|
| 3010 | n/a | } |
|---|
| 3011 | n/a | break; |
|---|
| 3012 | n/a | } |
|---|
| 3013 | n/a | if (save(self, item, 0) < 0) { |
|---|
| 3014 | n/a | Py_DECREF(item); |
|---|
| 3015 | n/a | Py_DECREF(iter); |
|---|
| 3016 | n/a | return -1; |
|---|
| 3017 | n/a | } |
|---|
| 3018 | n/a | Py_DECREF(item); |
|---|
| 3019 | n/a | } |
|---|
| 3020 | n/a | Py_DECREF(iter); |
|---|
| 3021 | n/a | |
|---|
| 3022 | n/a | /* If the object is already in the memo, this means it is |
|---|
| 3023 | n/a | recursive. In this case, throw away everything we put on the |
|---|
| 3024 | n/a | stack, and fetch the object back from the memo. */ |
|---|
| 3025 | n/a | if (PyMemoTable_Get(self->memo, obj)) { |
|---|
| 3026 | n/a | const char pop_mark_op = POP_MARK; |
|---|
| 3027 | n/a | |
|---|
| 3028 | n/a | if (_Pickler_Write(self, &pop_mark_op, 1) < 0) |
|---|
| 3029 | n/a | return -1; |
|---|
| 3030 | n/a | if (memo_get(self, obj) < 0) |
|---|
| 3031 | n/a | return -1; |
|---|
| 3032 | n/a | return 0; |
|---|
| 3033 | n/a | } |
|---|
| 3034 | n/a | |
|---|
| 3035 | n/a | if (_Pickler_Write(self, &frozenset_op, 1) < 0) |
|---|
| 3036 | n/a | return -1; |
|---|
| 3037 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 3038 | n/a | return -1; |
|---|
| 3039 | n/a | |
|---|
| 3040 | n/a | return 0; |
|---|
| 3041 | n/a | } |
|---|
| 3042 | n/a | |
|---|
| 3043 | n/a | static int |
|---|
| 3044 | n/a | fix_imports(PyObject **module_name, PyObject **global_name) |
|---|
| 3045 | n/a | { |
|---|
| 3046 | n/a | PyObject *key; |
|---|
| 3047 | n/a | PyObject *item; |
|---|
| 3048 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3049 | n/a | |
|---|
| 3050 | n/a | key = PyTuple_Pack(2, *module_name, *global_name); |
|---|
| 3051 | n/a | if (key == NULL) |
|---|
| 3052 | n/a | return -1; |
|---|
| 3053 | n/a | item = PyDict_GetItemWithError(st->name_mapping_3to2, key); |
|---|
| 3054 | n/a | Py_DECREF(key); |
|---|
| 3055 | n/a | if (item) { |
|---|
| 3056 | n/a | PyObject *fixed_module_name; |
|---|
| 3057 | n/a | PyObject *fixed_global_name; |
|---|
| 3058 | n/a | |
|---|
| 3059 | n/a | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
|---|
| 3060 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 3061 | n/a | "_compat_pickle.REVERSE_NAME_MAPPING values " |
|---|
| 3062 | n/a | "should be 2-tuples, not %.200s", |
|---|
| 3063 | n/a | Py_TYPE(item)->tp_name); |
|---|
| 3064 | n/a | return -1; |
|---|
| 3065 | n/a | } |
|---|
| 3066 | n/a | fixed_module_name = PyTuple_GET_ITEM(item, 0); |
|---|
| 3067 | n/a | fixed_global_name = PyTuple_GET_ITEM(item, 1); |
|---|
| 3068 | n/a | if (!PyUnicode_Check(fixed_module_name) || |
|---|
| 3069 | n/a | !PyUnicode_Check(fixed_global_name)) { |
|---|
| 3070 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 3071 | n/a | "_compat_pickle.REVERSE_NAME_MAPPING values " |
|---|
| 3072 | n/a | "should be pairs of str, not (%.200s, %.200s)", |
|---|
| 3073 | n/a | Py_TYPE(fixed_module_name)->tp_name, |
|---|
| 3074 | n/a | Py_TYPE(fixed_global_name)->tp_name); |
|---|
| 3075 | n/a | return -1; |
|---|
| 3076 | n/a | } |
|---|
| 3077 | n/a | |
|---|
| 3078 | n/a | Py_CLEAR(*module_name); |
|---|
| 3079 | n/a | Py_CLEAR(*global_name); |
|---|
| 3080 | n/a | Py_INCREF(fixed_module_name); |
|---|
| 3081 | n/a | Py_INCREF(fixed_global_name); |
|---|
| 3082 | n/a | *module_name = fixed_module_name; |
|---|
| 3083 | n/a | *global_name = fixed_global_name; |
|---|
| 3084 | n/a | return 0; |
|---|
| 3085 | n/a | } |
|---|
| 3086 | n/a | else if (PyErr_Occurred()) { |
|---|
| 3087 | n/a | return -1; |
|---|
| 3088 | n/a | } |
|---|
| 3089 | n/a | |
|---|
| 3090 | n/a | item = PyDict_GetItemWithError(st->import_mapping_3to2, *module_name); |
|---|
| 3091 | n/a | if (item) { |
|---|
| 3092 | n/a | if (!PyUnicode_Check(item)) { |
|---|
| 3093 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 3094 | n/a | "_compat_pickle.REVERSE_IMPORT_MAPPING values " |
|---|
| 3095 | n/a | "should be strings, not %.200s", |
|---|
| 3096 | n/a | Py_TYPE(item)->tp_name); |
|---|
| 3097 | n/a | return -1; |
|---|
| 3098 | n/a | } |
|---|
| 3099 | n/a | Py_INCREF(item); |
|---|
| 3100 | n/a | Py_XSETREF(*module_name, item); |
|---|
| 3101 | n/a | } |
|---|
| 3102 | n/a | else if (PyErr_Occurred()) { |
|---|
| 3103 | n/a | return -1; |
|---|
| 3104 | n/a | } |
|---|
| 3105 | n/a | |
|---|
| 3106 | n/a | return 0; |
|---|
| 3107 | n/a | } |
|---|
| 3108 | n/a | |
|---|
| 3109 | n/a | static int |
|---|
| 3110 | n/a | save_global(PicklerObject *self, PyObject *obj, PyObject *name) |
|---|
| 3111 | n/a | { |
|---|
| 3112 | n/a | PyObject *global_name = NULL; |
|---|
| 3113 | n/a | PyObject *module_name = NULL; |
|---|
| 3114 | n/a | PyObject *module = NULL; |
|---|
| 3115 | n/a | PyObject *parent = NULL; |
|---|
| 3116 | n/a | PyObject *dotted_path = NULL; |
|---|
| 3117 | n/a | PyObject *lastname = NULL; |
|---|
| 3118 | n/a | PyObject *cls; |
|---|
| 3119 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3120 | n/a | int status = 0; |
|---|
| 3121 | n/a | _Py_IDENTIFIER(__name__); |
|---|
| 3122 | n/a | _Py_IDENTIFIER(__qualname__); |
|---|
| 3123 | n/a | |
|---|
| 3124 | n/a | const char global_op = GLOBAL; |
|---|
| 3125 | n/a | |
|---|
| 3126 | n/a | if (name) { |
|---|
| 3127 | n/a | Py_INCREF(name); |
|---|
| 3128 | n/a | global_name = name; |
|---|
| 3129 | n/a | } |
|---|
| 3130 | n/a | else { |
|---|
| 3131 | n/a | global_name = _PyObject_GetAttrId(obj, &PyId___qualname__); |
|---|
| 3132 | n/a | if (global_name == NULL) { |
|---|
| 3133 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 3134 | n/a | goto error; |
|---|
| 3135 | n/a | PyErr_Clear(); |
|---|
| 3136 | n/a | } |
|---|
| 3137 | n/a | if (global_name == NULL) { |
|---|
| 3138 | n/a | global_name = _PyObject_GetAttrId(obj, &PyId___name__); |
|---|
| 3139 | n/a | if (global_name == NULL) |
|---|
| 3140 | n/a | goto error; |
|---|
| 3141 | n/a | } |
|---|
| 3142 | n/a | } |
|---|
| 3143 | n/a | |
|---|
| 3144 | n/a | dotted_path = get_dotted_path(module, global_name); |
|---|
| 3145 | n/a | if (dotted_path == NULL) |
|---|
| 3146 | n/a | goto error; |
|---|
| 3147 | n/a | module_name = whichmodule(obj, dotted_path); |
|---|
| 3148 | n/a | if (module_name == NULL) |
|---|
| 3149 | n/a | goto error; |
|---|
| 3150 | n/a | |
|---|
| 3151 | n/a | /* XXX: Change to use the import C API directly with level=0 to disallow |
|---|
| 3152 | n/a | relative imports. |
|---|
| 3153 | n/a | |
|---|
| 3154 | n/a | XXX: PyImport_ImportModuleLevel could be used. However, this bypasses |
|---|
| 3155 | n/a | builtins.__import__. Therefore, _pickle, unlike pickle.py, will ignore |
|---|
| 3156 | n/a | custom import functions (IMHO, this would be a nice security |
|---|
| 3157 | n/a | feature). The import C API would need to be extended to support the |
|---|
| 3158 | n/a | extra parameters of __import__ to fix that. */ |
|---|
| 3159 | n/a | module = PyImport_Import(module_name); |
|---|
| 3160 | n/a | if (module == NULL) { |
|---|
| 3161 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3162 | n/a | "Can't pickle %R: import of module %R failed", |
|---|
| 3163 | n/a | obj, module_name); |
|---|
| 3164 | n/a | goto error; |
|---|
| 3165 | n/a | } |
|---|
| 3166 | n/a | lastname = PyList_GET_ITEM(dotted_path, PyList_GET_SIZE(dotted_path)-1); |
|---|
| 3167 | n/a | Py_INCREF(lastname); |
|---|
| 3168 | n/a | cls = get_deep_attribute(module, dotted_path, &parent); |
|---|
| 3169 | n/a | Py_CLEAR(dotted_path); |
|---|
| 3170 | n/a | if (cls == NULL) { |
|---|
| 3171 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3172 | n/a | "Can't pickle %R: attribute lookup %S on %S failed", |
|---|
| 3173 | n/a | obj, global_name, module_name); |
|---|
| 3174 | n/a | goto error; |
|---|
| 3175 | n/a | } |
|---|
| 3176 | n/a | if (cls != obj) { |
|---|
| 3177 | n/a | Py_DECREF(cls); |
|---|
| 3178 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3179 | n/a | "Can't pickle %R: it's not the same object as %S.%S", |
|---|
| 3180 | n/a | obj, module_name, global_name); |
|---|
| 3181 | n/a | goto error; |
|---|
| 3182 | n/a | } |
|---|
| 3183 | n/a | Py_DECREF(cls); |
|---|
| 3184 | n/a | |
|---|
| 3185 | n/a | if (self->proto >= 2) { |
|---|
| 3186 | n/a | /* See whether this is in the extension registry, and if |
|---|
| 3187 | n/a | * so generate an EXT opcode. |
|---|
| 3188 | n/a | */ |
|---|
| 3189 | n/a | PyObject *extension_key; |
|---|
| 3190 | n/a | PyObject *code_obj; /* extension code as Python object */ |
|---|
| 3191 | n/a | long code; /* extension code as C value */ |
|---|
| 3192 | n/a | char pdata[5]; |
|---|
| 3193 | n/a | Py_ssize_t n; |
|---|
| 3194 | n/a | |
|---|
| 3195 | n/a | extension_key = PyTuple_Pack(2, module_name, global_name); |
|---|
| 3196 | n/a | if (extension_key == NULL) { |
|---|
| 3197 | n/a | goto error; |
|---|
| 3198 | n/a | } |
|---|
| 3199 | n/a | code_obj = PyDict_GetItemWithError(st->extension_registry, |
|---|
| 3200 | n/a | extension_key); |
|---|
| 3201 | n/a | Py_DECREF(extension_key); |
|---|
| 3202 | n/a | /* The object is not registered in the extension registry. |
|---|
| 3203 | n/a | This is the most likely code path. */ |
|---|
| 3204 | n/a | if (code_obj == NULL) { |
|---|
| 3205 | n/a | if (PyErr_Occurred()) { |
|---|
| 3206 | n/a | goto error; |
|---|
| 3207 | n/a | } |
|---|
| 3208 | n/a | goto gen_global; |
|---|
| 3209 | n/a | } |
|---|
| 3210 | n/a | |
|---|
| 3211 | n/a | /* XXX: pickle.py doesn't check neither the type, nor the range |
|---|
| 3212 | n/a | of the value returned by the extension_registry. It should for |
|---|
| 3213 | n/a | consistency. */ |
|---|
| 3214 | n/a | |
|---|
| 3215 | n/a | /* Verify code_obj has the right type and value. */ |
|---|
| 3216 | n/a | if (!PyLong_Check(code_obj)) { |
|---|
| 3217 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3218 | n/a | "Can't pickle %R: extension code %R isn't an integer", |
|---|
| 3219 | n/a | obj, code_obj); |
|---|
| 3220 | n/a | goto error; |
|---|
| 3221 | n/a | } |
|---|
| 3222 | n/a | code = PyLong_AS_LONG(code_obj); |
|---|
| 3223 | n/a | if (code <= 0 || code > 0x7fffffffL) { |
|---|
| 3224 | n/a | if (!PyErr_Occurred()) |
|---|
| 3225 | n/a | PyErr_Format(st->PicklingError, "Can't pickle %R: extension " |
|---|
| 3226 | n/a | "code %ld is out of range", obj, code); |
|---|
| 3227 | n/a | goto error; |
|---|
| 3228 | n/a | } |
|---|
| 3229 | n/a | |
|---|
| 3230 | n/a | /* Generate an EXT opcode. */ |
|---|
| 3231 | n/a | if (code <= 0xff) { |
|---|
| 3232 | n/a | pdata[0] = EXT1; |
|---|
| 3233 | n/a | pdata[1] = (unsigned char)code; |
|---|
| 3234 | n/a | n = 2; |
|---|
| 3235 | n/a | } |
|---|
| 3236 | n/a | else if (code <= 0xffff) { |
|---|
| 3237 | n/a | pdata[0] = EXT2; |
|---|
| 3238 | n/a | pdata[1] = (unsigned char)(code & 0xff); |
|---|
| 3239 | n/a | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
|---|
| 3240 | n/a | n = 3; |
|---|
| 3241 | n/a | } |
|---|
| 3242 | n/a | else { |
|---|
| 3243 | n/a | pdata[0] = EXT4; |
|---|
| 3244 | n/a | pdata[1] = (unsigned char)(code & 0xff); |
|---|
| 3245 | n/a | pdata[2] = (unsigned char)((code >> 8) & 0xff); |
|---|
| 3246 | n/a | pdata[3] = (unsigned char)((code >> 16) & 0xff); |
|---|
| 3247 | n/a | pdata[4] = (unsigned char)((code >> 24) & 0xff); |
|---|
| 3248 | n/a | n = 5; |
|---|
| 3249 | n/a | } |
|---|
| 3250 | n/a | |
|---|
| 3251 | n/a | if (_Pickler_Write(self, pdata, n) < 0) |
|---|
| 3252 | n/a | goto error; |
|---|
| 3253 | n/a | } |
|---|
| 3254 | n/a | else { |
|---|
| 3255 | n/a | gen_global: |
|---|
| 3256 | n/a | if (parent == module) { |
|---|
| 3257 | n/a | Py_INCREF(lastname); |
|---|
| 3258 | n/a | Py_DECREF(global_name); |
|---|
| 3259 | n/a | global_name = lastname; |
|---|
| 3260 | n/a | } |
|---|
| 3261 | n/a | if (self->proto >= 4) { |
|---|
| 3262 | n/a | const char stack_global_op = STACK_GLOBAL; |
|---|
| 3263 | n/a | |
|---|
| 3264 | n/a | if (save(self, module_name, 0) < 0) |
|---|
| 3265 | n/a | goto error; |
|---|
| 3266 | n/a | if (save(self, global_name, 0) < 0) |
|---|
| 3267 | n/a | goto error; |
|---|
| 3268 | n/a | |
|---|
| 3269 | n/a | if (_Pickler_Write(self, &stack_global_op, 1) < 0) |
|---|
| 3270 | n/a | goto error; |
|---|
| 3271 | n/a | } |
|---|
| 3272 | n/a | else if (parent != module) { |
|---|
| 3273 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3274 | n/a | PyObject *reduce_value = Py_BuildValue("(O(OO))", |
|---|
| 3275 | n/a | st->getattr, parent, lastname); |
|---|
| 3276 | n/a | status = save_reduce(self, reduce_value, NULL); |
|---|
| 3277 | n/a | Py_DECREF(reduce_value); |
|---|
| 3278 | n/a | if (status < 0) |
|---|
| 3279 | n/a | goto error; |
|---|
| 3280 | n/a | } |
|---|
| 3281 | n/a | else { |
|---|
| 3282 | n/a | /* Generate a normal global opcode if we are using a pickle |
|---|
| 3283 | n/a | protocol < 4, or if the object is not registered in the |
|---|
| 3284 | n/a | extension registry. */ |
|---|
| 3285 | n/a | PyObject *encoded; |
|---|
| 3286 | n/a | PyObject *(*unicode_encoder)(PyObject *); |
|---|
| 3287 | n/a | |
|---|
| 3288 | n/a | if (_Pickler_Write(self, &global_op, 1) < 0) |
|---|
| 3289 | n/a | goto error; |
|---|
| 3290 | n/a | |
|---|
| 3291 | n/a | /* For protocol < 3 and if the user didn't request against doing |
|---|
| 3292 | n/a | so, we convert module names to the old 2.x module names. */ |
|---|
| 3293 | n/a | if (self->proto < 3 && self->fix_imports) { |
|---|
| 3294 | n/a | if (fix_imports(&module_name, &global_name) < 0) { |
|---|
| 3295 | n/a | goto error; |
|---|
| 3296 | n/a | } |
|---|
| 3297 | n/a | } |
|---|
| 3298 | n/a | |
|---|
| 3299 | n/a | /* Since Python 3.0 now supports non-ASCII identifiers, we encode |
|---|
| 3300 | n/a | both the module name and the global name using UTF-8. We do so |
|---|
| 3301 | n/a | only when we are using the pickle protocol newer than version |
|---|
| 3302 | n/a | 3. This is to ensure compatibility with older Unpickler running |
|---|
| 3303 | n/a | on Python 2.x. */ |
|---|
| 3304 | n/a | if (self->proto == 3) { |
|---|
| 3305 | n/a | unicode_encoder = PyUnicode_AsUTF8String; |
|---|
| 3306 | n/a | } |
|---|
| 3307 | n/a | else { |
|---|
| 3308 | n/a | unicode_encoder = PyUnicode_AsASCIIString; |
|---|
| 3309 | n/a | } |
|---|
| 3310 | n/a | encoded = unicode_encoder(module_name); |
|---|
| 3311 | n/a | if (encoded == NULL) { |
|---|
| 3312 | n/a | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
|---|
| 3313 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3314 | n/a | "can't pickle module identifier '%S' using " |
|---|
| 3315 | n/a | "pickle protocol %i", |
|---|
| 3316 | n/a | module_name, self->proto); |
|---|
| 3317 | n/a | goto error; |
|---|
| 3318 | n/a | } |
|---|
| 3319 | n/a | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
|---|
| 3320 | n/a | PyBytes_GET_SIZE(encoded)) < 0) { |
|---|
| 3321 | n/a | Py_DECREF(encoded); |
|---|
| 3322 | n/a | goto error; |
|---|
| 3323 | n/a | } |
|---|
| 3324 | n/a | Py_DECREF(encoded); |
|---|
| 3325 | n/a | if(_Pickler_Write(self, "\n", 1) < 0) |
|---|
| 3326 | n/a | goto error; |
|---|
| 3327 | n/a | |
|---|
| 3328 | n/a | /* Save the name of the module. */ |
|---|
| 3329 | n/a | encoded = unicode_encoder(global_name); |
|---|
| 3330 | n/a | if (encoded == NULL) { |
|---|
| 3331 | n/a | if (PyErr_ExceptionMatches(PyExc_UnicodeEncodeError)) |
|---|
| 3332 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3333 | n/a | "can't pickle global identifier '%S' using " |
|---|
| 3334 | n/a | "pickle protocol %i", |
|---|
| 3335 | n/a | global_name, self->proto); |
|---|
| 3336 | n/a | goto error; |
|---|
| 3337 | n/a | } |
|---|
| 3338 | n/a | if (_Pickler_Write(self, PyBytes_AS_STRING(encoded), |
|---|
| 3339 | n/a | PyBytes_GET_SIZE(encoded)) < 0) { |
|---|
| 3340 | n/a | Py_DECREF(encoded); |
|---|
| 3341 | n/a | goto error; |
|---|
| 3342 | n/a | } |
|---|
| 3343 | n/a | Py_DECREF(encoded); |
|---|
| 3344 | n/a | if (_Pickler_Write(self, "\n", 1) < 0) |
|---|
| 3345 | n/a | goto error; |
|---|
| 3346 | n/a | } |
|---|
| 3347 | n/a | /* Memoize the object. */ |
|---|
| 3348 | n/a | if (memo_put(self, obj) < 0) |
|---|
| 3349 | n/a | goto error; |
|---|
| 3350 | n/a | } |
|---|
| 3351 | n/a | |
|---|
| 3352 | n/a | if (0) { |
|---|
| 3353 | n/a | error: |
|---|
| 3354 | n/a | status = -1; |
|---|
| 3355 | n/a | } |
|---|
| 3356 | n/a | Py_XDECREF(module_name); |
|---|
| 3357 | n/a | Py_XDECREF(global_name); |
|---|
| 3358 | n/a | Py_XDECREF(module); |
|---|
| 3359 | n/a | Py_XDECREF(parent); |
|---|
| 3360 | n/a | Py_XDECREF(dotted_path); |
|---|
| 3361 | n/a | Py_XDECREF(lastname); |
|---|
| 3362 | n/a | |
|---|
| 3363 | n/a | return status; |
|---|
| 3364 | n/a | } |
|---|
| 3365 | n/a | |
|---|
| 3366 | n/a | static int |
|---|
| 3367 | n/a | save_singleton_type(PicklerObject *self, PyObject *obj, PyObject *singleton) |
|---|
| 3368 | n/a | { |
|---|
| 3369 | n/a | PyObject *reduce_value; |
|---|
| 3370 | n/a | int status; |
|---|
| 3371 | n/a | |
|---|
| 3372 | n/a | reduce_value = Py_BuildValue("O(O)", &PyType_Type, singleton); |
|---|
| 3373 | n/a | if (reduce_value == NULL) { |
|---|
| 3374 | n/a | return -1; |
|---|
| 3375 | n/a | } |
|---|
| 3376 | n/a | status = save_reduce(self, reduce_value, obj); |
|---|
| 3377 | n/a | Py_DECREF(reduce_value); |
|---|
| 3378 | n/a | return status; |
|---|
| 3379 | n/a | } |
|---|
| 3380 | n/a | |
|---|
| 3381 | n/a | static int |
|---|
| 3382 | n/a | save_type(PicklerObject *self, PyObject *obj) |
|---|
| 3383 | n/a | { |
|---|
| 3384 | n/a | if (obj == (PyObject *)&_PyNone_Type) { |
|---|
| 3385 | n/a | return save_singleton_type(self, obj, Py_None); |
|---|
| 3386 | n/a | } |
|---|
| 3387 | n/a | else if (obj == (PyObject *)&PyEllipsis_Type) { |
|---|
| 3388 | n/a | return save_singleton_type(self, obj, Py_Ellipsis); |
|---|
| 3389 | n/a | } |
|---|
| 3390 | n/a | else if (obj == (PyObject *)&_PyNotImplemented_Type) { |
|---|
| 3391 | n/a | return save_singleton_type(self, obj, Py_NotImplemented); |
|---|
| 3392 | n/a | } |
|---|
| 3393 | n/a | return save_global(self, obj, NULL); |
|---|
| 3394 | n/a | } |
|---|
| 3395 | n/a | |
|---|
| 3396 | n/a | static int |
|---|
| 3397 | n/a | save_pers(PicklerObject *self, PyObject *obj, PyObject *func) |
|---|
| 3398 | n/a | { |
|---|
| 3399 | n/a | PyObject *pid = NULL; |
|---|
| 3400 | n/a | int status = 0; |
|---|
| 3401 | n/a | |
|---|
| 3402 | n/a | const char persid_op = PERSID; |
|---|
| 3403 | n/a | const char binpersid_op = BINPERSID; |
|---|
| 3404 | n/a | |
|---|
| 3405 | n/a | Py_INCREF(obj); |
|---|
| 3406 | n/a | pid = _Pickle_FastCall(func, obj); |
|---|
| 3407 | n/a | if (pid == NULL) |
|---|
| 3408 | n/a | return -1; |
|---|
| 3409 | n/a | |
|---|
| 3410 | n/a | if (pid != Py_None) { |
|---|
| 3411 | n/a | if (self->bin) { |
|---|
| 3412 | n/a | if (save(self, pid, 1) < 0 || |
|---|
| 3413 | n/a | _Pickler_Write(self, &binpersid_op, 1) < 0) |
|---|
| 3414 | n/a | goto error; |
|---|
| 3415 | n/a | } |
|---|
| 3416 | n/a | else { |
|---|
| 3417 | n/a | PyObject *pid_str; |
|---|
| 3418 | n/a | |
|---|
| 3419 | n/a | pid_str = PyObject_Str(pid); |
|---|
| 3420 | n/a | if (pid_str == NULL) |
|---|
| 3421 | n/a | goto error; |
|---|
| 3422 | n/a | |
|---|
| 3423 | n/a | /* XXX: Should it check whether the pid contains embedded |
|---|
| 3424 | n/a | newlines? */ |
|---|
| 3425 | n/a | if (!PyUnicode_IS_ASCII(pid_str)) { |
|---|
| 3426 | n/a | PyErr_SetString(_Pickle_GetGlobalState()->PicklingError, |
|---|
| 3427 | n/a | "persistent IDs in protocol 0 must be " |
|---|
| 3428 | n/a | "ASCII strings"); |
|---|
| 3429 | n/a | Py_DECREF(pid_str); |
|---|
| 3430 | n/a | goto error; |
|---|
| 3431 | n/a | } |
|---|
| 3432 | n/a | |
|---|
| 3433 | n/a | if (_Pickler_Write(self, &persid_op, 1) < 0 || |
|---|
| 3434 | n/a | _Pickler_Write(self, PyUnicode_DATA(pid_str), |
|---|
| 3435 | n/a | PyUnicode_GET_LENGTH(pid_str)) < 0 || |
|---|
| 3436 | n/a | _Pickler_Write(self, "\n", 1) < 0) { |
|---|
| 3437 | n/a | Py_DECREF(pid_str); |
|---|
| 3438 | n/a | goto error; |
|---|
| 3439 | n/a | } |
|---|
| 3440 | n/a | Py_DECREF(pid_str); |
|---|
| 3441 | n/a | } |
|---|
| 3442 | n/a | status = 1; |
|---|
| 3443 | n/a | } |
|---|
| 3444 | n/a | |
|---|
| 3445 | n/a | if (0) { |
|---|
| 3446 | n/a | error: |
|---|
| 3447 | n/a | status = -1; |
|---|
| 3448 | n/a | } |
|---|
| 3449 | n/a | Py_XDECREF(pid); |
|---|
| 3450 | n/a | |
|---|
| 3451 | n/a | return status; |
|---|
| 3452 | n/a | } |
|---|
| 3453 | n/a | |
|---|
| 3454 | n/a | static PyObject * |
|---|
| 3455 | n/a | get_class(PyObject *obj) |
|---|
| 3456 | n/a | { |
|---|
| 3457 | n/a | PyObject *cls; |
|---|
| 3458 | n/a | _Py_IDENTIFIER(__class__); |
|---|
| 3459 | n/a | |
|---|
| 3460 | n/a | cls = _PyObject_GetAttrId(obj, &PyId___class__); |
|---|
| 3461 | n/a | if (cls == NULL) { |
|---|
| 3462 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 3463 | n/a | PyErr_Clear(); |
|---|
| 3464 | n/a | cls = (PyObject *) Py_TYPE(obj); |
|---|
| 3465 | n/a | Py_INCREF(cls); |
|---|
| 3466 | n/a | } |
|---|
| 3467 | n/a | } |
|---|
| 3468 | n/a | return cls; |
|---|
| 3469 | n/a | } |
|---|
| 3470 | n/a | |
|---|
| 3471 | n/a | /* We're saving obj, and args is the 2-thru-5 tuple returned by the |
|---|
| 3472 | n/a | * appropriate __reduce__ method for obj. |
|---|
| 3473 | n/a | */ |
|---|
| 3474 | n/a | static int |
|---|
| 3475 | n/a | save_reduce(PicklerObject *self, PyObject *args, PyObject *obj) |
|---|
| 3476 | n/a | { |
|---|
| 3477 | n/a | PyObject *callable; |
|---|
| 3478 | n/a | PyObject *argtup; |
|---|
| 3479 | n/a | PyObject *state = NULL; |
|---|
| 3480 | n/a | PyObject *listitems = Py_None; |
|---|
| 3481 | n/a | PyObject *dictitems = Py_None; |
|---|
| 3482 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3483 | n/a | Py_ssize_t size; |
|---|
| 3484 | n/a | int use_newobj = 0, use_newobj_ex = 0; |
|---|
| 3485 | n/a | |
|---|
| 3486 | n/a | const char reduce_op = REDUCE; |
|---|
| 3487 | n/a | const char build_op = BUILD; |
|---|
| 3488 | n/a | const char newobj_op = NEWOBJ; |
|---|
| 3489 | n/a | const char newobj_ex_op = NEWOBJ_EX; |
|---|
| 3490 | n/a | |
|---|
| 3491 | n/a | size = PyTuple_Size(args); |
|---|
| 3492 | n/a | if (size < 2 || size > 5) { |
|---|
| 3493 | n/a | PyErr_SetString(st->PicklingError, "tuple returned by " |
|---|
| 3494 | n/a | "__reduce__ must contain 2 through 5 elements"); |
|---|
| 3495 | n/a | return -1; |
|---|
| 3496 | n/a | } |
|---|
| 3497 | n/a | |
|---|
| 3498 | n/a | if (!PyArg_UnpackTuple(args, "save_reduce", 2, 5, |
|---|
| 3499 | n/a | &callable, &argtup, &state, &listitems, &dictitems)) |
|---|
| 3500 | n/a | return -1; |
|---|
| 3501 | n/a | |
|---|
| 3502 | n/a | if (!PyCallable_Check(callable)) { |
|---|
| 3503 | n/a | PyErr_SetString(st->PicklingError, "first item of the tuple " |
|---|
| 3504 | n/a | "returned by __reduce__ must be callable"); |
|---|
| 3505 | n/a | return -1; |
|---|
| 3506 | n/a | } |
|---|
| 3507 | n/a | if (!PyTuple_Check(argtup)) { |
|---|
| 3508 | n/a | PyErr_SetString(st->PicklingError, "second item of the tuple " |
|---|
| 3509 | n/a | "returned by __reduce__ must be a tuple"); |
|---|
| 3510 | n/a | return -1; |
|---|
| 3511 | n/a | } |
|---|
| 3512 | n/a | |
|---|
| 3513 | n/a | if (state == Py_None) |
|---|
| 3514 | n/a | state = NULL; |
|---|
| 3515 | n/a | |
|---|
| 3516 | n/a | if (listitems == Py_None) |
|---|
| 3517 | n/a | listitems = NULL; |
|---|
| 3518 | n/a | else if (!PyIter_Check(listitems)) { |
|---|
| 3519 | n/a | PyErr_Format(st->PicklingError, "fourth element of the tuple " |
|---|
| 3520 | n/a | "returned by __reduce__ must be an iterator, not %s", |
|---|
| 3521 | n/a | Py_TYPE(listitems)->tp_name); |
|---|
| 3522 | n/a | return -1; |
|---|
| 3523 | n/a | } |
|---|
| 3524 | n/a | |
|---|
| 3525 | n/a | if (dictitems == Py_None) |
|---|
| 3526 | n/a | dictitems = NULL; |
|---|
| 3527 | n/a | else if (!PyIter_Check(dictitems)) { |
|---|
| 3528 | n/a | PyErr_Format(st->PicklingError, "fifth element of the tuple " |
|---|
| 3529 | n/a | "returned by __reduce__ must be an iterator, not %s", |
|---|
| 3530 | n/a | Py_TYPE(dictitems)->tp_name); |
|---|
| 3531 | n/a | return -1; |
|---|
| 3532 | n/a | } |
|---|
| 3533 | n/a | |
|---|
| 3534 | n/a | if (self->proto >= 2) { |
|---|
| 3535 | n/a | PyObject *name; |
|---|
| 3536 | n/a | _Py_IDENTIFIER(__name__); |
|---|
| 3537 | n/a | |
|---|
| 3538 | n/a | name = _PyObject_GetAttrId(callable, &PyId___name__); |
|---|
| 3539 | n/a | if (name == NULL) { |
|---|
| 3540 | n/a | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 3541 | n/a | return -1; |
|---|
| 3542 | n/a | } |
|---|
| 3543 | n/a | PyErr_Clear(); |
|---|
| 3544 | n/a | } |
|---|
| 3545 | n/a | else if (PyUnicode_Check(name)) { |
|---|
| 3546 | n/a | _Py_IDENTIFIER(__newobj_ex__); |
|---|
| 3547 | n/a | use_newobj_ex = _PyUnicode_EqualToASCIIId( |
|---|
| 3548 | n/a | name, &PyId___newobj_ex__); |
|---|
| 3549 | n/a | if (!use_newobj_ex) { |
|---|
| 3550 | n/a | _Py_IDENTIFIER(__newobj__); |
|---|
| 3551 | n/a | use_newobj = _PyUnicode_EqualToASCIIId(name, &PyId___newobj__); |
|---|
| 3552 | n/a | } |
|---|
| 3553 | n/a | } |
|---|
| 3554 | n/a | Py_XDECREF(name); |
|---|
| 3555 | n/a | } |
|---|
| 3556 | n/a | |
|---|
| 3557 | n/a | if (use_newobj_ex) { |
|---|
| 3558 | n/a | PyObject *cls; |
|---|
| 3559 | n/a | PyObject *args; |
|---|
| 3560 | n/a | PyObject *kwargs; |
|---|
| 3561 | n/a | |
|---|
| 3562 | n/a | if (Py_SIZE(argtup) != 3) { |
|---|
| 3563 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3564 | n/a | "length of the NEWOBJ_EX argument tuple must be " |
|---|
| 3565 | n/a | "exactly 3, not %zd", Py_SIZE(argtup)); |
|---|
| 3566 | n/a | return -1; |
|---|
| 3567 | n/a | } |
|---|
| 3568 | n/a | |
|---|
| 3569 | n/a | cls = PyTuple_GET_ITEM(argtup, 0); |
|---|
| 3570 | n/a | if (!PyType_Check(cls)) { |
|---|
| 3571 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3572 | n/a | "first item from NEWOBJ_EX argument tuple must " |
|---|
| 3573 | n/a | "be a class, not %.200s", Py_TYPE(cls)->tp_name); |
|---|
| 3574 | n/a | return -1; |
|---|
| 3575 | n/a | } |
|---|
| 3576 | n/a | args = PyTuple_GET_ITEM(argtup, 1); |
|---|
| 3577 | n/a | if (!PyTuple_Check(args)) { |
|---|
| 3578 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3579 | n/a | "second item from NEWOBJ_EX argument tuple must " |
|---|
| 3580 | n/a | "be a tuple, not %.200s", Py_TYPE(args)->tp_name); |
|---|
| 3581 | n/a | return -1; |
|---|
| 3582 | n/a | } |
|---|
| 3583 | n/a | kwargs = PyTuple_GET_ITEM(argtup, 2); |
|---|
| 3584 | n/a | if (!PyDict_Check(kwargs)) { |
|---|
| 3585 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3586 | n/a | "third item from NEWOBJ_EX argument tuple must " |
|---|
| 3587 | n/a | "be a dict, not %.200s", Py_TYPE(kwargs)->tp_name); |
|---|
| 3588 | n/a | return -1; |
|---|
| 3589 | n/a | } |
|---|
| 3590 | n/a | |
|---|
| 3591 | n/a | if (self->proto >= 4) { |
|---|
| 3592 | n/a | if (save(self, cls, 0) < 0 || |
|---|
| 3593 | n/a | save(self, args, 0) < 0 || |
|---|
| 3594 | n/a | save(self, kwargs, 0) < 0 || |
|---|
| 3595 | n/a | _Pickler_Write(self, &newobj_ex_op, 1) < 0) { |
|---|
| 3596 | n/a | return -1; |
|---|
| 3597 | n/a | } |
|---|
| 3598 | n/a | } |
|---|
| 3599 | n/a | else { |
|---|
| 3600 | n/a | PyObject *newargs; |
|---|
| 3601 | n/a | PyObject *cls_new; |
|---|
| 3602 | n/a | Py_ssize_t i; |
|---|
| 3603 | n/a | _Py_IDENTIFIER(__new__); |
|---|
| 3604 | n/a | |
|---|
| 3605 | n/a | newargs = PyTuple_New(Py_SIZE(args) + 2); |
|---|
| 3606 | n/a | if (newargs == NULL) |
|---|
| 3607 | n/a | return -1; |
|---|
| 3608 | n/a | |
|---|
| 3609 | n/a | cls_new = _PyObject_GetAttrId(cls, &PyId___new__); |
|---|
| 3610 | n/a | if (cls_new == NULL) { |
|---|
| 3611 | n/a | Py_DECREF(newargs); |
|---|
| 3612 | n/a | return -1; |
|---|
| 3613 | n/a | } |
|---|
| 3614 | n/a | PyTuple_SET_ITEM(newargs, 0, cls_new); |
|---|
| 3615 | n/a | Py_INCREF(cls); |
|---|
| 3616 | n/a | PyTuple_SET_ITEM(newargs, 1, cls); |
|---|
| 3617 | n/a | for (i = 0; i < Py_SIZE(args); i++) { |
|---|
| 3618 | n/a | PyObject *item = PyTuple_GET_ITEM(args, i); |
|---|
| 3619 | n/a | Py_INCREF(item); |
|---|
| 3620 | n/a | PyTuple_SET_ITEM(newargs, i + 2, item); |
|---|
| 3621 | n/a | } |
|---|
| 3622 | n/a | |
|---|
| 3623 | n/a | callable = PyObject_Call(st->partial, newargs, kwargs); |
|---|
| 3624 | n/a | Py_DECREF(newargs); |
|---|
| 3625 | n/a | if (callable == NULL) |
|---|
| 3626 | n/a | return -1; |
|---|
| 3627 | n/a | |
|---|
| 3628 | n/a | newargs = PyTuple_New(0); |
|---|
| 3629 | n/a | if (newargs == NULL) { |
|---|
| 3630 | n/a | Py_DECREF(callable); |
|---|
| 3631 | n/a | return -1; |
|---|
| 3632 | n/a | } |
|---|
| 3633 | n/a | |
|---|
| 3634 | n/a | if (save(self, callable, 0) < 0 || |
|---|
| 3635 | n/a | save(self, newargs, 0) < 0 || |
|---|
| 3636 | n/a | _Pickler_Write(self, &reduce_op, 1) < 0) { |
|---|
| 3637 | n/a | Py_DECREF(newargs); |
|---|
| 3638 | n/a | Py_DECREF(callable); |
|---|
| 3639 | n/a | return -1; |
|---|
| 3640 | n/a | } |
|---|
| 3641 | n/a | Py_DECREF(newargs); |
|---|
| 3642 | n/a | Py_DECREF(callable); |
|---|
| 3643 | n/a | } |
|---|
| 3644 | n/a | } |
|---|
| 3645 | n/a | else if (use_newobj) { |
|---|
| 3646 | n/a | PyObject *cls; |
|---|
| 3647 | n/a | PyObject *newargtup; |
|---|
| 3648 | n/a | PyObject *obj_class; |
|---|
| 3649 | n/a | int p; |
|---|
| 3650 | n/a | |
|---|
| 3651 | n/a | /* Sanity checks. */ |
|---|
| 3652 | n/a | if (Py_SIZE(argtup) < 1) { |
|---|
| 3653 | n/a | PyErr_SetString(st->PicklingError, "__newobj__ arglist is empty"); |
|---|
| 3654 | n/a | return -1; |
|---|
| 3655 | n/a | } |
|---|
| 3656 | n/a | |
|---|
| 3657 | n/a | cls = PyTuple_GET_ITEM(argtup, 0); |
|---|
| 3658 | n/a | if (!PyType_Check(cls)) { |
|---|
| 3659 | n/a | PyErr_SetString(st->PicklingError, "args[0] from " |
|---|
| 3660 | n/a | "__newobj__ args is not a type"); |
|---|
| 3661 | n/a | return -1; |
|---|
| 3662 | n/a | } |
|---|
| 3663 | n/a | |
|---|
| 3664 | n/a | if (obj != NULL) { |
|---|
| 3665 | n/a | obj_class = get_class(obj); |
|---|
| 3666 | n/a | p = obj_class != cls; /* true iff a problem */ |
|---|
| 3667 | n/a | Py_DECREF(obj_class); |
|---|
| 3668 | n/a | if (p) { |
|---|
| 3669 | n/a | PyErr_SetString(st->PicklingError, "args[0] from " |
|---|
| 3670 | n/a | "__newobj__ args has the wrong class"); |
|---|
| 3671 | n/a | return -1; |
|---|
| 3672 | n/a | } |
|---|
| 3673 | n/a | } |
|---|
| 3674 | n/a | /* XXX: These calls save() are prone to infinite recursion. Imagine |
|---|
| 3675 | n/a | what happen if the value returned by the __reduce__() method of |
|---|
| 3676 | n/a | some extension type contains another object of the same type. Ouch! |
|---|
| 3677 | n/a | |
|---|
| 3678 | n/a | Here is a quick example, that I ran into, to illustrate what I |
|---|
| 3679 | n/a | mean: |
|---|
| 3680 | n/a | |
|---|
| 3681 | n/a | >>> import pickle, copyreg |
|---|
| 3682 | n/a | >>> copyreg.dispatch_table.pop(complex) |
|---|
| 3683 | n/a | >>> pickle.dumps(1+2j) |
|---|
| 3684 | n/a | Traceback (most recent call last): |
|---|
| 3685 | n/a | ... |
|---|
| 3686 | n/a | RecursionError: maximum recursion depth exceeded |
|---|
| 3687 | n/a | |
|---|
| 3688 | n/a | Removing the complex class from copyreg.dispatch_table made the |
|---|
| 3689 | n/a | __reduce_ex__() method emit another complex object: |
|---|
| 3690 | n/a | |
|---|
| 3691 | n/a | >>> (1+1j).__reduce_ex__(2) |
|---|
| 3692 | n/a | (<function __newobj__ at 0xb7b71c3c>, |
|---|
| 3693 | n/a | (<class 'complex'>, (1+1j)), None, None, None) |
|---|
| 3694 | n/a | |
|---|
| 3695 | n/a | Thus when save() was called on newargstup (the 2nd item) recursion |
|---|
| 3696 | n/a | ensued. Of course, the bug was in the complex class which had a |
|---|
| 3697 | n/a | broken __getnewargs__() that emitted another complex object. But, |
|---|
| 3698 | n/a | the point, here, is it is quite easy to end up with a broken reduce |
|---|
| 3699 | n/a | function. */ |
|---|
| 3700 | n/a | |
|---|
| 3701 | n/a | /* Save the class and its __new__ arguments. */ |
|---|
| 3702 | n/a | if (save(self, cls, 0) < 0) |
|---|
| 3703 | n/a | return -1; |
|---|
| 3704 | n/a | |
|---|
| 3705 | n/a | newargtup = PyTuple_GetSlice(argtup, 1, Py_SIZE(argtup)); |
|---|
| 3706 | n/a | if (newargtup == NULL) |
|---|
| 3707 | n/a | return -1; |
|---|
| 3708 | n/a | |
|---|
| 3709 | n/a | p = save(self, newargtup, 0); |
|---|
| 3710 | n/a | Py_DECREF(newargtup); |
|---|
| 3711 | n/a | if (p < 0) |
|---|
| 3712 | n/a | return -1; |
|---|
| 3713 | n/a | |
|---|
| 3714 | n/a | /* Add NEWOBJ opcode. */ |
|---|
| 3715 | n/a | if (_Pickler_Write(self, &newobj_op, 1) < 0) |
|---|
| 3716 | n/a | return -1; |
|---|
| 3717 | n/a | } |
|---|
| 3718 | n/a | else { /* Not using NEWOBJ. */ |
|---|
| 3719 | n/a | if (save(self, callable, 0) < 0 || |
|---|
| 3720 | n/a | save(self, argtup, 0) < 0 || |
|---|
| 3721 | n/a | _Pickler_Write(self, &reduce_op, 1) < 0) |
|---|
| 3722 | n/a | return -1; |
|---|
| 3723 | n/a | } |
|---|
| 3724 | n/a | |
|---|
| 3725 | n/a | /* obj can be NULL when save_reduce() is used directly. A NULL obj means |
|---|
| 3726 | n/a | the caller do not want to memoize the object. Not particularly useful, |
|---|
| 3727 | n/a | but that is to mimic the behavior save_reduce() in pickle.py when |
|---|
| 3728 | n/a | obj is None. */ |
|---|
| 3729 | n/a | if (obj != NULL) { |
|---|
| 3730 | n/a | /* If the object is already in the memo, this means it is |
|---|
| 3731 | n/a | recursive. In this case, throw away everything we put on the |
|---|
| 3732 | n/a | stack, and fetch the object back from the memo. */ |
|---|
| 3733 | n/a | if (PyMemoTable_Get(self->memo, obj)) { |
|---|
| 3734 | n/a | const char pop_op = POP; |
|---|
| 3735 | n/a | |
|---|
| 3736 | n/a | if (_Pickler_Write(self, &pop_op, 1) < 0) |
|---|
| 3737 | n/a | return -1; |
|---|
| 3738 | n/a | if (memo_get(self, obj) < 0) |
|---|
| 3739 | n/a | return -1; |
|---|
| 3740 | n/a | |
|---|
| 3741 | n/a | return 0; |
|---|
| 3742 | n/a | } |
|---|
| 3743 | n/a | else if (memo_put(self, obj) < 0) |
|---|
| 3744 | n/a | return -1; |
|---|
| 3745 | n/a | } |
|---|
| 3746 | n/a | |
|---|
| 3747 | n/a | if (listitems && batch_list(self, listitems) < 0) |
|---|
| 3748 | n/a | return -1; |
|---|
| 3749 | n/a | |
|---|
| 3750 | n/a | if (dictitems && batch_dict(self, dictitems) < 0) |
|---|
| 3751 | n/a | return -1; |
|---|
| 3752 | n/a | |
|---|
| 3753 | n/a | if (state) { |
|---|
| 3754 | n/a | if (save(self, state, 0) < 0 || |
|---|
| 3755 | n/a | _Pickler_Write(self, &build_op, 1) < 0) |
|---|
| 3756 | n/a | return -1; |
|---|
| 3757 | n/a | } |
|---|
| 3758 | n/a | |
|---|
| 3759 | n/a | return 0; |
|---|
| 3760 | n/a | } |
|---|
| 3761 | n/a | |
|---|
| 3762 | n/a | static int |
|---|
| 3763 | n/a | save(PicklerObject *self, PyObject *obj, int pers_save) |
|---|
| 3764 | n/a | { |
|---|
| 3765 | n/a | PyTypeObject *type; |
|---|
| 3766 | n/a | PyObject *reduce_func = NULL; |
|---|
| 3767 | n/a | PyObject *reduce_value = NULL; |
|---|
| 3768 | n/a | int status = 0; |
|---|
| 3769 | n/a | |
|---|
| 3770 | n/a | if (_Pickler_OpcodeBoundary(self) < 0) |
|---|
| 3771 | n/a | return -1; |
|---|
| 3772 | n/a | |
|---|
| 3773 | n/a | if (Py_EnterRecursiveCall(" while pickling an object")) |
|---|
| 3774 | n/a | return -1; |
|---|
| 3775 | n/a | |
|---|
| 3776 | n/a | /* The extra pers_save argument is necessary to avoid calling save_pers() |
|---|
| 3777 | n/a | on its returned object. */ |
|---|
| 3778 | n/a | if (!pers_save && self->pers_func) { |
|---|
| 3779 | n/a | /* save_pers() returns: |
|---|
| 3780 | n/a | -1 to signal an error; |
|---|
| 3781 | n/a | 0 if it did nothing successfully; |
|---|
| 3782 | n/a | 1 if a persistent id was saved. |
|---|
| 3783 | n/a | */ |
|---|
| 3784 | n/a | if ((status = save_pers(self, obj, self->pers_func)) != 0) |
|---|
| 3785 | n/a | goto done; |
|---|
| 3786 | n/a | } |
|---|
| 3787 | n/a | |
|---|
| 3788 | n/a | type = Py_TYPE(obj); |
|---|
| 3789 | n/a | |
|---|
| 3790 | n/a | /* The old cPickle had an optimization that used switch-case statement |
|---|
| 3791 | n/a | dispatching on the first letter of the type name. This has was removed |
|---|
| 3792 | n/a | since benchmarks shown that this optimization was actually slowing |
|---|
| 3793 | n/a | things down. */ |
|---|
| 3794 | n/a | |
|---|
| 3795 | n/a | /* Atom types; these aren't memoized, so don't check the memo. */ |
|---|
| 3796 | n/a | |
|---|
| 3797 | n/a | if (obj == Py_None) { |
|---|
| 3798 | n/a | status = save_none(self, obj); |
|---|
| 3799 | n/a | goto done; |
|---|
| 3800 | n/a | } |
|---|
| 3801 | n/a | else if (obj == Py_False || obj == Py_True) { |
|---|
| 3802 | n/a | status = save_bool(self, obj); |
|---|
| 3803 | n/a | goto done; |
|---|
| 3804 | n/a | } |
|---|
| 3805 | n/a | else if (type == &PyLong_Type) { |
|---|
| 3806 | n/a | status = save_long(self, obj); |
|---|
| 3807 | n/a | goto done; |
|---|
| 3808 | n/a | } |
|---|
| 3809 | n/a | else if (type == &PyFloat_Type) { |
|---|
| 3810 | n/a | status = save_float(self, obj); |
|---|
| 3811 | n/a | goto done; |
|---|
| 3812 | n/a | } |
|---|
| 3813 | n/a | |
|---|
| 3814 | n/a | /* Check the memo to see if it has the object. If so, generate |
|---|
| 3815 | n/a | a GET (or BINGET) opcode, instead of pickling the object |
|---|
| 3816 | n/a | once again. */ |
|---|
| 3817 | n/a | if (PyMemoTable_Get(self->memo, obj)) { |
|---|
| 3818 | n/a | if (memo_get(self, obj) < 0) |
|---|
| 3819 | n/a | goto error; |
|---|
| 3820 | n/a | goto done; |
|---|
| 3821 | n/a | } |
|---|
| 3822 | n/a | |
|---|
| 3823 | n/a | if (type == &PyBytes_Type) { |
|---|
| 3824 | n/a | status = save_bytes(self, obj); |
|---|
| 3825 | n/a | goto done; |
|---|
| 3826 | n/a | } |
|---|
| 3827 | n/a | else if (type == &PyUnicode_Type) { |
|---|
| 3828 | n/a | status = save_unicode(self, obj); |
|---|
| 3829 | n/a | goto done; |
|---|
| 3830 | n/a | } |
|---|
| 3831 | n/a | else if (type == &PyDict_Type) { |
|---|
| 3832 | n/a | status = save_dict(self, obj); |
|---|
| 3833 | n/a | goto done; |
|---|
| 3834 | n/a | } |
|---|
| 3835 | n/a | else if (type == &PySet_Type) { |
|---|
| 3836 | n/a | status = save_set(self, obj); |
|---|
| 3837 | n/a | goto done; |
|---|
| 3838 | n/a | } |
|---|
| 3839 | n/a | else if (type == &PyFrozenSet_Type) { |
|---|
| 3840 | n/a | status = save_frozenset(self, obj); |
|---|
| 3841 | n/a | goto done; |
|---|
| 3842 | n/a | } |
|---|
| 3843 | n/a | else if (type == &PyList_Type) { |
|---|
| 3844 | n/a | status = save_list(self, obj); |
|---|
| 3845 | n/a | goto done; |
|---|
| 3846 | n/a | } |
|---|
| 3847 | n/a | else if (type == &PyTuple_Type) { |
|---|
| 3848 | n/a | status = save_tuple(self, obj); |
|---|
| 3849 | n/a | goto done; |
|---|
| 3850 | n/a | } |
|---|
| 3851 | n/a | else if (type == &PyType_Type) { |
|---|
| 3852 | n/a | status = save_type(self, obj); |
|---|
| 3853 | n/a | goto done; |
|---|
| 3854 | n/a | } |
|---|
| 3855 | n/a | else if (type == &PyFunction_Type) { |
|---|
| 3856 | n/a | status = save_global(self, obj, NULL); |
|---|
| 3857 | n/a | goto done; |
|---|
| 3858 | n/a | } |
|---|
| 3859 | n/a | |
|---|
| 3860 | n/a | /* XXX: This part needs some unit tests. */ |
|---|
| 3861 | n/a | |
|---|
| 3862 | n/a | /* Get a reduction callable, and call it. This may come from |
|---|
| 3863 | n/a | * self.dispatch_table, copyreg.dispatch_table, the object's |
|---|
| 3864 | n/a | * __reduce_ex__ method, or the object's __reduce__ method. |
|---|
| 3865 | n/a | */ |
|---|
| 3866 | n/a | if (self->dispatch_table == NULL) { |
|---|
| 3867 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3868 | n/a | reduce_func = PyDict_GetItemWithError(st->dispatch_table, |
|---|
| 3869 | n/a | (PyObject *)type); |
|---|
| 3870 | n/a | if (reduce_func == NULL) { |
|---|
| 3871 | n/a | if (PyErr_Occurred()) { |
|---|
| 3872 | n/a | goto error; |
|---|
| 3873 | n/a | } |
|---|
| 3874 | n/a | } else { |
|---|
| 3875 | n/a | /* PyDict_GetItemWithError() returns a borrowed reference. |
|---|
| 3876 | n/a | Increase the reference count to be consistent with |
|---|
| 3877 | n/a | PyObject_GetItem and _PyObject_GetAttrId used below. */ |
|---|
| 3878 | n/a | Py_INCREF(reduce_func); |
|---|
| 3879 | n/a | } |
|---|
| 3880 | n/a | } else { |
|---|
| 3881 | n/a | reduce_func = PyObject_GetItem(self->dispatch_table, |
|---|
| 3882 | n/a | (PyObject *)type); |
|---|
| 3883 | n/a | if (reduce_func == NULL) { |
|---|
| 3884 | n/a | if (PyErr_ExceptionMatches(PyExc_KeyError)) |
|---|
| 3885 | n/a | PyErr_Clear(); |
|---|
| 3886 | n/a | else |
|---|
| 3887 | n/a | goto error; |
|---|
| 3888 | n/a | } |
|---|
| 3889 | n/a | } |
|---|
| 3890 | n/a | if (reduce_func != NULL) { |
|---|
| 3891 | n/a | Py_INCREF(obj); |
|---|
| 3892 | n/a | reduce_value = _Pickle_FastCall(reduce_func, obj); |
|---|
| 3893 | n/a | } |
|---|
| 3894 | n/a | else if (PyType_IsSubtype(type, &PyType_Type)) { |
|---|
| 3895 | n/a | status = save_global(self, obj, NULL); |
|---|
| 3896 | n/a | goto done; |
|---|
| 3897 | n/a | } |
|---|
| 3898 | n/a | else { |
|---|
| 3899 | n/a | _Py_IDENTIFIER(__reduce__); |
|---|
| 3900 | n/a | _Py_IDENTIFIER(__reduce_ex__); |
|---|
| 3901 | n/a | |
|---|
| 3902 | n/a | |
|---|
| 3903 | n/a | /* XXX: If the __reduce__ method is defined, __reduce_ex__ is |
|---|
| 3904 | n/a | automatically defined as __reduce__. While this is convenient, this |
|---|
| 3905 | n/a | make it impossible to know which method was actually called. Of |
|---|
| 3906 | n/a | course, this is not a big deal. But still, it would be nice to let |
|---|
| 3907 | n/a | the user know which method was called when something go |
|---|
| 3908 | n/a | wrong. Incidentally, this means if __reduce_ex__ is not defined, we |
|---|
| 3909 | n/a | don't actually have to check for a __reduce__ method. */ |
|---|
| 3910 | n/a | |
|---|
| 3911 | n/a | /* Check for a __reduce_ex__ method. */ |
|---|
| 3912 | n/a | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce_ex__); |
|---|
| 3913 | n/a | if (reduce_func != NULL) { |
|---|
| 3914 | n/a | PyObject *proto; |
|---|
| 3915 | n/a | proto = PyLong_FromLong(self->proto); |
|---|
| 3916 | n/a | if (proto != NULL) { |
|---|
| 3917 | n/a | reduce_value = _Pickle_FastCall(reduce_func, proto); |
|---|
| 3918 | n/a | } |
|---|
| 3919 | n/a | } |
|---|
| 3920 | n/a | else { |
|---|
| 3921 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3922 | n/a | |
|---|
| 3923 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
|---|
| 3924 | n/a | PyErr_Clear(); |
|---|
| 3925 | n/a | } |
|---|
| 3926 | n/a | else { |
|---|
| 3927 | n/a | goto error; |
|---|
| 3928 | n/a | } |
|---|
| 3929 | n/a | /* Check for a __reduce__ method. */ |
|---|
| 3930 | n/a | reduce_func = _PyObject_GetAttrId(obj, &PyId___reduce__); |
|---|
| 3931 | n/a | if (reduce_func != NULL) { |
|---|
| 3932 | n/a | reduce_value = _PyObject_CallNoArg(reduce_func); |
|---|
| 3933 | n/a | } |
|---|
| 3934 | n/a | else { |
|---|
| 3935 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 3936 | n/a | "can't pickle '%.200s' object: %R", |
|---|
| 3937 | n/a | type->tp_name, obj); |
|---|
| 3938 | n/a | goto error; |
|---|
| 3939 | n/a | } |
|---|
| 3940 | n/a | } |
|---|
| 3941 | n/a | } |
|---|
| 3942 | n/a | |
|---|
| 3943 | n/a | if (reduce_value == NULL) |
|---|
| 3944 | n/a | goto error; |
|---|
| 3945 | n/a | |
|---|
| 3946 | n/a | if (PyUnicode_Check(reduce_value)) { |
|---|
| 3947 | n/a | status = save_global(self, obj, reduce_value); |
|---|
| 3948 | n/a | goto done; |
|---|
| 3949 | n/a | } |
|---|
| 3950 | n/a | |
|---|
| 3951 | n/a | if (!PyTuple_Check(reduce_value)) { |
|---|
| 3952 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 3953 | n/a | PyErr_SetString(st->PicklingError, |
|---|
| 3954 | n/a | "__reduce__ must return a string or tuple"); |
|---|
| 3955 | n/a | goto error; |
|---|
| 3956 | n/a | } |
|---|
| 3957 | n/a | |
|---|
| 3958 | n/a | status = save_reduce(self, reduce_value, obj); |
|---|
| 3959 | n/a | |
|---|
| 3960 | n/a | if (0) { |
|---|
| 3961 | n/a | error: |
|---|
| 3962 | n/a | status = -1; |
|---|
| 3963 | n/a | } |
|---|
| 3964 | n/a | done: |
|---|
| 3965 | n/a | |
|---|
| 3966 | n/a | Py_LeaveRecursiveCall(); |
|---|
| 3967 | n/a | Py_XDECREF(reduce_func); |
|---|
| 3968 | n/a | Py_XDECREF(reduce_value); |
|---|
| 3969 | n/a | |
|---|
| 3970 | n/a | return status; |
|---|
| 3971 | n/a | } |
|---|
| 3972 | n/a | |
|---|
| 3973 | n/a | static int |
|---|
| 3974 | n/a | dump(PicklerObject *self, PyObject *obj) |
|---|
| 3975 | n/a | { |
|---|
| 3976 | n/a | const char stop_op = STOP; |
|---|
| 3977 | n/a | |
|---|
| 3978 | n/a | if (self->proto >= 2) { |
|---|
| 3979 | n/a | char header[2]; |
|---|
| 3980 | n/a | |
|---|
| 3981 | n/a | header[0] = PROTO; |
|---|
| 3982 | n/a | assert(self->proto >= 0 && self->proto < 256); |
|---|
| 3983 | n/a | header[1] = (unsigned char)self->proto; |
|---|
| 3984 | n/a | if (_Pickler_Write(self, header, 2) < 0) |
|---|
| 3985 | n/a | return -1; |
|---|
| 3986 | n/a | if (self->proto >= 4) |
|---|
| 3987 | n/a | self->framing = 1; |
|---|
| 3988 | n/a | } |
|---|
| 3989 | n/a | |
|---|
| 3990 | n/a | if (save(self, obj, 0) < 0 || |
|---|
| 3991 | n/a | _Pickler_Write(self, &stop_op, 1) < 0) |
|---|
| 3992 | n/a | return -1; |
|---|
| 3993 | n/a | |
|---|
| 3994 | n/a | return 0; |
|---|
| 3995 | n/a | } |
|---|
| 3996 | n/a | |
|---|
| 3997 | n/a | /*[clinic input] |
|---|
| 3998 | n/a | |
|---|
| 3999 | n/a | _pickle.Pickler.clear_memo |
|---|
| 4000 | n/a | |
|---|
| 4001 | n/a | Clears the pickler's "memo". |
|---|
| 4002 | n/a | |
|---|
| 4003 | n/a | The memo is the data structure that remembers which objects the |
|---|
| 4004 | n/a | pickler has already seen, so that shared or recursive objects are |
|---|
| 4005 | n/a | pickled by reference and not by value. This method is useful when |
|---|
| 4006 | n/a | re-using picklers. |
|---|
| 4007 | n/a | [clinic start generated code]*/ |
|---|
| 4008 | n/a | |
|---|
| 4009 | n/a | static PyObject * |
|---|
| 4010 | n/a | _pickle_Pickler_clear_memo_impl(PicklerObject *self) |
|---|
| 4011 | n/a | /*[clinic end generated code: output=8665c8658aaa094b input=01bdad52f3d93e56]*/ |
|---|
| 4012 | n/a | { |
|---|
| 4013 | n/a | if (self->memo) |
|---|
| 4014 | n/a | PyMemoTable_Clear(self->memo); |
|---|
| 4015 | n/a | |
|---|
| 4016 | n/a | Py_RETURN_NONE; |
|---|
| 4017 | n/a | } |
|---|
| 4018 | n/a | |
|---|
| 4019 | n/a | /*[clinic input] |
|---|
| 4020 | n/a | |
|---|
| 4021 | n/a | _pickle.Pickler.dump |
|---|
| 4022 | n/a | |
|---|
| 4023 | n/a | obj: object |
|---|
| 4024 | n/a | / |
|---|
| 4025 | n/a | |
|---|
| 4026 | n/a | Write a pickled representation of the given object to the open file. |
|---|
| 4027 | n/a | [clinic start generated code]*/ |
|---|
| 4028 | n/a | |
|---|
| 4029 | n/a | static PyObject * |
|---|
| 4030 | n/a | _pickle_Pickler_dump(PicklerObject *self, PyObject *obj) |
|---|
| 4031 | n/a | /*[clinic end generated code: output=87ecad1261e02ac7 input=552eb1c0f52260d9]*/ |
|---|
| 4032 | n/a | { |
|---|
| 4033 | n/a | /* Check whether the Pickler was initialized correctly (issue3664). |
|---|
| 4034 | n/a | Developers often forget to call __init__() in their subclasses, which |
|---|
| 4035 | n/a | would trigger a segfault without this check. */ |
|---|
| 4036 | n/a | if (self->write == NULL) { |
|---|
| 4037 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 4038 | n/a | PyErr_Format(st->PicklingError, |
|---|
| 4039 | n/a | "Pickler.__init__() was not called by %s.__init__()", |
|---|
| 4040 | n/a | Py_TYPE(self)->tp_name); |
|---|
| 4041 | n/a | return NULL; |
|---|
| 4042 | n/a | } |
|---|
| 4043 | n/a | |
|---|
| 4044 | n/a | if (_Pickler_ClearBuffer(self) < 0) |
|---|
| 4045 | n/a | return NULL; |
|---|
| 4046 | n/a | |
|---|
| 4047 | n/a | if (dump(self, obj) < 0) |
|---|
| 4048 | n/a | return NULL; |
|---|
| 4049 | n/a | |
|---|
| 4050 | n/a | if (_Pickler_FlushToFile(self) < 0) |
|---|
| 4051 | n/a | return NULL; |
|---|
| 4052 | n/a | |
|---|
| 4053 | n/a | Py_RETURN_NONE; |
|---|
| 4054 | n/a | } |
|---|
| 4055 | n/a | |
|---|
| 4056 | n/a | /*[clinic input] |
|---|
| 4057 | n/a | |
|---|
| 4058 | n/a | _pickle.Pickler.__sizeof__ -> Py_ssize_t |
|---|
| 4059 | n/a | |
|---|
| 4060 | n/a | Returns size in memory, in bytes. |
|---|
| 4061 | n/a | [clinic start generated code]*/ |
|---|
| 4062 | n/a | |
|---|
| 4063 | n/a | static Py_ssize_t |
|---|
| 4064 | n/a | _pickle_Pickler___sizeof___impl(PicklerObject *self) |
|---|
| 4065 | n/a | /*[clinic end generated code: output=106edb3123f332e1 input=8cbbec9bd5540d42]*/ |
|---|
| 4066 | n/a | { |
|---|
| 4067 | n/a | Py_ssize_t res, s; |
|---|
| 4068 | n/a | |
|---|
| 4069 | n/a | res = _PyObject_SIZE(Py_TYPE(self)); |
|---|
| 4070 | n/a | if (self->memo != NULL) { |
|---|
| 4071 | n/a | res += sizeof(PyMemoTable); |
|---|
| 4072 | n/a | res += self->memo->mt_allocated * sizeof(PyMemoEntry); |
|---|
| 4073 | n/a | } |
|---|
| 4074 | n/a | if (self->output_buffer != NULL) { |
|---|
| 4075 | n/a | s = _PySys_GetSizeOf(self->output_buffer); |
|---|
| 4076 | n/a | if (s == -1) |
|---|
| 4077 | n/a | return -1; |
|---|
| 4078 | n/a | res += s; |
|---|
| 4079 | n/a | } |
|---|
| 4080 | n/a | return res; |
|---|
| 4081 | n/a | } |
|---|
| 4082 | n/a | |
|---|
| 4083 | n/a | static struct PyMethodDef Pickler_methods[] = { |
|---|
| 4084 | n/a | _PICKLE_PICKLER_DUMP_METHODDEF |
|---|
| 4085 | n/a | _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF |
|---|
| 4086 | n/a | _PICKLE_PICKLER___SIZEOF___METHODDEF |
|---|
| 4087 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 4088 | n/a | }; |
|---|
| 4089 | n/a | |
|---|
| 4090 | n/a | static void |
|---|
| 4091 | n/a | Pickler_dealloc(PicklerObject *self) |
|---|
| 4092 | n/a | { |
|---|
| 4093 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 4094 | n/a | |
|---|
| 4095 | n/a | Py_XDECREF(self->output_buffer); |
|---|
| 4096 | n/a | Py_XDECREF(self->write); |
|---|
| 4097 | n/a | Py_XDECREF(self->pers_func); |
|---|
| 4098 | n/a | Py_XDECREF(self->dispatch_table); |
|---|
| 4099 | n/a | Py_XDECREF(self->fast_memo); |
|---|
| 4100 | n/a | |
|---|
| 4101 | n/a | PyMemoTable_Del(self->memo); |
|---|
| 4102 | n/a | |
|---|
| 4103 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 4104 | n/a | } |
|---|
| 4105 | n/a | |
|---|
| 4106 | n/a | static int |
|---|
| 4107 | n/a | Pickler_traverse(PicklerObject *self, visitproc visit, void *arg) |
|---|
| 4108 | n/a | { |
|---|
| 4109 | n/a | Py_VISIT(self->write); |
|---|
| 4110 | n/a | Py_VISIT(self->pers_func); |
|---|
| 4111 | n/a | Py_VISIT(self->dispatch_table); |
|---|
| 4112 | n/a | Py_VISIT(self->fast_memo); |
|---|
| 4113 | n/a | return 0; |
|---|
| 4114 | n/a | } |
|---|
| 4115 | n/a | |
|---|
| 4116 | n/a | static int |
|---|
| 4117 | n/a | Pickler_clear(PicklerObject *self) |
|---|
| 4118 | n/a | { |
|---|
| 4119 | n/a | Py_CLEAR(self->output_buffer); |
|---|
| 4120 | n/a | Py_CLEAR(self->write); |
|---|
| 4121 | n/a | Py_CLEAR(self->pers_func); |
|---|
| 4122 | n/a | Py_CLEAR(self->dispatch_table); |
|---|
| 4123 | n/a | Py_CLEAR(self->fast_memo); |
|---|
| 4124 | n/a | |
|---|
| 4125 | n/a | if (self->memo != NULL) { |
|---|
| 4126 | n/a | PyMemoTable *memo = self->memo; |
|---|
| 4127 | n/a | self->memo = NULL; |
|---|
| 4128 | n/a | PyMemoTable_Del(memo); |
|---|
| 4129 | n/a | } |
|---|
| 4130 | n/a | return 0; |
|---|
| 4131 | n/a | } |
|---|
| 4132 | n/a | |
|---|
| 4133 | n/a | |
|---|
| 4134 | n/a | /*[clinic input] |
|---|
| 4135 | n/a | |
|---|
| 4136 | n/a | _pickle.Pickler.__init__ |
|---|
| 4137 | n/a | |
|---|
| 4138 | n/a | file: object |
|---|
| 4139 | n/a | protocol: object = NULL |
|---|
| 4140 | n/a | fix_imports: bool = True |
|---|
| 4141 | n/a | |
|---|
| 4142 | n/a | This takes a binary file for writing a pickle data stream. |
|---|
| 4143 | n/a | |
|---|
| 4144 | n/a | The optional *protocol* argument tells the pickler to use the given |
|---|
| 4145 | n/a | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
|---|
| 4146 | n/a | protocol is 3; a backward-incompatible protocol designed for Python 3. |
|---|
| 4147 | n/a | |
|---|
| 4148 | n/a | Specifying a negative protocol version selects the highest protocol |
|---|
| 4149 | n/a | version supported. The higher the protocol used, the more recent the |
|---|
| 4150 | n/a | version of Python needed to read the pickle produced. |
|---|
| 4151 | n/a | |
|---|
| 4152 | n/a | The *file* argument must have a write() method that accepts a single |
|---|
| 4153 | n/a | bytes argument. It can thus be a file object opened for binary |
|---|
| 4154 | n/a | writing, an io.BytesIO instance, or any other custom object that meets |
|---|
| 4155 | n/a | this interface. |
|---|
| 4156 | n/a | |
|---|
| 4157 | n/a | If *fix_imports* is True and protocol is less than 3, pickle will try |
|---|
| 4158 | n/a | to map the new Python 3 names to the old module names used in Python |
|---|
| 4159 | n/a | 2, so that the pickle data stream is readable with Python 2. |
|---|
| 4160 | n/a | [clinic start generated code]*/ |
|---|
| 4161 | n/a | |
|---|
| 4162 | n/a | static int |
|---|
| 4163 | n/a | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
|---|
| 4164 | n/a | PyObject *protocol, int fix_imports) |
|---|
| 4165 | n/a | /*[clinic end generated code: output=b5f31078dab17fb0 input=4faabdbc763c2389]*/ |
|---|
| 4166 | n/a | { |
|---|
| 4167 | n/a | _Py_IDENTIFIER(persistent_id); |
|---|
| 4168 | n/a | _Py_IDENTIFIER(dispatch_table); |
|---|
| 4169 | n/a | |
|---|
| 4170 | n/a | /* In case of multiple __init__() calls, clear previous content. */ |
|---|
| 4171 | n/a | if (self->write != NULL) |
|---|
| 4172 | n/a | (void)Pickler_clear(self); |
|---|
| 4173 | n/a | |
|---|
| 4174 | n/a | if (_Pickler_SetProtocol(self, protocol, fix_imports) < 0) |
|---|
| 4175 | n/a | return -1; |
|---|
| 4176 | n/a | |
|---|
| 4177 | n/a | if (_Pickler_SetOutputStream(self, file) < 0) |
|---|
| 4178 | n/a | return -1; |
|---|
| 4179 | n/a | |
|---|
| 4180 | n/a | /* memo and output_buffer may have already been created in _Pickler_New */ |
|---|
| 4181 | n/a | if (self->memo == NULL) { |
|---|
| 4182 | n/a | self->memo = PyMemoTable_New(); |
|---|
| 4183 | n/a | if (self->memo == NULL) |
|---|
| 4184 | n/a | return -1; |
|---|
| 4185 | n/a | } |
|---|
| 4186 | n/a | self->output_len = 0; |
|---|
| 4187 | n/a | if (self->output_buffer == NULL) { |
|---|
| 4188 | n/a | self->max_output_len = WRITE_BUF_SIZE; |
|---|
| 4189 | n/a | self->output_buffer = PyBytes_FromStringAndSize(NULL, |
|---|
| 4190 | n/a | self->max_output_len); |
|---|
| 4191 | n/a | if (self->output_buffer == NULL) |
|---|
| 4192 | n/a | return -1; |
|---|
| 4193 | n/a | } |
|---|
| 4194 | n/a | |
|---|
| 4195 | n/a | self->fast = 0; |
|---|
| 4196 | n/a | self->fast_nesting = 0; |
|---|
| 4197 | n/a | self->fast_memo = NULL; |
|---|
| 4198 | n/a | self->pers_func = NULL; |
|---|
| 4199 | n/a | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_id)) { |
|---|
| 4200 | n/a | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
|---|
| 4201 | n/a | &PyId_persistent_id); |
|---|
| 4202 | n/a | if (self->pers_func == NULL) |
|---|
| 4203 | n/a | return -1; |
|---|
| 4204 | n/a | } |
|---|
| 4205 | n/a | self->dispatch_table = NULL; |
|---|
| 4206 | n/a | if (_PyObject_HasAttrId((PyObject *)self, &PyId_dispatch_table)) { |
|---|
| 4207 | n/a | self->dispatch_table = _PyObject_GetAttrId((PyObject *)self, |
|---|
| 4208 | n/a | &PyId_dispatch_table); |
|---|
| 4209 | n/a | if (self->dispatch_table == NULL) |
|---|
| 4210 | n/a | return -1; |
|---|
| 4211 | n/a | } |
|---|
| 4212 | n/a | |
|---|
| 4213 | n/a | return 0; |
|---|
| 4214 | n/a | } |
|---|
| 4215 | n/a | |
|---|
| 4216 | n/a | |
|---|
| 4217 | n/a | /* Define a proxy object for the Pickler's internal memo object. This is to |
|---|
| 4218 | n/a | * avoid breaking code like: |
|---|
| 4219 | n/a | * pickler.memo.clear() |
|---|
| 4220 | n/a | * and |
|---|
| 4221 | n/a | * pickler.memo = saved_memo |
|---|
| 4222 | n/a | * Is this a good idea? Not really, but we don't want to break code that uses |
|---|
| 4223 | n/a | * it. Note that we don't implement the entire mapping API here. This is |
|---|
| 4224 | n/a | * intentional, as these should be treated as black-box implementation details. |
|---|
| 4225 | n/a | */ |
|---|
| 4226 | n/a | |
|---|
| 4227 | n/a | /*[clinic input] |
|---|
| 4228 | n/a | _pickle.PicklerMemoProxy.clear |
|---|
| 4229 | n/a | |
|---|
| 4230 | n/a | Remove all items from memo. |
|---|
| 4231 | n/a | [clinic start generated code]*/ |
|---|
| 4232 | n/a | |
|---|
| 4233 | n/a | static PyObject * |
|---|
| 4234 | n/a | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self) |
|---|
| 4235 | n/a | /*[clinic end generated code: output=5fb9370d48ae8b05 input=ccc186dacd0f1405]*/ |
|---|
| 4236 | n/a | { |
|---|
| 4237 | n/a | if (self->pickler->memo) |
|---|
| 4238 | n/a | PyMemoTable_Clear(self->pickler->memo); |
|---|
| 4239 | n/a | Py_RETURN_NONE; |
|---|
| 4240 | n/a | } |
|---|
| 4241 | n/a | |
|---|
| 4242 | n/a | /*[clinic input] |
|---|
| 4243 | n/a | _pickle.PicklerMemoProxy.copy |
|---|
| 4244 | n/a | |
|---|
| 4245 | n/a | Copy the memo to a new object. |
|---|
| 4246 | n/a | [clinic start generated code]*/ |
|---|
| 4247 | n/a | |
|---|
| 4248 | n/a | static PyObject * |
|---|
| 4249 | n/a | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self) |
|---|
| 4250 | n/a | /*[clinic end generated code: output=bb83a919d29225ef input=b73043485ac30b36]*/ |
|---|
| 4251 | n/a | { |
|---|
| 4252 | n/a | Py_ssize_t i; |
|---|
| 4253 | n/a | PyMemoTable *memo; |
|---|
| 4254 | n/a | PyObject *new_memo = PyDict_New(); |
|---|
| 4255 | n/a | if (new_memo == NULL) |
|---|
| 4256 | n/a | return NULL; |
|---|
| 4257 | n/a | |
|---|
| 4258 | n/a | memo = self->pickler->memo; |
|---|
| 4259 | n/a | for (i = 0; i < memo->mt_allocated; ++i) { |
|---|
| 4260 | n/a | PyMemoEntry entry = memo->mt_table[i]; |
|---|
| 4261 | n/a | if (entry.me_key != NULL) { |
|---|
| 4262 | n/a | int status; |
|---|
| 4263 | n/a | PyObject *key, *value; |
|---|
| 4264 | n/a | |
|---|
| 4265 | n/a | key = PyLong_FromVoidPtr(entry.me_key); |
|---|
| 4266 | n/a | value = Py_BuildValue("nO", entry.me_value, entry.me_key); |
|---|
| 4267 | n/a | |
|---|
| 4268 | n/a | if (key == NULL || value == NULL) { |
|---|
| 4269 | n/a | Py_XDECREF(key); |
|---|
| 4270 | n/a | Py_XDECREF(value); |
|---|
| 4271 | n/a | goto error; |
|---|
| 4272 | n/a | } |
|---|
| 4273 | n/a | status = PyDict_SetItem(new_memo, key, value); |
|---|
| 4274 | n/a | Py_DECREF(key); |
|---|
| 4275 | n/a | Py_DECREF(value); |
|---|
| 4276 | n/a | if (status < 0) |
|---|
| 4277 | n/a | goto error; |
|---|
| 4278 | n/a | } |
|---|
| 4279 | n/a | } |
|---|
| 4280 | n/a | return new_memo; |
|---|
| 4281 | n/a | |
|---|
| 4282 | n/a | error: |
|---|
| 4283 | n/a | Py_XDECREF(new_memo); |
|---|
| 4284 | n/a | return NULL; |
|---|
| 4285 | n/a | } |
|---|
| 4286 | n/a | |
|---|
| 4287 | n/a | /*[clinic input] |
|---|
| 4288 | n/a | _pickle.PicklerMemoProxy.__reduce__ |
|---|
| 4289 | n/a | |
|---|
| 4290 | n/a | Implement pickle support. |
|---|
| 4291 | n/a | [clinic start generated code]*/ |
|---|
| 4292 | n/a | |
|---|
| 4293 | n/a | static PyObject * |
|---|
| 4294 | n/a | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self) |
|---|
| 4295 | n/a | /*[clinic end generated code: output=bebba1168863ab1d input=2f7c540e24b7aae4]*/ |
|---|
| 4296 | n/a | { |
|---|
| 4297 | n/a | PyObject *reduce_value, *dict_args; |
|---|
| 4298 | n/a | PyObject *contents = _pickle_PicklerMemoProxy_copy_impl(self); |
|---|
| 4299 | n/a | if (contents == NULL) |
|---|
| 4300 | n/a | return NULL; |
|---|
| 4301 | n/a | |
|---|
| 4302 | n/a | reduce_value = PyTuple_New(2); |
|---|
| 4303 | n/a | if (reduce_value == NULL) { |
|---|
| 4304 | n/a | Py_DECREF(contents); |
|---|
| 4305 | n/a | return NULL; |
|---|
| 4306 | n/a | } |
|---|
| 4307 | n/a | dict_args = PyTuple_New(1); |
|---|
| 4308 | n/a | if (dict_args == NULL) { |
|---|
| 4309 | n/a | Py_DECREF(contents); |
|---|
| 4310 | n/a | Py_DECREF(reduce_value); |
|---|
| 4311 | n/a | return NULL; |
|---|
| 4312 | n/a | } |
|---|
| 4313 | n/a | PyTuple_SET_ITEM(dict_args, 0, contents); |
|---|
| 4314 | n/a | Py_INCREF((PyObject *)&PyDict_Type); |
|---|
| 4315 | n/a | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
|---|
| 4316 | n/a | PyTuple_SET_ITEM(reduce_value, 1, dict_args); |
|---|
| 4317 | n/a | return reduce_value; |
|---|
| 4318 | n/a | } |
|---|
| 4319 | n/a | |
|---|
| 4320 | n/a | static PyMethodDef picklerproxy_methods[] = { |
|---|
| 4321 | n/a | _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF |
|---|
| 4322 | n/a | _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF |
|---|
| 4323 | n/a | _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF |
|---|
| 4324 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 4325 | n/a | }; |
|---|
| 4326 | n/a | |
|---|
| 4327 | n/a | static void |
|---|
| 4328 | n/a | PicklerMemoProxy_dealloc(PicklerMemoProxyObject *self) |
|---|
| 4329 | n/a | { |
|---|
| 4330 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 4331 | n/a | Py_XDECREF(self->pickler); |
|---|
| 4332 | n/a | PyObject_GC_Del((PyObject *)self); |
|---|
| 4333 | n/a | } |
|---|
| 4334 | n/a | |
|---|
| 4335 | n/a | static int |
|---|
| 4336 | n/a | PicklerMemoProxy_traverse(PicklerMemoProxyObject *self, |
|---|
| 4337 | n/a | visitproc visit, void *arg) |
|---|
| 4338 | n/a | { |
|---|
| 4339 | n/a | Py_VISIT(self->pickler); |
|---|
| 4340 | n/a | return 0; |
|---|
| 4341 | n/a | } |
|---|
| 4342 | n/a | |
|---|
| 4343 | n/a | static int |
|---|
| 4344 | n/a | PicklerMemoProxy_clear(PicklerMemoProxyObject *self) |
|---|
| 4345 | n/a | { |
|---|
| 4346 | n/a | Py_CLEAR(self->pickler); |
|---|
| 4347 | n/a | return 0; |
|---|
| 4348 | n/a | } |
|---|
| 4349 | n/a | |
|---|
| 4350 | n/a | static PyTypeObject PicklerMemoProxyType = { |
|---|
| 4351 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4352 | n/a | "_pickle.PicklerMemoProxy", /*tp_name*/ |
|---|
| 4353 | n/a | sizeof(PicklerMemoProxyObject), /*tp_basicsize*/ |
|---|
| 4354 | n/a | 0, |
|---|
| 4355 | n/a | (destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */ |
|---|
| 4356 | n/a | 0, /* tp_print */ |
|---|
| 4357 | n/a | 0, /* tp_getattr */ |
|---|
| 4358 | n/a | 0, /* tp_setattr */ |
|---|
| 4359 | n/a | 0, /* tp_compare */ |
|---|
| 4360 | n/a | 0, /* tp_repr */ |
|---|
| 4361 | n/a | 0, /* tp_as_number */ |
|---|
| 4362 | n/a | 0, /* tp_as_sequence */ |
|---|
| 4363 | n/a | 0, /* tp_as_mapping */ |
|---|
| 4364 | n/a | PyObject_HashNotImplemented, /* tp_hash */ |
|---|
| 4365 | n/a | 0, /* tp_call */ |
|---|
| 4366 | n/a | 0, /* tp_str */ |
|---|
| 4367 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 4368 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
|---|
| 4369 | n/a | 0, /* tp_as_buffer */ |
|---|
| 4370 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
|---|
| 4371 | n/a | 0, /* tp_doc */ |
|---|
| 4372 | n/a | (traverseproc)PicklerMemoProxy_traverse, /* tp_traverse */ |
|---|
| 4373 | n/a | (inquiry)PicklerMemoProxy_clear, /* tp_clear */ |
|---|
| 4374 | n/a | 0, /* tp_richcompare */ |
|---|
| 4375 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 4376 | n/a | 0, /* tp_iter */ |
|---|
| 4377 | n/a | 0, /* tp_iternext */ |
|---|
| 4378 | n/a | picklerproxy_methods, /* tp_methods */ |
|---|
| 4379 | n/a | }; |
|---|
| 4380 | n/a | |
|---|
| 4381 | n/a | static PyObject * |
|---|
| 4382 | n/a | PicklerMemoProxy_New(PicklerObject *pickler) |
|---|
| 4383 | n/a | { |
|---|
| 4384 | n/a | PicklerMemoProxyObject *self; |
|---|
| 4385 | n/a | |
|---|
| 4386 | n/a | self = PyObject_GC_New(PicklerMemoProxyObject, &PicklerMemoProxyType); |
|---|
| 4387 | n/a | if (self == NULL) |
|---|
| 4388 | n/a | return NULL; |
|---|
| 4389 | n/a | Py_INCREF(pickler); |
|---|
| 4390 | n/a | self->pickler = pickler; |
|---|
| 4391 | n/a | PyObject_GC_Track(self); |
|---|
| 4392 | n/a | return (PyObject *)self; |
|---|
| 4393 | n/a | } |
|---|
| 4394 | n/a | |
|---|
| 4395 | n/a | /*****************************************************************************/ |
|---|
| 4396 | n/a | |
|---|
| 4397 | n/a | static PyObject * |
|---|
| 4398 | n/a | Pickler_get_memo(PicklerObject *self) |
|---|
| 4399 | n/a | { |
|---|
| 4400 | n/a | return PicklerMemoProxy_New(self); |
|---|
| 4401 | n/a | } |
|---|
| 4402 | n/a | |
|---|
| 4403 | n/a | static int |
|---|
| 4404 | n/a | Pickler_set_memo(PicklerObject *self, PyObject *obj) |
|---|
| 4405 | n/a | { |
|---|
| 4406 | n/a | PyMemoTable *new_memo = NULL; |
|---|
| 4407 | n/a | |
|---|
| 4408 | n/a | if (obj == NULL) { |
|---|
| 4409 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4410 | n/a | "attribute deletion is not supported"); |
|---|
| 4411 | n/a | return -1; |
|---|
| 4412 | n/a | } |
|---|
| 4413 | n/a | |
|---|
| 4414 | n/a | if (Py_TYPE(obj) == &PicklerMemoProxyType) { |
|---|
| 4415 | n/a | PicklerObject *pickler = |
|---|
| 4416 | n/a | ((PicklerMemoProxyObject *)obj)->pickler; |
|---|
| 4417 | n/a | |
|---|
| 4418 | n/a | new_memo = PyMemoTable_Copy(pickler->memo); |
|---|
| 4419 | n/a | if (new_memo == NULL) |
|---|
| 4420 | n/a | return -1; |
|---|
| 4421 | n/a | } |
|---|
| 4422 | n/a | else if (PyDict_Check(obj)) { |
|---|
| 4423 | n/a | Py_ssize_t i = 0; |
|---|
| 4424 | n/a | PyObject *key, *value; |
|---|
| 4425 | n/a | |
|---|
| 4426 | n/a | new_memo = PyMemoTable_New(); |
|---|
| 4427 | n/a | if (new_memo == NULL) |
|---|
| 4428 | n/a | return -1; |
|---|
| 4429 | n/a | |
|---|
| 4430 | n/a | while (PyDict_Next(obj, &i, &key, &value)) { |
|---|
| 4431 | n/a | Py_ssize_t memo_id; |
|---|
| 4432 | n/a | PyObject *memo_obj; |
|---|
| 4433 | n/a | |
|---|
| 4434 | n/a | if (!PyTuple_Check(value) || Py_SIZE(value) != 2) { |
|---|
| 4435 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4436 | n/a | "'memo' values must be 2-item tuples"); |
|---|
| 4437 | n/a | goto error; |
|---|
| 4438 | n/a | } |
|---|
| 4439 | n/a | memo_id = PyLong_AsSsize_t(PyTuple_GET_ITEM(value, 0)); |
|---|
| 4440 | n/a | if (memo_id == -1 && PyErr_Occurred()) |
|---|
| 4441 | n/a | goto error; |
|---|
| 4442 | n/a | memo_obj = PyTuple_GET_ITEM(value, 1); |
|---|
| 4443 | n/a | if (PyMemoTable_Set(new_memo, memo_obj, memo_id) < 0) |
|---|
| 4444 | n/a | goto error; |
|---|
| 4445 | n/a | } |
|---|
| 4446 | n/a | } |
|---|
| 4447 | n/a | else { |
|---|
| 4448 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 4449 | n/a | "'memo' attribute must be a PicklerMemoProxy object" |
|---|
| 4450 | n/a | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
|---|
| 4451 | n/a | return -1; |
|---|
| 4452 | n/a | } |
|---|
| 4453 | n/a | |
|---|
| 4454 | n/a | PyMemoTable_Del(self->memo); |
|---|
| 4455 | n/a | self->memo = new_memo; |
|---|
| 4456 | n/a | |
|---|
| 4457 | n/a | return 0; |
|---|
| 4458 | n/a | |
|---|
| 4459 | n/a | error: |
|---|
| 4460 | n/a | if (new_memo) |
|---|
| 4461 | n/a | PyMemoTable_Del(new_memo); |
|---|
| 4462 | n/a | return -1; |
|---|
| 4463 | n/a | } |
|---|
| 4464 | n/a | |
|---|
| 4465 | n/a | static PyObject * |
|---|
| 4466 | n/a | Pickler_get_persid(PicklerObject *self) |
|---|
| 4467 | n/a | { |
|---|
| 4468 | n/a | if (self->pers_func == NULL) |
|---|
| 4469 | n/a | PyErr_SetString(PyExc_AttributeError, "persistent_id"); |
|---|
| 4470 | n/a | else |
|---|
| 4471 | n/a | Py_INCREF(self->pers_func); |
|---|
| 4472 | n/a | return self->pers_func; |
|---|
| 4473 | n/a | } |
|---|
| 4474 | n/a | |
|---|
| 4475 | n/a | static int |
|---|
| 4476 | n/a | Pickler_set_persid(PicklerObject *self, PyObject *value) |
|---|
| 4477 | n/a | { |
|---|
| 4478 | n/a | if (value == NULL) { |
|---|
| 4479 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4480 | n/a | "attribute deletion is not supported"); |
|---|
| 4481 | n/a | return -1; |
|---|
| 4482 | n/a | } |
|---|
| 4483 | n/a | if (!PyCallable_Check(value)) { |
|---|
| 4484 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 4485 | n/a | "persistent_id must be a callable taking one argument"); |
|---|
| 4486 | n/a | return -1; |
|---|
| 4487 | n/a | } |
|---|
| 4488 | n/a | |
|---|
| 4489 | n/a | Py_INCREF(value); |
|---|
| 4490 | n/a | Py_XSETREF(self->pers_func, value); |
|---|
| 4491 | n/a | |
|---|
| 4492 | n/a | return 0; |
|---|
| 4493 | n/a | } |
|---|
| 4494 | n/a | |
|---|
| 4495 | n/a | static PyMemberDef Pickler_members[] = { |
|---|
| 4496 | n/a | {"bin", T_INT, offsetof(PicklerObject, bin)}, |
|---|
| 4497 | n/a | {"fast", T_INT, offsetof(PicklerObject, fast)}, |
|---|
| 4498 | n/a | {"dispatch_table", T_OBJECT_EX, offsetof(PicklerObject, dispatch_table)}, |
|---|
| 4499 | n/a | {NULL} |
|---|
| 4500 | n/a | }; |
|---|
| 4501 | n/a | |
|---|
| 4502 | n/a | static PyGetSetDef Pickler_getsets[] = { |
|---|
| 4503 | n/a | {"memo", (getter)Pickler_get_memo, |
|---|
| 4504 | n/a | (setter)Pickler_set_memo}, |
|---|
| 4505 | n/a | {"persistent_id", (getter)Pickler_get_persid, |
|---|
| 4506 | n/a | (setter)Pickler_set_persid}, |
|---|
| 4507 | n/a | {NULL} |
|---|
| 4508 | n/a | }; |
|---|
| 4509 | n/a | |
|---|
| 4510 | n/a | static PyTypeObject Pickler_Type = { |
|---|
| 4511 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 4512 | n/a | "_pickle.Pickler" , /*tp_name*/ |
|---|
| 4513 | n/a | sizeof(PicklerObject), /*tp_basicsize*/ |
|---|
| 4514 | n/a | 0, /*tp_itemsize*/ |
|---|
| 4515 | n/a | (destructor)Pickler_dealloc, /*tp_dealloc*/ |
|---|
| 4516 | n/a | 0, /*tp_print*/ |
|---|
| 4517 | n/a | 0, /*tp_getattr*/ |
|---|
| 4518 | n/a | 0, /*tp_setattr*/ |
|---|
| 4519 | n/a | 0, /*tp_reserved*/ |
|---|
| 4520 | n/a | 0, /*tp_repr*/ |
|---|
| 4521 | n/a | 0, /*tp_as_number*/ |
|---|
| 4522 | n/a | 0, /*tp_as_sequence*/ |
|---|
| 4523 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 4524 | n/a | 0, /*tp_hash*/ |
|---|
| 4525 | n/a | 0, /*tp_call*/ |
|---|
| 4526 | n/a | 0, /*tp_str*/ |
|---|
| 4527 | n/a | 0, /*tp_getattro*/ |
|---|
| 4528 | n/a | 0, /*tp_setattro*/ |
|---|
| 4529 | n/a | 0, /*tp_as_buffer*/ |
|---|
| 4530 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
|---|
| 4531 | n/a | _pickle_Pickler___init____doc__, /*tp_doc*/ |
|---|
| 4532 | n/a | (traverseproc)Pickler_traverse, /*tp_traverse*/ |
|---|
| 4533 | n/a | (inquiry)Pickler_clear, /*tp_clear*/ |
|---|
| 4534 | n/a | 0, /*tp_richcompare*/ |
|---|
| 4535 | n/a | 0, /*tp_weaklistoffset*/ |
|---|
| 4536 | n/a | 0, /*tp_iter*/ |
|---|
| 4537 | n/a | 0, /*tp_iternext*/ |
|---|
| 4538 | n/a | Pickler_methods, /*tp_methods*/ |
|---|
| 4539 | n/a | Pickler_members, /*tp_members*/ |
|---|
| 4540 | n/a | Pickler_getsets, /*tp_getset*/ |
|---|
| 4541 | n/a | 0, /*tp_base*/ |
|---|
| 4542 | n/a | 0, /*tp_dict*/ |
|---|
| 4543 | n/a | 0, /*tp_descr_get*/ |
|---|
| 4544 | n/a | 0, /*tp_descr_set*/ |
|---|
| 4545 | n/a | 0, /*tp_dictoffset*/ |
|---|
| 4546 | n/a | _pickle_Pickler___init__, /*tp_init*/ |
|---|
| 4547 | n/a | PyType_GenericAlloc, /*tp_alloc*/ |
|---|
| 4548 | n/a | PyType_GenericNew, /*tp_new*/ |
|---|
| 4549 | n/a | PyObject_GC_Del, /*tp_free*/ |
|---|
| 4550 | n/a | 0, /*tp_is_gc*/ |
|---|
| 4551 | n/a | }; |
|---|
| 4552 | n/a | |
|---|
| 4553 | n/a | /* Temporary helper for calling self.find_class(). |
|---|
| 4554 | n/a | |
|---|
| 4555 | n/a | XXX: It would be nice to able to avoid Python function call overhead, by |
|---|
| 4556 | n/a | using directly the C version of find_class(), when find_class() is not |
|---|
| 4557 | n/a | overridden by a subclass. Although, this could become rather hackish. A |
|---|
| 4558 | n/a | simpler optimization would be to call the C function when self is not a |
|---|
| 4559 | n/a | subclass instance. */ |
|---|
| 4560 | n/a | static PyObject * |
|---|
| 4561 | n/a | find_class(UnpicklerObject *self, PyObject *module_name, PyObject *global_name) |
|---|
| 4562 | n/a | { |
|---|
| 4563 | n/a | _Py_IDENTIFIER(find_class); |
|---|
| 4564 | n/a | |
|---|
| 4565 | n/a | return _PyObject_CallMethodIdObjArgs((PyObject *)self, &PyId_find_class, |
|---|
| 4566 | n/a | module_name, global_name, NULL); |
|---|
| 4567 | n/a | } |
|---|
| 4568 | n/a | |
|---|
| 4569 | n/a | static Py_ssize_t |
|---|
| 4570 | n/a | marker(UnpicklerObject *self) |
|---|
| 4571 | n/a | { |
|---|
| 4572 | n/a | Py_ssize_t mark; |
|---|
| 4573 | n/a | |
|---|
| 4574 | n/a | if (self->num_marks < 1) { |
|---|
| 4575 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 4576 | n/a | PyErr_SetString(st->UnpicklingError, "could not find MARK"); |
|---|
| 4577 | n/a | return -1; |
|---|
| 4578 | n/a | } |
|---|
| 4579 | n/a | |
|---|
| 4580 | n/a | mark = self->marks[--self->num_marks]; |
|---|
| 4581 | n/a | self->stack->mark_set = self->num_marks != 0; |
|---|
| 4582 | n/a | self->stack->fence = self->num_marks ? |
|---|
| 4583 | n/a | self->marks[self->num_marks - 1] : 0; |
|---|
| 4584 | n/a | return mark; |
|---|
| 4585 | n/a | } |
|---|
| 4586 | n/a | |
|---|
| 4587 | n/a | static int |
|---|
| 4588 | n/a | load_none(UnpicklerObject *self) |
|---|
| 4589 | n/a | { |
|---|
| 4590 | n/a | PDATA_APPEND(self->stack, Py_None, -1); |
|---|
| 4591 | n/a | return 0; |
|---|
| 4592 | n/a | } |
|---|
| 4593 | n/a | |
|---|
| 4594 | n/a | static int |
|---|
| 4595 | n/a | load_int(UnpicklerObject *self) |
|---|
| 4596 | n/a | { |
|---|
| 4597 | n/a | PyObject *value; |
|---|
| 4598 | n/a | char *endptr, *s; |
|---|
| 4599 | n/a | Py_ssize_t len; |
|---|
| 4600 | n/a | long x; |
|---|
| 4601 | n/a | |
|---|
| 4602 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 4603 | n/a | return -1; |
|---|
| 4604 | n/a | if (len < 2) |
|---|
| 4605 | n/a | return bad_readline(); |
|---|
| 4606 | n/a | |
|---|
| 4607 | n/a | errno = 0; |
|---|
| 4608 | n/a | /* XXX: Should the base argument of strtol() be explicitly set to 10? |
|---|
| 4609 | n/a | XXX(avassalotti): Should this uses PyOS_strtol()? */ |
|---|
| 4610 | n/a | x = strtol(s, &endptr, 0); |
|---|
| 4611 | n/a | |
|---|
| 4612 | n/a | if (errno || (*endptr != '\n' && *endptr != '\0')) { |
|---|
| 4613 | n/a | /* Hm, maybe we've got something long. Let's try reading |
|---|
| 4614 | n/a | * it as a Python int object. */ |
|---|
| 4615 | n/a | errno = 0; |
|---|
| 4616 | n/a | /* XXX: Same thing about the base here. */ |
|---|
| 4617 | n/a | value = PyLong_FromString(s, NULL, 0); |
|---|
| 4618 | n/a | if (value == NULL) { |
|---|
| 4619 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 4620 | n/a | "could not convert string to int"); |
|---|
| 4621 | n/a | return -1; |
|---|
| 4622 | n/a | } |
|---|
| 4623 | n/a | } |
|---|
| 4624 | n/a | else { |
|---|
| 4625 | n/a | if (len == 3 && (x == 0 || x == 1)) { |
|---|
| 4626 | n/a | if ((value = PyBool_FromLong(x)) == NULL) |
|---|
| 4627 | n/a | return -1; |
|---|
| 4628 | n/a | } |
|---|
| 4629 | n/a | else { |
|---|
| 4630 | n/a | if ((value = PyLong_FromLong(x)) == NULL) |
|---|
| 4631 | n/a | return -1; |
|---|
| 4632 | n/a | } |
|---|
| 4633 | n/a | } |
|---|
| 4634 | n/a | |
|---|
| 4635 | n/a | PDATA_PUSH(self->stack, value, -1); |
|---|
| 4636 | n/a | return 0; |
|---|
| 4637 | n/a | } |
|---|
| 4638 | n/a | |
|---|
| 4639 | n/a | static int |
|---|
| 4640 | n/a | load_bool(UnpicklerObject *self, PyObject *boolean) |
|---|
| 4641 | n/a | { |
|---|
| 4642 | n/a | assert(boolean == Py_True || boolean == Py_False); |
|---|
| 4643 | n/a | PDATA_APPEND(self->stack, boolean, -1); |
|---|
| 4644 | n/a | return 0; |
|---|
| 4645 | n/a | } |
|---|
| 4646 | n/a | |
|---|
| 4647 | n/a | /* s contains x bytes of an unsigned little-endian integer. Return its value |
|---|
| 4648 | n/a | * as a C Py_ssize_t, or -1 if it's higher than PY_SSIZE_T_MAX. |
|---|
| 4649 | n/a | */ |
|---|
| 4650 | n/a | static Py_ssize_t |
|---|
| 4651 | n/a | calc_binsize(char *bytes, int nbytes) |
|---|
| 4652 | n/a | { |
|---|
| 4653 | n/a | unsigned char *s = (unsigned char *)bytes; |
|---|
| 4654 | n/a | int i; |
|---|
| 4655 | n/a | size_t x = 0; |
|---|
| 4656 | n/a | |
|---|
| 4657 | n/a | if (nbytes > (int)sizeof(size_t)) { |
|---|
| 4658 | n/a | /* Check for integer overflow. BINBYTES8 and BINUNICODE8 opcodes |
|---|
| 4659 | n/a | * have 64-bit size that can't be represented on 32-bit platform. |
|---|
| 4660 | n/a | */ |
|---|
| 4661 | n/a | for (i = (int)sizeof(size_t); i < nbytes; i++) { |
|---|
| 4662 | n/a | if (s[i]) |
|---|
| 4663 | n/a | return -1; |
|---|
| 4664 | n/a | } |
|---|
| 4665 | n/a | nbytes = (int)sizeof(size_t); |
|---|
| 4666 | n/a | } |
|---|
| 4667 | n/a | for (i = 0; i < nbytes; i++) { |
|---|
| 4668 | n/a | x |= (size_t) s[i] << (8 * i); |
|---|
| 4669 | n/a | } |
|---|
| 4670 | n/a | |
|---|
| 4671 | n/a | if (x > PY_SSIZE_T_MAX) |
|---|
| 4672 | n/a | return -1; |
|---|
| 4673 | n/a | else |
|---|
| 4674 | n/a | return (Py_ssize_t) x; |
|---|
| 4675 | n/a | } |
|---|
| 4676 | n/a | |
|---|
| 4677 | n/a | /* s contains x bytes of a little-endian integer. Return its value as a |
|---|
| 4678 | n/a | * C int. Obscure: when x is 1 or 2, this is an unsigned little-endian |
|---|
| 4679 | n/a | * int, but when x is 4 it's a signed one. This is a historical source |
|---|
| 4680 | n/a | * of x-platform bugs. |
|---|
| 4681 | n/a | */ |
|---|
| 4682 | n/a | static long |
|---|
| 4683 | n/a | calc_binint(char *bytes, int nbytes) |
|---|
| 4684 | n/a | { |
|---|
| 4685 | n/a | unsigned char *s = (unsigned char *)bytes; |
|---|
| 4686 | n/a | Py_ssize_t i; |
|---|
| 4687 | n/a | long x = 0; |
|---|
| 4688 | n/a | |
|---|
| 4689 | n/a | for (i = 0; i < nbytes; i++) { |
|---|
| 4690 | n/a | x |= (long)s[i] << (8 * i); |
|---|
| 4691 | n/a | } |
|---|
| 4692 | n/a | |
|---|
| 4693 | n/a | /* Unlike BININT1 and BININT2, BININT (more accurately BININT4) |
|---|
| 4694 | n/a | * is signed, so on a box with longs bigger than 4 bytes we need |
|---|
| 4695 | n/a | * to extend a BININT's sign bit to the full width. |
|---|
| 4696 | n/a | */ |
|---|
| 4697 | n/a | if (SIZEOF_LONG > 4 && nbytes == 4) { |
|---|
| 4698 | n/a | x |= -(x & (1L << 31)); |
|---|
| 4699 | n/a | } |
|---|
| 4700 | n/a | |
|---|
| 4701 | n/a | return x; |
|---|
| 4702 | n/a | } |
|---|
| 4703 | n/a | |
|---|
| 4704 | n/a | static int |
|---|
| 4705 | n/a | load_binintx(UnpicklerObject *self, char *s, int size) |
|---|
| 4706 | n/a | { |
|---|
| 4707 | n/a | PyObject *value; |
|---|
| 4708 | n/a | long x; |
|---|
| 4709 | n/a | |
|---|
| 4710 | n/a | x = calc_binint(s, size); |
|---|
| 4711 | n/a | |
|---|
| 4712 | n/a | if ((value = PyLong_FromLong(x)) == NULL) |
|---|
| 4713 | n/a | return -1; |
|---|
| 4714 | n/a | |
|---|
| 4715 | n/a | PDATA_PUSH(self->stack, value, -1); |
|---|
| 4716 | n/a | return 0; |
|---|
| 4717 | n/a | } |
|---|
| 4718 | n/a | |
|---|
| 4719 | n/a | static int |
|---|
| 4720 | n/a | load_binint(UnpicklerObject *self) |
|---|
| 4721 | n/a | { |
|---|
| 4722 | n/a | char *s; |
|---|
| 4723 | n/a | |
|---|
| 4724 | n/a | if (_Unpickler_Read(self, &s, 4) < 0) |
|---|
| 4725 | n/a | return -1; |
|---|
| 4726 | n/a | |
|---|
| 4727 | n/a | return load_binintx(self, s, 4); |
|---|
| 4728 | n/a | } |
|---|
| 4729 | n/a | |
|---|
| 4730 | n/a | static int |
|---|
| 4731 | n/a | load_binint1(UnpicklerObject *self) |
|---|
| 4732 | n/a | { |
|---|
| 4733 | n/a | char *s; |
|---|
| 4734 | n/a | |
|---|
| 4735 | n/a | if (_Unpickler_Read(self, &s, 1) < 0) |
|---|
| 4736 | n/a | return -1; |
|---|
| 4737 | n/a | |
|---|
| 4738 | n/a | return load_binintx(self, s, 1); |
|---|
| 4739 | n/a | } |
|---|
| 4740 | n/a | |
|---|
| 4741 | n/a | static int |
|---|
| 4742 | n/a | load_binint2(UnpicklerObject *self) |
|---|
| 4743 | n/a | { |
|---|
| 4744 | n/a | char *s; |
|---|
| 4745 | n/a | |
|---|
| 4746 | n/a | if (_Unpickler_Read(self, &s, 2) < 0) |
|---|
| 4747 | n/a | return -1; |
|---|
| 4748 | n/a | |
|---|
| 4749 | n/a | return load_binintx(self, s, 2); |
|---|
| 4750 | n/a | } |
|---|
| 4751 | n/a | |
|---|
| 4752 | n/a | static int |
|---|
| 4753 | n/a | load_long(UnpicklerObject *self) |
|---|
| 4754 | n/a | { |
|---|
| 4755 | n/a | PyObject *value; |
|---|
| 4756 | n/a | char *s = NULL; |
|---|
| 4757 | n/a | Py_ssize_t len; |
|---|
| 4758 | n/a | |
|---|
| 4759 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 4760 | n/a | return -1; |
|---|
| 4761 | n/a | if (len < 2) |
|---|
| 4762 | n/a | return bad_readline(); |
|---|
| 4763 | n/a | |
|---|
| 4764 | n/a | /* s[len-2] will usually be 'L' (and s[len-1] is '\n'); we need to remove |
|---|
| 4765 | n/a | the 'L' before calling PyLong_FromString. In order to maintain |
|---|
| 4766 | n/a | compatibility with Python 3.0.0, we don't actually *require* |
|---|
| 4767 | n/a | the 'L' to be present. */ |
|---|
| 4768 | n/a | if (s[len-2] == 'L') |
|---|
| 4769 | n/a | s[len-2] = '\0'; |
|---|
| 4770 | n/a | /* XXX: Should the base argument explicitly set to 10? */ |
|---|
| 4771 | n/a | value = PyLong_FromString(s, NULL, 0); |
|---|
| 4772 | n/a | if (value == NULL) |
|---|
| 4773 | n/a | return -1; |
|---|
| 4774 | n/a | |
|---|
| 4775 | n/a | PDATA_PUSH(self->stack, value, -1); |
|---|
| 4776 | n/a | return 0; |
|---|
| 4777 | n/a | } |
|---|
| 4778 | n/a | |
|---|
| 4779 | n/a | /* 'size' bytes contain the # of bytes of little-endian 256's-complement |
|---|
| 4780 | n/a | * data following. |
|---|
| 4781 | n/a | */ |
|---|
| 4782 | n/a | static int |
|---|
| 4783 | n/a | load_counted_long(UnpicklerObject *self, int size) |
|---|
| 4784 | n/a | { |
|---|
| 4785 | n/a | PyObject *value; |
|---|
| 4786 | n/a | char *nbytes; |
|---|
| 4787 | n/a | char *pdata; |
|---|
| 4788 | n/a | |
|---|
| 4789 | n/a | assert(size == 1 || size == 4); |
|---|
| 4790 | n/a | if (_Unpickler_Read(self, &nbytes, size) < 0) |
|---|
| 4791 | n/a | return -1; |
|---|
| 4792 | n/a | |
|---|
| 4793 | n/a | size = calc_binint(nbytes, size); |
|---|
| 4794 | n/a | if (size < 0) { |
|---|
| 4795 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 4796 | n/a | /* Corrupt or hostile pickle -- we never write one like this */ |
|---|
| 4797 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 4798 | n/a | "LONG pickle has negative byte count"); |
|---|
| 4799 | n/a | return -1; |
|---|
| 4800 | n/a | } |
|---|
| 4801 | n/a | |
|---|
| 4802 | n/a | if (size == 0) |
|---|
| 4803 | n/a | value = PyLong_FromLong(0L); |
|---|
| 4804 | n/a | else { |
|---|
| 4805 | n/a | /* Read the raw little-endian bytes and convert. */ |
|---|
| 4806 | n/a | if (_Unpickler_Read(self, &pdata, size) < 0) |
|---|
| 4807 | n/a | return -1; |
|---|
| 4808 | n/a | value = _PyLong_FromByteArray((unsigned char *)pdata, (size_t)size, |
|---|
| 4809 | n/a | 1 /* little endian */ , 1 /* signed */ ); |
|---|
| 4810 | n/a | } |
|---|
| 4811 | n/a | if (value == NULL) |
|---|
| 4812 | n/a | return -1; |
|---|
| 4813 | n/a | PDATA_PUSH(self->stack, value, -1); |
|---|
| 4814 | n/a | return 0; |
|---|
| 4815 | n/a | } |
|---|
| 4816 | n/a | |
|---|
| 4817 | n/a | static int |
|---|
| 4818 | n/a | load_float(UnpicklerObject *self) |
|---|
| 4819 | n/a | { |
|---|
| 4820 | n/a | PyObject *value; |
|---|
| 4821 | n/a | char *endptr, *s; |
|---|
| 4822 | n/a | Py_ssize_t len; |
|---|
| 4823 | n/a | double d; |
|---|
| 4824 | n/a | |
|---|
| 4825 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 4826 | n/a | return -1; |
|---|
| 4827 | n/a | if (len < 2) |
|---|
| 4828 | n/a | return bad_readline(); |
|---|
| 4829 | n/a | |
|---|
| 4830 | n/a | errno = 0; |
|---|
| 4831 | n/a | d = PyOS_string_to_double(s, &endptr, PyExc_OverflowError); |
|---|
| 4832 | n/a | if (d == -1.0 && PyErr_Occurred()) |
|---|
| 4833 | n/a | return -1; |
|---|
| 4834 | n/a | if ((endptr[0] != '\n') && (endptr[0] != '\0')) { |
|---|
| 4835 | n/a | PyErr_SetString(PyExc_ValueError, "could not convert string to float"); |
|---|
| 4836 | n/a | return -1; |
|---|
| 4837 | n/a | } |
|---|
| 4838 | n/a | value = PyFloat_FromDouble(d); |
|---|
| 4839 | n/a | if (value == NULL) |
|---|
| 4840 | n/a | return -1; |
|---|
| 4841 | n/a | |
|---|
| 4842 | n/a | PDATA_PUSH(self->stack, value, -1); |
|---|
| 4843 | n/a | return 0; |
|---|
| 4844 | n/a | } |
|---|
| 4845 | n/a | |
|---|
| 4846 | n/a | static int |
|---|
| 4847 | n/a | load_binfloat(UnpicklerObject *self) |
|---|
| 4848 | n/a | { |
|---|
| 4849 | n/a | PyObject *value; |
|---|
| 4850 | n/a | double x; |
|---|
| 4851 | n/a | char *s; |
|---|
| 4852 | n/a | |
|---|
| 4853 | n/a | if (_Unpickler_Read(self, &s, 8) < 0) |
|---|
| 4854 | n/a | return -1; |
|---|
| 4855 | n/a | |
|---|
| 4856 | n/a | x = _PyFloat_Unpack8((unsigned char *)s, 0); |
|---|
| 4857 | n/a | if (x == -1.0 && PyErr_Occurred()) |
|---|
| 4858 | n/a | return -1; |
|---|
| 4859 | n/a | |
|---|
| 4860 | n/a | if ((value = PyFloat_FromDouble(x)) == NULL) |
|---|
| 4861 | n/a | return -1; |
|---|
| 4862 | n/a | |
|---|
| 4863 | n/a | PDATA_PUSH(self->stack, value, -1); |
|---|
| 4864 | n/a | return 0; |
|---|
| 4865 | n/a | } |
|---|
| 4866 | n/a | |
|---|
| 4867 | n/a | static int |
|---|
| 4868 | n/a | load_string(UnpicklerObject *self) |
|---|
| 4869 | n/a | { |
|---|
| 4870 | n/a | PyObject *bytes; |
|---|
| 4871 | n/a | PyObject *obj; |
|---|
| 4872 | n/a | Py_ssize_t len; |
|---|
| 4873 | n/a | char *s, *p; |
|---|
| 4874 | n/a | |
|---|
| 4875 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 4876 | n/a | return -1; |
|---|
| 4877 | n/a | /* Strip the newline */ |
|---|
| 4878 | n/a | len--; |
|---|
| 4879 | n/a | /* Strip outermost quotes */ |
|---|
| 4880 | n/a | if (len >= 2 && s[0] == s[len - 1] && (s[0] == '\'' || s[0] == '"')) { |
|---|
| 4881 | n/a | p = s + 1; |
|---|
| 4882 | n/a | len -= 2; |
|---|
| 4883 | n/a | } |
|---|
| 4884 | n/a | else { |
|---|
| 4885 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 4886 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 4887 | n/a | "the STRING opcode argument must be quoted"); |
|---|
| 4888 | n/a | return -1; |
|---|
| 4889 | n/a | } |
|---|
| 4890 | n/a | assert(len >= 0); |
|---|
| 4891 | n/a | |
|---|
| 4892 | n/a | /* Use the PyBytes API to decode the string, since that is what is used |
|---|
| 4893 | n/a | to encode, and then coerce the result to Unicode. */ |
|---|
| 4894 | n/a | bytes = PyBytes_DecodeEscape(p, len, NULL, 0, NULL); |
|---|
| 4895 | n/a | if (bytes == NULL) |
|---|
| 4896 | n/a | return -1; |
|---|
| 4897 | n/a | |
|---|
| 4898 | n/a | /* Leave the Python 2.x strings as bytes if the *encoding* given to the |
|---|
| 4899 | n/a | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
|---|
| 4900 | n/a | if (strcmp(self->encoding, "bytes") == 0) { |
|---|
| 4901 | n/a | obj = bytes; |
|---|
| 4902 | n/a | } |
|---|
| 4903 | n/a | else { |
|---|
| 4904 | n/a | obj = PyUnicode_FromEncodedObject(bytes, self->encoding, self->errors); |
|---|
| 4905 | n/a | Py_DECREF(bytes); |
|---|
| 4906 | n/a | if (obj == NULL) { |
|---|
| 4907 | n/a | return -1; |
|---|
| 4908 | n/a | } |
|---|
| 4909 | n/a | } |
|---|
| 4910 | n/a | |
|---|
| 4911 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 4912 | n/a | return 0; |
|---|
| 4913 | n/a | } |
|---|
| 4914 | n/a | |
|---|
| 4915 | n/a | static int |
|---|
| 4916 | n/a | load_counted_binstring(UnpicklerObject *self, int nbytes) |
|---|
| 4917 | n/a | { |
|---|
| 4918 | n/a | PyObject *obj; |
|---|
| 4919 | n/a | Py_ssize_t size; |
|---|
| 4920 | n/a | char *s; |
|---|
| 4921 | n/a | |
|---|
| 4922 | n/a | if (_Unpickler_Read(self, &s, nbytes) < 0) |
|---|
| 4923 | n/a | return -1; |
|---|
| 4924 | n/a | |
|---|
| 4925 | n/a | size = calc_binsize(s, nbytes); |
|---|
| 4926 | n/a | if (size < 0) { |
|---|
| 4927 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 4928 | n/a | PyErr_Format(st->UnpicklingError, |
|---|
| 4929 | n/a | "BINSTRING exceeds system's maximum size of %zd bytes", |
|---|
| 4930 | n/a | PY_SSIZE_T_MAX); |
|---|
| 4931 | n/a | return -1; |
|---|
| 4932 | n/a | } |
|---|
| 4933 | n/a | |
|---|
| 4934 | n/a | if (_Unpickler_Read(self, &s, size) < 0) |
|---|
| 4935 | n/a | return -1; |
|---|
| 4936 | n/a | |
|---|
| 4937 | n/a | /* Convert Python 2.x strings to bytes if the *encoding* given to the |
|---|
| 4938 | n/a | Unpickler was 'bytes'. Otherwise, convert them to unicode. */ |
|---|
| 4939 | n/a | if (strcmp(self->encoding, "bytes") == 0) { |
|---|
| 4940 | n/a | obj = PyBytes_FromStringAndSize(s, size); |
|---|
| 4941 | n/a | } |
|---|
| 4942 | n/a | else { |
|---|
| 4943 | n/a | obj = PyUnicode_Decode(s, size, self->encoding, self->errors); |
|---|
| 4944 | n/a | } |
|---|
| 4945 | n/a | if (obj == NULL) { |
|---|
| 4946 | n/a | return -1; |
|---|
| 4947 | n/a | } |
|---|
| 4948 | n/a | |
|---|
| 4949 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 4950 | n/a | return 0; |
|---|
| 4951 | n/a | } |
|---|
| 4952 | n/a | |
|---|
| 4953 | n/a | static int |
|---|
| 4954 | n/a | load_counted_binbytes(UnpicklerObject *self, int nbytes) |
|---|
| 4955 | n/a | { |
|---|
| 4956 | n/a | PyObject *bytes; |
|---|
| 4957 | n/a | Py_ssize_t size; |
|---|
| 4958 | n/a | char *s; |
|---|
| 4959 | n/a | |
|---|
| 4960 | n/a | if (_Unpickler_Read(self, &s, nbytes) < 0) |
|---|
| 4961 | n/a | return -1; |
|---|
| 4962 | n/a | |
|---|
| 4963 | n/a | size = calc_binsize(s, nbytes); |
|---|
| 4964 | n/a | if (size < 0) { |
|---|
| 4965 | n/a | PyErr_Format(PyExc_OverflowError, |
|---|
| 4966 | n/a | "BINBYTES exceeds system's maximum size of %zd bytes", |
|---|
| 4967 | n/a | PY_SSIZE_T_MAX); |
|---|
| 4968 | n/a | return -1; |
|---|
| 4969 | n/a | } |
|---|
| 4970 | n/a | |
|---|
| 4971 | n/a | if (_Unpickler_Read(self, &s, size) < 0) |
|---|
| 4972 | n/a | return -1; |
|---|
| 4973 | n/a | |
|---|
| 4974 | n/a | bytes = PyBytes_FromStringAndSize(s, size); |
|---|
| 4975 | n/a | if (bytes == NULL) |
|---|
| 4976 | n/a | return -1; |
|---|
| 4977 | n/a | |
|---|
| 4978 | n/a | PDATA_PUSH(self->stack, bytes, -1); |
|---|
| 4979 | n/a | return 0; |
|---|
| 4980 | n/a | } |
|---|
| 4981 | n/a | |
|---|
| 4982 | n/a | static int |
|---|
| 4983 | n/a | load_unicode(UnpicklerObject *self) |
|---|
| 4984 | n/a | { |
|---|
| 4985 | n/a | PyObject *str; |
|---|
| 4986 | n/a | Py_ssize_t len; |
|---|
| 4987 | n/a | char *s = NULL; |
|---|
| 4988 | n/a | |
|---|
| 4989 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 4990 | n/a | return -1; |
|---|
| 4991 | n/a | if (len < 1) |
|---|
| 4992 | n/a | return bad_readline(); |
|---|
| 4993 | n/a | |
|---|
| 4994 | n/a | str = PyUnicode_DecodeRawUnicodeEscape(s, len - 1, NULL); |
|---|
| 4995 | n/a | if (str == NULL) |
|---|
| 4996 | n/a | return -1; |
|---|
| 4997 | n/a | |
|---|
| 4998 | n/a | PDATA_PUSH(self->stack, str, -1); |
|---|
| 4999 | n/a | return 0; |
|---|
| 5000 | n/a | } |
|---|
| 5001 | n/a | |
|---|
| 5002 | n/a | static int |
|---|
| 5003 | n/a | load_counted_binunicode(UnpicklerObject *self, int nbytes) |
|---|
| 5004 | n/a | { |
|---|
| 5005 | n/a | PyObject *str; |
|---|
| 5006 | n/a | Py_ssize_t size; |
|---|
| 5007 | n/a | char *s; |
|---|
| 5008 | n/a | |
|---|
| 5009 | n/a | if (_Unpickler_Read(self, &s, nbytes) < 0) |
|---|
| 5010 | n/a | return -1; |
|---|
| 5011 | n/a | |
|---|
| 5012 | n/a | size = calc_binsize(s, nbytes); |
|---|
| 5013 | n/a | if (size < 0) { |
|---|
| 5014 | n/a | PyErr_Format(PyExc_OverflowError, |
|---|
| 5015 | n/a | "BINUNICODE exceeds system's maximum size of %zd bytes", |
|---|
| 5016 | n/a | PY_SSIZE_T_MAX); |
|---|
| 5017 | n/a | return -1; |
|---|
| 5018 | n/a | } |
|---|
| 5019 | n/a | |
|---|
| 5020 | n/a | if (_Unpickler_Read(self, &s, size) < 0) |
|---|
| 5021 | n/a | return -1; |
|---|
| 5022 | n/a | |
|---|
| 5023 | n/a | str = PyUnicode_DecodeUTF8(s, size, "surrogatepass"); |
|---|
| 5024 | n/a | if (str == NULL) |
|---|
| 5025 | n/a | return -1; |
|---|
| 5026 | n/a | |
|---|
| 5027 | n/a | PDATA_PUSH(self->stack, str, -1); |
|---|
| 5028 | n/a | return 0; |
|---|
| 5029 | n/a | } |
|---|
| 5030 | n/a | |
|---|
| 5031 | n/a | static int |
|---|
| 5032 | n/a | load_counted_tuple(UnpicklerObject *self, Py_ssize_t len) |
|---|
| 5033 | n/a | { |
|---|
| 5034 | n/a | PyObject *tuple; |
|---|
| 5035 | n/a | |
|---|
| 5036 | n/a | if (Py_SIZE(self->stack) < len) |
|---|
| 5037 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5038 | n/a | |
|---|
| 5039 | n/a | tuple = Pdata_poptuple(self->stack, Py_SIZE(self->stack) - len); |
|---|
| 5040 | n/a | if (tuple == NULL) |
|---|
| 5041 | n/a | return -1; |
|---|
| 5042 | n/a | PDATA_PUSH(self->stack, tuple, -1); |
|---|
| 5043 | n/a | return 0; |
|---|
| 5044 | n/a | } |
|---|
| 5045 | n/a | |
|---|
| 5046 | n/a | static int |
|---|
| 5047 | n/a | load_tuple(UnpicklerObject *self) |
|---|
| 5048 | n/a | { |
|---|
| 5049 | n/a | Py_ssize_t i; |
|---|
| 5050 | n/a | |
|---|
| 5051 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5052 | n/a | return -1; |
|---|
| 5053 | n/a | |
|---|
| 5054 | n/a | return load_counted_tuple(self, Py_SIZE(self->stack) - i); |
|---|
| 5055 | n/a | } |
|---|
| 5056 | n/a | |
|---|
| 5057 | n/a | static int |
|---|
| 5058 | n/a | load_empty_list(UnpicklerObject *self) |
|---|
| 5059 | n/a | { |
|---|
| 5060 | n/a | PyObject *list; |
|---|
| 5061 | n/a | |
|---|
| 5062 | n/a | if ((list = PyList_New(0)) == NULL) |
|---|
| 5063 | n/a | return -1; |
|---|
| 5064 | n/a | PDATA_PUSH(self->stack, list, -1); |
|---|
| 5065 | n/a | return 0; |
|---|
| 5066 | n/a | } |
|---|
| 5067 | n/a | |
|---|
| 5068 | n/a | static int |
|---|
| 5069 | n/a | load_empty_dict(UnpicklerObject *self) |
|---|
| 5070 | n/a | { |
|---|
| 5071 | n/a | PyObject *dict; |
|---|
| 5072 | n/a | |
|---|
| 5073 | n/a | if ((dict = PyDict_New()) == NULL) |
|---|
| 5074 | n/a | return -1; |
|---|
| 5075 | n/a | PDATA_PUSH(self->stack, dict, -1); |
|---|
| 5076 | n/a | return 0; |
|---|
| 5077 | n/a | } |
|---|
| 5078 | n/a | |
|---|
| 5079 | n/a | static int |
|---|
| 5080 | n/a | load_empty_set(UnpicklerObject *self) |
|---|
| 5081 | n/a | { |
|---|
| 5082 | n/a | PyObject *set; |
|---|
| 5083 | n/a | |
|---|
| 5084 | n/a | if ((set = PySet_New(NULL)) == NULL) |
|---|
| 5085 | n/a | return -1; |
|---|
| 5086 | n/a | PDATA_PUSH(self->stack, set, -1); |
|---|
| 5087 | n/a | return 0; |
|---|
| 5088 | n/a | } |
|---|
| 5089 | n/a | |
|---|
| 5090 | n/a | static int |
|---|
| 5091 | n/a | load_list(UnpicklerObject *self) |
|---|
| 5092 | n/a | { |
|---|
| 5093 | n/a | PyObject *list; |
|---|
| 5094 | n/a | Py_ssize_t i; |
|---|
| 5095 | n/a | |
|---|
| 5096 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5097 | n/a | return -1; |
|---|
| 5098 | n/a | |
|---|
| 5099 | n/a | list = Pdata_poplist(self->stack, i); |
|---|
| 5100 | n/a | if (list == NULL) |
|---|
| 5101 | n/a | return -1; |
|---|
| 5102 | n/a | PDATA_PUSH(self->stack, list, -1); |
|---|
| 5103 | n/a | return 0; |
|---|
| 5104 | n/a | } |
|---|
| 5105 | n/a | |
|---|
| 5106 | n/a | static int |
|---|
| 5107 | n/a | load_dict(UnpicklerObject *self) |
|---|
| 5108 | n/a | { |
|---|
| 5109 | n/a | PyObject *dict, *key, *value; |
|---|
| 5110 | n/a | Py_ssize_t i, j, k; |
|---|
| 5111 | n/a | |
|---|
| 5112 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5113 | n/a | return -1; |
|---|
| 5114 | n/a | j = Py_SIZE(self->stack); |
|---|
| 5115 | n/a | |
|---|
| 5116 | n/a | if ((dict = PyDict_New()) == NULL) |
|---|
| 5117 | n/a | return -1; |
|---|
| 5118 | n/a | |
|---|
| 5119 | n/a | if ((j - i) % 2 != 0) { |
|---|
| 5120 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5121 | n/a | PyErr_SetString(st->UnpicklingError, "odd number of items for DICT"); |
|---|
| 5122 | n/a | Py_DECREF(dict); |
|---|
| 5123 | n/a | return -1; |
|---|
| 5124 | n/a | } |
|---|
| 5125 | n/a | |
|---|
| 5126 | n/a | for (k = i + 1; k < j; k += 2) { |
|---|
| 5127 | n/a | key = self->stack->data[k - 1]; |
|---|
| 5128 | n/a | value = self->stack->data[k]; |
|---|
| 5129 | n/a | if (PyDict_SetItem(dict, key, value) < 0) { |
|---|
| 5130 | n/a | Py_DECREF(dict); |
|---|
| 5131 | n/a | return -1; |
|---|
| 5132 | n/a | } |
|---|
| 5133 | n/a | } |
|---|
| 5134 | n/a | Pdata_clear(self->stack, i); |
|---|
| 5135 | n/a | PDATA_PUSH(self->stack, dict, -1); |
|---|
| 5136 | n/a | return 0; |
|---|
| 5137 | n/a | } |
|---|
| 5138 | n/a | |
|---|
| 5139 | n/a | static int |
|---|
| 5140 | n/a | load_frozenset(UnpicklerObject *self) |
|---|
| 5141 | n/a | { |
|---|
| 5142 | n/a | PyObject *items; |
|---|
| 5143 | n/a | PyObject *frozenset; |
|---|
| 5144 | n/a | Py_ssize_t i; |
|---|
| 5145 | n/a | |
|---|
| 5146 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5147 | n/a | return -1; |
|---|
| 5148 | n/a | |
|---|
| 5149 | n/a | items = Pdata_poptuple(self->stack, i); |
|---|
| 5150 | n/a | if (items == NULL) |
|---|
| 5151 | n/a | return -1; |
|---|
| 5152 | n/a | |
|---|
| 5153 | n/a | frozenset = PyFrozenSet_New(items); |
|---|
| 5154 | n/a | Py_DECREF(items); |
|---|
| 5155 | n/a | if (frozenset == NULL) |
|---|
| 5156 | n/a | return -1; |
|---|
| 5157 | n/a | |
|---|
| 5158 | n/a | PDATA_PUSH(self->stack, frozenset, -1); |
|---|
| 5159 | n/a | return 0; |
|---|
| 5160 | n/a | } |
|---|
| 5161 | n/a | |
|---|
| 5162 | n/a | static PyObject * |
|---|
| 5163 | n/a | instantiate(PyObject *cls, PyObject *args) |
|---|
| 5164 | n/a | { |
|---|
| 5165 | n/a | PyObject *result = NULL; |
|---|
| 5166 | n/a | _Py_IDENTIFIER(__getinitargs__); |
|---|
| 5167 | n/a | /* Caller must assure args are a tuple. Normally, args come from |
|---|
| 5168 | n/a | Pdata_poptuple which packs objects from the top of the stack |
|---|
| 5169 | n/a | into a newly created tuple. */ |
|---|
| 5170 | n/a | assert(PyTuple_Check(args)); |
|---|
| 5171 | n/a | if (Py_SIZE(args) > 0 || !PyType_Check(cls) || |
|---|
| 5172 | n/a | _PyObject_HasAttrId(cls, &PyId___getinitargs__)) { |
|---|
| 5173 | n/a | result = PyObject_CallObject(cls, args); |
|---|
| 5174 | n/a | } |
|---|
| 5175 | n/a | else { |
|---|
| 5176 | n/a | _Py_IDENTIFIER(__new__); |
|---|
| 5177 | n/a | |
|---|
| 5178 | n/a | result = _PyObject_CallMethodIdObjArgs(cls, &PyId___new__, cls, NULL); |
|---|
| 5179 | n/a | } |
|---|
| 5180 | n/a | return result; |
|---|
| 5181 | n/a | } |
|---|
| 5182 | n/a | |
|---|
| 5183 | n/a | static int |
|---|
| 5184 | n/a | load_obj(UnpicklerObject *self) |
|---|
| 5185 | n/a | { |
|---|
| 5186 | n/a | PyObject *cls, *args, *obj = NULL; |
|---|
| 5187 | n/a | Py_ssize_t i; |
|---|
| 5188 | n/a | |
|---|
| 5189 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5190 | n/a | return -1; |
|---|
| 5191 | n/a | |
|---|
| 5192 | n/a | if (Py_SIZE(self->stack) - i < 1) |
|---|
| 5193 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5194 | n/a | |
|---|
| 5195 | n/a | args = Pdata_poptuple(self->stack, i + 1); |
|---|
| 5196 | n/a | if (args == NULL) |
|---|
| 5197 | n/a | return -1; |
|---|
| 5198 | n/a | |
|---|
| 5199 | n/a | PDATA_POP(self->stack, cls); |
|---|
| 5200 | n/a | if (cls) { |
|---|
| 5201 | n/a | obj = instantiate(cls, args); |
|---|
| 5202 | n/a | Py_DECREF(cls); |
|---|
| 5203 | n/a | } |
|---|
| 5204 | n/a | Py_DECREF(args); |
|---|
| 5205 | n/a | if (obj == NULL) |
|---|
| 5206 | n/a | return -1; |
|---|
| 5207 | n/a | |
|---|
| 5208 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 5209 | n/a | return 0; |
|---|
| 5210 | n/a | } |
|---|
| 5211 | n/a | |
|---|
| 5212 | n/a | static int |
|---|
| 5213 | n/a | load_inst(UnpicklerObject *self) |
|---|
| 5214 | n/a | { |
|---|
| 5215 | n/a | PyObject *cls = NULL; |
|---|
| 5216 | n/a | PyObject *args = NULL; |
|---|
| 5217 | n/a | PyObject *obj = NULL; |
|---|
| 5218 | n/a | PyObject *module_name; |
|---|
| 5219 | n/a | PyObject *class_name; |
|---|
| 5220 | n/a | Py_ssize_t len; |
|---|
| 5221 | n/a | Py_ssize_t i; |
|---|
| 5222 | n/a | char *s; |
|---|
| 5223 | n/a | |
|---|
| 5224 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5225 | n/a | return -1; |
|---|
| 5226 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 5227 | n/a | return -1; |
|---|
| 5228 | n/a | if (len < 2) |
|---|
| 5229 | n/a | return bad_readline(); |
|---|
| 5230 | n/a | |
|---|
| 5231 | n/a | /* Here it is safe to use PyUnicode_DecodeASCII(), even though non-ASCII |
|---|
| 5232 | n/a | identifiers are permitted in Python 3.0, since the INST opcode is only |
|---|
| 5233 | n/a | supported by older protocols on Python 2.x. */ |
|---|
| 5234 | n/a | module_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
|---|
| 5235 | n/a | if (module_name == NULL) |
|---|
| 5236 | n/a | return -1; |
|---|
| 5237 | n/a | |
|---|
| 5238 | n/a | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
|---|
| 5239 | n/a | if (len < 2) { |
|---|
| 5240 | n/a | Py_DECREF(module_name); |
|---|
| 5241 | n/a | return bad_readline(); |
|---|
| 5242 | n/a | } |
|---|
| 5243 | n/a | class_name = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
|---|
| 5244 | n/a | if (class_name != NULL) { |
|---|
| 5245 | n/a | cls = find_class(self, module_name, class_name); |
|---|
| 5246 | n/a | Py_DECREF(class_name); |
|---|
| 5247 | n/a | } |
|---|
| 5248 | n/a | } |
|---|
| 5249 | n/a | Py_DECREF(module_name); |
|---|
| 5250 | n/a | |
|---|
| 5251 | n/a | if (cls == NULL) |
|---|
| 5252 | n/a | return -1; |
|---|
| 5253 | n/a | |
|---|
| 5254 | n/a | if ((args = Pdata_poptuple(self->stack, i)) != NULL) { |
|---|
| 5255 | n/a | obj = instantiate(cls, args); |
|---|
| 5256 | n/a | Py_DECREF(args); |
|---|
| 5257 | n/a | } |
|---|
| 5258 | n/a | Py_DECREF(cls); |
|---|
| 5259 | n/a | |
|---|
| 5260 | n/a | if (obj == NULL) |
|---|
| 5261 | n/a | return -1; |
|---|
| 5262 | n/a | |
|---|
| 5263 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 5264 | n/a | return 0; |
|---|
| 5265 | n/a | } |
|---|
| 5266 | n/a | |
|---|
| 5267 | n/a | static int |
|---|
| 5268 | n/a | load_newobj(UnpicklerObject *self) |
|---|
| 5269 | n/a | { |
|---|
| 5270 | n/a | PyObject *args = NULL; |
|---|
| 5271 | n/a | PyObject *clsraw = NULL; |
|---|
| 5272 | n/a | PyTypeObject *cls; /* clsraw cast to its true type */ |
|---|
| 5273 | n/a | PyObject *obj; |
|---|
| 5274 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5275 | n/a | |
|---|
| 5276 | n/a | /* Stack is ... cls argtuple, and we want to call |
|---|
| 5277 | n/a | * cls.__new__(cls, *argtuple). |
|---|
| 5278 | n/a | */ |
|---|
| 5279 | n/a | PDATA_POP(self->stack, args); |
|---|
| 5280 | n/a | if (args == NULL) |
|---|
| 5281 | n/a | goto error; |
|---|
| 5282 | n/a | if (!PyTuple_Check(args)) { |
|---|
| 5283 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 5284 | n/a | "NEWOBJ expected an arg " "tuple."); |
|---|
| 5285 | n/a | goto error; |
|---|
| 5286 | n/a | } |
|---|
| 5287 | n/a | |
|---|
| 5288 | n/a | PDATA_POP(self->stack, clsraw); |
|---|
| 5289 | n/a | cls = (PyTypeObject *)clsraw; |
|---|
| 5290 | n/a | if (cls == NULL) |
|---|
| 5291 | n/a | goto error; |
|---|
| 5292 | n/a | if (!PyType_Check(cls)) { |
|---|
| 5293 | n/a | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
|---|
| 5294 | n/a | "isn't a type object"); |
|---|
| 5295 | n/a | goto error; |
|---|
| 5296 | n/a | } |
|---|
| 5297 | n/a | if (cls->tp_new == NULL) { |
|---|
| 5298 | n/a | PyErr_SetString(st->UnpicklingError, "NEWOBJ class argument " |
|---|
| 5299 | n/a | "has NULL tp_new"); |
|---|
| 5300 | n/a | goto error; |
|---|
| 5301 | n/a | } |
|---|
| 5302 | n/a | |
|---|
| 5303 | n/a | /* Call __new__. */ |
|---|
| 5304 | n/a | obj = cls->tp_new(cls, args, NULL); |
|---|
| 5305 | n/a | if (obj == NULL) |
|---|
| 5306 | n/a | goto error; |
|---|
| 5307 | n/a | |
|---|
| 5308 | n/a | Py_DECREF(args); |
|---|
| 5309 | n/a | Py_DECREF(clsraw); |
|---|
| 5310 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 5311 | n/a | return 0; |
|---|
| 5312 | n/a | |
|---|
| 5313 | n/a | error: |
|---|
| 5314 | n/a | Py_XDECREF(args); |
|---|
| 5315 | n/a | Py_XDECREF(clsraw); |
|---|
| 5316 | n/a | return -1; |
|---|
| 5317 | n/a | } |
|---|
| 5318 | n/a | |
|---|
| 5319 | n/a | static int |
|---|
| 5320 | n/a | load_newobj_ex(UnpicklerObject *self) |
|---|
| 5321 | n/a | { |
|---|
| 5322 | n/a | PyObject *cls, *args, *kwargs; |
|---|
| 5323 | n/a | PyObject *obj; |
|---|
| 5324 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5325 | n/a | |
|---|
| 5326 | n/a | PDATA_POP(self->stack, kwargs); |
|---|
| 5327 | n/a | if (kwargs == NULL) { |
|---|
| 5328 | n/a | return -1; |
|---|
| 5329 | n/a | } |
|---|
| 5330 | n/a | PDATA_POP(self->stack, args); |
|---|
| 5331 | n/a | if (args == NULL) { |
|---|
| 5332 | n/a | Py_DECREF(kwargs); |
|---|
| 5333 | n/a | return -1; |
|---|
| 5334 | n/a | } |
|---|
| 5335 | n/a | PDATA_POP(self->stack, cls); |
|---|
| 5336 | n/a | if (cls == NULL) { |
|---|
| 5337 | n/a | Py_DECREF(kwargs); |
|---|
| 5338 | n/a | Py_DECREF(args); |
|---|
| 5339 | n/a | return -1; |
|---|
| 5340 | n/a | } |
|---|
| 5341 | n/a | |
|---|
| 5342 | n/a | if (!PyType_Check(cls)) { |
|---|
| 5343 | n/a | Py_DECREF(kwargs); |
|---|
| 5344 | n/a | Py_DECREF(args); |
|---|
| 5345 | n/a | PyErr_Format(st->UnpicklingError, |
|---|
| 5346 | n/a | "NEWOBJ_EX class argument must be a type, not %.200s", |
|---|
| 5347 | n/a | Py_TYPE(cls)->tp_name); |
|---|
| 5348 | n/a | Py_DECREF(cls); |
|---|
| 5349 | n/a | return -1; |
|---|
| 5350 | n/a | } |
|---|
| 5351 | n/a | |
|---|
| 5352 | n/a | if (((PyTypeObject *)cls)->tp_new == NULL) { |
|---|
| 5353 | n/a | Py_DECREF(kwargs); |
|---|
| 5354 | n/a | Py_DECREF(args); |
|---|
| 5355 | n/a | Py_DECREF(cls); |
|---|
| 5356 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 5357 | n/a | "NEWOBJ_EX class argument doesn't have __new__"); |
|---|
| 5358 | n/a | return -1; |
|---|
| 5359 | n/a | } |
|---|
| 5360 | n/a | obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs); |
|---|
| 5361 | n/a | Py_DECREF(kwargs); |
|---|
| 5362 | n/a | Py_DECREF(args); |
|---|
| 5363 | n/a | Py_DECREF(cls); |
|---|
| 5364 | n/a | if (obj == NULL) { |
|---|
| 5365 | n/a | return -1; |
|---|
| 5366 | n/a | } |
|---|
| 5367 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 5368 | n/a | return 0; |
|---|
| 5369 | n/a | } |
|---|
| 5370 | n/a | |
|---|
| 5371 | n/a | static int |
|---|
| 5372 | n/a | load_global(UnpicklerObject *self) |
|---|
| 5373 | n/a | { |
|---|
| 5374 | n/a | PyObject *global = NULL; |
|---|
| 5375 | n/a | PyObject *module_name; |
|---|
| 5376 | n/a | PyObject *global_name; |
|---|
| 5377 | n/a | Py_ssize_t len; |
|---|
| 5378 | n/a | char *s; |
|---|
| 5379 | n/a | |
|---|
| 5380 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 5381 | n/a | return -1; |
|---|
| 5382 | n/a | if (len < 2) |
|---|
| 5383 | n/a | return bad_readline(); |
|---|
| 5384 | n/a | module_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
|---|
| 5385 | n/a | if (!module_name) |
|---|
| 5386 | n/a | return -1; |
|---|
| 5387 | n/a | |
|---|
| 5388 | n/a | if ((len = _Unpickler_Readline(self, &s)) >= 0) { |
|---|
| 5389 | n/a | if (len < 2) { |
|---|
| 5390 | n/a | Py_DECREF(module_name); |
|---|
| 5391 | n/a | return bad_readline(); |
|---|
| 5392 | n/a | } |
|---|
| 5393 | n/a | global_name = PyUnicode_DecodeUTF8(s, len - 1, "strict"); |
|---|
| 5394 | n/a | if (global_name) { |
|---|
| 5395 | n/a | global = find_class(self, module_name, global_name); |
|---|
| 5396 | n/a | Py_DECREF(global_name); |
|---|
| 5397 | n/a | } |
|---|
| 5398 | n/a | } |
|---|
| 5399 | n/a | Py_DECREF(module_name); |
|---|
| 5400 | n/a | |
|---|
| 5401 | n/a | if (global == NULL) |
|---|
| 5402 | n/a | return -1; |
|---|
| 5403 | n/a | PDATA_PUSH(self->stack, global, -1); |
|---|
| 5404 | n/a | return 0; |
|---|
| 5405 | n/a | } |
|---|
| 5406 | n/a | |
|---|
| 5407 | n/a | static int |
|---|
| 5408 | n/a | load_stack_global(UnpicklerObject *self) |
|---|
| 5409 | n/a | { |
|---|
| 5410 | n/a | PyObject *global; |
|---|
| 5411 | n/a | PyObject *module_name; |
|---|
| 5412 | n/a | PyObject *global_name; |
|---|
| 5413 | n/a | |
|---|
| 5414 | n/a | PDATA_POP(self->stack, global_name); |
|---|
| 5415 | n/a | PDATA_POP(self->stack, module_name); |
|---|
| 5416 | n/a | if (module_name == NULL || !PyUnicode_CheckExact(module_name) || |
|---|
| 5417 | n/a | global_name == NULL || !PyUnicode_CheckExact(global_name)) { |
|---|
| 5418 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5419 | n/a | PyErr_SetString(st->UnpicklingError, "STACK_GLOBAL requires str"); |
|---|
| 5420 | n/a | Py_XDECREF(global_name); |
|---|
| 5421 | n/a | Py_XDECREF(module_name); |
|---|
| 5422 | n/a | return -1; |
|---|
| 5423 | n/a | } |
|---|
| 5424 | n/a | global = find_class(self, module_name, global_name); |
|---|
| 5425 | n/a | Py_DECREF(global_name); |
|---|
| 5426 | n/a | Py_DECREF(module_name); |
|---|
| 5427 | n/a | if (global == NULL) |
|---|
| 5428 | n/a | return -1; |
|---|
| 5429 | n/a | PDATA_PUSH(self->stack, global, -1); |
|---|
| 5430 | n/a | return 0; |
|---|
| 5431 | n/a | } |
|---|
| 5432 | n/a | |
|---|
| 5433 | n/a | static int |
|---|
| 5434 | n/a | load_persid(UnpicklerObject *self) |
|---|
| 5435 | n/a | { |
|---|
| 5436 | n/a | PyObject *pid; |
|---|
| 5437 | n/a | Py_ssize_t len; |
|---|
| 5438 | n/a | char *s; |
|---|
| 5439 | n/a | |
|---|
| 5440 | n/a | if (self->pers_func) { |
|---|
| 5441 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 5442 | n/a | return -1; |
|---|
| 5443 | n/a | if (len < 1) |
|---|
| 5444 | n/a | return bad_readline(); |
|---|
| 5445 | n/a | |
|---|
| 5446 | n/a | pid = PyUnicode_DecodeASCII(s, len - 1, "strict"); |
|---|
| 5447 | n/a | if (pid == NULL) { |
|---|
| 5448 | n/a | if (PyErr_ExceptionMatches(PyExc_UnicodeDecodeError)) { |
|---|
| 5449 | n/a | PyErr_SetString(_Pickle_GetGlobalState()->UnpicklingError, |
|---|
| 5450 | n/a | "persistent IDs in protocol 0 must be " |
|---|
| 5451 | n/a | "ASCII strings"); |
|---|
| 5452 | n/a | } |
|---|
| 5453 | n/a | return -1; |
|---|
| 5454 | n/a | } |
|---|
| 5455 | n/a | |
|---|
| 5456 | n/a | /* This does not leak since _Pickle_FastCall() steals the reference |
|---|
| 5457 | n/a | to pid first. */ |
|---|
| 5458 | n/a | pid = _Pickle_FastCall(self->pers_func, pid); |
|---|
| 5459 | n/a | if (pid == NULL) |
|---|
| 5460 | n/a | return -1; |
|---|
| 5461 | n/a | |
|---|
| 5462 | n/a | PDATA_PUSH(self->stack, pid, -1); |
|---|
| 5463 | n/a | return 0; |
|---|
| 5464 | n/a | } |
|---|
| 5465 | n/a | else { |
|---|
| 5466 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5467 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 5468 | n/a | "A load persistent id instruction was encountered,\n" |
|---|
| 5469 | n/a | "but no persistent_load function was specified."); |
|---|
| 5470 | n/a | return -1; |
|---|
| 5471 | n/a | } |
|---|
| 5472 | n/a | } |
|---|
| 5473 | n/a | |
|---|
| 5474 | n/a | static int |
|---|
| 5475 | n/a | load_binpersid(UnpicklerObject *self) |
|---|
| 5476 | n/a | { |
|---|
| 5477 | n/a | PyObject *pid; |
|---|
| 5478 | n/a | |
|---|
| 5479 | n/a | if (self->pers_func) { |
|---|
| 5480 | n/a | PDATA_POP(self->stack, pid); |
|---|
| 5481 | n/a | if (pid == NULL) |
|---|
| 5482 | n/a | return -1; |
|---|
| 5483 | n/a | |
|---|
| 5484 | n/a | /* This does not leak since _Pickle_FastCall() steals the |
|---|
| 5485 | n/a | reference to pid first. */ |
|---|
| 5486 | n/a | pid = _Pickle_FastCall(self->pers_func, pid); |
|---|
| 5487 | n/a | if (pid == NULL) |
|---|
| 5488 | n/a | return -1; |
|---|
| 5489 | n/a | |
|---|
| 5490 | n/a | PDATA_PUSH(self->stack, pid, -1); |
|---|
| 5491 | n/a | return 0; |
|---|
| 5492 | n/a | } |
|---|
| 5493 | n/a | else { |
|---|
| 5494 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5495 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 5496 | n/a | "A load persistent id instruction was encountered,\n" |
|---|
| 5497 | n/a | "but no persistent_load function was specified."); |
|---|
| 5498 | n/a | return -1; |
|---|
| 5499 | n/a | } |
|---|
| 5500 | n/a | } |
|---|
| 5501 | n/a | |
|---|
| 5502 | n/a | static int |
|---|
| 5503 | n/a | load_pop(UnpicklerObject *self) |
|---|
| 5504 | n/a | { |
|---|
| 5505 | n/a | Py_ssize_t len = Py_SIZE(self->stack); |
|---|
| 5506 | n/a | |
|---|
| 5507 | n/a | /* Note that we split the (pickle.py) stack into two stacks, |
|---|
| 5508 | n/a | * an object stack and a mark stack. We have to be clever and |
|---|
| 5509 | n/a | * pop the right one. We do this by looking at the top of the |
|---|
| 5510 | n/a | * mark stack first, and only signalling a stack underflow if |
|---|
| 5511 | n/a | * the object stack is empty and the mark stack doesn't match |
|---|
| 5512 | n/a | * our expectations. |
|---|
| 5513 | n/a | */ |
|---|
| 5514 | n/a | if (self->num_marks > 0 && self->marks[self->num_marks - 1] == len) { |
|---|
| 5515 | n/a | self->num_marks--; |
|---|
| 5516 | n/a | self->stack->mark_set = self->num_marks != 0; |
|---|
| 5517 | n/a | self->stack->fence = self->num_marks ? |
|---|
| 5518 | n/a | self->marks[self->num_marks - 1] : 0; |
|---|
| 5519 | n/a | } else if (len <= self->stack->fence) |
|---|
| 5520 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5521 | n/a | else { |
|---|
| 5522 | n/a | len--; |
|---|
| 5523 | n/a | Py_DECREF(self->stack->data[len]); |
|---|
| 5524 | n/a | Py_SIZE(self->stack) = len; |
|---|
| 5525 | n/a | } |
|---|
| 5526 | n/a | return 0; |
|---|
| 5527 | n/a | } |
|---|
| 5528 | n/a | |
|---|
| 5529 | n/a | static int |
|---|
| 5530 | n/a | load_pop_mark(UnpicklerObject *self) |
|---|
| 5531 | n/a | { |
|---|
| 5532 | n/a | Py_ssize_t i; |
|---|
| 5533 | n/a | |
|---|
| 5534 | n/a | if ((i = marker(self)) < 0) |
|---|
| 5535 | n/a | return -1; |
|---|
| 5536 | n/a | |
|---|
| 5537 | n/a | Pdata_clear(self->stack, i); |
|---|
| 5538 | n/a | |
|---|
| 5539 | n/a | return 0; |
|---|
| 5540 | n/a | } |
|---|
| 5541 | n/a | |
|---|
| 5542 | n/a | static int |
|---|
| 5543 | n/a | load_dup(UnpicklerObject *self) |
|---|
| 5544 | n/a | { |
|---|
| 5545 | n/a | PyObject *last; |
|---|
| 5546 | n/a | Py_ssize_t len = Py_SIZE(self->stack); |
|---|
| 5547 | n/a | |
|---|
| 5548 | n/a | if (len <= self->stack->fence) |
|---|
| 5549 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5550 | n/a | last = self->stack->data[len - 1]; |
|---|
| 5551 | n/a | PDATA_APPEND(self->stack, last, -1); |
|---|
| 5552 | n/a | return 0; |
|---|
| 5553 | n/a | } |
|---|
| 5554 | n/a | |
|---|
| 5555 | n/a | static int |
|---|
| 5556 | n/a | load_get(UnpicklerObject *self) |
|---|
| 5557 | n/a | { |
|---|
| 5558 | n/a | PyObject *key, *value; |
|---|
| 5559 | n/a | Py_ssize_t idx; |
|---|
| 5560 | n/a | Py_ssize_t len; |
|---|
| 5561 | n/a | char *s; |
|---|
| 5562 | n/a | |
|---|
| 5563 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 5564 | n/a | return -1; |
|---|
| 5565 | n/a | if (len < 2) |
|---|
| 5566 | n/a | return bad_readline(); |
|---|
| 5567 | n/a | |
|---|
| 5568 | n/a | key = PyLong_FromString(s, NULL, 10); |
|---|
| 5569 | n/a | if (key == NULL) |
|---|
| 5570 | n/a | return -1; |
|---|
| 5571 | n/a | idx = PyLong_AsSsize_t(key); |
|---|
| 5572 | n/a | if (idx == -1 && PyErr_Occurred()) { |
|---|
| 5573 | n/a | Py_DECREF(key); |
|---|
| 5574 | n/a | return -1; |
|---|
| 5575 | n/a | } |
|---|
| 5576 | n/a | |
|---|
| 5577 | n/a | value = _Unpickler_MemoGet(self, idx); |
|---|
| 5578 | n/a | if (value == NULL) { |
|---|
| 5579 | n/a | if (!PyErr_Occurred()) |
|---|
| 5580 | n/a | PyErr_SetObject(PyExc_KeyError, key); |
|---|
| 5581 | n/a | Py_DECREF(key); |
|---|
| 5582 | n/a | return -1; |
|---|
| 5583 | n/a | } |
|---|
| 5584 | n/a | Py_DECREF(key); |
|---|
| 5585 | n/a | |
|---|
| 5586 | n/a | PDATA_APPEND(self->stack, value, -1); |
|---|
| 5587 | n/a | return 0; |
|---|
| 5588 | n/a | } |
|---|
| 5589 | n/a | |
|---|
| 5590 | n/a | static int |
|---|
| 5591 | n/a | load_binget(UnpicklerObject *self) |
|---|
| 5592 | n/a | { |
|---|
| 5593 | n/a | PyObject *value; |
|---|
| 5594 | n/a | Py_ssize_t idx; |
|---|
| 5595 | n/a | char *s; |
|---|
| 5596 | n/a | |
|---|
| 5597 | n/a | if (_Unpickler_Read(self, &s, 1) < 0) |
|---|
| 5598 | n/a | return -1; |
|---|
| 5599 | n/a | |
|---|
| 5600 | n/a | idx = Py_CHARMASK(s[0]); |
|---|
| 5601 | n/a | |
|---|
| 5602 | n/a | value = _Unpickler_MemoGet(self, idx); |
|---|
| 5603 | n/a | if (value == NULL) { |
|---|
| 5604 | n/a | PyObject *key = PyLong_FromSsize_t(idx); |
|---|
| 5605 | n/a | if (key != NULL) { |
|---|
| 5606 | n/a | PyErr_SetObject(PyExc_KeyError, key); |
|---|
| 5607 | n/a | Py_DECREF(key); |
|---|
| 5608 | n/a | } |
|---|
| 5609 | n/a | return -1; |
|---|
| 5610 | n/a | } |
|---|
| 5611 | n/a | |
|---|
| 5612 | n/a | PDATA_APPEND(self->stack, value, -1); |
|---|
| 5613 | n/a | return 0; |
|---|
| 5614 | n/a | } |
|---|
| 5615 | n/a | |
|---|
| 5616 | n/a | static int |
|---|
| 5617 | n/a | load_long_binget(UnpicklerObject *self) |
|---|
| 5618 | n/a | { |
|---|
| 5619 | n/a | PyObject *value; |
|---|
| 5620 | n/a | Py_ssize_t idx; |
|---|
| 5621 | n/a | char *s; |
|---|
| 5622 | n/a | |
|---|
| 5623 | n/a | if (_Unpickler_Read(self, &s, 4) < 0) |
|---|
| 5624 | n/a | return -1; |
|---|
| 5625 | n/a | |
|---|
| 5626 | n/a | idx = calc_binsize(s, 4); |
|---|
| 5627 | n/a | |
|---|
| 5628 | n/a | value = _Unpickler_MemoGet(self, idx); |
|---|
| 5629 | n/a | if (value == NULL) { |
|---|
| 5630 | n/a | PyObject *key = PyLong_FromSsize_t(idx); |
|---|
| 5631 | n/a | if (key != NULL) { |
|---|
| 5632 | n/a | PyErr_SetObject(PyExc_KeyError, key); |
|---|
| 5633 | n/a | Py_DECREF(key); |
|---|
| 5634 | n/a | } |
|---|
| 5635 | n/a | return -1; |
|---|
| 5636 | n/a | } |
|---|
| 5637 | n/a | |
|---|
| 5638 | n/a | PDATA_APPEND(self->stack, value, -1); |
|---|
| 5639 | n/a | return 0; |
|---|
| 5640 | n/a | } |
|---|
| 5641 | n/a | |
|---|
| 5642 | n/a | /* Push an object from the extension registry (EXT[124]). nbytes is |
|---|
| 5643 | n/a | * the number of bytes following the opcode, holding the index (code) value. |
|---|
| 5644 | n/a | */ |
|---|
| 5645 | n/a | static int |
|---|
| 5646 | n/a | load_extension(UnpicklerObject *self, int nbytes) |
|---|
| 5647 | n/a | { |
|---|
| 5648 | n/a | char *codebytes; /* the nbytes bytes after the opcode */ |
|---|
| 5649 | n/a | long code; /* calc_binint returns long */ |
|---|
| 5650 | n/a | PyObject *py_code; /* code as a Python int */ |
|---|
| 5651 | n/a | PyObject *obj; /* the object to push */ |
|---|
| 5652 | n/a | PyObject *pair; /* (module_name, class_name) */ |
|---|
| 5653 | n/a | PyObject *module_name, *class_name; |
|---|
| 5654 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5655 | n/a | |
|---|
| 5656 | n/a | assert(nbytes == 1 || nbytes == 2 || nbytes == 4); |
|---|
| 5657 | n/a | if (_Unpickler_Read(self, &codebytes, nbytes) < 0) |
|---|
| 5658 | n/a | return -1; |
|---|
| 5659 | n/a | code = calc_binint(codebytes, nbytes); |
|---|
| 5660 | n/a | if (code <= 0) { /* note that 0 is forbidden */ |
|---|
| 5661 | n/a | /* Corrupt or hostile pickle. */ |
|---|
| 5662 | n/a | PyErr_SetString(st->UnpicklingError, "EXT specifies code <= 0"); |
|---|
| 5663 | n/a | return -1; |
|---|
| 5664 | n/a | } |
|---|
| 5665 | n/a | |
|---|
| 5666 | n/a | /* Look for the code in the cache. */ |
|---|
| 5667 | n/a | py_code = PyLong_FromLong(code); |
|---|
| 5668 | n/a | if (py_code == NULL) |
|---|
| 5669 | n/a | return -1; |
|---|
| 5670 | n/a | obj = PyDict_GetItemWithError(st->extension_cache, py_code); |
|---|
| 5671 | n/a | if (obj != NULL) { |
|---|
| 5672 | n/a | /* Bingo. */ |
|---|
| 5673 | n/a | Py_DECREF(py_code); |
|---|
| 5674 | n/a | PDATA_APPEND(self->stack, obj, -1); |
|---|
| 5675 | n/a | return 0; |
|---|
| 5676 | n/a | } |
|---|
| 5677 | n/a | if (PyErr_Occurred()) { |
|---|
| 5678 | n/a | Py_DECREF(py_code); |
|---|
| 5679 | n/a | return -1; |
|---|
| 5680 | n/a | } |
|---|
| 5681 | n/a | |
|---|
| 5682 | n/a | /* Look up the (module_name, class_name) pair. */ |
|---|
| 5683 | n/a | pair = PyDict_GetItemWithError(st->inverted_registry, py_code); |
|---|
| 5684 | n/a | if (pair == NULL) { |
|---|
| 5685 | n/a | Py_DECREF(py_code); |
|---|
| 5686 | n/a | if (!PyErr_Occurred()) { |
|---|
| 5687 | n/a | PyErr_Format(PyExc_ValueError, "unregistered extension " |
|---|
| 5688 | n/a | "code %ld", code); |
|---|
| 5689 | n/a | } |
|---|
| 5690 | n/a | return -1; |
|---|
| 5691 | n/a | } |
|---|
| 5692 | n/a | /* Since the extension registry is manipulable via Python code, |
|---|
| 5693 | n/a | * confirm that pair is really a 2-tuple of strings. |
|---|
| 5694 | n/a | */ |
|---|
| 5695 | n/a | if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2 || |
|---|
| 5696 | n/a | !PyUnicode_Check(module_name = PyTuple_GET_ITEM(pair, 0)) || |
|---|
| 5697 | n/a | !PyUnicode_Check(class_name = PyTuple_GET_ITEM(pair, 1))) { |
|---|
| 5698 | n/a | Py_DECREF(py_code); |
|---|
| 5699 | n/a | PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] " |
|---|
| 5700 | n/a | "isn't a 2-tuple of strings", code); |
|---|
| 5701 | n/a | return -1; |
|---|
| 5702 | n/a | } |
|---|
| 5703 | n/a | /* Load the object. */ |
|---|
| 5704 | n/a | obj = find_class(self, module_name, class_name); |
|---|
| 5705 | n/a | if (obj == NULL) { |
|---|
| 5706 | n/a | Py_DECREF(py_code); |
|---|
| 5707 | n/a | return -1; |
|---|
| 5708 | n/a | } |
|---|
| 5709 | n/a | /* Cache code -> obj. */ |
|---|
| 5710 | n/a | code = PyDict_SetItem(st->extension_cache, py_code, obj); |
|---|
| 5711 | n/a | Py_DECREF(py_code); |
|---|
| 5712 | n/a | if (code < 0) { |
|---|
| 5713 | n/a | Py_DECREF(obj); |
|---|
| 5714 | n/a | return -1; |
|---|
| 5715 | n/a | } |
|---|
| 5716 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 5717 | n/a | return 0; |
|---|
| 5718 | n/a | } |
|---|
| 5719 | n/a | |
|---|
| 5720 | n/a | static int |
|---|
| 5721 | n/a | load_put(UnpicklerObject *self) |
|---|
| 5722 | n/a | { |
|---|
| 5723 | n/a | PyObject *key, *value; |
|---|
| 5724 | n/a | Py_ssize_t idx; |
|---|
| 5725 | n/a | Py_ssize_t len; |
|---|
| 5726 | n/a | char *s = NULL; |
|---|
| 5727 | n/a | |
|---|
| 5728 | n/a | if ((len = _Unpickler_Readline(self, &s)) < 0) |
|---|
| 5729 | n/a | return -1; |
|---|
| 5730 | n/a | if (len < 2) |
|---|
| 5731 | n/a | return bad_readline(); |
|---|
| 5732 | n/a | if (Py_SIZE(self->stack) <= self->stack->fence) |
|---|
| 5733 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5734 | n/a | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
|---|
| 5735 | n/a | |
|---|
| 5736 | n/a | key = PyLong_FromString(s, NULL, 10); |
|---|
| 5737 | n/a | if (key == NULL) |
|---|
| 5738 | n/a | return -1; |
|---|
| 5739 | n/a | idx = PyLong_AsSsize_t(key); |
|---|
| 5740 | n/a | Py_DECREF(key); |
|---|
| 5741 | n/a | if (idx < 0) { |
|---|
| 5742 | n/a | if (!PyErr_Occurred()) |
|---|
| 5743 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 5744 | n/a | "negative PUT argument"); |
|---|
| 5745 | n/a | return -1; |
|---|
| 5746 | n/a | } |
|---|
| 5747 | n/a | |
|---|
| 5748 | n/a | return _Unpickler_MemoPut(self, idx, value); |
|---|
| 5749 | n/a | } |
|---|
| 5750 | n/a | |
|---|
| 5751 | n/a | static int |
|---|
| 5752 | n/a | load_binput(UnpicklerObject *self) |
|---|
| 5753 | n/a | { |
|---|
| 5754 | n/a | PyObject *value; |
|---|
| 5755 | n/a | Py_ssize_t idx; |
|---|
| 5756 | n/a | char *s; |
|---|
| 5757 | n/a | |
|---|
| 5758 | n/a | if (_Unpickler_Read(self, &s, 1) < 0) |
|---|
| 5759 | n/a | return -1; |
|---|
| 5760 | n/a | |
|---|
| 5761 | n/a | if (Py_SIZE(self->stack) <= self->stack->fence) |
|---|
| 5762 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5763 | n/a | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
|---|
| 5764 | n/a | |
|---|
| 5765 | n/a | idx = Py_CHARMASK(s[0]); |
|---|
| 5766 | n/a | |
|---|
| 5767 | n/a | return _Unpickler_MemoPut(self, idx, value); |
|---|
| 5768 | n/a | } |
|---|
| 5769 | n/a | |
|---|
| 5770 | n/a | static int |
|---|
| 5771 | n/a | load_long_binput(UnpicklerObject *self) |
|---|
| 5772 | n/a | { |
|---|
| 5773 | n/a | PyObject *value; |
|---|
| 5774 | n/a | Py_ssize_t idx; |
|---|
| 5775 | n/a | char *s; |
|---|
| 5776 | n/a | |
|---|
| 5777 | n/a | if (_Unpickler_Read(self, &s, 4) < 0) |
|---|
| 5778 | n/a | return -1; |
|---|
| 5779 | n/a | |
|---|
| 5780 | n/a | if (Py_SIZE(self->stack) <= self->stack->fence) |
|---|
| 5781 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5782 | n/a | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
|---|
| 5783 | n/a | |
|---|
| 5784 | n/a | idx = calc_binsize(s, 4); |
|---|
| 5785 | n/a | if (idx < 0) { |
|---|
| 5786 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 5787 | n/a | "negative LONG_BINPUT argument"); |
|---|
| 5788 | n/a | return -1; |
|---|
| 5789 | n/a | } |
|---|
| 5790 | n/a | |
|---|
| 5791 | n/a | return _Unpickler_MemoPut(self, idx, value); |
|---|
| 5792 | n/a | } |
|---|
| 5793 | n/a | |
|---|
| 5794 | n/a | static int |
|---|
| 5795 | n/a | load_memoize(UnpicklerObject *self) |
|---|
| 5796 | n/a | { |
|---|
| 5797 | n/a | PyObject *value; |
|---|
| 5798 | n/a | |
|---|
| 5799 | n/a | if (Py_SIZE(self->stack) <= self->stack->fence) |
|---|
| 5800 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5801 | n/a | value = self->stack->data[Py_SIZE(self->stack) - 1]; |
|---|
| 5802 | n/a | |
|---|
| 5803 | n/a | return _Unpickler_MemoPut(self, self->memo_len, value); |
|---|
| 5804 | n/a | } |
|---|
| 5805 | n/a | |
|---|
| 5806 | n/a | static int |
|---|
| 5807 | n/a | do_append(UnpicklerObject *self, Py_ssize_t x) |
|---|
| 5808 | n/a | { |
|---|
| 5809 | n/a | PyObject *value; |
|---|
| 5810 | n/a | PyObject *slice; |
|---|
| 5811 | n/a | PyObject *list; |
|---|
| 5812 | n/a | PyObject *result; |
|---|
| 5813 | n/a | Py_ssize_t len, i; |
|---|
| 5814 | n/a | |
|---|
| 5815 | n/a | len = Py_SIZE(self->stack); |
|---|
| 5816 | n/a | if (x > len || x <= self->stack->fence) |
|---|
| 5817 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5818 | n/a | if (len == x) /* nothing to do */ |
|---|
| 5819 | n/a | return 0; |
|---|
| 5820 | n/a | |
|---|
| 5821 | n/a | list = self->stack->data[x - 1]; |
|---|
| 5822 | n/a | |
|---|
| 5823 | n/a | if (PyList_CheckExact(list)) { |
|---|
| 5824 | n/a | Py_ssize_t list_len; |
|---|
| 5825 | n/a | int ret; |
|---|
| 5826 | n/a | |
|---|
| 5827 | n/a | slice = Pdata_poplist(self->stack, x); |
|---|
| 5828 | n/a | if (!slice) |
|---|
| 5829 | n/a | return -1; |
|---|
| 5830 | n/a | list_len = PyList_GET_SIZE(list); |
|---|
| 5831 | n/a | ret = PyList_SetSlice(list, list_len, list_len, slice); |
|---|
| 5832 | n/a | Py_DECREF(slice); |
|---|
| 5833 | n/a | return ret; |
|---|
| 5834 | n/a | } |
|---|
| 5835 | n/a | else { |
|---|
| 5836 | n/a | PyObject *extend_func; |
|---|
| 5837 | n/a | _Py_IDENTIFIER(extend); |
|---|
| 5838 | n/a | |
|---|
| 5839 | n/a | extend_func = _PyObject_GetAttrId(list, &PyId_extend); |
|---|
| 5840 | n/a | if (extend_func != NULL) { |
|---|
| 5841 | n/a | slice = Pdata_poplist(self->stack, x); |
|---|
| 5842 | n/a | if (!slice) { |
|---|
| 5843 | n/a | Py_DECREF(extend_func); |
|---|
| 5844 | n/a | return -1; |
|---|
| 5845 | n/a | } |
|---|
| 5846 | n/a | result = _Pickle_FastCall(extend_func, slice); |
|---|
| 5847 | n/a | Py_DECREF(extend_func); |
|---|
| 5848 | n/a | if (result == NULL) |
|---|
| 5849 | n/a | return -1; |
|---|
| 5850 | n/a | Py_DECREF(result); |
|---|
| 5851 | n/a | } |
|---|
| 5852 | n/a | else { |
|---|
| 5853 | n/a | PyObject *append_func; |
|---|
| 5854 | n/a | _Py_IDENTIFIER(append); |
|---|
| 5855 | n/a | |
|---|
| 5856 | n/a | /* Even if the PEP 307 requires extend() and append() methods, |
|---|
| 5857 | n/a | fall back on append() if the object has no extend() method |
|---|
| 5858 | n/a | for backward compatibility. */ |
|---|
| 5859 | n/a | PyErr_Clear(); |
|---|
| 5860 | n/a | append_func = _PyObject_GetAttrId(list, &PyId_append); |
|---|
| 5861 | n/a | if (append_func == NULL) |
|---|
| 5862 | n/a | return -1; |
|---|
| 5863 | n/a | for (i = x; i < len; i++) { |
|---|
| 5864 | n/a | value = self->stack->data[i]; |
|---|
| 5865 | n/a | result = _Pickle_FastCall(append_func, value); |
|---|
| 5866 | n/a | if (result == NULL) { |
|---|
| 5867 | n/a | Pdata_clear(self->stack, i + 1); |
|---|
| 5868 | n/a | Py_SIZE(self->stack) = x; |
|---|
| 5869 | n/a | Py_DECREF(append_func); |
|---|
| 5870 | n/a | return -1; |
|---|
| 5871 | n/a | } |
|---|
| 5872 | n/a | Py_DECREF(result); |
|---|
| 5873 | n/a | } |
|---|
| 5874 | n/a | Py_SIZE(self->stack) = x; |
|---|
| 5875 | n/a | Py_DECREF(append_func); |
|---|
| 5876 | n/a | } |
|---|
| 5877 | n/a | } |
|---|
| 5878 | n/a | |
|---|
| 5879 | n/a | return 0; |
|---|
| 5880 | n/a | } |
|---|
| 5881 | n/a | |
|---|
| 5882 | n/a | static int |
|---|
| 5883 | n/a | load_append(UnpicklerObject *self) |
|---|
| 5884 | n/a | { |
|---|
| 5885 | n/a | if (Py_SIZE(self->stack) - 1 <= self->stack->fence) |
|---|
| 5886 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5887 | n/a | return do_append(self, Py_SIZE(self->stack) - 1); |
|---|
| 5888 | n/a | } |
|---|
| 5889 | n/a | |
|---|
| 5890 | n/a | static int |
|---|
| 5891 | n/a | load_appends(UnpicklerObject *self) |
|---|
| 5892 | n/a | { |
|---|
| 5893 | n/a | Py_ssize_t i = marker(self); |
|---|
| 5894 | n/a | if (i < 0) |
|---|
| 5895 | n/a | return -1; |
|---|
| 5896 | n/a | return do_append(self, i); |
|---|
| 5897 | n/a | } |
|---|
| 5898 | n/a | |
|---|
| 5899 | n/a | static int |
|---|
| 5900 | n/a | do_setitems(UnpicklerObject *self, Py_ssize_t x) |
|---|
| 5901 | n/a | { |
|---|
| 5902 | n/a | PyObject *value, *key; |
|---|
| 5903 | n/a | PyObject *dict; |
|---|
| 5904 | n/a | Py_ssize_t len, i; |
|---|
| 5905 | n/a | int status = 0; |
|---|
| 5906 | n/a | |
|---|
| 5907 | n/a | len = Py_SIZE(self->stack); |
|---|
| 5908 | n/a | if (x > len || x <= self->stack->fence) |
|---|
| 5909 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5910 | n/a | if (len == x) /* nothing to do */ |
|---|
| 5911 | n/a | return 0; |
|---|
| 5912 | n/a | if ((len - x) % 2 != 0) { |
|---|
| 5913 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 5914 | n/a | /* Currupt or hostile pickle -- we never write one like this. */ |
|---|
| 5915 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 5916 | n/a | "odd number of items for SETITEMS"); |
|---|
| 5917 | n/a | return -1; |
|---|
| 5918 | n/a | } |
|---|
| 5919 | n/a | |
|---|
| 5920 | n/a | /* Here, dict does not actually need to be a PyDict; it could be anything |
|---|
| 5921 | n/a | that supports the __setitem__ attribute. */ |
|---|
| 5922 | n/a | dict = self->stack->data[x - 1]; |
|---|
| 5923 | n/a | |
|---|
| 5924 | n/a | for (i = x + 1; i < len; i += 2) { |
|---|
| 5925 | n/a | key = self->stack->data[i - 1]; |
|---|
| 5926 | n/a | value = self->stack->data[i]; |
|---|
| 5927 | n/a | if (PyObject_SetItem(dict, key, value) < 0) { |
|---|
| 5928 | n/a | status = -1; |
|---|
| 5929 | n/a | break; |
|---|
| 5930 | n/a | } |
|---|
| 5931 | n/a | } |
|---|
| 5932 | n/a | |
|---|
| 5933 | n/a | Pdata_clear(self->stack, x); |
|---|
| 5934 | n/a | return status; |
|---|
| 5935 | n/a | } |
|---|
| 5936 | n/a | |
|---|
| 5937 | n/a | static int |
|---|
| 5938 | n/a | load_setitem(UnpicklerObject *self) |
|---|
| 5939 | n/a | { |
|---|
| 5940 | n/a | return do_setitems(self, Py_SIZE(self->stack) - 2); |
|---|
| 5941 | n/a | } |
|---|
| 5942 | n/a | |
|---|
| 5943 | n/a | static int |
|---|
| 5944 | n/a | load_setitems(UnpicklerObject *self) |
|---|
| 5945 | n/a | { |
|---|
| 5946 | n/a | Py_ssize_t i = marker(self); |
|---|
| 5947 | n/a | if (i < 0) |
|---|
| 5948 | n/a | return -1; |
|---|
| 5949 | n/a | return do_setitems(self, i); |
|---|
| 5950 | n/a | } |
|---|
| 5951 | n/a | |
|---|
| 5952 | n/a | static int |
|---|
| 5953 | n/a | load_additems(UnpicklerObject *self) |
|---|
| 5954 | n/a | { |
|---|
| 5955 | n/a | PyObject *set; |
|---|
| 5956 | n/a | Py_ssize_t mark, len, i; |
|---|
| 5957 | n/a | |
|---|
| 5958 | n/a | mark = marker(self); |
|---|
| 5959 | n/a | if (mark < 0) |
|---|
| 5960 | n/a | return -1; |
|---|
| 5961 | n/a | len = Py_SIZE(self->stack); |
|---|
| 5962 | n/a | if (mark > len || mark <= self->stack->fence) |
|---|
| 5963 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 5964 | n/a | if (len == mark) /* nothing to do */ |
|---|
| 5965 | n/a | return 0; |
|---|
| 5966 | n/a | |
|---|
| 5967 | n/a | set = self->stack->data[mark - 1]; |
|---|
| 5968 | n/a | |
|---|
| 5969 | n/a | if (PySet_Check(set)) { |
|---|
| 5970 | n/a | PyObject *items; |
|---|
| 5971 | n/a | int status; |
|---|
| 5972 | n/a | |
|---|
| 5973 | n/a | items = Pdata_poptuple(self->stack, mark); |
|---|
| 5974 | n/a | if (items == NULL) |
|---|
| 5975 | n/a | return -1; |
|---|
| 5976 | n/a | |
|---|
| 5977 | n/a | status = _PySet_Update(set, items); |
|---|
| 5978 | n/a | Py_DECREF(items); |
|---|
| 5979 | n/a | return status; |
|---|
| 5980 | n/a | } |
|---|
| 5981 | n/a | else { |
|---|
| 5982 | n/a | PyObject *add_func; |
|---|
| 5983 | n/a | _Py_IDENTIFIER(add); |
|---|
| 5984 | n/a | |
|---|
| 5985 | n/a | add_func = _PyObject_GetAttrId(set, &PyId_add); |
|---|
| 5986 | n/a | if (add_func == NULL) |
|---|
| 5987 | n/a | return -1; |
|---|
| 5988 | n/a | for (i = mark; i < len; i++) { |
|---|
| 5989 | n/a | PyObject *result; |
|---|
| 5990 | n/a | PyObject *item; |
|---|
| 5991 | n/a | |
|---|
| 5992 | n/a | item = self->stack->data[i]; |
|---|
| 5993 | n/a | result = _Pickle_FastCall(add_func, item); |
|---|
| 5994 | n/a | if (result == NULL) { |
|---|
| 5995 | n/a | Pdata_clear(self->stack, i + 1); |
|---|
| 5996 | n/a | Py_SIZE(self->stack) = mark; |
|---|
| 5997 | n/a | return -1; |
|---|
| 5998 | n/a | } |
|---|
| 5999 | n/a | Py_DECREF(result); |
|---|
| 6000 | n/a | } |
|---|
| 6001 | n/a | Py_SIZE(self->stack) = mark; |
|---|
| 6002 | n/a | } |
|---|
| 6003 | n/a | |
|---|
| 6004 | n/a | return 0; |
|---|
| 6005 | n/a | } |
|---|
| 6006 | n/a | |
|---|
| 6007 | n/a | static int |
|---|
| 6008 | n/a | load_build(UnpicklerObject *self) |
|---|
| 6009 | n/a | { |
|---|
| 6010 | n/a | PyObject *state, *inst, *slotstate; |
|---|
| 6011 | n/a | PyObject *setstate; |
|---|
| 6012 | n/a | int status = 0; |
|---|
| 6013 | n/a | _Py_IDENTIFIER(__setstate__); |
|---|
| 6014 | n/a | |
|---|
| 6015 | n/a | /* Stack is ... instance, state. We want to leave instance at |
|---|
| 6016 | n/a | * the stack top, possibly mutated via instance.__setstate__(state). |
|---|
| 6017 | n/a | */ |
|---|
| 6018 | n/a | if (Py_SIZE(self->stack) - 2 < self->stack->fence) |
|---|
| 6019 | n/a | return Pdata_stack_underflow(self->stack); |
|---|
| 6020 | n/a | |
|---|
| 6021 | n/a | PDATA_POP(self->stack, state); |
|---|
| 6022 | n/a | if (state == NULL) |
|---|
| 6023 | n/a | return -1; |
|---|
| 6024 | n/a | |
|---|
| 6025 | n/a | inst = self->stack->data[Py_SIZE(self->stack) - 1]; |
|---|
| 6026 | n/a | |
|---|
| 6027 | n/a | setstate = _PyObject_GetAttrId(inst, &PyId___setstate__); |
|---|
| 6028 | n/a | if (setstate == NULL) { |
|---|
| 6029 | n/a | if (PyErr_ExceptionMatches(PyExc_AttributeError)) |
|---|
| 6030 | n/a | PyErr_Clear(); |
|---|
| 6031 | n/a | else { |
|---|
| 6032 | n/a | Py_DECREF(state); |
|---|
| 6033 | n/a | return -1; |
|---|
| 6034 | n/a | } |
|---|
| 6035 | n/a | } |
|---|
| 6036 | n/a | else { |
|---|
| 6037 | n/a | PyObject *result; |
|---|
| 6038 | n/a | |
|---|
| 6039 | n/a | /* The explicit __setstate__ is responsible for everything. */ |
|---|
| 6040 | n/a | result = _Pickle_FastCall(setstate, state); |
|---|
| 6041 | n/a | Py_DECREF(setstate); |
|---|
| 6042 | n/a | if (result == NULL) |
|---|
| 6043 | n/a | return -1; |
|---|
| 6044 | n/a | Py_DECREF(result); |
|---|
| 6045 | n/a | return 0; |
|---|
| 6046 | n/a | } |
|---|
| 6047 | n/a | |
|---|
| 6048 | n/a | /* A default __setstate__. First see whether state embeds a |
|---|
| 6049 | n/a | * slot state dict too (a proto 2 addition). |
|---|
| 6050 | n/a | */ |
|---|
| 6051 | n/a | if (PyTuple_Check(state) && Py_SIZE(state) == 2) { |
|---|
| 6052 | n/a | PyObject *tmp = state; |
|---|
| 6053 | n/a | |
|---|
| 6054 | n/a | state = PyTuple_GET_ITEM(tmp, 0); |
|---|
| 6055 | n/a | slotstate = PyTuple_GET_ITEM(tmp, 1); |
|---|
| 6056 | n/a | Py_INCREF(state); |
|---|
| 6057 | n/a | Py_INCREF(slotstate); |
|---|
| 6058 | n/a | Py_DECREF(tmp); |
|---|
| 6059 | n/a | } |
|---|
| 6060 | n/a | else |
|---|
| 6061 | n/a | slotstate = NULL; |
|---|
| 6062 | n/a | |
|---|
| 6063 | n/a | /* Set inst.__dict__ from the state dict (if any). */ |
|---|
| 6064 | n/a | if (state != Py_None) { |
|---|
| 6065 | n/a | PyObject *dict; |
|---|
| 6066 | n/a | PyObject *d_key, *d_value; |
|---|
| 6067 | n/a | Py_ssize_t i; |
|---|
| 6068 | n/a | _Py_IDENTIFIER(__dict__); |
|---|
| 6069 | n/a | |
|---|
| 6070 | n/a | if (!PyDict_Check(state)) { |
|---|
| 6071 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 6072 | n/a | PyErr_SetString(st->UnpicklingError, "state is not a dictionary"); |
|---|
| 6073 | n/a | goto error; |
|---|
| 6074 | n/a | } |
|---|
| 6075 | n/a | dict = _PyObject_GetAttrId(inst, &PyId___dict__); |
|---|
| 6076 | n/a | if (dict == NULL) |
|---|
| 6077 | n/a | goto error; |
|---|
| 6078 | n/a | |
|---|
| 6079 | n/a | i = 0; |
|---|
| 6080 | n/a | while (PyDict_Next(state, &i, &d_key, &d_value)) { |
|---|
| 6081 | n/a | /* normally the keys for instance attributes are |
|---|
| 6082 | n/a | interned. we should try to do that here. */ |
|---|
| 6083 | n/a | Py_INCREF(d_key); |
|---|
| 6084 | n/a | if (PyUnicode_CheckExact(d_key)) |
|---|
| 6085 | n/a | PyUnicode_InternInPlace(&d_key); |
|---|
| 6086 | n/a | if (PyObject_SetItem(dict, d_key, d_value) < 0) { |
|---|
| 6087 | n/a | Py_DECREF(d_key); |
|---|
| 6088 | n/a | goto error; |
|---|
| 6089 | n/a | } |
|---|
| 6090 | n/a | Py_DECREF(d_key); |
|---|
| 6091 | n/a | } |
|---|
| 6092 | n/a | Py_DECREF(dict); |
|---|
| 6093 | n/a | } |
|---|
| 6094 | n/a | |
|---|
| 6095 | n/a | /* Also set instance attributes from the slotstate dict (if any). */ |
|---|
| 6096 | n/a | if (slotstate != NULL) { |
|---|
| 6097 | n/a | PyObject *d_key, *d_value; |
|---|
| 6098 | n/a | Py_ssize_t i; |
|---|
| 6099 | n/a | |
|---|
| 6100 | n/a | if (!PyDict_Check(slotstate)) { |
|---|
| 6101 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 6102 | n/a | PyErr_SetString(st->UnpicklingError, |
|---|
| 6103 | n/a | "slot state is not a dictionary"); |
|---|
| 6104 | n/a | goto error; |
|---|
| 6105 | n/a | } |
|---|
| 6106 | n/a | i = 0; |
|---|
| 6107 | n/a | while (PyDict_Next(slotstate, &i, &d_key, &d_value)) { |
|---|
| 6108 | n/a | if (PyObject_SetAttr(inst, d_key, d_value) < 0) |
|---|
| 6109 | n/a | goto error; |
|---|
| 6110 | n/a | } |
|---|
| 6111 | n/a | } |
|---|
| 6112 | n/a | |
|---|
| 6113 | n/a | if (0) { |
|---|
| 6114 | n/a | error: |
|---|
| 6115 | n/a | status = -1; |
|---|
| 6116 | n/a | } |
|---|
| 6117 | n/a | |
|---|
| 6118 | n/a | Py_DECREF(state); |
|---|
| 6119 | n/a | Py_XDECREF(slotstate); |
|---|
| 6120 | n/a | return status; |
|---|
| 6121 | n/a | } |
|---|
| 6122 | n/a | |
|---|
| 6123 | n/a | static int |
|---|
| 6124 | n/a | load_mark(UnpicklerObject *self) |
|---|
| 6125 | n/a | { |
|---|
| 6126 | n/a | |
|---|
| 6127 | n/a | /* Note that we split the (pickle.py) stack into two stacks, an |
|---|
| 6128 | n/a | * object stack and a mark stack. Here we push a mark onto the |
|---|
| 6129 | n/a | * mark stack. |
|---|
| 6130 | n/a | */ |
|---|
| 6131 | n/a | |
|---|
| 6132 | n/a | if ((self->num_marks + 1) >= self->marks_size) { |
|---|
| 6133 | n/a | size_t alloc; |
|---|
| 6134 | n/a | |
|---|
| 6135 | n/a | /* Use the size_t type to check for overflow. */ |
|---|
| 6136 | n/a | alloc = ((size_t)self->num_marks << 1) + 20; |
|---|
| 6137 | n/a | if (alloc > (PY_SSIZE_T_MAX / sizeof(Py_ssize_t)) || |
|---|
| 6138 | n/a | alloc <= ((size_t)self->num_marks + 1)) { |
|---|
| 6139 | n/a | PyErr_NoMemory(); |
|---|
| 6140 | n/a | return -1; |
|---|
| 6141 | n/a | } |
|---|
| 6142 | n/a | |
|---|
| 6143 | n/a | if (self->marks == NULL) |
|---|
| 6144 | n/a | self->marks = PyMem_NEW(Py_ssize_t, alloc); |
|---|
| 6145 | n/a | else |
|---|
| 6146 | n/a | PyMem_RESIZE(self->marks, Py_ssize_t, alloc); |
|---|
| 6147 | n/a | if (self->marks == NULL) { |
|---|
| 6148 | n/a | self->marks_size = 0; |
|---|
| 6149 | n/a | PyErr_NoMemory(); |
|---|
| 6150 | n/a | return -1; |
|---|
| 6151 | n/a | } |
|---|
| 6152 | n/a | self->marks_size = (Py_ssize_t)alloc; |
|---|
| 6153 | n/a | } |
|---|
| 6154 | n/a | |
|---|
| 6155 | n/a | self->stack->mark_set = 1; |
|---|
| 6156 | n/a | self->marks[self->num_marks++] = self->stack->fence = Py_SIZE(self->stack); |
|---|
| 6157 | n/a | |
|---|
| 6158 | n/a | return 0; |
|---|
| 6159 | n/a | } |
|---|
| 6160 | n/a | |
|---|
| 6161 | n/a | static int |
|---|
| 6162 | n/a | load_reduce(UnpicklerObject *self) |
|---|
| 6163 | n/a | { |
|---|
| 6164 | n/a | PyObject *callable = NULL; |
|---|
| 6165 | n/a | PyObject *argtup = NULL; |
|---|
| 6166 | n/a | PyObject *obj = NULL; |
|---|
| 6167 | n/a | |
|---|
| 6168 | n/a | PDATA_POP(self->stack, argtup); |
|---|
| 6169 | n/a | if (argtup == NULL) |
|---|
| 6170 | n/a | return -1; |
|---|
| 6171 | n/a | PDATA_POP(self->stack, callable); |
|---|
| 6172 | n/a | if (callable) { |
|---|
| 6173 | n/a | obj = PyObject_CallObject(callable, argtup); |
|---|
| 6174 | n/a | Py_DECREF(callable); |
|---|
| 6175 | n/a | } |
|---|
| 6176 | n/a | Py_DECREF(argtup); |
|---|
| 6177 | n/a | |
|---|
| 6178 | n/a | if (obj == NULL) |
|---|
| 6179 | n/a | return -1; |
|---|
| 6180 | n/a | |
|---|
| 6181 | n/a | PDATA_PUSH(self->stack, obj, -1); |
|---|
| 6182 | n/a | return 0; |
|---|
| 6183 | n/a | } |
|---|
| 6184 | n/a | |
|---|
| 6185 | n/a | /* Just raises an error if we don't know the protocol specified. PROTO |
|---|
| 6186 | n/a | * is the first opcode for protocols >= 2. |
|---|
| 6187 | n/a | */ |
|---|
| 6188 | n/a | static int |
|---|
| 6189 | n/a | load_proto(UnpicklerObject *self) |
|---|
| 6190 | n/a | { |
|---|
| 6191 | n/a | char *s; |
|---|
| 6192 | n/a | int i; |
|---|
| 6193 | n/a | |
|---|
| 6194 | n/a | if (_Unpickler_Read(self, &s, 1) < 0) |
|---|
| 6195 | n/a | return -1; |
|---|
| 6196 | n/a | |
|---|
| 6197 | n/a | i = (unsigned char)s[0]; |
|---|
| 6198 | n/a | if (i <= HIGHEST_PROTOCOL) { |
|---|
| 6199 | n/a | self->proto = i; |
|---|
| 6200 | n/a | return 0; |
|---|
| 6201 | n/a | } |
|---|
| 6202 | n/a | |
|---|
| 6203 | n/a | PyErr_Format(PyExc_ValueError, "unsupported pickle protocol: %d", i); |
|---|
| 6204 | n/a | return -1; |
|---|
| 6205 | n/a | } |
|---|
| 6206 | n/a | |
|---|
| 6207 | n/a | static int |
|---|
| 6208 | n/a | load_frame(UnpicklerObject *self) |
|---|
| 6209 | n/a | { |
|---|
| 6210 | n/a | char *s; |
|---|
| 6211 | n/a | Py_ssize_t frame_len; |
|---|
| 6212 | n/a | |
|---|
| 6213 | n/a | if (_Unpickler_Read(self, &s, 8) < 0) |
|---|
| 6214 | n/a | return -1; |
|---|
| 6215 | n/a | |
|---|
| 6216 | n/a | frame_len = calc_binsize(s, 8); |
|---|
| 6217 | n/a | if (frame_len < 0) { |
|---|
| 6218 | n/a | PyErr_Format(PyExc_OverflowError, |
|---|
| 6219 | n/a | "FRAME length exceeds system's maximum of %zd bytes", |
|---|
| 6220 | n/a | PY_SSIZE_T_MAX); |
|---|
| 6221 | n/a | return -1; |
|---|
| 6222 | n/a | } |
|---|
| 6223 | n/a | |
|---|
| 6224 | n/a | if (_Unpickler_Read(self, &s, frame_len) < 0) |
|---|
| 6225 | n/a | return -1; |
|---|
| 6226 | n/a | |
|---|
| 6227 | n/a | /* Rewind to start of frame */ |
|---|
| 6228 | n/a | self->next_read_idx -= frame_len; |
|---|
| 6229 | n/a | return 0; |
|---|
| 6230 | n/a | } |
|---|
| 6231 | n/a | |
|---|
| 6232 | n/a | static PyObject * |
|---|
| 6233 | n/a | load(UnpicklerObject *self) |
|---|
| 6234 | n/a | { |
|---|
| 6235 | n/a | PyObject *value = NULL; |
|---|
| 6236 | n/a | char *s = NULL; |
|---|
| 6237 | n/a | |
|---|
| 6238 | n/a | self->num_marks = 0; |
|---|
| 6239 | n/a | self->stack->mark_set = 0; |
|---|
| 6240 | n/a | self->stack->fence = 0; |
|---|
| 6241 | n/a | self->proto = 0; |
|---|
| 6242 | n/a | if (Py_SIZE(self->stack)) |
|---|
| 6243 | n/a | Pdata_clear(self->stack, 0); |
|---|
| 6244 | n/a | |
|---|
| 6245 | n/a | /* Convenient macros for the dispatch while-switch loop just below. */ |
|---|
| 6246 | n/a | #define OP(opcode, load_func) \ |
|---|
| 6247 | n/a | case opcode: if (load_func(self) < 0) break; continue; |
|---|
| 6248 | n/a | |
|---|
| 6249 | n/a | #define OP_ARG(opcode, load_func, arg) \ |
|---|
| 6250 | n/a | case opcode: if (load_func(self, (arg)) < 0) break; continue; |
|---|
| 6251 | n/a | |
|---|
| 6252 | n/a | while (1) { |
|---|
| 6253 | n/a | if (_Unpickler_Read(self, &s, 1) < 0) { |
|---|
| 6254 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 6255 | n/a | if (PyErr_ExceptionMatches(st->UnpicklingError)) { |
|---|
| 6256 | n/a | PyErr_Format(PyExc_EOFError, "Ran out of input"); |
|---|
| 6257 | n/a | } |
|---|
| 6258 | n/a | return NULL; |
|---|
| 6259 | n/a | } |
|---|
| 6260 | n/a | |
|---|
| 6261 | n/a | switch ((enum opcode)s[0]) { |
|---|
| 6262 | n/a | OP(NONE, load_none) |
|---|
| 6263 | n/a | OP(BININT, load_binint) |
|---|
| 6264 | n/a | OP(BININT1, load_binint1) |
|---|
| 6265 | n/a | OP(BININT2, load_binint2) |
|---|
| 6266 | n/a | OP(INT, load_int) |
|---|
| 6267 | n/a | OP(LONG, load_long) |
|---|
| 6268 | n/a | OP_ARG(LONG1, load_counted_long, 1) |
|---|
| 6269 | n/a | OP_ARG(LONG4, load_counted_long, 4) |
|---|
| 6270 | n/a | OP(FLOAT, load_float) |
|---|
| 6271 | n/a | OP(BINFLOAT, load_binfloat) |
|---|
| 6272 | n/a | OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1) |
|---|
| 6273 | n/a | OP_ARG(BINBYTES, load_counted_binbytes, 4) |
|---|
| 6274 | n/a | OP_ARG(BINBYTES8, load_counted_binbytes, 8) |
|---|
| 6275 | n/a | OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1) |
|---|
| 6276 | n/a | OP_ARG(BINSTRING, load_counted_binstring, 4) |
|---|
| 6277 | n/a | OP(STRING, load_string) |
|---|
| 6278 | n/a | OP(UNICODE, load_unicode) |
|---|
| 6279 | n/a | OP_ARG(SHORT_BINUNICODE, load_counted_binunicode, 1) |
|---|
| 6280 | n/a | OP_ARG(BINUNICODE, load_counted_binunicode, 4) |
|---|
| 6281 | n/a | OP_ARG(BINUNICODE8, load_counted_binunicode, 8) |
|---|
| 6282 | n/a | OP_ARG(EMPTY_TUPLE, load_counted_tuple, 0) |
|---|
| 6283 | n/a | OP_ARG(TUPLE1, load_counted_tuple, 1) |
|---|
| 6284 | n/a | OP_ARG(TUPLE2, load_counted_tuple, 2) |
|---|
| 6285 | n/a | OP_ARG(TUPLE3, load_counted_tuple, 3) |
|---|
| 6286 | n/a | OP(TUPLE, load_tuple) |
|---|
| 6287 | n/a | OP(EMPTY_LIST, load_empty_list) |
|---|
| 6288 | n/a | OP(LIST, load_list) |
|---|
| 6289 | n/a | OP(EMPTY_DICT, load_empty_dict) |
|---|
| 6290 | n/a | OP(DICT, load_dict) |
|---|
| 6291 | n/a | OP(EMPTY_SET, load_empty_set) |
|---|
| 6292 | n/a | OP(ADDITEMS, load_additems) |
|---|
| 6293 | n/a | OP(FROZENSET, load_frozenset) |
|---|
| 6294 | n/a | OP(OBJ, load_obj) |
|---|
| 6295 | n/a | OP(INST, load_inst) |
|---|
| 6296 | n/a | OP(NEWOBJ, load_newobj) |
|---|
| 6297 | n/a | OP(NEWOBJ_EX, load_newobj_ex) |
|---|
| 6298 | n/a | OP(GLOBAL, load_global) |
|---|
| 6299 | n/a | OP(STACK_GLOBAL, load_stack_global) |
|---|
| 6300 | n/a | OP(APPEND, load_append) |
|---|
| 6301 | n/a | OP(APPENDS, load_appends) |
|---|
| 6302 | n/a | OP(BUILD, load_build) |
|---|
| 6303 | n/a | OP(DUP, load_dup) |
|---|
| 6304 | n/a | OP(BINGET, load_binget) |
|---|
| 6305 | n/a | OP(LONG_BINGET, load_long_binget) |
|---|
| 6306 | n/a | OP(GET, load_get) |
|---|
| 6307 | n/a | OP(MARK, load_mark) |
|---|
| 6308 | n/a | OP(BINPUT, load_binput) |
|---|
| 6309 | n/a | OP(LONG_BINPUT, load_long_binput) |
|---|
| 6310 | n/a | OP(PUT, load_put) |
|---|
| 6311 | n/a | OP(MEMOIZE, load_memoize) |
|---|
| 6312 | n/a | OP(POP, load_pop) |
|---|
| 6313 | n/a | OP(POP_MARK, load_pop_mark) |
|---|
| 6314 | n/a | OP(SETITEM, load_setitem) |
|---|
| 6315 | n/a | OP(SETITEMS, load_setitems) |
|---|
| 6316 | n/a | OP(PERSID, load_persid) |
|---|
| 6317 | n/a | OP(BINPERSID, load_binpersid) |
|---|
| 6318 | n/a | OP(REDUCE, load_reduce) |
|---|
| 6319 | n/a | OP(PROTO, load_proto) |
|---|
| 6320 | n/a | OP(FRAME, load_frame) |
|---|
| 6321 | n/a | OP_ARG(EXT1, load_extension, 1) |
|---|
| 6322 | n/a | OP_ARG(EXT2, load_extension, 2) |
|---|
| 6323 | n/a | OP_ARG(EXT4, load_extension, 4) |
|---|
| 6324 | n/a | OP_ARG(NEWTRUE, load_bool, Py_True) |
|---|
| 6325 | n/a | OP_ARG(NEWFALSE, load_bool, Py_False) |
|---|
| 6326 | n/a | |
|---|
| 6327 | n/a | case STOP: |
|---|
| 6328 | n/a | break; |
|---|
| 6329 | n/a | |
|---|
| 6330 | n/a | default: |
|---|
| 6331 | n/a | { |
|---|
| 6332 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 6333 | n/a | unsigned char c = (unsigned char) *s; |
|---|
| 6334 | n/a | if (0x20 <= c && c <= 0x7e && c != '\'' && c != '\\') { |
|---|
| 6335 | n/a | PyErr_Format(st->UnpicklingError, |
|---|
| 6336 | n/a | "invalid load key, '%c'.", c); |
|---|
| 6337 | n/a | } |
|---|
| 6338 | n/a | else { |
|---|
| 6339 | n/a | PyErr_Format(st->UnpicklingError, |
|---|
| 6340 | n/a | "invalid load key, '\\x%02x'.", c); |
|---|
| 6341 | n/a | } |
|---|
| 6342 | n/a | return NULL; |
|---|
| 6343 | n/a | } |
|---|
| 6344 | n/a | } |
|---|
| 6345 | n/a | |
|---|
| 6346 | n/a | break; /* and we are done! */ |
|---|
| 6347 | n/a | } |
|---|
| 6348 | n/a | |
|---|
| 6349 | n/a | if (PyErr_Occurred()) { |
|---|
| 6350 | n/a | return NULL; |
|---|
| 6351 | n/a | } |
|---|
| 6352 | n/a | |
|---|
| 6353 | n/a | if (_Unpickler_SkipConsumed(self) < 0) |
|---|
| 6354 | n/a | return NULL; |
|---|
| 6355 | n/a | |
|---|
| 6356 | n/a | PDATA_POP(self->stack, value); |
|---|
| 6357 | n/a | return value; |
|---|
| 6358 | n/a | } |
|---|
| 6359 | n/a | |
|---|
| 6360 | n/a | /*[clinic input] |
|---|
| 6361 | n/a | |
|---|
| 6362 | n/a | _pickle.Unpickler.load |
|---|
| 6363 | n/a | |
|---|
| 6364 | n/a | Load a pickle. |
|---|
| 6365 | n/a | |
|---|
| 6366 | n/a | Read a pickled object representation from the open file object given |
|---|
| 6367 | n/a | in the constructor, and return the reconstituted object hierarchy |
|---|
| 6368 | n/a | specified therein. |
|---|
| 6369 | n/a | [clinic start generated code]*/ |
|---|
| 6370 | n/a | |
|---|
| 6371 | n/a | static PyObject * |
|---|
| 6372 | n/a | _pickle_Unpickler_load_impl(UnpicklerObject *self) |
|---|
| 6373 | n/a | /*[clinic end generated code: output=fdcc488aad675b14 input=acbb91a42fa9b7b9]*/ |
|---|
| 6374 | n/a | { |
|---|
| 6375 | n/a | UnpicklerObject *unpickler = (UnpicklerObject*)self; |
|---|
| 6376 | n/a | |
|---|
| 6377 | n/a | /* Check whether the Unpickler was initialized correctly. This prevents |
|---|
| 6378 | n/a | segfaulting if a subclass overridden __init__ with a function that does |
|---|
| 6379 | n/a | not call Unpickler.__init__(). Here, we simply ensure that self->read |
|---|
| 6380 | n/a | is not NULL. */ |
|---|
| 6381 | n/a | if (unpickler->read == NULL) { |
|---|
| 6382 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 6383 | n/a | PyErr_Format(st->UnpicklingError, |
|---|
| 6384 | n/a | "Unpickler.__init__() was not called by %s.__init__()", |
|---|
| 6385 | n/a | Py_TYPE(unpickler)->tp_name); |
|---|
| 6386 | n/a | return NULL; |
|---|
| 6387 | n/a | } |
|---|
| 6388 | n/a | |
|---|
| 6389 | n/a | return load(unpickler); |
|---|
| 6390 | n/a | } |
|---|
| 6391 | n/a | |
|---|
| 6392 | n/a | /* The name of find_class() is misleading. In newer pickle protocols, this |
|---|
| 6393 | n/a | function is used for loading any global (i.e., functions), not just |
|---|
| 6394 | n/a | classes. The name is kept only for backward compatibility. */ |
|---|
| 6395 | n/a | |
|---|
| 6396 | n/a | /*[clinic input] |
|---|
| 6397 | n/a | |
|---|
| 6398 | n/a | _pickle.Unpickler.find_class |
|---|
| 6399 | n/a | |
|---|
| 6400 | n/a | module_name: object |
|---|
| 6401 | n/a | global_name: object |
|---|
| 6402 | n/a | / |
|---|
| 6403 | n/a | |
|---|
| 6404 | n/a | Return an object from a specified module. |
|---|
| 6405 | n/a | |
|---|
| 6406 | n/a | If necessary, the module will be imported. Subclasses may override |
|---|
| 6407 | n/a | this method (e.g. to restrict unpickling of arbitrary classes and |
|---|
| 6408 | n/a | functions). |
|---|
| 6409 | n/a | |
|---|
| 6410 | n/a | This method is called whenever a class or a function object is |
|---|
| 6411 | n/a | needed. Both arguments passed are str objects. |
|---|
| 6412 | n/a | [clinic start generated code]*/ |
|---|
| 6413 | n/a | |
|---|
| 6414 | n/a | static PyObject * |
|---|
| 6415 | n/a | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
|---|
| 6416 | n/a | PyObject *module_name, |
|---|
| 6417 | n/a | PyObject *global_name) |
|---|
| 6418 | n/a | /*[clinic end generated code: output=becc08d7f9ed41e3 input=e2e6a865de093ef4]*/ |
|---|
| 6419 | n/a | { |
|---|
| 6420 | n/a | PyObject *global; |
|---|
| 6421 | n/a | PyObject *modules_dict; |
|---|
| 6422 | n/a | PyObject *module; |
|---|
| 6423 | n/a | _Py_IDENTIFIER(modules); |
|---|
| 6424 | n/a | |
|---|
| 6425 | n/a | /* Try to map the old names used in Python 2.x to the new ones used in |
|---|
| 6426 | n/a | Python 3.x. We do this only with old pickle protocols and when the |
|---|
| 6427 | n/a | user has not disabled the feature. */ |
|---|
| 6428 | n/a | if (self->proto < 3 && self->fix_imports) { |
|---|
| 6429 | n/a | PyObject *key; |
|---|
| 6430 | n/a | PyObject *item; |
|---|
| 6431 | n/a | PickleState *st = _Pickle_GetGlobalState(); |
|---|
| 6432 | n/a | |
|---|
| 6433 | n/a | /* Check if the global (i.e., a function or a class) was renamed |
|---|
| 6434 | n/a | or moved to another module. */ |
|---|
| 6435 | n/a | key = PyTuple_Pack(2, module_name, global_name); |
|---|
| 6436 | n/a | if (key == NULL) |
|---|
| 6437 | n/a | return NULL; |
|---|
| 6438 | n/a | item = PyDict_GetItemWithError(st->name_mapping_2to3, key); |
|---|
| 6439 | n/a | Py_DECREF(key); |
|---|
| 6440 | n/a | if (item) { |
|---|
| 6441 | n/a | if (!PyTuple_Check(item) || PyTuple_GET_SIZE(item) != 2) { |
|---|
| 6442 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 6443 | n/a | "_compat_pickle.NAME_MAPPING values should be " |
|---|
| 6444 | n/a | "2-tuples, not %.200s", Py_TYPE(item)->tp_name); |
|---|
| 6445 | n/a | return NULL; |
|---|
| 6446 | n/a | } |
|---|
| 6447 | n/a | module_name = PyTuple_GET_ITEM(item, 0); |
|---|
| 6448 | n/a | global_name = PyTuple_GET_ITEM(item, 1); |
|---|
| 6449 | n/a | if (!PyUnicode_Check(module_name) || |
|---|
| 6450 | n/a | !PyUnicode_Check(global_name)) { |
|---|
| 6451 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 6452 | n/a | "_compat_pickle.NAME_MAPPING values should be " |
|---|
| 6453 | n/a | "pairs of str, not (%.200s, %.200s)", |
|---|
| 6454 | n/a | Py_TYPE(module_name)->tp_name, |
|---|
| 6455 | n/a | Py_TYPE(global_name)->tp_name); |
|---|
| 6456 | n/a | return NULL; |
|---|
| 6457 | n/a | } |
|---|
| 6458 | n/a | } |
|---|
| 6459 | n/a | else if (PyErr_Occurred()) { |
|---|
| 6460 | n/a | return NULL; |
|---|
| 6461 | n/a | } |
|---|
| 6462 | n/a | else { |
|---|
| 6463 | n/a | /* Check if the module was renamed. */ |
|---|
| 6464 | n/a | item = PyDict_GetItemWithError(st->import_mapping_2to3, module_name); |
|---|
| 6465 | n/a | if (item) { |
|---|
| 6466 | n/a | if (!PyUnicode_Check(item)) { |
|---|
| 6467 | n/a | PyErr_Format(PyExc_RuntimeError, |
|---|
| 6468 | n/a | "_compat_pickle.IMPORT_MAPPING values should be " |
|---|
| 6469 | n/a | "strings, not %.200s", Py_TYPE(item)->tp_name); |
|---|
| 6470 | n/a | return NULL; |
|---|
| 6471 | n/a | } |
|---|
| 6472 | n/a | module_name = item; |
|---|
| 6473 | n/a | } |
|---|
| 6474 | n/a | else if (PyErr_Occurred()) { |
|---|
| 6475 | n/a | return NULL; |
|---|
| 6476 | n/a | } |
|---|
| 6477 | n/a | } |
|---|
| 6478 | n/a | } |
|---|
| 6479 | n/a | |
|---|
| 6480 | n/a | modules_dict = _PySys_GetObjectId(&PyId_modules); |
|---|
| 6481 | n/a | if (modules_dict == NULL) { |
|---|
| 6482 | n/a | PyErr_SetString(PyExc_RuntimeError, "unable to get sys.modules"); |
|---|
| 6483 | n/a | return NULL; |
|---|
| 6484 | n/a | } |
|---|
| 6485 | n/a | |
|---|
| 6486 | n/a | module = PyDict_GetItemWithError(modules_dict, module_name); |
|---|
| 6487 | n/a | if (module == NULL) { |
|---|
| 6488 | n/a | if (PyErr_Occurred()) |
|---|
| 6489 | n/a | return NULL; |
|---|
| 6490 | n/a | module = PyImport_Import(module_name); |
|---|
| 6491 | n/a | if (module == NULL) |
|---|
| 6492 | n/a | return NULL; |
|---|
| 6493 | n/a | global = getattribute(module, global_name, self->proto >= 4); |
|---|
| 6494 | n/a | Py_DECREF(module); |
|---|
| 6495 | n/a | } |
|---|
| 6496 | n/a | else { |
|---|
| 6497 | n/a | global = getattribute(module, global_name, self->proto >= 4); |
|---|
| 6498 | n/a | } |
|---|
| 6499 | n/a | return global; |
|---|
| 6500 | n/a | } |
|---|
| 6501 | n/a | |
|---|
| 6502 | n/a | /*[clinic input] |
|---|
| 6503 | n/a | |
|---|
| 6504 | n/a | _pickle.Unpickler.__sizeof__ -> Py_ssize_t |
|---|
| 6505 | n/a | |
|---|
| 6506 | n/a | Returns size in memory, in bytes. |
|---|
| 6507 | n/a | [clinic start generated code]*/ |
|---|
| 6508 | n/a | |
|---|
| 6509 | n/a | static Py_ssize_t |
|---|
| 6510 | n/a | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self) |
|---|
| 6511 | n/a | /*[clinic end generated code: output=119d9d03ad4c7651 input=13333471fdeedf5e]*/ |
|---|
| 6512 | n/a | { |
|---|
| 6513 | n/a | Py_ssize_t res; |
|---|
| 6514 | n/a | |
|---|
| 6515 | n/a | res = _PyObject_SIZE(Py_TYPE(self)); |
|---|
| 6516 | n/a | if (self->memo != NULL) |
|---|
| 6517 | n/a | res += self->memo_size * sizeof(PyObject *); |
|---|
| 6518 | n/a | if (self->marks != NULL) |
|---|
| 6519 | n/a | res += self->marks_size * sizeof(Py_ssize_t); |
|---|
| 6520 | n/a | if (self->input_line != NULL) |
|---|
| 6521 | n/a | res += strlen(self->input_line) + 1; |
|---|
| 6522 | n/a | if (self->encoding != NULL) |
|---|
| 6523 | n/a | res += strlen(self->encoding) + 1; |
|---|
| 6524 | n/a | if (self->errors != NULL) |
|---|
| 6525 | n/a | res += strlen(self->errors) + 1; |
|---|
| 6526 | n/a | return res; |
|---|
| 6527 | n/a | } |
|---|
| 6528 | n/a | |
|---|
| 6529 | n/a | static struct PyMethodDef Unpickler_methods[] = { |
|---|
| 6530 | n/a | _PICKLE_UNPICKLER_LOAD_METHODDEF |
|---|
| 6531 | n/a | _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF |
|---|
| 6532 | n/a | _PICKLE_UNPICKLER___SIZEOF___METHODDEF |
|---|
| 6533 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 6534 | n/a | }; |
|---|
| 6535 | n/a | |
|---|
| 6536 | n/a | static void |
|---|
| 6537 | n/a | Unpickler_dealloc(UnpicklerObject *self) |
|---|
| 6538 | n/a | { |
|---|
| 6539 | n/a | PyObject_GC_UnTrack((PyObject *)self); |
|---|
| 6540 | n/a | Py_XDECREF(self->readline); |
|---|
| 6541 | n/a | Py_XDECREF(self->read); |
|---|
| 6542 | n/a | Py_XDECREF(self->peek); |
|---|
| 6543 | n/a | Py_XDECREF(self->stack); |
|---|
| 6544 | n/a | Py_XDECREF(self->pers_func); |
|---|
| 6545 | n/a | if (self->buffer.buf != NULL) { |
|---|
| 6546 | n/a | PyBuffer_Release(&self->buffer); |
|---|
| 6547 | n/a | self->buffer.buf = NULL; |
|---|
| 6548 | n/a | } |
|---|
| 6549 | n/a | |
|---|
| 6550 | n/a | _Unpickler_MemoCleanup(self); |
|---|
| 6551 | n/a | PyMem_Free(self->marks); |
|---|
| 6552 | n/a | PyMem_Free(self->input_line); |
|---|
| 6553 | n/a | PyMem_Free(self->encoding); |
|---|
| 6554 | n/a | PyMem_Free(self->errors); |
|---|
| 6555 | n/a | |
|---|
| 6556 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
|---|
| 6557 | n/a | } |
|---|
| 6558 | n/a | |
|---|
| 6559 | n/a | static int |
|---|
| 6560 | n/a | Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg) |
|---|
| 6561 | n/a | { |
|---|
| 6562 | n/a | Py_VISIT(self->readline); |
|---|
| 6563 | n/a | Py_VISIT(self->read); |
|---|
| 6564 | n/a | Py_VISIT(self->peek); |
|---|
| 6565 | n/a | Py_VISIT(self->stack); |
|---|
| 6566 | n/a | Py_VISIT(self->pers_func); |
|---|
| 6567 | n/a | return 0; |
|---|
| 6568 | n/a | } |
|---|
| 6569 | n/a | |
|---|
| 6570 | n/a | static int |
|---|
| 6571 | n/a | Unpickler_clear(UnpicklerObject *self) |
|---|
| 6572 | n/a | { |
|---|
| 6573 | n/a | Py_CLEAR(self->readline); |
|---|
| 6574 | n/a | Py_CLEAR(self->read); |
|---|
| 6575 | n/a | Py_CLEAR(self->peek); |
|---|
| 6576 | n/a | Py_CLEAR(self->stack); |
|---|
| 6577 | n/a | Py_CLEAR(self->pers_func); |
|---|
| 6578 | n/a | if (self->buffer.buf != NULL) { |
|---|
| 6579 | n/a | PyBuffer_Release(&self->buffer); |
|---|
| 6580 | n/a | self->buffer.buf = NULL; |
|---|
| 6581 | n/a | } |
|---|
| 6582 | n/a | |
|---|
| 6583 | n/a | _Unpickler_MemoCleanup(self); |
|---|
| 6584 | n/a | PyMem_Free(self->marks); |
|---|
| 6585 | n/a | self->marks = NULL; |
|---|
| 6586 | n/a | PyMem_Free(self->input_line); |
|---|
| 6587 | n/a | self->input_line = NULL; |
|---|
| 6588 | n/a | PyMem_Free(self->encoding); |
|---|
| 6589 | n/a | self->encoding = NULL; |
|---|
| 6590 | n/a | PyMem_Free(self->errors); |
|---|
| 6591 | n/a | self->errors = NULL; |
|---|
| 6592 | n/a | |
|---|
| 6593 | n/a | return 0; |
|---|
| 6594 | n/a | } |
|---|
| 6595 | n/a | |
|---|
| 6596 | n/a | /*[clinic input] |
|---|
| 6597 | n/a | |
|---|
| 6598 | n/a | _pickle.Unpickler.__init__ |
|---|
| 6599 | n/a | |
|---|
| 6600 | n/a | file: object |
|---|
| 6601 | n/a | * |
|---|
| 6602 | n/a | fix_imports: bool = True |
|---|
| 6603 | n/a | encoding: str = 'ASCII' |
|---|
| 6604 | n/a | errors: str = 'strict' |
|---|
| 6605 | n/a | |
|---|
| 6606 | n/a | This takes a binary file for reading a pickle data stream. |
|---|
| 6607 | n/a | |
|---|
| 6608 | n/a | The protocol version of the pickle is detected automatically, so no |
|---|
| 6609 | n/a | protocol argument is needed. Bytes past the pickled object's |
|---|
| 6610 | n/a | representation are ignored. |
|---|
| 6611 | n/a | |
|---|
| 6612 | n/a | The argument *file* must have two methods, a read() method that takes |
|---|
| 6613 | n/a | an integer argument, and a readline() method that requires no |
|---|
| 6614 | n/a | arguments. Both methods should return bytes. Thus *file* can be a |
|---|
| 6615 | n/a | binary file object opened for reading, an io.BytesIO object, or any |
|---|
| 6616 | n/a | other custom object that meets this interface. |
|---|
| 6617 | n/a | |
|---|
| 6618 | n/a | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
|---|
| 6619 | n/a | which are used to control compatibility support for pickle stream |
|---|
| 6620 | n/a | generated by Python 2. If *fix_imports* is True, pickle will try to |
|---|
| 6621 | n/a | map the old Python 2 names to the new names used in Python 3. The |
|---|
| 6622 | n/a | *encoding* and *errors* tell pickle how to decode 8-bit string |
|---|
| 6623 | n/a | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
|---|
| 6624 | n/a | respectively. The *encoding* can be 'bytes' to read these 8-bit |
|---|
| 6625 | n/a | string instances as bytes objects. |
|---|
| 6626 | n/a | [clinic start generated code]*/ |
|---|
| 6627 | n/a | |
|---|
| 6628 | n/a | static int |
|---|
| 6629 | n/a | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
|---|
| 6630 | n/a | int fix_imports, const char *encoding, |
|---|
| 6631 | n/a | const char *errors) |
|---|
| 6632 | n/a | /*[clinic end generated code: output=e2c8ce748edc57b0 input=f9b7da04f5f4f335]*/ |
|---|
| 6633 | n/a | { |
|---|
| 6634 | n/a | _Py_IDENTIFIER(persistent_load); |
|---|
| 6635 | n/a | |
|---|
| 6636 | n/a | /* In case of multiple __init__() calls, clear previous content. */ |
|---|
| 6637 | n/a | if (self->read != NULL) |
|---|
| 6638 | n/a | (void)Unpickler_clear(self); |
|---|
| 6639 | n/a | |
|---|
| 6640 | n/a | if (_Unpickler_SetInputStream(self, file) < 0) |
|---|
| 6641 | n/a | return -1; |
|---|
| 6642 | n/a | |
|---|
| 6643 | n/a | if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0) |
|---|
| 6644 | n/a | return -1; |
|---|
| 6645 | n/a | |
|---|
| 6646 | n/a | self->fix_imports = fix_imports; |
|---|
| 6647 | n/a | if (self->fix_imports == -1) |
|---|
| 6648 | n/a | return -1; |
|---|
| 6649 | n/a | |
|---|
| 6650 | n/a | if (_PyObject_HasAttrId((PyObject *)self, &PyId_persistent_load)) { |
|---|
| 6651 | n/a | self->pers_func = _PyObject_GetAttrId((PyObject *)self, |
|---|
| 6652 | n/a | &PyId_persistent_load); |
|---|
| 6653 | n/a | if (self->pers_func == NULL) |
|---|
| 6654 | n/a | return 1; |
|---|
| 6655 | n/a | } |
|---|
| 6656 | n/a | else { |
|---|
| 6657 | n/a | self->pers_func = NULL; |
|---|
| 6658 | n/a | } |
|---|
| 6659 | n/a | |
|---|
| 6660 | n/a | self->stack = (Pdata *)Pdata_New(); |
|---|
| 6661 | n/a | if (self->stack == NULL) |
|---|
| 6662 | n/a | return 1; |
|---|
| 6663 | n/a | |
|---|
| 6664 | n/a | self->memo_size = 32; |
|---|
| 6665 | n/a | self->memo = _Unpickler_NewMemo(self->memo_size); |
|---|
| 6666 | n/a | if (self->memo == NULL) |
|---|
| 6667 | n/a | return -1; |
|---|
| 6668 | n/a | |
|---|
| 6669 | n/a | self->proto = 0; |
|---|
| 6670 | n/a | |
|---|
| 6671 | n/a | return 0; |
|---|
| 6672 | n/a | } |
|---|
| 6673 | n/a | |
|---|
| 6674 | n/a | |
|---|
| 6675 | n/a | /* Define a proxy object for the Unpickler's internal memo object. This is to |
|---|
| 6676 | n/a | * avoid breaking code like: |
|---|
| 6677 | n/a | * unpickler.memo.clear() |
|---|
| 6678 | n/a | * and |
|---|
| 6679 | n/a | * unpickler.memo = saved_memo |
|---|
| 6680 | n/a | * Is this a good idea? Not really, but we don't want to break code that uses |
|---|
| 6681 | n/a | * it. Note that we don't implement the entire mapping API here. This is |
|---|
| 6682 | n/a | * intentional, as these should be treated as black-box implementation details. |
|---|
| 6683 | n/a | * |
|---|
| 6684 | n/a | * We do, however, have to implement pickling/unpickling support because of |
|---|
| 6685 | n/a | * real-world code like cvs2svn. |
|---|
| 6686 | n/a | */ |
|---|
| 6687 | n/a | |
|---|
| 6688 | n/a | /*[clinic input] |
|---|
| 6689 | n/a | _pickle.UnpicklerMemoProxy.clear |
|---|
| 6690 | n/a | |
|---|
| 6691 | n/a | Remove all items from memo. |
|---|
| 6692 | n/a | [clinic start generated code]*/ |
|---|
| 6693 | n/a | |
|---|
| 6694 | n/a | static PyObject * |
|---|
| 6695 | n/a | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self) |
|---|
| 6696 | n/a | /*[clinic end generated code: output=d20cd43f4ba1fb1f input=b1df7c52e7afd9bd]*/ |
|---|
| 6697 | n/a | { |
|---|
| 6698 | n/a | _Unpickler_MemoCleanup(self->unpickler); |
|---|
| 6699 | n/a | self->unpickler->memo = _Unpickler_NewMemo(self->unpickler->memo_size); |
|---|
| 6700 | n/a | if (self->unpickler->memo == NULL) |
|---|
| 6701 | n/a | return NULL; |
|---|
| 6702 | n/a | Py_RETURN_NONE; |
|---|
| 6703 | n/a | } |
|---|
| 6704 | n/a | |
|---|
| 6705 | n/a | /*[clinic input] |
|---|
| 6706 | n/a | _pickle.UnpicklerMemoProxy.copy |
|---|
| 6707 | n/a | |
|---|
| 6708 | n/a | Copy the memo to a new object. |
|---|
| 6709 | n/a | [clinic start generated code]*/ |
|---|
| 6710 | n/a | |
|---|
| 6711 | n/a | static PyObject * |
|---|
| 6712 | n/a | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self) |
|---|
| 6713 | n/a | /*[clinic end generated code: output=e12af7e9bc1e4c77 input=97769247ce032c1d]*/ |
|---|
| 6714 | n/a | { |
|---|
| 6715 | n/a | Py_ssize_t i; |
|---|
| 6716 | n/a | PyObject *new_memo = PyDict_New(); |
|---|
| 6717 | n/a | if (new_memo == NULL) |
|---|
| 6718 | n/a | return NULL; |
|---|
| 6719 | n/a | |
|---|
| 6720 | n/a | for (i = 0; i < self->unpickler->memo_size; i++) { |
|---|
| 6721 | n/a | int status; |
|---|
| 6722 | n/a | PyObject *key, *value; |
|---|
| 6723 | n/a | |
|---|
| 6724 | n/a | value = self->unpickler->memo[i]; |
|---|
| 6725 | n/a | if (value == NULL) |
|---|
| 6726 | n/a | continue; |
|---|
| 6727 | n/a | |
|---|
| 6728 | n/a | key = PyLong_FromSsize_t(i); |
|---|
| 6729 | n/a | if (key == NULL) |
|---|
| 6730 | n/a | goto error; |
|---|
| 6731 | n/a | status = PyDict_SetItem(new_memo, key, value); |
|---|
| 6732 | n/a | Py_DECREF(key); |
|---|
| 6733 | n/a | if (status < 0) |
|---|
| 6734 | n/a | goto error; |
|---|
| 6735 | n/a | } |
|---|
| 6736 | n/a | return new_memo; |
|---|
| 6737 | n/a | |
|---|
| 6738 | n/a | error: |
|---|
| 6739 | n/a | Py_DECREF(new_memo); |
|---|
| 6740 | n/a | return NULL; |
|---|
| 6741 | n/a | } |
|---|
| 6742 | n/a | |
|---|
| 6743 | n/a | /*[clinic input] |
|---|
| 6744 | n/a | _pickle.UnpicklerMemoProxy.__reduce__ |
|---|
| 6745 | n/a | |
|---|
| 6746 | n/a | Implement pickling support. |
|---|
| 6747 | n/a | [clinic start generated code]*/ |
|---|
| 6748 | n/a | |
|---|
| 6749 | n/a | static PyObject * |
|---|
| 6750 | n/a | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self) |
|---|
| 6751 | n/a | /*[clinic end generated code: output=6da34ac048d94cca input=6920862413407199]*/ |
|---|
| 6752 | n/a | { |
|---|
| 6753 | n/a | PyObject *reduce_value; |
|---|
| 6754 | n/a | PyObject *constructor_args; |
|---|
| 6755 | n/a | PyObject *contents = _pickle_UnpicklerMemoProxy_copy_impl(self); |
|---|
| 6756 | n/a | if (contents == NULL) |
|---|
| 6757 | n/a | return NULL; |
|---|
| 6758 | n/a | |
|---|
| 6759 | n/a | reduce_value = PyTuple_New(2); |
|---|
| 6760 | n/a | if (reduce_value == NULL) { |
|---|
| 6761 | n/a | Py_DECREF(contents); |
|---|
| 6762 | n/a | return NULL; |
|---|
| 6763 | n/a | } |
|---|
| 6764 | n/a | constructor_args = PyTuple_New(1); |
|---|
| 6765 | n/a | if (constructor_args == NULL) { |
|---|
| 6766 | n/a | Py_DECREF(contents); |
|---|
| 6767 | n/a | Py_DECREF(reduce_value); |
|---|
| 6768 | n/a | return NULL; |
|---|
| 6769 | n/a | } |
|---|
| 6770 | n/a | PyTuple_SET_ITEM(constructor_args, 0, contents); |
|---|
| 6771 | n/a | Py_INCREF((PyObject *)&PyDict_Type); |
|---|
| 6772 | n/a | PyTuple_SET_ITEM(reduce_value, 0, (PyObject *)&PyDict_Type); |
|---|
| 6773 | n/a | PyTuple_SET_ITEM(reduce_value, 1, constructor_args); |
|---|
| 6774 | n/a | return reduce_value; |
|---|
| 6775 | n/a | } |
|---|
| 6776 | n/a | |
|---|
| 6777 | n/a | static PyMethodDef unpicklerproxy_methods[] = { |
|---|
| 6778 | n/a | _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF |
|---|
| 6779 | n/a | _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF |
|---|
| 6780 | n/a | _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF |
|---|
| 6781 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 6782 | n/a | }; |
|---|
| 6783 | n/a | |
|---|
| 6784 | n/a | static void |
|---|
| 6785 | n/a | UnpicklerMemoProxy_dealloc(UnpicklerMemoProxyObject *self) |
|---|
| 6786 | n/a | { |
|---|
| 6787 | n/a | PyObject_GC_UnTrack(self); |
|---|
| 6788 | n/a | Py_XDECREF(self->unpickler); |
|---|
| 6789 | n/a | PyObject_GC_Del((PyObject *)self); |
|---|
| 6790 | n/a | } |
|---|
| 6791 | n/a | |
|---|
| 6792 | n/a | static int |
|---|
| 6793 | n/a | UnpicklerMemoProxy_traverse(UnpicklerMemoProxyObject *self, |
|---|
| 6794 | n/a | visitproc visit, void *arg) |
|---|
| 6795 | n/a | { |
|---|
| 6796 | n/a | Py_VISIT(self->unpickler); |
|---|
| 6797 | n/a | return 0; |
|---|
| 6798 | n/a | } |
|---|
| 6799 | n/a | |
|---|
| 6800 | n/a | static int |
|---|
| 6801 | n/a | UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self) |
|---|
| 6802 | n/a | { |
|---|
| 6803 | n/a | Py_CLEAR(self->unpickler); |
|---|
| 6804 | n/a | return 0; |
|---|
| 6805 | n/a | } |
|---|
| 6806 | n/a | |
|---|
| 6807 | n/a | static PyTypeObject UnpicklerMemoProxyType = { |
|---|
| 6808 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 6809 | n/a | "_pickle.UnpicklerMemoProxy", /*tp_name*/ |
|---|
| 6810 | n/a | sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/ |
|---|
| 6811 | n/a | 0, |
|---|
| 6812 | n/a | (destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */ |
|---|
| 6813 | n/a | 0, /* tp_print */ |
|---|
| 6814 | n/a | 0, /* tp_getattr */ |
|---|
| 6815 | n/a | 0, /* tp_setattr */ |
|---|
| 6816 | n/a | 0, /* tp_compare */ |
|---|
| 6817 | n/a | 0, /* tp_repr */ |
|---|
| 6818 | n/a | 0, /* tp_as_number */ |
|---|
| 6819 | n/a | 0, /* tp_as_sequence */ |
|---|
| 6820 | n/a | 0, /* tp_as_mapping */ |
|---|
| 6821 | n/a | PyObject_HashNotImplemented, /* tp_hash */ |
|---|
| 6822 | n/a | 0, /* tp_call */ |
|---|
| 6823 | n/a | 0, /* tp_str */ |
|---|
| 6824 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
|---|
| 6825 | n/a | PyObject_GenericSetAttr, /* tp_setattro */ |
|---|
| 6826 | n/a | 0, /* tp_as_buffer */ |
|---|
| 6827 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
|---|
| 6828 | n/a | 0, /* tp_doc */ |
|---|
| 6829 | n/a | (traverseproc)UnpicklerMemoProxy_traverse, /* tp_traverse */ |
|---|
| 6830 | n/a | (inquiry)UnpicklerMemoProxy_clear, /* tp_clear */ |
|---|
| 6831 | n/a | 0, /* tp_richcompare */ |
|---|
| 6832 | n/a | 0, /* tp_weaklistoffset */ |
|---|
| 6833 | n/a | 0, /* tp_iter */ |
|---|
| 6834 | n/a | 0, /* tp_iternext */ |
|---|
| 6835 | n/a | unpicklerproxy_methods, /* tp_methods */ |
|---|
| 6836 | n/a | }; |
|---|
| 6837 | n/a | |
|---|
| 6838 | n/a | static PyObject * |
|---|
| 6839 | n/a | UnpicklerMemoProxy_New(UnpicklerObject *unpickler) |
|---|
| 6840 | n/a | { |
|---|
| 6841 | n/a | UnpicklerMemoProxyObject *self; |
|---|
| 6842 | n/a | |
|---|
| 6843 | n/a | self = PyObject_GC_New(UnpicklerMemoProxyObject, |
|---|
| 6844 | n/a | &UnpicklerMemoProxyType); |
|---|
| 6845 | n/a | if (self == NULL) |
|---|
| 6846 | n/a | return NULL; |
|---|
| 6847 | n/a | Py_INCREF(unpickler); |
|---|
| 6848 | n/a | self->unpickler = unpickler; |
|---|
| 6849 | n/a | PyObject_GC_Track(self); |
|---|
| 6850 | n/a | return (PyObject *)self; |
|---|
| 6851 | n/a | } |
|---|
| 6852 | n/a | |
|---|
| 6853 | n/a | /*****************************************************************************/ |
|---|
| 6854 | n/a | |
|---|
| 6855 | n/a | |
|---|
| 6856 | n/a | static PyObject * |
|---|
| 6857 | n/a | Unpickler_get_memo(UnpicklerObject *self) |
|---|
| 6858 | n/a | { |
|---|
| 6859 | n/a | return UnpicklerMemoProxy_New(self); |
|---|
| 6860 | n/a | } |
|---|
| 6861 | n/a | |
|---|
| 6862 | n/a | static int |
|---|
| 6863 | n/a | Unpickler_set_memo(UnpicklerObject *self, PyObject *obj) |
|---|
| 6864 | n/a | { |
|---|
| 6865 | n/a | PyObject **new_memo; |
|---|
| 6866 | n/a | Py_ssize_t new_memo_size = 0; |
|---|
| 6867 | n/a | Py_ssize_t i; |
|---|
| 6868 | n/a | |
|---|
| 6869 | n/a | if (obj == NULL) { |
|---|
| 6870 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 6871 | n/a | "attribute deletion is not supported"); |
|---|
| 6872 | n/a | return -1; |
|---|
| 6873 | n/a | } |
|---|
| 6874 | n/a | |
|---|
| 6875 | n/a | if (Py_TYPE(obj) == &UnpicklerMemoProxyType) { |
|---|
| 6876 | n/a | UnpicklerObject *unpickler = |
|---|
| 6877 | n/a | ((UnpicklerMemoProxyObject *)obj)->unpickler; |
|---|
| 6878 | n/a | |
|---|
| 6879 | n/a | new_memo_size = unpickler->memo_size; |
|---|
| 6880 | n/a | new_memo = _Unpickler_NewMemo(new_memo_size); |
|---|
| 6881 | n/a | if (new_memo == NULL) |
|---|
| 6882 | n/a | return -1; |
|---|
| 6883 | n/a | |
|---|
| 6884 | n/a | for (i = 0; i < new_memo_size; i++) { |
|---|
| 6885 | n/a | Py_XINCREF(unpickler->memo[i]); |
|---|
| 6886 | n/a | new_memo[i] = unpickler->memo[i]; |
|---|
| 6887 | n/a | } |
|---|
| 6888 | n/a | } |
|---|
| 6889 | n/a | else if (PyDict_Check(obj)) { |
|---|
| 6890 | n/a | Py_ssize_t i = 0; |
|---|
| 6891 | n/a | PyObject *key, *value; |
|---|
| 6892 | n/a | |
|---|
| 6893 | n/a | new_memo_size = PyDict_GET_SIZE(obj); |
|---|
| 6894 | n/a | new_memo = _Unpickler_NewMemo(new_memo_size); |
|---|
| 6895 | n/a | if (new_memo == NULL) |
|---|
| 6896 | n/a | return -1; |
|---|
| 6897 | n/a | |
|---|
| 6898 | n/a | while (PyDict_Next(obj, &i, &key, &value)) { |
|---|
| 6899 | n/a | Py_ssize_t idx; |
|---|
| 6900 | n/a | if (!PyLong_Check(key)) { |
|---|
| 6901 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 6902 | n/a | "memo key must be integers"); |
|---|
| 6903 | n/a | goto error; |
|---|
| 6904 | n/a | } |
|---|
| 6905 | n/a | idx = PyLong_AsSsize_t(key); |
|---|
| 6906 | n/a | if (idx == -1 && PyErr_Occurred()) |
|---|
| 6907 | n/a | goto error; |
|---|
| 6908 | n/a | if (idx < 0) { |
|---|
| 6909 | n/a | PyErr_SetString(PyExc_ValueError, |
|---|
| 6910 | n/a | "memo key must be positive integers."); |
|---|
| 6911 | n/a | goto error; |
|---|
| 6912 | n/a | } |
|---|
| 6913 | n/a | if (_Unpickler_MemoPut(self, idx, value) < 0) |
|---|
| 6914 | n/a | goto error; |
|---|
| 6915 | n/a | } |
|---|
| 6916 | n/a | } |
|---|
| 6917 | n/a | else { |
|---|
| 6918 | n/a | PyErr_Format(PyExc_TypeError, |
|---|
| 6919 | n/a | "'memo' attribute must be an UnpicklerMemoProxy object" |
|---|
| 6920 | n/a | "or dict, not %.200s", Py_TYPE(obj)->tp_name); |
|---|
| 6921 | n/a | return -1; |
|---|
| 6922 | n/a | } |
|---|
| 6923 | n/a | |
|---|
| 6924 | n/a | _Unpickler_MemoCleanup(self); |
|---|
| 6925 | n/a | self->memo_size = new_memo_size; |
|---|
| 6926 | n/a | self->memo = new_memo; |
|---|
| 6927 | n/a | |
|---|
| 6928 | n/a | return 0; |
|---|
| 6929 | n/a | |
|---|
| 6930 | n/a | error: |
|---|
| 6931 | n/a | if (new_memo_size) { |
|---|
| 6932 | n/a | i = new_memo_size; |
|---|
| 6933 | n/a | while (--i >= 0) { |
|---|
| 6934 | n/a | Py_XDECREF(new_memo[i]); |
|---|
| 6935 | n/a | } |
|---|
| 6936 | n/a | PyMem_FREE(new_memo); |
|---|
| 6937 | n/a | } |
|---|
| 6938 | n/a | return -1; |
|---|
| 6939 | n/a | } |
|---|
| 6940 | n/a | |
|---|
| 6941 | n/a | static PyObject * |
|---|
| 6942 | n/a | Unpickler_get_persload(UnpicklerObject *self) |
|---|
| 6943 | n/a | { |
|---|
| 6944 | n/a | if (self->pers_func == NULL) |
|---|
| 6945 | n/a | PyErr_SetString(PyExc_AttributeError, "persistent_load"); |
|---|
| 6946 | n/a | else |
|---|
| 6947 | n/a | Py_INCREF(self->pers_func); |
|---|
| 6948 | n/a | return self->pers_func; |
|---|
| 6949 | n/a | } |
|---|
| 6950 | n/a | |
|---|
| 6951 | n/a | static int |
|---|
| 6952 | n/a | Unpickler_set_persload(UnpicklerObject *self, PyObject *value) |
|---|
| 6953 | n/a | { |
|---|
| 6954 | n/a | if (value == NULL) { |
|---|
| 6955 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 6956 | n/a | "attribute deletion is not supported"); |
|---|
| 6957 | n/a | return -1; |
|---|
| 6958 | n/a | } |
|---|
| 6959 | n/a | if (!PyCallable_Check(value)) { |
|---|
| 6960 | n/a | PyErr_SetString(PyExc_TypeError, |
|---|
| 6961 | n/a | "persistent_load must be a callable taking " |
|---|
| 6962 | n/a | "one argument"); |
|---|
| 6963 | n/a | return -1; |
|---|
| 6964 | n/a | } |
|---|
| 6965 | n/a | |
|---|
| 6966 | n/a | Py_INCREF(value); |
|---|
| 6967 | n/a | Py_XSETREF(self->pers_func, value); |
|---|
| 6968 | n/a | |
|---|
| 6969 | n/a | return 0; |
|---|
| 6970 | n/a | } |
|---|
| 6971 | n/a | |
|---|
| 6972 | n/a | static PyGetSetDef Unpickler_getsets[] = { |
|---|
| 6973 | n/a | {"memo", (getter)Unpickler_get_memo, (setter)Unpickler_set_memo}, |
|---|
| 6974 | n/a | {"persistent_load", (getter)Unpickler_get_persload, |
|---|
| 6975 | n/a | (setter)Unpickler_set_persload}, |
|---|
| 6976 | n/a | {NULL} |
|---|
| 6977 | n/a | }; |
|---|
| 6978 | n/a | |
|---|
| 6979 | n/a | static PyTypeObject Unpickler_Type = { |
|---|
| 6980 | n/a | PyVarObject_HEAD_INIT(NULL, 0) |
|---|
| 6981 | n/a | "_pickle.Unpickler", /*tp_name*/ |
|---|
| 6982 | n/a | sizeof(UnpicklerObject), /*tp_basicsize*/ |
|---|
| 6983 | n/a | 0, /*tp_itemsize*/ |
|---|
| 6984 | n/a | (destructor)Unpickler_dealloc, /*tp_dealloc*/ |
|---|
| 6985 | n/a | 0, /*tp_print*/ |
|---|
| 6986 | n/a | 0, /*tp_getattr*/ |
|---|
| 6987 | n/a | 0, /*tp_setattr*/ |
|---|
| 6988 | n/a | 0, /*tp_reserved*/ |
|---|
| 6989 | n/a | 0, /*tp_repr*/ |
|---|
| 6990 | n/a | 0, /*tp_as_number*/ |
|---|
| 6991 | n/a | 0, /*tp_as_sequence*/ |
|---|
| 6992 | n/a | 0, /*tp_as_mapping*/ |
|---|
| 6993 | n/a | 0, /*tp_hash*/ |
|---|
| 6994 | n/a | 0, /*tp_call*/ |
|---|
| 6995 | n/a | 0, /*tp_str*/ |
|---|
| 6996 | n/a | 0, /*tp_getattro*/ |
|---|
| 6997 | n/a | 0, /*tp_setattro*/ |
|---|
| 6998 | n/a | 0, /*tp_as_buffer*/ |
|---|
| 6999 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC, |
|---|
| 7000 | n/a | _pickle_Unpickler___init____doc__, /*tp_doc*/ |
|---|
| 7001 | n/a | (traverseproc)Unpickler_traverse, /*tp_traverse*/ |
|---|
| 7002 | n/a | (inquiry)Unpickler_clear, /*tp_clear*/ |
|---|
| 7003 | n/a | 0, /*tp_richcompare*/ |
|---|
| 7004 | n/a | 0, /*tp_weaklistoffset*/ |
|---|
| 7005 | n/a | 0, /*tp_iter*/ |
|---|
| 7006 | n/a | 0, /*tp_iternext*/ |
|---|
| 7007 | n/a | Unpickler_methods, /*tp_methods*/ |
|---|
| 7008 | n/a | 0, /*tp_members*/ |
|---|
| 7009 | n/a | Unpickler_getsets, /*tp_getset*/ |
|---|
| 7010 | n/a | 0, /*tp_base*/ |
|---|
| 7011 | n/a | 0, /*tp_dict*/ |
|---|
| 7012 | n/a | 0, /*tp_descr_get*/ |
|---|
| 7013 | n/a | 0, /*tp_descr_set*/ |
|---|
| 7014 | n/a | 0, /*tp_dictoffset*/ |
|---|
| 7015 | n/a | _pickle_Unpickler___init__, /*tp_init*/ |
|---|
| 7016 | n/a | PyType_GenericAlloc, /*tp_alloc*/ |
|---|
| 7017 | n/a | PyType_GenericNew, /*tp_new*/ |
|---|
| 7018 | n/a | PyObject_GC_Del, /*tp_free*/ |
|---|
| 7019 | n/a | 0, /*tp_is_gc*/ |
|---|
| 7020 | n/a | }; |
|---|
| 7021 | n/a | |
|---|
| 7022 | n/a | /*[clinic input] |
|---|
| 7023 | n/a | |
|---|
| 7024 | n/a | _pickle.dump |
|---|
| 7025 | n/a | |
|---|
| 7026 | n/a | obj: object |
|---|
| 7027 | n/a | file: object |
|---|
| 7028 | n/a | protocol: object = NULL |
|---|
| 7029 | n/a | * |
|---|
| 7030 | n/a | fix_imports: bool = True |
|---|
| 7031 | n/a | |
|---|
| 7032 | n/a | Write a pickled representation of obj to the open file object file. |
|---|
| 7033 | n/a | |
|---|
| 7034 | n/a | This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may |
|---|
| 7035 | n/a | be more efficient. |
|---|
| 7036 | n/a | |
|---|
| 7037 | n/a | The optional *protocol* argument tells the pickler to use the given |
|---|
| 7038 | n/a | protocol supported protocols are 0, 1, 2, 3 and 4. The default |
|---|
| 7039 | n/a | protocol is 3; a backward-incompatible protocol designed for Python 3. |
|---|
| 7040 | n/a | |
|---|
| 7041 | n/a | Specifying a negative protocol version selects the highest protocol |
|---|
| 7042 | n/a | version supported. The higher the protocol used, the more recent the |
|---|
| 7043 | n/a | version of Python needed to read the pickle produced. |
|---|
| 7044 | n/a | |
|---|
| 7045 | n/a | The *file* argument must have a write() method that accepts a single |
|---|
| 7046 | n/a | bytes argument. It can thus be a file object opened for binary |
|---|
| 7047 | n/a | writing, an io.BytesIO instance, or any other custom object that meets |
|---|
| 7048 | n/a | this interface. |
|---|
| 7049 | n/a | |
|---|
| 7050 | n/a | If *fix_imports* is True and protocol is less than 3, pickle will try |
|---|
| 7051 | n/a | to map the new Python 3 names to the old module names used in Python |
|---|
| 7052 | n/a | 2, so that the pickle data stream is readable with Python 2. |
|---|
| 7053 | n/a | [clinic start generated code]*/ |
|---|
| 7054 | n/a | |
|---|
| 7055 | n/a | static PyObject * |
|---|
| 7056 | n/a | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
|---|
| 7057 | n/a | PyObject *protocol, int fix_imports) |
|---|
| 7058 | n/a | /*[clinic end generated code: output=a4774d5fde7d34de input=830f8a64cef6f042]*/ |
|---|
| 7059 | n/a | { |
|---|
| 7060 | n/a | PicklerObject *pickler = _Pickler_New(); |
|---|
| 7061 | n/a | |
|---|
| 7062 | n/a | if (pickler == NULL) |
|---|
| 7063 | n/a | return NULL; |
|---|
| 7064 | n/a | |
|---|
| 7065 | n/a | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
|---|
| 7066 | n/a | goto error; |
|---|
| 7067 | n/a | |
|---|
| 7068 | n/a | if (_Pickler_SetOutputStream(pickler, file) < 0) |
|---|
| 7069 | n/a | goto error; |
|---|
| 7070 | n/a | |
|---|
| 7071 | n/a | if (dump(pickler, obj) < 0) |
|---|
| 7072 | n/a | goto error; |
|---|
| 7073 | n/a | |
|---|
| 7074 | n/a | if (_Pickler_FlushToFile(pickler) < 0) |
|---|
| 7075 | n/a | goto error; |
|---|
| 7076 | n/a | |
|---|
| 7077 | n/a | Py_DECREF(pickler); |
|---|
| 7078 | n/a | Py_RETURN_NONE; |
|---|
| 7079 | n/a | |
|---|
| 7080 | n/a | error: |
|---|
| 7081 | n/a | Py_XDECREF(pickler); |
|---|
| 7082 | n/a | return NULL; |
|---|
| 7083 | n/a | } |
|---|
| 7084 | n/a | |
|---|
| 7085 | n/a | /*[clinic input] |
|---|
| 7086 | n/a | |
|---|
| 7087 | n/a | _pickle.dumps |
|---|
| 7088 | n/a | |
|---|
| 7089 | n/a | obj: object |
|---|
| 7090 | n/a | protocol: object = NULL |
|---|
| 7091 | n/a | * |
|---|
| 7092 | n/a | fix_imports: bool = True |
|---|
| 7093 | n/a | |
|---|
| 7094 | n/a | Return the pickled representation of the object as a bytes object. |
|---|
| 7095 | n/a | |
|---|
| 7096 | n/a | The optional *protocol* argument tells the pickler to use the given |
|---|
| 7097 | n/a | protocol; supported protocols are 0, 1, 2, 3 and 4. The default |
|---|
| 7098 | n/a | protocol is 3; a backward-incompatible protocol designed for Python 3. |
|---|
| 7099 | n/a | |
|---|
| 7100 | n/a | Specifying a negative protocol version selects the highest protocol |
|---|
| 7101 | n/a | version supported. The higher the protocol used, the more recent the |
|---|
| 7102 | n/a | version of Python needed to read the pickle produced. |
|---|
| 7103 | n/a | |
|---|
| 7104 | n/a | If *fix_imports* is True and *protocol* is less than 3, pickle will |
|---|
| 7105 | n/a | try to map the new Python 3 names to the old module names used in |
|---|
| 7106 | n/a | Python 2, so that the pickle data stream is readable with Python 2. |
|---|
| 7107 | n/a | [clinic start generated code]*/ |
|---|
| 7108 | n/a | |
|---|
| 7109 | n/a | static PyObject * |
|---|
| 7110 | n/a | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
|---|
| 7111 | n/a | int fix_imports) |
|---|
| 7112 | n/a | /*[clinic end generated code: output=d75d5cda456fd261 input=293dbeda181580b7]*/ |
|---|
| 7113 | n/a | { |
|---|
| 7114 | n/a | PyObject *result; |
|---|
| 7115 | n/a | PicklerObject *pickler = _Pickler_New(); |
|---|
| 7116 | n/a | |
|---|
| 7117 | n/a | if (pickler == NULL) |
|---|
| 7118 | n/a | return NULL; |
|---|
| 7119 | n/a | |
|---|
| 7120 | n/a | if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0) |
|---|
| 7121 | n/a | goto error; |
|---|
| 7122 | n/a | |
|---|
| 7123 | n/a | if (dump(pickler, obj) < 0) |
|---|
| 7124 | n/a | goto error; |
|---|
| 7125 | n/a | |
|---|
| 7126 | n/a | result = _Pickler_GetString(pickler); |
|---|
| 7127 | n/a | Py_DECREF(pickler); |
|---|
| 7128 | n/a | return result; |
|---|
| 7129 | n/a | |
|---|
| 7130 | n/a | error: |
|---|
| 7131 | n/a | Py_XDECREF(pickler); |
|---|
| 7132 | n/a | return NULL; |
|---|
| 7133 | n/a | } |
|---|
| 7134 | n/a | |
|---|
| 7135 | n/a | /*[clinic input] |
|---|
| 7136 | n/a | |
|---|
| 7137 | n/a | _pickle.load |
|---|
| 7138 | n/a | |
|---|
| 7139 | n/a | file: object |
|---|
| 7140 | n/a | * |
|---|
| 7141 | n/a | fix_imports: bool = True |
|---|
| 7142 | n/a | encoding: str = 'ASCII' |
|---|
| 7143 | n/a | errors: str = 'strict' |
|---|
| 7144 | n/a | |
|---|
| 7145 | n/a | Read and return an object from the pickle data stored in a file. |
|---|
| 7146 | n/a | |
|---|
| 7147 | n/a | This is equivalent to ``Unpickler(file).load()``, but may be more |
|---|
| 7148 | n/a | efficient. |
|---|
| 7149 | n/a | |
|---|
| 7150 | n/a | The protocol version of the pickle is detected automatically, so no |
|---|
| 7151 | n/a | protocol argument is needed. Bytes past the pickled object's |
|---|
| 7152 | n/a | representation are ignored. |
|---|
| 7153 | n/a | |
|---|
| 7154 | n/a | The argument *file* must have two methods, a read() method that takes |
|---|
| 7155 | n/a | an integer argument, and a readline() method that requires no |
|---|
| 7156 | n/a | arguments. Both methods should return bytes. Thus *file* can be a |
|---|
| 7157 | n/a | binary file object opened for reading, an io.BytesIO object, or any |
|---|
| 7158 | n/a | other custom object that meets this interface. |
|---|
| 7159 | n/a | |
|---|
| 7160 | n/a | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
|---|
| 7161 | n/a | which are used to control compatibility support for pickle stream |
|---|
| 7162 | n/a | generated by Python 2. If *fix_imports* is True, pickle will try to |
|---|
| 7163 | n/a | map the old Python 2 names to the new names used in Python 3. The |
|---|
| 7164 | n/a | *encoding* and *errors* tell pickle how to decode 8-bit string |
|---|
| 7165 | n/a | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
|---|
| 7166 | n/a | respectively. The *encoding* can be 'bytes' to read these 8-bit |
|---|
| 7167 | n/a | string instances as bytes objects. |
|---|
| 7168 | n/a | [clinic start generated code]*/ |
|---|
| 7169 | n/a | |
|---|
| 7170 | n/a | static PyObject * |
|---|
| 7171 | n/a | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
|---|
| 7172 | n/a | const char *encoding, const char *errors) |
|---|
| 7173 | n/a | /*[clinic end generated code: output=69e298160285199e input=01b44dd3fc07afa7]*/ |
|---|
| 7174 | n/a | { |
|---|
| 7175 | n/a | PyObject *result; |
|---|
| 7176 | n/a | UnpicklerObject *unpickler = _Unpickler_New(); |
|---|
| 7177 | n/a | |
|---|
| 7178 | n/a | if (unpickler == NULL) |
|---|
| 7179 | n/a | return NULL; |
|---|
| 7180 | n/a | |
|---|
| 7181 | n/a | if (_Unpickler_SetInputStream(unpickler, file) < 0) |
|---|
| 7182 | n/a | goto error; |
|---|
| 7183 | n/a | |
|---|
| 7184 | n/a | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
|---|
| 7185 | n/a | goto error; |
|---|
| 7186 | n/a | |
|---|
| 7187 | n/a | unpickler->fix_imports = fix_imports; |
|---|
| 7188 | n/a | |
|---|
| 7189 | n/a | result = load(unpickler); |
|---|
| 7190 | n/a | Py_DECREF(unpickler); |
|---|
| 7191 | n/a | return result; |
|---|
| 7192 | n/a | |
|---|
| 7193 | n/a | error: |
|---|
| 7194 | n/a | Py_XDECREF(unpickler); |
|---|
| 7195 | n/a | return NULL; |
|---|
| 7196 | n/a | } |
|---|
| 7197 | n/a | |
|---|
| 7198 | n/a | /*[clinic input] |
|---|
| 7199 | n/a | |
|---|
| 7200 | n/a | _pickle.loads |
|---|
| 7201 | n/a | |
|---|
| 7202 | n/a | data: object |
|---|
| 7203 | n/a | * |
|---|
| 7204 | n/a | fix_imports: bool = True |
|---|
| 7205 | n/a | encoding: str = 'ASCII' |
|---|
| 7206 | n/a | errors: str = 'strict' |
|---|
| 7207 | n/a | |
|---|
| 7208 | n/a | Read and return an object from the given pickle data. |
|---|
| 7209 | n/a | |
|---|
| 7210 | n/a | The protocol version of the pickle is detected automatically, so no |
|---|
| 7211 | n/a | protocol argument is needed. Bytes past the pickled object's |
|---|
| 7212 | n/a | representation are ignored. |
|---|
| 7213 | n/a | |
|---|
| 7214 | n/a | Optional keyword arguments are *fix_imports*, *encoding* and *errors*, |
|---|
| 7215 | n/a | which are used to control compatibility support for pickle stream |
|---|
| 7216 | n/a | generated by Python 2. If *fix_imports* is True, pickle will try to |
|---|
| 7217 | n/a | map the old Python 2 names to the new names used in Python 3. The |
|---|
| 7218 | n/a | *encoding* and *errors* tell pickle how to decode 8-bit string |
|---|
| 7219 | n/a | instances pickled by Python 2; these default to 'ASCII' and 'strict', |
|---|
| 7220 | n/a | respectively. The *encoding* can be 'bytes' to read these 8-bit |
|---|
| 7221 | n/a | string instances as bytes objects. |
|---|
| 7222 | n/a | [clinic start generated code]*/ |
|---|
| 7223 | n/a | |
|---|
| 7224 | n/a | static PyObject * |
|---|
| 7225 | n/a | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
|---|
| 7226 | n/a | const char *encoding, const char *errors) |
|---|
| 7227 | n/a | /*[clinic end generated code: output=1e7cb2343f2c440f input=70605948a719feb9]*/ |
|---|
| 7228 | n/a | { |
|---|
| 7229 | n/a | PyObject *result; |
|---|
| 7230 | n/a | UnpicklerObject *unpickler = _Unpickler_New(); |
|---|
| 7231 | n/a | |
|---|
| 7232 | n/a | if (unpickler == NULL) |
|---|
| 7233 | n/a | return NULL; |
|---|
| 7234 | n/a | |
|---|
| 7235 | n/a | if (_Unpickler_SetStringInput(unpickler, data) < 0) |
|---|
| 7236 | n/a | goto error; |
|---|
| 7237 | n/a | |
|---|
| 7238 | n/a | if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0) |
|---|
| 7239 | n/a | goto error; |
|---|
| 7240 | n/a | |
|---|
| 7241 | n/a | unpickler->fix_imports = fix_imports; |
|---|
| 7242 | n/a | |
|---|
| 7243 | n/a | result = load(unpickler); |
|---|
| 7244 | n/a | Py_DECREF(unpickler); |
|---|
| 7245 | n/a | return result; |
|---|
| 7246 | n/a | |
|---|
| 7247 | n/a | error: |
|---|
| 7248 | n/a | Py_XDECREF(unpickler); |
|---|
| 7249 | n/a | return NULL; |
|---|
| 7250 | n/a | } |
|---|
| 7251 | n/a | |
|---|
| 7252 | n/a | static struct PyMethodDef pickle_methods[] = { |
|---|
| 7253 | n/a | _PICKLE_DUMP_METHODDEF |
|---|
| 7254 | n/a | _PICKLE_DUMPS_METHODDEF |
|---|
| 7255 | n/a | _PICKLE_LOAD_METHODDEF |
|---|
| 7256 | n/a | _PICKLE_LOADS_METHODDEF |
|---|
| 7257 | n/a | {NULL, NULL} /* sentinel */ |
|---|
| 7258 | n/a | }; |
|---|
| 7259 | n/a | |
|---|
| 7260 | n/a | static int |
|---|
| 7261 | n/a | pickle_clear(PyObject *m) |
|---|
| 7262 | n/a | { |
|---|
| 7263 | n/a | _Pickle_ClearState(_Pickle_GetState(m)); |
|---|
| 7264 | n/a | return 0; |
|---|
| 7265 | n/a | } |
|---|
| 7266 | n/a | |
|---|
| 7267 | n/a | static void |
|---|
| 7268 | n/a | pickle_free(PyObject *m) |
|---|
| 7269 | n/a | { |
|---|
| 7270 | n/a | _Pickle_ClearState(_Pickle_GetState(m)); |
|---|
| 7271 | n/a | } |
|---|
| 7272 | n/a | |
|---|
| 7273 | n/a | static int |
|---|
| 7274 | n/a | pickle_traverse(PyObject *m, visitproc visit, void *arg) |
|---|
| 7275 | n/a | { |
|---|
| 7276 | n/a | PickleState *st = _Pickle_GetState(m); |
|---|
| 7277 | n/a | Py_VISIT(st->PickleError); |
|---|
| 7278 | n/a | Py_VISIT(st->PicklingError); |
|---|
| 7279 | n/a | Py_VISIT(st->UnpicklingError); |
|---|
| 7280 | n/a | Py_VISIT(st->dispatch_table); |
|---|
| 7281 | n/a | Py_VISIT(st->extension_registry); |
|---|
| 7282 | n/a | Py_VISIT(st->extension_cache); |
|---|
| 7283 | n/a | Py_VISIT(st->inverted_registry); |
|---|
| 7284 | n/a | Py_VISIT(st->name_mapping_2to3); |
|---|
| 7285 | n/a | Py_VISIT(st->import_mapping_2to3); |
|---|
| 7286 | n/a | Py_VISIT(st->name_mapping_3to2); |
|---|
| 7287 | n/a | Py_VISIT(st->import_mapping_3to2); |
|---|
| 7288 | n/a | Py_VISIT(st->codecs_encode); |
|---|
| 7289 | n/a | Py_VISIT(st->getattr); |
|---|
| 7290 | n/a | return 0; |
|---|
| 7291 | n/a | } |
|---|
| 7292 | n/a | |
|---|
| 7293 | n/a | static struct PyModuleDef _picklemodule = { |
|---|
| 7294 | n/a | PyModuleDef_HEAD_INIT, |
|---|
| 7295 | n/a | "_pickle", /* m_name */ |
|---|
| 7296 | n/a | pickle_module_doc, /* m_doc */ |
|---|
| 7297 | n/a | sizeof(PickleState), /* m_size */ |
|---|
| 7298 | n/a | pickle_methods, /* m_methods */ |
|---|
| 7299 | n/a | NULL, /* m_reload */ |
|---|
| 7300 | n/a | pickle_traverse, /* m_traverse */ |
|---|
| 7301 | n/a | pickle_clear, /* m_clear */ |
|---|
| 7302 | n/a | (freefunc)pickle_free /* m_free */ |
|---|
| 7303 | n/a | }; |
|---|
| 7304 | n/a | |
|---|
| 7305 | n/a | PyMODINIT_FUNC |
|---|
| 7306 | n/a | PyInit__pickle(void) |
|---|
| 7307 | n/a | { |
|---|
| 7308 | n/a | PyObject *m; |
|---|
| 7309 | n/a | PickleState *st; |
|---|
| 7310 | n/a | |
|---|
| 7311 | n/a | m = PyState_FindModule(&_picklemodule); |
|---|
| 7312 | n/a | if (m) { |
|---|
| 7313 | n/a | Py_INCREF(m); |
|---|
| 7314 | n/a | return m; |
|---|
| 7315 | n/a | } |
|---|
| 7316 | n/a | |
|---|
| 7317 | n/a | if (PyType_Ready(&Unpickler_Type) < 0) |
|---|
| 7318 | n/a | return NULL; |
|---|
| 7319 | n/a | if (PyType_Ready(&Pickler_Type) < 0) |
|---|
| 7320 | n/a | return NULL; |
|---|
| 7321 | n/a | if (PyType_Ready(&Pdata_Type) < 0) |
|---|
| 7322 | n/a | return NULL; |
|---|
| 7323 | n/a | if (PyType_Ready(&PicklerMemoProxyType) < 0) |
|---|
| 7324 | n/a | return NULL; |
|---|
| 7325 | n/a | if (PyType_Ready(&UnpicklerMemoProxyType) < 0) |
|---|
| 7326 | n/a | return NULL; |
|---|
| 7327 | n/a | |
|---|
| 7328 | n/a | /* Create the module and add the functions. */ |
|---|
| 7329 | n/a | m = PyModule_Create(&_picklemodule); |
|---|
| 7330 | n/a | if (m == NULL) |
|---|
| 7331 | n/a | return NULL; |
|---|
| 7332 | n/a | |
|---|
| 7333 | n/a | Py_INCREF(&Pickler_Type); |
|---|
| 7334 | n/a | if (PyModule_AddObject(m, "Pickler", (PyObject *)&Pickler_Type) < 0) |
|---|
| 7335 | n/a | return NULL; |
|---|
| 7336 | n/a | Py_INCREF(&Unpickler_Type); |
|---|
| 7337 | n/a | if (PyModule_AddObject(m, "Unpickler", (PyObject *)&Unpickler_Type) < 0) |
|---|
| 7338 | n/a | return NULL; |
|---|
| 7339 | n/a | |
|---|
| 7340 | n/a | st = _Pickle_GetState(m); |
|---|
| 7341 | n/a | |
|---|
| 7342 | n/a | /* Initialize the exceptions. */ |
|---|
| 7343 | n/a | st->PickleError = PyErr_NewException("_pickle.PickleError", NULL, NULL); |
|---|
| 7344 | n/a | if (st->PickleError == NULL) |
|---|
| 7345 | n/a | return NULL; |
|---|
| 7346 | n/a | st->PicklingError = \ |
|---|
| 7347 | n/a | PyErr_NewException("_pickle.PicklingError", st->PickleError, NULL); |
|---|
| 7348 | n/a | if (st->PicklingError == NULL) |
|---|
| 7349 | n/a | return NULL; |
|---|
| 7350 | n/a | st->UnpicklingError = \ |
|---|
| 7351 | n/a | PyErr_NewException("_pickle.UnpicklingError", st->PickleError, NULL); |
|---|
| 7352 | n/a | if (st->UnpicklingError == NULL) |
|---|
| 7353 | n/a | return NULL; |
|---|
| 7354 | n/a | |
|---|
| 7355 | n/a | Py_INCREF(st->PickleError); |
|---|
| 7356 | n/a | if (PyModule_AddObject(m, "PickleError", st->PickleError) < 0) |
|---|
| 7357 | n/a | return NULL; |
|---|
| 7358 | n/a | Py_INCREF(st->PicklingError); |
|---|
| 7359 | n/a | if (PyModule_AddObject(m, "PicklingError", st->PicklingError) < 0) |
|---|
| 7360 | n/a | return NULL; |
|---|
| 7361 | n/a | Py_INCREF(st->UnpicklingError); |
|---|
| 7362 | n/a | if (PyModule_AddObject(m, "UnpicklingError", st->UnpicklingError) < 0) |
|---|
| 7363 | n/a | return NULL; |
|---|
| 7364 | n/a | |
|---|
| 7365 | n/a | if (_Pickle_InitState(st) < 0) |
|---|
| 7366 | n/a | return NULL; |
|---|
| 7367 | n/a | |
|---|
| 7368 | n/a | return m; |
|---|
| 7369 | n/a | } |
|---|