Debian Codegraph @

Modules/clinic/_pickle.c.h

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