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