Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 6 | "clear_memo($self, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 9 | "Clears the pickler\'s \"memo\".\n" |
| 10 | "\n" |
| 11 | "The memo is the data structure that remembers which objects the\n" |
| 12 | "pickler has already seen, so that shared or recursive objects are\n" |
| 13 | "pickled by reference and not by value. This method is useful when\n" |
| 14 | "re-using picklers."); |
| 15 | |
| 16 | #define _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF \ |
| 17 | {"clear_memo", (PyCFunction)_pickle_Pickler_clear_memo, METH_NOARGS, _pickle_Pickler_clear_memo__doc__}, |
| 18 | |
| 19 | static PyObject * |
| 20 | _pickle_Pickler_clear_memo_impl(PicklerObject *self); |
| 21 | |
| 22 | static PyObject * |
| 23 | _pickle_Pickler_clear_memo(PicklerObject *self, PyObject *Py_UNUSED(ignored)) |
| 24 | { |
| 25 | return _pickle_Pickler_clear_memo_impl(self); |
| 26 | } |
| 27 | |
| 28 | PyDoc_STRVAR(_pickle_Pickler_dump__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 29 | "dump($self, obj, /)\n" |
| 30 | "--\n" |
| 31 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 32 | "Write a pickled representation of the given object to the open file."); |
| 33 | |
| 34 | #define _PICKLE_PICKLER_DUMP_METHODDEF \ |
| 35 | {"dump", (PyCFunction)_pickle_Pickler_dump, METH_O, _pickle_Pickler_dump__doc__}, |
| 36 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 37 | PyDoc_STRVAR(_pickle_Pickler___sizeof____doc__, |
| 38 | "__sizeof__($self, /)\n" |
| 39 | "--\n" |
| 40 | "\n" |
| 41 | "Returns size in memory, in bytes."); |
| 42 | |
| 43 | #define _PICKLE_PICKLER___SIZEOF___METHODDEF \ |
| 44 | {"__sizeof__", (PyCFunction)_pickle_Pickler___sizeof__, METH_NOARGS, _pickle_Pickler___sizeof____doc__}, |
| 45 | |
| 46 | static Py_ssize_t |
| 47 | _pickle_Pickler___sizeof___impl(PicklerObject *self); |
| 48 | |
| 49 | static PyObject * |
| 50 | _pickle_Pickler___sizeof__(PicklerObject *self, PyObject *Py_UNUSED(ignored)) |
| 51 | { |
| 52 | PyObject *return_value = NULL; |
| 53 | Py_ssize_t _return_value; |
| 54 | |
| 55 | _return_value = _pickle_Pickler___sizeof___impl(self); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 56 | if ((_return_value == -1) && PyErr_Occurred()) { |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 57 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 58 | } |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 59 | return_value = PyLong_FromSsize_t(_return_value); |
| 60 | |
| 61 | exit: |
| 62 | return return_value; |
| 63 | } |
| 64 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 65 | PyDoc_STRVAR(_pickle_Pickler___init____doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 66 | "Pickler(file, protocol=None, fix_imports=True)\n" |
| 67 | "--\n" |
| 68 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 69 | "This takes a binary file for writing a pickle data stream.\n" |
| 70 | "\n" |
| 71 | "The optional *protocol* argument tells the pickler to use the given\n" |
| 72 | "protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n" |
| 73 | "protocol is 3; a backward-incompatible protocol designed for Python 3.\n" |
| 74 | "\n" |
| 75 | "Specifying a negative protocol version selects the highest protocol\n" |
| 76 | "version supported. The higher the protocol used, the more recent the\n" |
| 77 | "version of Python needed to read the pickle produced.\n" |
| 78 | "\n" |
| 79 | "The *file* argument must have a write() method that accepts a single\n" |
| 80 | "bytes argument. It can thus be a file object opened for binary\n" |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 81 | "writing, an io.BytesIO instance, or any other custom object that meets\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 82 | "this interface.\n" |
| 83 | "\n" |
| 84 | "If *fix_imports* is True and protocol is less than 3, pickle will try\n" |
| 85 | "to map the new Python 3 names to the old module names used in Python\n" |
| 86 | "2, so that the pickle data stream is readable with Python 2."); |
| 87 | |
| 88 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 89 | _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file, |
| 90 | PyObject *protocol, int fix_imports); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 91 | |
| 92 | static int |
| 93 | _pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
| 94 | { |
| 95 | int return_value = -1; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 96 | static const char * const _keywords[] = {"file", "protocol", "fix_imports", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 97 | static _PyArg_Parser _parser = {NULL, _keywords, "Pickler", 0}; |
| 98 | PyObject *argsbuf[3]; |
| 99 | PyObject * const *fastargs; |
| 100 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 101 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 102 | PyObject *file; |
| 103 | PyObject *protocol = NULL; |
| 104 | int fix_imports = 1; |
| 105 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 106 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 3, 0, argsbuf); |
| 107 | if (!fastargs) { |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 108 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 109 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 110 | file = fastargs[0]; |
| 111 | if (!noptargs) { |
| 112 | goto skip_optional_pos; |
| 113 | } |
| 114 | if (fastargs[1]) { |
| 115 | protocol = fastargs[1]; |
| 116 | if (!--noptargs) { |
| 117 | goto skip_optional_pos; |
| 118 | } |
| 119 | } |
| 120 | fix_imports = PyObject_IsTrue(fastargs[2]); |
| 121 | if (fix_imports < 0) { |
| 122 | goto exit; |
| 123 | } |
| 124 | skip_optional_pos: |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 125 | return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports); |
| 126 | |
| 127 | exit: |
| 128 | return return_value; |
| 129 | } |
| 130 | |
| 131 | PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 132 | "clear($self, /)\n" |
| 133 | "--\n" |
| 134 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 135 | "Remove all items from memo."); |
| 136 | |
| 137 | #define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \ |
| 138 | {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__}, |
| 139 | |
| 140 | static PyObject * |
| 141 | _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self); |
| 142 | |
| 143 | static PyObject * |
| 144 | _pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) |
| 145 | { |
| 146 | return _pickle_PicklerMemoProxy_clear_impl(self); |
| 147 | } |
| 148 | |
| 149 | PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 150 | "copy($self, /)\n" |
| 151 | "--\n" |
| 152 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 153 | "Copy the memo to a new object."); |
| 154 | |
| 155 | #define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \ |
| 156 | {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__}, |
| 157 | |
| 158 | static PyObject * |
| 159 | _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self); |
| 160 | |
| 161 | static PyObject * |
| 162 | _pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) |
| 163 | { |
| 164 | return _pickle_PicklerMemoProxy_copy_impl(self); |
| 165 | } |
| 166 | |
| 167 | PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 168 | "__reduce__($self, /)\n" |
| 169 | "--\n" |
| 170 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 171 | "Implement pickle support."); |
| 172 | |
| 173 | #define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \ |
| 174 | {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__}, |
| 175 | |
| 176 | static PyObject * |
| 177 | _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self); |
| 178 | |
| 179 | static PyObject * |
| 180 | _pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) |
| 181 | { |
| 182 | return _pickle_PicklerMemoProxy___reduce___impl(self); |
| 183 | } |
| 184 | |
| 185 | PyDoc_STRVAR(_pickle_Unpickler_load__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 186 | "load($self, /)\n" |
| 187 | "--\n" |
| 188 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 189 | "Load a pickle.\n" |
| 190 | "\n" |
| 191 | "Read a pickled object representation from the open file object given\n" |
| 192 | "in the constructor, and return the reconstituted object hierarchy\n" |
| 193 | "specified therein."); |
| 194 | |
| 195 | #define _PICKLE_UNPICKLER_LOAD_METHODDEF \ |
| 196 | {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__}, |
| 197 | |
| 198 | static PyObject * |
| 199 | _pickle_Unpickler_load_impl(UnpicklerObject *self); |
| 200 | |
| 201 | static PyObject * |
| 202 | _pickle_Unpickler_load(UnpicklerObject *self, PyObject *Py_UNUSED(ignored)) |
| 203 | { |
| 204 | return _pickle_Unpickler_load_impl(self); |
| 205 | } |
| 206 | |
| 207 | PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 208 | "find_class($self, module_name, global_name, /)\n" |
| 209 | "--\n" |
| 210 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 211 | "Return an object from a specified module.\n" |
| 212 | "\n" |
| 213 | "If necessary, the module will be imported. Subclasses may override\n" |
| 214 | "this method (e.g. to restrict unpickling of arbitrary classes and\n" |
| 215 | "functions).\n" |
| 216 | "\n" |
| 217 | "This method is called whenever a class or a function object is\n" |
| 218 | "needed. Both arguments passed are str objects."); |
| 219 | |
| 220 | #define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 221 | {"find_class", (PyCFunction)(void(*)(void))_pickle_Unpickler_find_class, METH_FASTCALL, _pickle_Unpickler_find_class__doc__}, |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 222 | |
| 223 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 224 | _pickle_Unpickler_find_class_impl(UnpicklerObject *self, |
| 225 | PyObject *module_name, |
| 226 | PyObject *global_name); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 227 | |
| 228 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 229 | _pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 230 | { |
| 231 | PyObject *return_value = NULL; |
| 232 | PyObject *module_name; |
| 233 | PyObject *global_name; |
| 234 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 235 | if (!_PyArg_CheckPositional("find_class", nargs, 2, 2)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 236 | goto exit; |
| 237 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 238 | module_name = args[0]; |
| 239 | global_name = args[1]; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 240 | return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name); |
| 241 | |
| 242 | exit: |
| 243 | return return_value; |
| 244 | } |
| 245 | |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 246 | PyDoc_STRVAR(_pickle_Unpickler___sizeof____doc__, |
| 247 | "__sizeof__($self, /)\n" |
| 248 | "--\n" |
| 249 | "\n" |
| 250 | "Returns size in memory, in bytes."); |
| 251 | |
| 252 | #define _PICKLE_UNPICKLER___SIZEOF___METHODDEF \ |
| 253 | {"__sizeof__", (PyCFunction)_pickle_Unpickler___sizeof__, METH_NOARGS, _pickle_Unpickler___sizeof____doc__}, |
| 254 | |
| 255 | static Py_ssize_t |
| 256 | _pickle_Unpickler___sizeof___impl(UnpicklerObject *self); |
| 257 | |
| 258 | static PyObject * |
| 259 | _pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored)) |
| 260 | { |
| 261 | PyObject *return_value = NULL; |
| 262 | Py_ssize_t _return_value; |
| 263 | |
| 264 | _return_value = _pickle_Unpickler___sizeof___impl(self); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 265 | if ((_return_value == -1) && PyErr_Occurred()) { |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 266 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 267 | } |
Serhiy Storchaka | 5bbd231 | 2014-12-16 19:39:08 +0200 | [diff] [blame] | 268 | return_value = PyLong_FromSsize_t(_return_value); |
| 269 | |
| 270 | exit: |
| 271 | return return_value; |
| 272 | } |
| 273 | |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 274 | PyDoc_STRVAR(_pickle_Unpickler___init____doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 275 | "Unpickler(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n" |
| 276 | "--\n" |
| 277 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 278 | "This takes a binary file for reading a pickle data stream.\n" |
| 279 | "\n" |
| 280 | "The protocol version of the pickle is detected automatically, so no\n" |
| 281 | "protocol argument is needed. Bytes past the pickled object\'s\n" |
| 282 | "representation are ignored.\n" |
| 283 | "\n" |
| 284 | "The argument *file* must have two methods, a read() method that takes\n" |
| 285 | "an integer argument, and a readline() method that requires no\n" |
| 286 | "arguments. Both methods should return bytes. Thus *file* can be a\n" |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 287 | "binary file object opened for reading, an io.BytesIO object, or any\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 288 | "other custom object that meets this interface.\n" |
| 289 | "\n" |
| 290 | "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n" |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 291 | "which are used to control compatibility support for pickle stream\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 292 | "generated by Python 2. If *fix_imports* is True, pickle will try to\n" |
| 293 | "map the old Python 2 names to the new names used in Python 3. The\n" |
| 294 | "*encoding* and *errors* tell pickle how to decode 8-bit string\n" |
| 295 | "instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n" |
| 296 | "respectively. The *encoding* can be \'bytes\' to read these 8-bit\n" |
| 297 | "string instances as bytes objects."); |
| 298 | |
| 299 | static int |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 300 | _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file, |
| 301 | int fix_imports, const char *encoding, |
| 302 | const char *errors); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 303 | |
| 304 | static int |
| 305 | _pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs) |
| 306 | { |
| 307 | int return_value = -1; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 308 | static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 309 | static _PyArg_Parser _parser = {NULL, _keywords, "Unpickler", 0}; |
| 310 | PyObject *argsbuf[4]; |
| 311 | PyObject * const *fastargs; |
| 312 | Py_ssize_t nargs = PyTuple_GET_SIZE(args); |
| 313 | Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 314 | PyObject *file; |
| 315 | int fix_imports = 1; |
| 316 | const char *encoding = "ASCII"; |
| 317 | const char *errors = "strict"; |
| 318 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 319 | fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf); |
| 320 | if (!fastargs) { |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 321 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 322 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 323 | file = fastargs[0]; |
| 324 | if (!noptargs) { |
| 325 | goto skip_optional_kwonly; |
| 326 | } |
| 327 | if (fastargs[1]) { |
| 328 | fix_imports = PyObject_IsTrue(fastargs[1]); |
| 329 | if (fix_imports < 0) { |
| 330 | goto exit; |
| 331 | } |
| 332 | if (!--noptargs) { |
| 333 | goto skip_optional_kwonly; |
| 334 | } |
| 335 | } |
| 336 | if (fastargs[2]) { |
| 337 | if (!PyUnicode_Check(fastargs[2])) { |
| 338 | _PyArg_BadArgument("Unpickler", 3, "str", fastargs[2]); |
| 339 | goto exit; |
| 340 | } |
| 341 | Py_ssize_t encoding_length; |
| 342 | encoding = PyUnicode_AsUTF8AndSize(fastargs[2], &encoding_length); |
| 343 | if (encoding == NULL) { |
| 344 | goto exit; |
| 345 | } |
| 346 | if (strlen(encoding) != (size_t)encoding_length) { |
| 347 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 348 | goto exit; |
| 349 | } |
| 350 | if (!--noptargs) { |
| 351 | goto skip_optional_kwonly; |
| 352 | } |
| 353 | } |
| 354 | if (!PyUnicode_Check(fastargs[3])) { |
| 355 | _PyArg_BadArgument("Unpickler", 4, "str", fastargs[3]); |
| 356 | goto exit; |
| 357 | } |
| 358 | Py_ssize_t errors_length; |
| 359 | errors = PyUnicode_AsUTF8AndSize(fastargs[3], &errors_length); |
| 360 | if (errors == NULL) { |
| 361 | goto exit; |
| 362 | } |
| 363 | if (strlen(errors) != (size_t)errors_length) { |
| 364 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 365 | goto exit; |
| 366 | } |
| 367 | skip_optional_kwonly: |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 368 | return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors); |
| 369 | |
| 370 | exit: |
| 371 | return return_value; |
| 372 | } |
| 373 | |
| 374 | PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 375 | "clear($self, /)\n" |
| 376 | "--\n" |
| 377 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 378 | "Remove all items from memo."); |
| 379 | |
| 380 | #define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \ |
| 381 | {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__}, |
| 382 | |
| 383 | static PyObject * |
| 384 | _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self); |
| 385 | |
| 386 | static PyObject * |
| 387 | _pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) |
| 388 | { |
| 389 | return _pickle_UnpicklerMemoProxy_clear_impl(self); |
| 390 | } |
| 391 | |
| 392 | PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 393 | "copy($self, /)\n" |
| 394 | "--\n" |
| 395 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 396 | "Copy the memo to a new object."); |
| 397 | |
| 398 | #define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \ |
| 399 | {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__}, |
| 400 | |
| 401 | static PyObject * |
| 402 | _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self); |
| 403 | |
| 404 | static PyObject * |
| 405 | _pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) |
| 406 | { |
| 407 | return _pickle_UnpicklerMemoProxy_copy_impl(self); |
| 408 | } |
| 409 | |
| 410 | PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 411 | "__reduce__($self, /)\n" |
| 412 | "--\n" |
| 413 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 414 | "Implement pickling support."); |
| 415 | |
| 416 | #define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \ |
| 417 | {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__}, |
| 418 | |
| 419 | static PyObject * |
| 420 | _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self); |
| 421 | |
| 422 | static PyObject * |
| 423 | _pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored)) |
| 424 | { |
| 425 | return _pickle_UnpicklerMemoProxy___reduce___impl(self); |
| 426 | } |
| 427 | |
| 428 | PyDoc_STRVAR(_pickle_dump__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 429 | "dump($module, /, obj, file, protocol=None, *, fix_imports=True)\n" |
| 430 | "--\n" |
| 431 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 432 | "Write a pickled representation of obj to the open file object file.\n" |
| 433 | "\n" |
| 434 | "This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n" |
| 435 | "be more efficient.\n" |
| 436 | "\n" |
| 437 | "The optional *protocol* argument tells the pickler to use the given\n" |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 438 | "protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n" |
| 439 | "protocol is 4. It was introduced in Python 3.4, it is incompatible\n" |
| 440 | "with previous versions.\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 441 | "\n" |
| 442 | "Specifying a negative protocol version selects the highest protocol\n" |
| 443 | "version supported. The higher the protocol used, the more recent the\n" |
| 444 | "version of Python needed to read the pickle produced.\n" |
| 445 | "\n" |
| 446 | "The *file* argument must have a write() method that accepts a single\n" |
| 447 | "bytes argument. It can thus be a file object opened for binary\n" |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 448 | "writing, an io.BytesIO instance, or any other custom object that meets\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 449 | "this interface.\n" |
| 450 | "\n" |
| 451 | "If *fix_imports* is True and protocol is less than 3, pickle will try\n" |
| 452 | "to map the new Python 3 names to the old module names used in Python\n" |
| 453 | "2, so that the pickle data stream is readable with Python 2."); |
| 454 | |
| 455 | #define _PICKLE_DUMP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 456 | {"dump", (PyCFunction)(void(*)(void))_pickle_dump, METH_FASTCALL|METH_KEYWORDS, _pickle_dump__doc__}, |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 457 | |
| 458 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 459 | _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 460 | PyObject *protocol, int fix_imports); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 461 | |
| 462 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 463 | _pickle_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 464 | { |
| 465 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 466 | static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 467 | static _PyArg_Parser _parser = {NULL, _keywords, "dump", 0}; |
| 468 | PyObject *argsbuf[4]; |
| 469 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 470 | PyObject *obj; |
| 471 | PyObject *file; |
| 472 | PyObject *protocol = NULL; |
| 473 | int fix_imports = 1; |
| 474 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 475 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf); |
| 476 | if (!args) { |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 477 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 478 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 479 | obj = args[0]; |
| 480 | file = args[1]; |
| 481 | if (!noptargs) { |
| 482 | goto skip_optional_pos; |
| 483 | } |
| 484 | if (args[2]) { |
| 485 | protocol = args[2]; |
| 486 | if (!--noptargs) { |
| 487 | goto skip_optional_pos; |
| 488 | } |
| 489 | } |
| 490 | skip_optional_pos: |
| 491 | if (!noptargs) { |
| 492 | goto skip_optional_kwonly; |
| 493 | } |
| 494 | fix_imports = PyObject_IsTrue(args[3]); |
| 495 | if (fix_imports < 0) { |
| 496 | goto exit; |
| 497 | } |
| 498 | skip_optional_kwonly: |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 499 | return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports); |
| 500 | |
| 501 | exit: |
| 502 | return return_value; |
| 503 | } |
| 504 | |
| 505 | PyDoc_STRVAR(_pickle_dumps__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 506 | "dumps($module, /, obj, protocol=None, *, fix_imports=True)\n" |
| 507 | "--\n" |
| 508 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 509 | "Return the pickled representation of the object as a bytes object.\n" |
| 510 | "\n" |
| 511 | "The optional *protocol* argument tells the pickler to use the given\n" |
| 512 | "protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n" |
Łukasz Langa | c51d8c9 | 2018-04-03 23:06:53 -0700 | [diff] [blame] | 513 | "protocol is 4. It was introduced in Python 3.4, it is incompatible\n" |
| 514 | "with previous versions.\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 515 | "\n" |
| 516 | "Specifying a negative protocol version selects the highest protocol\n" |
| 517 | "version supported. The higher the protocol used, the more recent the\n" |
| 518 | "version of Python needed to read the pickle produced.\n" |
| 519 | "\n" |
| 520 | "If *fix_imports* is True and *protocol* is less than 3, pickle will\n" |
| 521 | "try to map the new Python 3 names to the old module names used in\n" |
| 522 | "Python 2, so that the pickle data stream is readable with Python 2."); |
| 523 | |
| 524 | #define _PICKLE_DUMPS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 525 | {"dumps", (PyCFunction)(void(*)(void))_pickle_dumps, METH_FASTCALL|METH_KEYWORDS, _pickle_dumps__doc__}, |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 526 | |
| 527 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 528 | _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 529 | int fix_imports); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 530 | |
| 531 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 532 | _pickle_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 533 | { |
| 534 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 535 | static const char * const _keywords[] = {"obj", "protocol", "fix_imports", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 536 | static _PyArg_Parser _parser = {NULL, _keywords, "dumps", 0}; |
| 537 | PyObject *argsbuf[3]; |
| 538 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 539 | PyObject *obj; |
| 540 | PyObject *protocol = NULL; |
| 541 | int fix_imports = 1; |
| 542 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 543 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 544 | if (!args) { |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 545 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 546 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 547 | obj = args[0]; |
| 548 | if (!noptargs) { |
| 549 | goto skip_optional_pos; |
| 550 | } |
| 551 | if (args[1]) { |
| 552 | protocol = args[1]; |
| 553 | if (!--noptargs) { |
| 554 | goto skip_optional_pos; |
| 555 | } |
| 556 | } |
| 557 | skip_optional_pos: |
| 558 | if (!noptargs) { |
| 559 | goto skip_optional_kwonly; |
| 560 | } |
| 561 | fix_imports = PyObject_IsTrue(args[2]); |
| 562 | if (fix_imports < 0) { |
| 563 | goto exit; |
| 564 | } |
| 565 | skip_optional_kwonly: |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 566 | return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports); |
| 567 | |
| 568 | exit: |
| 569 | return return_value; |
| 570 | } |
| 571 | |
| 572 | PyDoc_STRVAR(_pickle_load__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 573 | "load($module, /, file, *, fix_imports=True, encoding=\'ASCII\',\n" |
| 574 | " errors=\'strict\')\n" |
| 575 | "--\n" |
| 576 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 577 | "Read and return an object from the pickle data stored in a file.\n" |
| 578 | "\n" |
| 579 | "This is equivalent to ``Unpickler(file).load()``, but may be more\n" |
| 580 | "efficient.\n" |
| 581 | "\n" |
| 582 | "The protocol version of the pickle is detected automatically, so no\n" |
| 583 | "protocol argument is needed. Bytes past the pickled object\'s\n" |
| 584 | "representation are ignored.\n" |
| 585 | "\n" |
| 586 | "The argument *file* must have two methods, a read() method that takes\n" |
| 587 | "an integer argument, and a readline() method that requires no\n" |
| 588 | "arguments. Both methods should return bytes. Thus *file* can be a\n" |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 589 | "binary file object opened for reading, an io.BytesIO object, or any\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 590 | "other custom object that meets this interface.\n" |
| 591 | "\n" |
| 592 | "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n" |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 593 | "which are used to control compatibility support for pickle stream\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 594 | "generated by Python 2. If *fix_imports* is True, pickle will try to\n" |
| 595 | "map the old Python 2 names to the new names used in Python 3. The\n" |
| 596 | "*encoding* and *errors* tell pickle how to decode 8-bit string\n" |
| 597 | "instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n" |
| 598 | "respectively. The *encoding* can be \'bytes\' to read these 8-bit\n" |
| 599 | "string instances as bytes objects."); |
| 600 | |
| 601 | #define _PICKLE_LOAD_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 602 | {"load", (PyCFunction)(void(*)(void))_pickle_load, METH_FASTCALL|METH_KEYWORDS, _pickle_load__doc__}, |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 603 | |
| 604 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 605 | _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 606 | const char *encoding, const char *errors); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 607 | |
| 608 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 609 | _pickle_load(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 610 | { |
| 611 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 612 | static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 613 | static _PyArg_Parser _parser = {NULL, _keywords, "load", 0}; |
| 614 | PyObject *argsbuf[4]; |
| 615 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 616 | PyObject *file; |
| 617 | int fix_imports = 1; |
| 618 | const char *encoding = "ASCII"; |
| 619 | const char *errors = "strict"; |
| 620 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 621 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); |
| 622 | if (!args) { |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 623 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 624 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 625 | file = args[0]; |
| 626 | if (!noptargs) { |
| 627 | goto skip_optional_kwonly; |
| 628 | } |
| 629 | if (args[1]) { |
| 630 | fix_imports = PyObject_IsTrue(args[1]); |
| 631 | if (fix_imports < 0) { |
| 632 | goto exit; |
| 633 | } |
| 634 | if (!--noptargs) { |
| 635 | goto skip_optional_kwonly; |
| 636 | } |
| 637 | } |
| 638 | if (args[2]) { |
| 639 | if (!PyUnicode_Check(args[2])) { |
| 640 | _PyArg_BadArgument("load", 3, "str", args[2]); |
| 641 | goto exit; |
| 642 | } |
| 643 | Py_ssize_t encoding_length; |
| 644 | encoding = PyUnicode_AsUTF8AndSize(args[2], &encoding_length); |
| 645 | if (encoding == NULL) { |
| 646 | goto exit; |
| 647 | } |
| 648 | if (strlen(encoding) != (size_t)encoding_length) { |
| 649 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 650 | goto exit; |
| 651 | } |
| 652 | if (!--noptargs) { |
| 653 | goto skip_optional_kwonly; |
| 654 | } |
| 655 | } |
| 656 | if (!PyUnicode_Check(args[3])) { |
| 657 | _PyArg_BadArgument("load", 4, "str", args[3]); |
| 658 | goto exit; |
| 659 | } |
| 660 | Py_ssize_t errors_length; |
| 661 | errors = PyUnicode_AsUTF8AndSize(args[3], &errors_length); |
| 662 | if (errors == NULL) { |
| 663 | goto exit; |
| 664 | } |
| 665 | if (strlen(errors) != (size_t)errors_length) { |
| 666 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 667 | goto exit; |
| 668 | } |
| 669 | skip_optional_kwonly: |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 670 | return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors); |
| 671 | |
| 672 | exit: |
| 673 | return return_value; |
| 674 | } |
| 675 | |
| 676 | PyDoc_STRVAR(_pickle_loads__doc__, |
Larry Hastings | 2623c8c | 2014-02-08 22:15:29 -0800 | [diff] [blame] | 677 | "loads($module, /, data, *, fix_imports=True, encoding=\'ASCII\',\n" |
| 678 | " errors=\'strict\')\n" |
| 679 | "--\n" |
| 680 | "\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 681 | "Read and return an object from the given pickle data.\n" |
| 682 | "\n" |
| 683 | "The protocol version of the pickle is detected automatically, so no\n" |
| 684 | "protocol argument is needed. Bytes past the pickled object\'s\n" |
| 685 | "representation are ignored.\n" |
| 686 | "\n" |
| 687 | "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n" |
Martin Panter | 46f5072 | 2016-05-26 05:35:26 +0000 | [diff] [blame] | 688 | "which are used to control compatibility support for pickle stream\n" |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 689 | "generated by Python 2. If *fix_imports* is True, pickle will try to\n" |
| 690 | "map the old Python 2 names to the new names used in Python 3. The\n" |
| 691 | "*encoding* and *errors* tell pickle how to decode 8-bit string\n" |
| 692 | "instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n" |
| 693 | "respectively. The *encoding* can be \'bytes\' to read these 8-bit\n" |
| 694 | "string instances as bytes objects."); |
| 695 | |
| 696 | #define _PICKLE_LOADS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 697 | {"loads", (PyCFunction)(void(*)(void))_pickle_loads, METH_FASTCALL|METH_KEYWORDS, _pickle_loads__doc__}, |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 698 | |
| 699 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 700 | _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 701 | const char *encoding, const char *errors); |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 702 | |
| 703 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 704 | _pickle_loads(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 705 | { |
| 706 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 707 | static const char * const _keywords[] = {"data", "fix_imports", "encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 708 | static _PyArg_Parser _parser = {NULL, _keywords, "loads", 0}; |
| 709 | PyObject *argsbuf[4]; |
| 710 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 711 | PyObject *data; |
| 712 | int fix_imports = 1; |
| 713 | const char *encoding = "ASCII"; |
| 714 | const char *errors = "strict"; |
| 715 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 716 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf); |
| 717 | if (!args) { |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 718 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 719 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 720 | data = args[0]; |
| 721 | if (!noptargs) { |
| 722 | goto skip_optional_kwonly; |
| 723 | } |
| 724 | if (args[1]) { |
| 725 | fix_imports = PyObject_IsTrue(args[1]); |
| 726 | if (fix_imports < 0) { |
| 727 | goto exit; |
| 728 | } |
| 729 | if (!--noptargs) { |
| 730 | goto skip_optional_kwonly; |
| 731 | } |
| 732 | } |
| 733 | if (args[2]) { |
| 734 | if (!PyUnicode_Check(args[2])) { |
| 735 | _PyArg_BadArgument("loads", 3, "str", args[2]); |
| 736 | goto exit; |
| 737 | } |
| 738 | Py_ssize_t encoding_length; |
| 739 | encoding = PyUnicode_AsUTF8AndSize(args[2], &encoding_length); |
| 740 | if (encoding == NULL) { |
| 741 | goto exit; |
| 742 | } |
| 743 | if (strlen(encoding) != (size_t)encoding_length) { |
| 744 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 745 | goto exit; |
| 746 | } |
| 747 | if (!--noptargs) { |
| 748 | goto skip_optional_kwonly; |
| 749 | } |
| 750 | } |
| 751 | if (!PyUnicode_Check(args[3])) { |
| 752 | _PyArg_BadArgument("loads", 4, "str", args[3]); |
| 753 | goto exit; |
| 754 | } |
| 755 | Py_ssize_t errors_length; |
| 756 | errors = PyUnicode_AsUTF8AndSize(args[3], &errors_length); |
| 757 | if (errors == NULL) { |
| 758 | goto exit; |
| 759 | } |
| 760 | if (strlen(errors) != (size_t)errors_length) { |
| 761 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 762 | goto exit; |
| 763 | } |
| 764 | skip_optional_kwonly: |
Serhiy Storchaka | 3c1f0f1 | 2014-01-27 10:34:22 +0200 | [diff] [blame] | 765 | return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors); |
| 766 | |
| 767 | exit: |
| 768 | return return_value; |
| 769 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 770 | /*[clinic end generated code: output=8f972562c8f71e2b input=a9049054013a1b77]*/ |