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