• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*[clinic input]
2 preserve
3 [clinic start generated code]*/
4 
5 #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
6 #  include "pycore_gc.h"          // PyGC_Head
7 #  include "pycore_runtime.h"     // _Py_SINGLETON()
8 #endif
9 #include "pycore_modsupport.h"    // _PyArg_UnpackKeywords()
10 
11 PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__,
12 "clear_memo($self, /)\n"
13 "--\n"
14 "\n"
15 "Clears the pickler\'s \"memo\".\n"
16 "\n"
17 "The memo is the data structure that remembers which objects the\n"
18 "pickler has already seen, so that shared or recursive objects are\n"
19 "pickled by reference and not by value.  This method is useful when\n"
20 "re-using picklers.");
21 
22 #define _PICKLE_PICKLER_CLEAR_MEMO_METHODDEF    \
23     {"clear_memo", (PyCFunction)_pickle_Pickler_clear_memo, METH_NOARGS, _pickle_Pickler_clear_memo__doc__},
24 
25 static PyObject *
26 _pickle_Pickler_clear_memo_impl(PicklerObject *self);
27 
28 static PyObject *
_pickle_Pickler_clear_memo(PicklerObject * self,PyObject * Py_UNUSED (ignored))29 _pickle_Pickler_clear_memo(PicklerObject *self, PyObject *Py_UNUSED(ignored))
30 {
31     return _pickle_Pickler_clear_memo_impl(self);
32 }
33 
34 PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
35 "dump($self, obj, /)\n"
36 "--\n"
37 "\n"
38 "Write a pickled representation of the given object to the open file.");
39 
40 #define _PICKLE_PICKLER_DUMP_METHODDEF    \
41     {"dump", _PyCFunction_CAST(_pickle_Pickler_dump), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Pickler_dump__doc__},
42 
43 static PyObject *
44 _pickle_Pickler_dump_impl(PicklerObject *self, PyTypeObject *cls,
45                           PyObject *obj);
46 
47 static PyObject *
_pickle_Pickler_dump(PicklerObject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)48 _pickle_Pickler_dump(PicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
49 {
50     PyObject *return_value = NULL;
51     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
52     #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
53     #else
54     #  define KWTUPLE NULL
55     #endif
56 
57     static const char * const _keywords[] = {"", NULL};
58     static _PyArg_Parser _parser = {
59         .keywords = _keywords,
60         .fname = "dump",
61         .kwtuple = KWTUPLE,
62     };
63     #undef KWTUPLE
64     PyObject *argsbuf[1];
65     PyObject *obj;
66 
67     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
68     if (!args) {
69         goto exit;
70     }
71     obj = args[0];
72     return_value = _pickle_Pickler_dump_impl(self, cls, obj);
73 
74 exit:
75     return return_value;
76 }
77 
78 PyDoc_STRVAR(_pickle_Pickler___sizeof____doc__,
79 "__sizeof__($self, /)\n"
80 "--\n"
81 "\n"
82 "Returns size in memory, in bytes.");
83 
84 #define _PICKLE_PICKLER___SIZEOF___METHODDEF    \
85     {"__sizeof__", (PyCFunction)_pickle_Pickler___sizeof__, METH_NOARGS, _pickle_Pickler___sizeof____doc__},
86 
87 static size_t
88 _pickle_Pickler___sizeof___impl(PicklerObject *self);
89 
90 static PyObject *
_pickle_Pickler___sizeof__(PicklerObject * self,PyObject * Py_UNUSED (ignored))91 _pickle_Pickler___sizeof__(PicklerObject *self, PyObject *Py_UNUSED(ignored))
92 {
93     PyObject *return_value = NULL;
94     size_t _return_value;
95 
96     _return_value = _pickle_Pickler___sizeof___impl(self);
97     if ((_return_value == (size_t)-1) && PyErr_Occurred()) {
98         goto exit;
99     }
100     return_value = PyLong_FromSize_t(_return_value);
101 
102 exit:
103     return return_value;
104 }
105 
106 PyDoc_STRVAR(_pickle_Pickler___init____doc__,
107 "Pickler(file, protocol=None, fix_imports=True, buffer_callback=None)\n"
108 "--\n"
109 "\n"
110 "This takes a binary file for writing a pickle data stream.\n"
111 "\n"
112 "The optional *protocol* argument tells the pickler to use the given\n"
113 "protocol; supported protocols are 0, 1, 2, 3, 4 and 5.  The default\n"
114 "protocol is 4. It was introduced in Python 3.4, and is incompatible\n"
115 "with previous versions.\n"
116 "\n"
117 "Specifying a negative protocol version selects the highest protocol\n"
118 "version supported.  The higher the protocol used, the more recent the\n"
119 "version of Python needed to read the pickle produced.\n"
120 "\n"
121 "The *file* argument must have a write() method that accepts a single\n"
122 "bytes argument. It can thus be a file object opened for binary\n"
123 "writing, an io.BytesIO instance, or any other custom object that meets\n"
124 "this interface.\n"
125 "\n"
126 "If *fix_imports* is True and protocol is less than 3, pickle will try\n"
127 "to map the new Python 3 names to the old module names used in Python\n"
128 "2, so that the pickle data stream is readable with Python 2.\n"
129 "\n"
130 "If *buffer_callback* is None (the default), buffer views are\n"
131 "serialized into *file* as part of the pickle stream.\n"
132 "\n"
133 "If *buffer_callback* is not None, then it can be called any number\n"
134 "of times with a buffer view.  If the callback returns a false value\n"
135 "(such as None), the given buffer is out-of-band; otherwise the\n"
136 "buffer is serialized in-band, i.e. inside the pickle stream.\n"
137 "\n"
138 "It is an error if *buffer_callback* is not None and *protocol*\n"
139 "is None or smaller than 5.");
140 
141 static int
142 _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
143                               PyObject *protocol, int fix_imports,
144                               PyObject *buffer_callback);
145 
146 static int
_pickle_Pickler___init__(PyObject * self,PyObject * args,PyObject * kwargs)147 _pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
148 {
149     int return_value = -1;
150     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
151 
152     #define NUM_KEYWORDS 4
153     static struct {
154         PyGC_Head _this_is_not_used;
155         PyObject_VAR_HEAD
156         PyObject *ob_item[NUM_KEYWORDS];
157     } _kwtuple = {
158         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
159         .ob_item = { &_Py_ID(file), &_Py_ID(protocol), &_Py_ID(fix_imports), &_Py_ID(buffer_callback), },
160     };
161     #undef NUM_KEYWORDS
162     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
163 
164     #else  // !Py_BUILD_CORE
165     #  define KWTUPLE NULL
166     #endif  // !Py_BUILD_CORE
167 
168     static const char * const _keywords[] = {"file", "protocol", "fix_imports", "buffer_callback", NULL};
169     static _PyArg_Parser _parser = {
170         .keywords = _keywords,
171         .fname = "Pickler",
172         .kwtuple = KWTUPLE,
173     };
174     #undef KWTUPLE
175     PyObject *argsbuf[4];
176     PyObject * const *fastargs;
177     Py_ssize_t nargs = PyTuple_GET_SIZE(args);
178     Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
179     PyObject *file;
180     PyObject *protocol = Py_None;
181     int fix_imports = 1;
182     PyObject *buffer_callback = Py_None;
183 
184     fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 4, 0, argsbuf);
185     if (!fastargs) {
186         goto exit;
187     }
188     file = fastargs[0];
189     if (!noptargs) {
190         goto skip_optional_pos;
191     }
192     if (fastargs[1]) {
193         protocol = fastargs[1];
194         if (!--noptargs) {
195             goto skip_optional_pos;
196         }
197     }
198     if (fastargs[2]) {
199         fix_imports = PyObject_IsTrue(fastargs[2]);
200         if (fix_imports < 0) {
201             goto exit;
202         }
203         if (!--noptargs) {
204             goto skip_optional_pos;
205         }
206     }
207     buffer_callback = fastargs[3];
208 skip_optional_pos:
209     return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports, buffer_callback);
210 
211 exit:
212     return return_value;
213 }
214 
215 PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__,
216 "clear($self, /)\n"
217 "--\n"
218 "\n"
219 "Remove all items from memo.");
220 
221 #define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF    \
222     {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__},
223 
224 static PyObject *
225 _pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self);
226 
227 static PyObject *
_pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject * self,PyObject * Py_UNUSED (ignored))228 _pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
229 {
230     return _pickle_PicklerMemoProxy_clear_impl(self);
231 }
232 
233 PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__,
234 "copy($self, /)\n"
235 "--\n"
236 "\n"
237 "Copy the memo to a new object.");
238 
239 #define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF    \
240     {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__},
241 
242 static PyObject *
243 _pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self);
244 
245 static PyObject *
_pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject * self,PyObject * Py_UNUSED (ignored))246 _pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
247 {
248     return _pickle_PicklerMemoProxy_copy_impl(self);
249 }
250 
251 PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__,
252 "__reduce__($self, /)\n"
253 "--\n"
254 "\n"
255 "Implement pickle support.");
256 
257 #define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF    \
258     {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__},
259 
260 static PyObject *
261 _pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self);
262 
263 static PyObject *
_pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject * self,PyObject * Py_UNUSED (ignored))264 _pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
265 {
266     return _pickle_PicklerMemoProxy___reduce___impl(self);
267 }
268 
269 PyDoc_STRVAR(_pickle_Unpickler_persistent_load__doc__,
270 "persistent_load($self, pid, /)\n"
271 "--\n"
272 "\n");
273 
274 #define _PICKLE_UNPICKLER_PERSISTENT_LOAD_METHODDEF    \
275     {"persistent_load", _PyCFunction_CAST(_pickle_Unpickler_persistent_load), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Unpickler_persistent_load__doc__},
276 
277 static PyObject *
278 _pickle_Unpickler_persistent_load_impl(UnpicklerObject *self,
279                                        PyTypeObject *cls, PyObject *pid);
280 
281 static PyObject *
_pickle_Unpickler_persistent_load(UnpicklerObject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)282 _pickle_Unpickler_persistent_load(UnpicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
283 {
284     PyObject *return_value = NULL;
285     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
286     #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
287     #else
288     #  define KWTUPLE NULL
289     #endif
290 
291     static const char * const _keywords[] = {"", NULL};
292     static _PyArg_Parser _parser = {
293         .keywords = _keywords,
294         .fname = "persistent_load",
295         .kwtuple = KWTUPLE,
296     };
297     #undef KWTUPLE
298     PyObject *argsbuf[1];
299     PyObject *pid;
300 
301     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
302     if (!args) {
303         goto exit;
304     }
305     pid = args[0];
306     return_value = _pickle_Unpickler_persistent_load_impl(self, cls, pid);
307 
308 exit:
309     return return_value;
310 }
311 
312 PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
313 "load($self, /)\n"
314 "--\n"
315 "\n"
316 "Load a pickle.\n"
317 "\n"
318 "Read a pickled object representation from the open file object given\n"
319 "in the constructor, and return the reconstituted object hierarchy\n"
320 "specified therein.");
321 
322 #define _PICKLE_UNPICKLER_LOAD_METHODDEF    \
323     {"load", _PyCFunction_CAST(_pickle_Unpickler_load), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Unpickler_load__doc__},
324 
325 static PyObject *
326 _pickle_Unpickler_load_impl(UnpicklerObject *self, PyTypeObject *cls);
327 
328 static PyObject *
_pickle_Unpickler_load(UnpicklerObject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)329 _pickle_Unpickler_load(UnpicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
330 {
331     if (nargs || (kwnames && PyTuple_GET_SIZE(kwnames))) {
332         PyErr_SetString(PyExc_TypeError, "load() takes no arguments");
333         return NULL;
334     }
335     return _pickle_Unpickler_load_impl(self, cls);
336 }
337 
338 PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
339 "find_class($self, module_name, global_name, /)\n"
340 "--\n"
341 "\n"
342 "Return an object from a specified module.\n"
343 "\n"
344 "If necessary, the module will be imported. Subclasses may override\n"
345 "this method (e.g. to restrict unpickling of arbitrary classes and\n"
346 "functions).\n"
347 "\n"
348 "This method is called whenever a class or a function object is\n"
349 "needed.  Both arguments passed are str objects.");
350 
351 #define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF    \
352     {"find_class", _PyCFunction_CAST(_pickle_Unpickler_find_class), METH_METHOD|METH_FASTCALL|METH_KEYWORDS, _pickle_Unpickler_find_class__doc__},
353 
354 static PyObject *
355 _pickle_Unpickler_find_class_impl(UnpicklerObject *self, PyTypeObject *cls,
356                                   PyObject *module_name,
357                                   PyObject *global_name);
358 
359 static PyObject *
_pickle_Unpickler_find_class(UnpicklerObject * self,PyTypeObject * cls,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)360 _pickle_Unpickler_find_class(UnpicklerObject *self, PyTypeObject *cls, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
361 {
362     PyObject *return_value = NULL;
363     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
364     #  define KWTUPLE (PyObject *)&_Py_SINGLETON(tuple_empty)
365     #else
366     #  define KWTUPLE NULL
367     #endif
368 
369     static const char * const _keywords[] = {"", "", NULL};
370     static _PyArg_Parser _parser = {
371         .keywords = _keywords,
372         .fname = "find_class",
373         .kwtuple = KWTUPLE,
374     };
375     #undef KWTUPLE
376     PyObject *argsbuf[2];
377     PyObject *module_name;
378     PyObject *global_name;
379 
380     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 2, 0, argsbuf);
381     if (!args) {
382         goto exit;
383     }
384     module_name = args[0];
385     global_name = args[1];
386     return_value = _pickle_Unpickler_find_class_impl(self, cls, module_name, global_name);
387 
388 exit:
389     return return_value;
390 }
391 
392 PyDoc_STRVAR(_pickle_Unpickler___sizeof____doc__,
393 "__sizeof__($self, /)\n"
394 "--\n"
395 "\n"
396 "Returns size in memory, in bytes.");
397 
398 #define _PICKLE_UNPICKLER___SIZEOF___METHODDEF    \
399     {"__sizeof__", (PyCFunction)_pickle_Unpickler___sizeof__, METH_NOARGS, _pickle_Unpickler___sizeof____doc__},
400 
401 static size_t
402 _pickle_Unpickler___sizeof___impl(UnpicklerObject *self);
403 
404 static PyObject *
_pickle_Unpickler___sizeof__(UnpicklerObject * self,PyObject * Py_UNUSED (ignored))405 _pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
406 {
407     PyObject *return_value = NULL;
408     size_t _return_value;
409 
410     _return_value = _pickle_Unpickler___sizeof___impl(self);
411     if ((_return_value == (size_t)-1) && PyErr_Occurred()) {
412         goto exit;
413     }
414     return_value = PyLong_FromSize_t(_return_value);
415 
416 exit:
417     return return_value;
418 }
419 
420 PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
421 "Unpickler(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\',\n"
422 "          buffers=())\n"
423 "--\n"
424 "\n"
425 "This takes a binary file for reading a pickle data stream.\n"
426 "\n"
427 "The protocol version of the pickle is detected automatically, so no\n"
428 "protocol argument is needed.  Bytes past the pickled object\'s\n"
429 "representation are ignored.\n"
430 "\n"
431 "The argument *file* must have two methods, a read() method that takes\n"
432 "an integer argument, and a readline() method that requires no\n"
433 "arguments.  Both methods should return bytes.  Thus *file* can be a\n"
434 "binary file object opened for reading, an io.BytesIO object, or any\n"
435 "other custom object that meets this interface.\n"
436 "\n"
437 "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
438 "which are used to control compatibility support for pickle stream\n"
439 "generated by Python 2.  If *fix_imports* is True, pickle will try to\n"
440 "map the old Python 2 names to the new names used in Python 3.  The\n"
441 "*encoding* and *errors* tell pickle how to decode 8-bit string\n"
442 "instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
443 "respectively.  The *encoding* can be \'bytes\' to read these 8-bit\n"
444 "string instances as bytes objects.");
445 
446 static int
447 _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
448                                 int fix_imports, const char *encoding,
449                                 const char *errors, PyObject *buffers);
450 
451 static int
_pickle_Unpickler___init__(PyObject * self,PyObject * args,PyObject * kwargs)452 _pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
453 {
454     int return_value = -1;
455     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
456 
457     #define NUM_KEYWORDS 5
458     static struct {
459         PyGC_Head _this_is_not_used;
460         PyObject_VAR_HEAD
461         PyObject *ob_item[NUM_KEYWORDS];
462     } _kwtuple = {
463         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
464         .ob_item = { &_Py_ID(file), &_Py_ID(fix_imports), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(buffers), },
465     };
466     #undef NUM_KEYWORDS
467     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
468 
469     #else  // !Py_BUILD_CORE
470     #  define KWTUPLE NULL
471     #endif  // !Py_BUILD_CORE
472 
473     static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", "buffers", NULL};
474     static _PyArg_Parser _parser = {
475         .keywords = _keywords,
476         .fname = "Unpickler",
477         .kwtuple = KWTUPLE,
478     };
479     #undef KWTUPLE
480     PyObject *argsbuf[5];
481     PyObject * const *fastargs;
482     Py_ssize_t nargs = PyTuple_GET_SIZE(args);
483     Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
484     PyObject *file;
485     int fix_imports = 1;
486     const char *encoding = "ASCII";
487     const char *errors = "strict";
488     PyObject *buffers = NULL;
489 
490     fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
491     if (!fastargs) {
492         goto exit;
493     }
494     file = fastargs[0];
495     if (!noptargs) {
496         goto skip_optional_kwonly;
497     }
498     if (fastargs[1]) {
499         fix_imports = PyObject_IsTrue(fastargs[1]);
500         if (fix_imports < 0) {
501             goto exit;
502         }
503         if (!--noptargs) {
504             goto skip_optional_kwonly;
505         }
506     }
507     if (fastargs[2]) {
508         if (!PyUnicode_Check(fastargs[2])) {
509             _PyArg_BadArgument("Unpickler", "argument 'encoding'", "str", fastargs[2]);
510             goto exit;
511         }
512         Py_ssize_t encoding_length;
513         encoding = PyUnicode_AsUTF8AndSize(fastargs[2], &encoding_length);
514         if (encoding == NULL) {
515             goto exit;
516         }
517         if (strlen(encoding) != (size_t)encoding_length) {
518             PyErr_SetString(PyExc_ValueError, "embedded null character");
519             goto exit;
520         }
521         if (!--noptargs) {
522             goto skip_optional_kwonly;
523         }
524     }
525     if (fastargs[3]) {
526         if (!PyUnicode_Check(fastargs[3])) {
527             _PyArg_BadArgument("Unpickler", "argument 'errors'", "str", fastargs[3]);
528             goto exit;
529         }
530         Py_ssize_t errors_length;
531         errors = PyUnicode_AsUTF8AndSize(fastargs[3], &errors_length);
532         if (errors == NULL) {
533             goto exit;
534         }
535         if (strlen(errors) != (size_t)errors_length) {
536             PyErr_SetString(PyExc_ValueError, "embedded null character");
537             goto exit;
538         }
539         if (!--noptargs) {
540             goto skip_optional_kwonly;
541         }
542     }
543     buffers = fastargs[4];
544 skip_optional_kwonly:
545     return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors, buffers);
546 
547 exit:
548     return return_value;
549 }
550 
551 PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
552 "clear($self, /)\n"
553 "--\n"
554 "\n"
555 "Remove all items from memo.");
556 
557 #define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF    \
558     {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
559 
560 static PyObject *
561 _pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self);
562 
563 static PyObject *
_pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject * self,PyObject * Py_UNUSED (ignored))564 _pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
565 {
566     return _pickle_UnpicklerMemoProxy_clear_impl(self);
567 }
568 
569 PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
570 "copy($self, /)\n"
571 "--\n"
572 "\n"
573 "Copy the memo to a new object.");
574 
575 #define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF    \
576     {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
577 
578 static PyObject *
579 _pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self);
580 
581 static PyObject *
_pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject * self,PyObject * Py_UNUSED (ignored))582 _pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
583 {
584     return _pickle_UnpicklerMemoProxy_copy_impl(self);
585 }
586 
587 PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
588 "__reduce__($self, /)\n"
589 "--\n"
590 "\n"
591 "Implement pickling support.");
592 
593 #define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF    \
594     {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
595 
596 static PyObject *
597 _pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self);
598 
599 static PyObject *
_pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject * self,PyObject * Py_UNUSED (ignored))600 _pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
601 {
602     return _pickle_UnpicklerMemoProxy___reduce___impl(self);
603 }
604 
605 PyDoc_STRVAR(_pickle_dump__doc__,
606 "dump($module, /, obj, file, protocol=None, *, fix_imports=True,\n"
607 "     buffer_callback=None)\n"
608 "--\n"
609 "\n"
610 "Write a pickled representation of obj to the open file object file.\n"
611 "\n"
612 "This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n"
613 "be more efficient.\n"
614 "\n"
615 "The optional *protocol* argument tells the pickler to use the given\n"
616 "protocol; supported protocols are 0, 1, 2, 3, 4 and 5.  The default\n"
617 "protocol is 4. It was introduced in Python 3.4, and is incompatible\n"
618 "with previous versions.\n"
619 "\n"
620 "Specifying a negative protocol version selects the highest protocol\n"
621 "version supported.  The higher the protocol used, the more recent the\n"
622 "version of Python needed to read the pickle produced.\n"
623 "\n"
624 "The *file* argument must have a write() method that accepts a single\n"
625 "bytes argument.  It can thus be a file object opened for binary\n"
626 "writing, an io.BytesIO instance, or any other custom object that meets\n"
627 "this interface.\n"
628 "\n"
629 "If *fix_imports* is True and protocol is less than 3, pickle will try\n"
630 "to map the new Python 3 names to the old module names used in Python\n"
631 "2, so that the pickle data stream is readable with Python 2.\n"
632 "\n"
633 "If *buffer_callback* is None (the default), buffer views are serialized\n"
634 "into *file* as part of the pickle stream.  It is an error if\n"
635 "*buffer_callback* is not None and *protocol* is None or smaller than 5.");
636 
637 #define _PICKLE_DUMP_METHODDEF    \
638     {"dump", _PyCFunction_CAST(_pickle_dump), METH_FASTCALL|METH_KEYWORDS, _pickle_dump__doc__},
639 
640 static PyObject *
641 _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
642                   PyObject *protocol, int fix_imports,
643                   PyObject *buffer_callback);
644 
645 static PyObject *
_pickle_dump(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)646 _pickle_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
647 {
648     PyObject *return_value = NULL;
649     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
650 
651     #define NUM_KEYWORDS 5
652     static struct {
653         PyGC_Head _this_is_not_used;
654         PyObject_VAR_HEAD
655         PyObject *ob_item[NUM_KEYWORDS];
656     } _kwtuple = {
657         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
658         .ob_item = { &_Py_ID(obj), &_Py_ID(file), &_Py_ID(protocol), &_Py_ID(fix_imports), &_Py_ID(buffer_callback), },
659     };
660     #undef NUM_KEYWORDS
661     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
662 
663     #else  // !Py_BUILD_CORE
664     #  define KWTUPLE NULL
665     #endif  // !Py_BUILD_CORE
666 
667     static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", "buffer_callback", NULL};
668     static _PyArg_Parser _parser = {
669         .keywords = _keywords,
670         .fname = "dump",
671         .kwtuple = KWTUPLE,
672     };
673     #undef KWTUPLE
674     PyObject *argsbuf[5];
675     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
676     PyObject *obj;
677     PyObject *file;
678     PyObject *protocol = Py_None;
679     int fix_imports = 1;
680     PyObject *buffer_callback = Py_None;
681 
682     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
683     if (!args) {
684         goto exit;
685     }
686     obj = args[0];
687     file = args[1];
688     if (!noptargs) {
689         goto skip_optional_pos;
690     }
691     if (args[2]) {
692         protocol = args[2];
693         if (!--noptargs) {
694             goto skip_optional_pos;
695         }
696     }
697 skip_optional_pos:
698     if (!noptargs) {
699         goto skip_optional_kwonly;
700     }
701     if (args[3]) {
702         fix_imports = PyObject_IsTrue(args[3]);
703         if (fix_imports < 0) {
704             goto exit;
705         }
706         if (!--noptargs) {
707             goto skip_optional_kwonly;
708         }
709     }
710     buffer_callback = args[4];
711 skip_optional_kwonly:
712     return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports, buffer_callback);
713 
714 exit:
715     return return_value;
716 }
717 
718 PyDoc_STRVAR(_pickle_dumps__doc__,
719 "dumps($module, /, obj, protocol=None, *, fix_imports=True,\n"
720 "      buffer_callback=None)\n"
721 "--\n"
722 "\n"
723 "Return the pickled representation of the object as a bytes object.\n"
724 "\n"
725 "The optional *protocol* argument tells the pickler to use the given\n"
726 "protocol; supported protocols are 0, 1, 2, 3, 4 and 5.  The default\n"
727 "protocol is 4. It was introduced in Python 3.4, and is incompatible\n"
728 "with previous versions.\n"
729 "\n"
730 "Specifying a negative protocol version selects the highest protocol\n"
731 "version supported.  The higher the protocol used, the more recent the\n"
732 "version of Python needed to read the pickle produced.\n"
733 "\n"
734 "If *fix_imports* is True and *protocol* is less than 3, pickle will\n"
735 "try to map the new Python 3 names to the old module names used in\n"
736 "Python 2, so that the pickle data stream is readable with Python 2.\n"
737 "\n"
738 "If *buffer_callback* is None (the default), buffer views are serialized\n"
739 "into *file* as part of the pickle stream.  It is an error if\n"
740 "*buffer_callback* is not None and *protocol* is None or smaller than 5.");
741 
742 #define _PICKLE_DUMPS_METHODDEF    \
743     {"dumps", _PyCFunction_CAST(_pickle_dumps), METH_FASTCALL|METH_KEYWORDS, _pickle_dumps__doc__},
744 
745 static PyObject *
746 _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
747                    int fix_imports, PyObject *buffer_callback);
748 
749 static PyObject *
_pickle_dumps(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)750 _pickle_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
751 {
752     PyObject *return_value = NULL;
753     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
754 
755     #define NUM_KEYWORDS 4
756     static struct {
757         PyGC_Head _this_is_not_used;
758         PyObject_VAR_HEAD
759         PyObject *ob_item[NUM_KEYWORDS];
760     } _kwtuple = {
761         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
762         .ob_item = { &_Py_ID(obj), &_Py_ID(protocol), &_Py_ID(fix_imports), &_Py_ID(buffer_callback), },
763     };
764     #undef NUM_KEYWORDS
765     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
766 
767     #else  // !Py_BUILD_CORE
768     #  define KWTUPLE NULL
769     #endif  // !Py_BUILD_CORE
770 
771     static const char * const _keywords[] = {"obj", "protocol", "fix_imports", "buffer_callback", NULL};
772     static _PyArg_Parser _parser = {
773         .keywords = _keywords,
774         .fname = "dumps",
775         .kwtuple = KWTUPLE,
776     };
777     #undef KWTUPLE
778     PyObject *argsbuf[4];
779     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
780     PyObject *obj;
781     PyObject *protocol = Py_None;
782     int fix_imports = 1;
783     PyObject *buffer_callback = Py_None;
784 
785     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
786     if (!args) {
787         goto exit;
788     }
789     obj = args[0];
790     if (!noptargs) {
791         goto skip_optional_pos;
792     }
793     if (args[1]) {
794         protocol = args[1];
795         if (!--noptargs) {
796             goto skip_optional_pos;
797         }
798     }
799 skip_optional_pos:
800     if (!noptargs) {
801         goto skip_optional_kwonly;
802     }
803     if (args[2]) {
804         fix_imports = PyObject_IsTrue(args[2]);
805         if (fix_imports < 0) {
806             goto exit;
807         }
808         if (!--noptargs) {
809             goto skip_optional_kwonly;
810         }
811     }
812     buffer_callback = args[3];
813 skip_optional_kwonly:
814     return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports, buffer_callback);
815 
816 exit:
817     return return_value;
818 }
819 
820 PyDoc_STRVAR(_pickle_load__doc__,
821 "load($module, /, file, *, fix_imports=True, encoding=\'ASCII\',\n"
822 "     errors=\'strict\', buffers=())\n"
823 "--\n"
824 "\n"
825 "Read and return an object from the pickle data stored in a file.\n"
826 "\n"
827 "This is equivalent to ``Unpickler(file).load()``, but may be more\n"
828 "efficient.\n"
829 "\n"
830 "The protocol version of the pickle is detected automatically, so no\n"
831 "protocol argument is needed.  Bytes past the pickled object\'s\n"
832 "representation are ignored.\n"
833 "\n"
834 "The argument *file* must have two methods, a read() method that takes\n"
835 "an integer argument, and a readline() method that requires no\n"
836 "arguments.  Both methods should return bytes.  Thus *file* can be a\n"
837 "binary file object opened for reading, an io.BytesIO object, or any\n"
838 "other custom object that meets this interface.\n"
839 "\n"
840 "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
841 "which are used to control compatibility support for pickle stream\n"
842 "generated by Python 2.  If *fix_imports* is True, pickle will try to\n"
843 "map the old Python 2 names to the new names used in Python 3.  The\n"
844 "*encoding* and *errors* tell pickle how to decode 8-bit string\n"
845 "instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
846 "respectively.  The *encoding* can be \'bytes\' to read these 8-bit\n"
847 "string instances as bytes objects.");
848 
849 #define _PICKLE_LOAD_METHODDEF    \
850     {"load", _PyCFunction_CAST(_pickle_load), METH_FASTCALL|METH_KEYWORDS, _pickle_load__doc__},
851 
852 static PyObject *
853 _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
854                   const char *encoding, const char *errors,
855                   PyObject *buffers);
856 
857 static PyObject *
_pickle_load(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)858 _pickle_load(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
859 {
860     PyObject *return_value = NULL;
861     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
862 
863     #define NUM_KEYWORDS 5
864     static struct {
865         PyGC_Head _this_is_not_used;
866         PyObject_VAR_HEAD
867         PyObject *ob_item[NUM_KEYWORDS];
868     } _kwtuple = {
869         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
870         .ob_item = { &_Py_ID(file), &_Py_ID(fix_imports), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(buffers), },
871     };
872     #undef NUM_KEYWORDS
873     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
874 
875     #else  // !Py_BUILD_CORE
876     #  define KWTUPLE NULL
877     #endif  // !Py_BUILD_CORE
878 
879     static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", "buffers", NULL};
880     static _PyArg_Parser _parser = {
881         .keywords = _keywords,
882         .fname = "load",
883         .kwtuple = KWTUPLE,
884     };
885     #undef KWTUPLE
886     PyObject *argsbuf[5];
887     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
888     PyObject *file;
889     int fix_imports = 1;
890     const char *encoding = "ASCII";
891     const char *errors = "strict";
892     PyObject *buffers = NULL;
893 
894     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
895     if (!args) {
896         goto exit;
897     }
898     file = args[0];
899     if (!noptargs) {
900         goto skip_optional_kwonly;
901     }
902     if (args[1]) {
903         fix_imports = PyObject_IsTrue(args[1]);
904         if (fix_imports < 0) {
905             goto exit;
906         }
907         if (!--noptargs) {
908             goto skip_optional_kwonly;
909         }
910     }
911     if (args[2]) {
912         if (!PyUnicode_Check(args[2])) {
913             _PyArg_BadArgument("load", "argument 'encoding'", "str", args[2]);
914             goto exit;
915         }
916         Py_ssize_t encoding_length;
917         encoding = PyUnicode_AsUTF8AndSize(args[2], &encoding_length);
918         if (encoding == NULL) {
919             goto exit;
920         }
921         if (strlen(encoding) != (size_t)encoding_length) {
922             PyErr_SetString(PyExc_ValueError, "embedded null character");
923             goto exit;
924         }
925         if (!--noptargs) {
926             goto skip_optional_kwonly;
927         }
928     }
929     if (args[3]) {
930         if (!PyUnicode_Check(args[3])) {
931             _PyArg_BadArgument("load", "argument 'errors'", "str", args[3]);
932             goto exit;
933         }
934         Py_ssize_t errors_length;
935         errors = PyUnicode_AsUTF8AndSize(args[3], &errors_length);
936         if (errors == NULL) {
937             goto exit;
938         }
939         if (strlen(errors) != (size_t)errors_length) {
940             PyErr_SetString(PyExc_ValueError, "embedded null character");
941             goto exit;
942         }
943         if (!--noptargs) {
944             goto skip_optional_kwonly;
945         }
946     }
947     buffers = args[4];
948 skip_optional_kwonly:
949     return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors, buffers);
950 
951 exit:
952     return return_value;
953 }
954 
955 PyDoc_STRVAR(_pickle_loads__doc__,
956 "loads($module, data, /, *, fix_imports=True, encoding=\'ASCII\',\n"
957 "      errors=\'strict\', buffers=())\n"
958 "--\n"
959 "\n"
960 "Read and return an object from the given pickle data.\n"
961 "\n"
962 "The protocol version of the pickle is detected automatically, so no\n"
963 "protocol argument is needed.  Bytes past the pickled object\'s\n"
964 "representation are ignored.\n"
965 "\n"
966 "Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
967 "which are used to control compatibility support for pickle stream\n"
968 "generated by Python 2.  If *fix_imports* is True, pickle will try to\n"
969 "map the old Python 2 names to the new names used in Python 3.  The\n"
970 "*encoding* and *errors* tell pickle how to decode 8-bit string\n"
971 "instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
972 "respectively.  The *encoding* can be \'bytes\' to read these 8-bit\n"
973 "string instances as bytes objects.");
974 
975 #define _PICKLE_LOADS_METHODDEF    \
976     {"loads", _PyCFunction_CAST(_pickle_loads), METH_FASTCALL|METH_KEYWORDS, _pickle_loads__doc__},
977 
978 static PyObject *
979 _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
980                    const char *encoding, const char *errors,
981                    PyObject *buffers);
982 
983 static PyObject *
_pickle_loads(PyObject * module,PyObject * const * args,Py_ssize_t nargs,PyObject * kwnames)984 _pickle_loads(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
985 {
986     PyObject *return_value = NULL;
987     #if defined(Py_BUILD_CORE) && !defined(Py_BUILD_CORE_MODULE)
988 
989     #define NUM_KEYWORDS 4
990     static struct {
991         PyGC_Head _this_is_not_used;
992         PyObject_VAR_HEAD
993         PyObject *ob_item[NUM_KEYWORDS];
994     } _kwtuple = {
995         .ob_base = PyVarObject_HEAD_INIT(&PyTuple_Type, NUM_KEYWORDS)
996         .ob_item = { &_Py_ID(fix_imports), &_Py_ID(encoding), &_Py_ID(errors), &_Py_ID(buffers), },
997     };
998     #undef NUM_KEYWORDS
999     #define KWTUPLE (&_kwtuple.ob_base.ob_base)
1000 
1001     #else  // !Py_BUILD_CORE
1002     #  define KWTUPLE NULL
1003     #endif  // !Py_BUILD_CORE
1004 
1005     static const char * const _keywords[] = {"", "fix_imports", "encoding", "errors", "buffers", NULL};
1006     static _PyArg_Parser _parser = {
1007         .keywords = _keywords,
1008         .fname = "loads",
1009         .kwtuple = KWTUPLE,
1010     };
1011     #undef KWTUPLE
1012     PyObject *argsbuf[5];
1013     Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
1014     PyObject *data;
1015     int fix_imports = 1;
1016     const char *encoding = "ASCII";
1017     const char *errors = "strict";
1018     PyObject *buffers = NULL;
1019 
1020     args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
1021     if (!args) {
1022         goto exit;
1023     }
1024     data = args[0];
1025     if (!noptargs) {
1026         goto skip_optional_kwonly;
1027     }
1028     if (args[1]) {
1029         fix_imports = PyObject_IsTrue(args[1]);
1030         if (fix_imports < 0) {
1031             goto exit;
1032         }
1033         if (!--noptargs) {
1034             goto skip_optional_kwonly;
1035         }
1036     }
1037     if (args[2]) {
1038         if (!PyUnicode_Check(args[2])) {
1039             _PyArg_BadArgument("loads", "argument 'encoding'", "str", args[2]);
1040             goto exit;
1041         }
1042         Py_ssize_t encoding_length;
1043         encoding = PyUnicode_AsUTF8AndSize(args[2], &encoding_length);
1044         if (encoding == NULL) {
1045             goto exit;
1046         }
1047         if (strlen(encoding) != (size_t)encoding_length) {
1048             PyErr_SetString(PyExc_ValueError, "embedded null character");
1049             goto exit;
1050         }
1051         if (!--noptargs) {
1052             goto skip_optional_kwonly;
1053         }
1054     }
1055     if (args[3]) {
1056         if (!PyUnicode_Check(args[3])) {
1057             _PyArg_BadArgument("loads", "argument 'errors'", "str", args[3]);
1058             goto exit;
1059         }
1060         Py_ssize_t errors_length;
1061         errors = PyUnicode_AsUTF8AndSize(args[3], &errors_length);
1062         if (errors == NULL) {
1063             goto exit;
1064         }
1065         if (strlen(errors) != (size_t)errors_length) {
1066             PyErr_SetString(PyExc_ValueError, "embedded null character");
1067             goto exit;
1068         }
1069         if (!--noptargs) {
1070             goto skip_optional_kwonly;
1071         }
1072     }
1073     buffers = args[4];
1074 skip_optional_kwonly:
1075     return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors, buffers);
1076 
1077 exit:
1078     return return_value;
1079 }
1080 /*[clinic end generated code: output=c7dd60d20ee4895f input=a9049054013a1b77]*/
1081