blob: 1da2f936be4399ee7b465437a1a940fab0d40ba4 [file] [log] [blame]
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +02001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
5PyDoc_STRVAR(_pickle_Pickler_clear_memo__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -08006"clear_memo($self, /)\n"
7"--\n"
8"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +02009"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
19static PyObject *
20_pickle_Pickler_clear_memo_impl(PicklerObject *self);
21
22static PyObject *
23_pickle_Pickler_clear_memo(PicklerObject *self, PyObject *Py_UNUSED(ignored))
24{
25 return _pickle_Pickler_clear_memo_impl(self);
26}
27
28PyDoc_STRVAR(_pickle_Pickler_dump__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080029"dump($self, obj, /)\n"
30"--\n"
31"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +020032"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 Storchaka5bbd2312014-12-16 19:39:08 +020037PyDoc_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
46static Py_ssize_t
47_pickle_Pickler___sizeof___impl(PicklerObject *self);
48
49static 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 Storchaka5dee6552016-06-09 16:16:06 +030056 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +020057 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030058 }
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +020059 return_value = PyLong_FromSsize_t(_return_value);
60
61exit:
62 return return_value;
63}
64
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +020065PyDoc_STRVAR(_pickle_Pickler___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -080066"Pickler(file, protocol=None, fix_imports=True)\n"
67"--\n"
68"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +020069"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 Panter7462b6492015-11-02 03:37:02 +000081"writing, an io.BytesIO instance, or any other custom object that meets\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +020082"this interface.\n"
83"\n"
84"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
85"to map the new Python 3 names to the old module names used in Python\n"
86"2, so that the pickle data stream is readable with Python 2.");
87
88static int
Larry Hastings89964c42015-04-14 18:07:59 -040089_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
90 PyObject *protocol, int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +020091
92static int
93_pickle_Pickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
94{
95 int return_value = -1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030096 static const char * const _keywords[] = {"file", "protocol", "fix_imports", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020097 static _PyArg_Parser _parser = {NULL, _keywords, "Pickler", 0};
98 PyObject *argsbuf[3];
99 PyObject * const *fastargs;
100 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
101 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200102 PyObject *file;
103 PyObject *protocol = NULL;
104 int fix_imports = 1;
105
Serhiy Storchaka31913912019-03-14 10:32:22 +0200106 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 3, 0, argsbuf);
107 if (!fastargs) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200108 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300109 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200110 file = fastargs[0];
111 if (!noptargs) {
112 goto skip_optional_pos;
113 }
114 if (fastargs[1]) {
115 protocol = fastargs[1];
116 if (!--noptargs) {
117 goto skip_optional_pos;
118 }
119 }
120 fix_imports = PyObject_IsTrue(fastargs[2]);
121 if (fix_imports < 0) {
122 goto exit;
123 }
124skip_optional_pos:
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200125 return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports);
126
127exit:
128 return return_value;
129}
130
131PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800132"clear($self, /)\n"
133"--\n"
134"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200135"Remove all items from memo.");
136
137#define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \
138 {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__},
139
140static PyObject *
141_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self);
142
143static PyObject *
144_pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
145{
146 return _pickle_PicklerMemoProxy_clear_impl(self);
147}
148
149PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800150"copy($self, /)\n"
151"--\n"
152"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200153"Copy the memo to a new object.");
154
155#define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \
156 {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__},
157
158static PyObject *
159_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self);
160
161static PyObject *
162_pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
163{
164 return _pickle_PicklerMemoProxy_copy_impl(self);
165}
166
167PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800168"__reduce__($self, /)\n"
169"--\n"
170"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200171"Implement pickle support.");
172
173#define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \
174 {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__},
175
176static PyObject *
177_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self);
178
179static PyObject *
180_pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
181{
182 return _pickle_PicklerMemoProxy___reduce___impl(self);
183}
184
185PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800186"load($self, /)\n"
187"--\n"
188"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200189"Load a pickle.\n"
190"\n"
191"Read a pickled object representation from the open file object given\n"
192"in the constructor, and return the reconstituted object hierarchy\n"
193"specified therein.");
194
195#define _PICKLE_UNPICKLER_LOAD_METHODDEF \
196 {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__},
197
198static PyObject *
199_pickle_Unpickler_load_impl(UnpicklerObject *self);
200
201static PyObject *
202_pickle_Unpickler_load(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
203{
204 return _pickle_Unpickler_load_impl(self);
205}
206
207PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800208"find_class($self, module_name, global_name, /)\n"
209"--\n"
210"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200211"Return an object from a specified module.\n"
212"\n"
213"If necessary, the module will be imported. Subclasses may override\n"
214"this method (e.g. to restrict unpickling of arbitrary classes and\n"
215"functions).\n"
216"\n"
217"This method is called whenever a class or a function object is\n"
218"needed. Both arguments passed are str objects.");
219
220#define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200221 {"find_class", (PyCFunction)(void(*)(void))_pickle_Unpickler_find_class, METH_FASTCALL, _pickle_Unpickler_find_class__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200222
223static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400224_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
225 PyObject *module_name,
226 PyObject *global_name);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200227
228static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200229_pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200230{
231 PyObject *return_value = NULL;
232 PyObject *module_name;
233 PyObject *global_name;
234
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200235 if (!_PyArg_CheckPositional("find_class", nargs, 2, 2)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100236 goto exit;
237 }
Serhiy Storchaka2a39d252019-01-11 18:01:42 +0200238 module_name = args[0];
239 global_name = args[1];
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200240 return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name);
241
242exit:
243 return return_value;
244}
245
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200246PyDoc_STRVAR(_pickle_Unpickler___sizeof____doc__,
247"__sizeof__($self, /)\n"
248"--\n"
249"\n"
250"Returns size in memory, in bytes.");
251
252#define _PICKLE_UNPICKLER___SIZEOF___METHODDEF \
253 {"__sizeof__", (PyCFunction)_pickle_Unpickler___sizeof__, METH_NOARGS, _pickle_Unpickler___sizeof____doc__},
254
255static Py_ssize_t
256_pickle_Unpickler___sizeof___impl(UnpicklerObject *self);
257
258static PyObject *
259_pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
260{
261 PyObject *return_value = NULL;
262 Py_ssize_t _return_value;
263
264 _return_value = _pickle_Unpickler___sizeof___impl(self);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300265 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200266 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300267 }
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200268 return_value = PyLong_FromSsize_t(_return_value);
269
270exit:
271 return return_value;
272}
273
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200274PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800275"Unpickler(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
276"--\n"
277"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200278"This takes a binary file for reading a pickle data stream.\n"
279"\n"
280"The protocol version of the pickle is detected automatically, so no\n"
281"protocol argument is needed. Bytes past the pickled object\'s\n"
282"representation are ignored.\n"
283"\n"
284"The argument *file* must have two methods, a read() method that takes\n"
285"an integer argument, and a readline() method that requires no\n"
286"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000287"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200288"other custom object that meets this interface.\n"
289"\n"
290"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000291"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200292"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
293"map the old Python 2 names to the new names used in Python 3. The\n"
294"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
295"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
296"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
297"string instances as bytes objects.");
298
299static int
Larry Hastings89964c42015-04-14 18:07:59 -0400300_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
301 int fix_imports, const char *encoding,
302 const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200303
304static int
305_pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
306{
307 int return_value = -1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300308 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200309 static _PyArg_Parser _parser = {NULL, _keywords, "Unpickler", 0};
310 PyObject *argsbuf[4];
311 PyObject * const *fastargs;
312 Py_ssize_t nargs = PyTuple_GET_SIZE(args);
313 Py_ssize_t noptargs = nargs + (kwargs ? PyDict_GET_SIZE(kwargs) : 0) - 1;
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200314 PyObject *file;
315 int fix_imports = 1;
316 const char *encoding = "ASCII";
317 const char *errors = "strict";
318
Serhiy Storchaka31913912019-03-14 10:32:22 +0200319 fastargs = _PyArg_UnpackKeywords(_PyTuple_CAST(args)->ob_item, nargs, kwargs, NULL, &_parser, 1, 1, 0, argsbuf);
320 if (!fastargs) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200321 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300322 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200323 file = fastargs[0];
324 if (!noptargs) {
325 goto skip_optional_kwonly;
326 }
327 if (fastargs[1]) {
328 fix_imports = PyObject_IsTrue(fastargs[1]);
329 if (fix_imports < 0) {
330 goto exit;
331 }
332 if (!--noptargs) {
333 goto skip_optional_kwonly;
334 }
335 }
336 if (fastargs[2]) {
337 if (!PyUnicode_Check(fastargs[2])) {
338 _PyArg_BadArgument("Unpickler", 3, "str", fastargs[2]);
339 goto exit;
340 }
341 Py_ssize_t encoding_length;
342 encoding = PyUnicode_AsUTF8AndSize(fastargs[2], &encoding_length);
343 if (encoding == NULL) {
344 goto exit;
345 }
346 if (strlen(encoding) != (size_t)encoding_length) {
347 PyErr_SetString(PyExc_ValueError, "embedded null character");
348 goto exit;
349 }
350 if (!--noptargs) {
351 goto skip_optional_kwonly;
352 }
353 }
354 if (!PyUnicode_Check(fastargs[3])) {
355 _PyArg_BadArgument("Unpickler", 4, "str", fastargs[3]);
356 goto exit;
357 }
358 Py_ssize_t errors_length;
359 errors = PyUnicode_AsUTF8AndSize(fastargs[3], &errors_length);
360 if (errors == NULL) {
361 goto exit;
362 }
363 if (strlen(errors) != (size_t)errors_length) {
364 PyErr_SetString(PyExc_ValueError, "embedded null character");
365 goto exit;
366 }
367skip_optional_kwonly:
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200368 return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
369
370exit:
371 return return_value;
372}
373
374PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800375"clear($self, /)\n"
376"--\n"
377"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200378"Remove all items from memo.");
379
380#define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \
381 {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
382
383static PyObject *
384_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self);
385
386static PyObject *
387_pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
388{
389 return _pickle_UnpicklerMemoProxy_clear_impl(self);
390}
391
392PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800393"copy($self, /)\n"
394"--\n"
395"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200396"Copy the memo to a new object.");
397
398#define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \
399 {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
400
401static PyObject *
402_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self);
403
404static PyObject *
405_pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
406{
407 return _pickle_UnpicklerMemoProxy_copy_impl(self);
408}
409
410PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800411"__reduce__($self, /)\n"
412"--\n"
413"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200414"Implement pickling support.");
415
416#define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \
417 {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
418
419static PyObject *
420_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self);
421
422static PyObject *
423_pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
424{
425 return _pickle_UnpicklerMemoProxy___reduce___impl(self);
426}
427
428PyDoc_STRVAR(_pickle_dump__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800429"dump($module, /, obj, file, protocol=None, *, fix_imports=True)\n"
430"--\n"
431"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200432"Write a pickled representation of obj to the open file object file.\n"
433"\n"
434"This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n"
435"be more efficient.\n"
436"\n"
437"The optional *protocol* argument tells the pickler to use the given\n"
Łukasz Langac51d8c92018-04-03 23:06:53 -0700438"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
439"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
440"with previous versions.\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200441"\n"
442"Specifying a negative protocol version selects the highest protocol\n"
443"version supported. The higher the protocol used, the more recent the\n"
444"version of Python needed to read the pickle produced.\n"
445"\n"
446"The *file* argument must have a write() method that accepts a single\n"
447"bytes argument. It can thus be a file object opened for binary\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000448"writing, an io.BytesIO instance, or any other custom object that meets\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200449"this interface.\n"
450"\n"
451"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
452"to map the new Python 3 names to the old module names used in Python\n"
453"2, so that the pickle data stream is readable with Python 2.");
454
455#define _PICKLE_DUMP_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200456 {"dump", (PyCFunction)(void(*)(void))_pickle_dump, METH_FASTCALL|METH_KEYWORDS, _pickle_dump__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200457
458static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300459_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -0400460 PyObject *protocol, int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200461
462static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200463_pickle_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200464{
465 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300466 static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200467 static _PyArg_Parser _parser = {NULL, _keywords, "dump", 0};
468 PyObject *argsbuf[4];
469 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 2;
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200470 PyObject *obj;
471 PyObject *file;
472 PyObject *protocol = NULL;
473 int fix_imports = 1;
474
Serhiy Storchaka31913912019-03-14 10:32:22 +0200475 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 2, 3, 0, argsbuf);
476 if (!args) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200477 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300478 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200479 obj = args[0];
480 file = args[1];
481 if (!noptargs) {
482 goto skip_optional_pos;
483 }
484 if (args[2]) {
485 protocol = args[2];
486 if (!--noptargs) {
487 goto skip_optional_pos;
488 }
489 }
490skip_optional_pos:
491 if (!noptargs) {
492 goto skip_optional_kwonly;
493 }
494 fix_imports = PyObject_IsTrue(args[3]);
495 if (fix_imports < 0) {
496 goto exit;
497 }
498skip_optional_kwonly:
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200499 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
500
501exit:
502 return return_value;
503}
504
505PyDoc_STRVAR(_pickle_dumps__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800506"dumps($module, /, obj, protocol=None, *, fix_imports=True)\n"
507"--\n"
508"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200509"Return the pickled representation of the object as a bytes object.\n"
510"\n"
511"The optional *protocol* argument tells the pickler to use the given\n"
512"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
Łukasz Langac51d8c92018-04-03 23:06:53 -0700513"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
514"with previous versions.\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200515"\n"
516"Specifying a negative protocol version selects the highest protocol\n"
517"version supported. The higher the protocol used, the more recent the\n"
518"version of Python needed to read the pickle produced.\n"
519"\n"
520"If *fix_imports* is True and *protocol* is less than 3, pickle will\n"
521"try to map the new Python 3 names to the old module names used in\n"
522"Python 2, so that the pickle data stream is readable with Python 2.");
523
524#define _PICKLE_DUMPS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200525 {"dumps", (PyCFunction)(void(*)(void))_pickle_dumps, METH_FASTCALL|METH_KEYWORDS, _pickle_dumps__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200526
527static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300528_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -0400529 int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200530
531static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200532_pickle_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200533{
534 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300535 static const char * const _keywords[] = {"obj", "protocol", "fix_imports", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200536 static _PyArg_Parser _parser = {NULL, _keywords, "dumps", 0};
537 PyObject *argsbuf[3];
538 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200539 PyObject *obj;
540 PyObject *protocol = NULL;
541 int fix_imports = 1;
542
Serhiy Storchaka31913912019-03-14 10:32:22 +0200543 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf);
544 if (!args) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200545 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300546 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200547 obj = args[0];
548 if (!noptargs) {
549 goto skip_optional_pos;
550 }
551 if (args[1]) {
552 protocol = args[1];
553 if (!--noptargs) {
554 goto skip_optional_pos;
555 }
556 }
557skip_optional_pos:
558 if (!noptargs) {
559 goto skip_optional_kwonly;
560 }
561 fix_imports = PyObject_IsTrue(args[2]);
562 if (fix_imports < 0) {
563 goto exit;
564 }
565skip_optional_kwonly:
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200566 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
567
568exit:
569 return return_value;
570}
571
572PyDoc_STRVAR(_pickle_load__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800573"load($module, /, file, *, fix_imports=True, encoding=\'ASCII\',\n"
574" errors=\'strict\')\n"
575"--\n"
576"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200577"Read and return an object from the pickle data stored in a file.\n"
578"\n"
579"This is equivalent to ``Unpickler(file).load()``, but may be more\n"
580"efficient.\n"
581"\n"
582"The protocol version of the pickle is detected automatically, so no\n"
583"protocol argument is needed. Bytes past the pickled object\'s\n"
584"representation are ignored.\n"
585"\n"
586"The argument *file* must have two methods, a read() method that takes\n"
587"an integer argument, and a readline() method that requires no\n"
588"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000589"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200590"other custom object that meets this interface.\n"
591"\n"
592"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000593"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200594"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
595"map the old Python 2 names to the new names used in Python 3. The\n"
596"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
597"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
598"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
599"string instances as bytes objects.");
600
601#define _PICKLE_LOAD_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200602 {"load", (PyCFunction)(void(*)(void))_pickle_load, METH_FASTCALL|METH_KEYWORDS, _pickle_load__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200603
604static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300605_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400606 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200607
608static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200609_pickle_load(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200610{
611 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300612 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200613 static _PyArg_Parser _parser = {NULL, _keywords, "load", 0};
614 PyObject *argsbuf[4];
615 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200616 PyObject *file;
617 int fix_imports = 1;
618 const char *encoding = "ASCII";
619 const char *errors = "strict";
620
Serhiy Storchaka31913912019-03-14 10:32:22 +0200621 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
622 if (!args) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200623 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300624 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200625 file = args[0];
626 if (!noptargs) {
627 goto skip_optional_kwonly;
628 }
629 if (args[1]) {
630 fix_imports = PyObject_IsTrue(args[1]);
631 if (fix_imports < 0) {
632 goto exit;
633 }
634 if (!--noptargs) {
635 goto skip_optional_kwonly;
636 }
637 }
638 if (args[2]) {
639 if (!PyUnicode_Check(args[2])) {
640 _PyArg_BadArgument("load", 3, "str", args[2]);
641 goto exit;
642 }
643 Py_ssize_t encoding_length;
644 encoding = PyUnicode_AsUTF8AndSize(args[2], &encoding_length);
645 if (encoding == NULL) {
646 goto exit;
647 }
648 if (strlen(encoding) != (size_t)encoding_length) {
649 PyErr_SetString(PyExc_ValueError, "embedded null character");
650 goto exit;
651 }
652 if (!--noptargs) {
653 goto skip_optional_kwonly;
654 }
655 }
656 if (!PyUnicode_Check(args[3])) {
657 _PyArg_BadArgument("load", 4, "str", args[3]);
658 goto exit;
659 }
660 Py_ssize_t errors_length;
661 errors = PyUnicode_AsUTF8AndSize(args[3], &errors_length);
662 if (errors == NULL) {
663 goto exit;
664 }
665 if (strlen(errors) != (size_t)errors_length) {
666 PyErr_SetString(PyExc_ValueError, "embedded null character");
667 goto exit;
668 }
669skip_optional_kwonly:
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200670 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
671
672exit:
673 return return_value;
674}
675
676PyDoc_STRVAR(_pickle_loads__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800677"loads($module, /, data, *, fix_imports=True, encoding=\'ASCII\',\n"
678" errors=\'strict\')\n"
679"--\n"
680"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200681"Read and return an object from the given pickle data.\n"
682"\n"
683"The protocol version of the pickle is detected automatically, so no\n"
684"protocol argument is needed. Bytes past the pickled object\'s\n"
685"representation are ignored.\n"
686"\n"
687"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000688"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200689"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
690"map the old Python 2 names to the new names used in Python 3. The\n"
691"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
692"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
693"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
694"string instances as bytes objects.");
695
696#define _PICKLE_LOADS_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200697 {"loads", (PyCFunction)(void(*)(void))_pickle_loads, METH_FASTCALL|METH_KEYWORDS, _pickle_loads__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200698
699static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300700_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400701 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200702
703static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200704_pickle_loads(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200705{
706 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300707 static const char * const _keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200708 static _PyArg_Parser _parser = {NULL, _keywords, "loads", 0};
709 PyObject *argsbuf[4];
710 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200711 PyObject *data;
712 int fix_imports = 1;
713 const char *encoding = "ASCII";
714 const char *errors = "strict";
715
Serhiy Storchaka31913912019-03-14 10:32:22 +0200716 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 1, 0, argsbuf);
717 if (!args) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200718 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300719 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200720 data = args[0];
721 if (!noptargs) {
722 goto skip_optional_kwonly;
723 }
724 if (args[1]) {
725 fix_imports = PyObject_IsTrue(args[1]);
726 if (fix_imports < 0) {
727 goto exit;
728 }
729 if (!--noptargs) {
730 goto skip_optional_kwonly;
731 }
732 }
733 if (args[2]) {
734 if (!PyUnicode_Check(args[2])) {
735 _PyArg_BadArgument("loads", 3, "str", args[2]);
736 goto exit;
737 }
738 Py_ssize_t encoding_length;
739 encoding = PyUnicode_AsUTF8AndSize(args[2], &encoding_length);
740 if (encoding == NULL) {
741 goto exit;
742 }
743 if (strlen(encoding) != (size_t)encoding_length) {
744 PyErr_SetString(PyExc_ValueError, "embedded null character");
745 goto exit;
746 }
747 if (!--noptargs) {
748 goto skip_optional_kwonly;
749 }
750 }
751 if (!PyUnicode_Check(args[3])) {
752 _PyArg_BadArgument("loads", 4, "str", args[3]);
753 goto exit;
754 }
755 Py_ssize_t errors_length;
756 errors = PyUnicode_AsUTF8AndSize(args[3], &errors_length);
757 if (errors == NULL) {
758 goto exit;
759 }
760 if (strlen(errors) != (size_t)errors_length) {
761 PyErr_SetString(PyExc_ValueError, "embedded null character");
762 goto exit;
763 }
764skip_optional_kwonly:
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200765 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
766
767exit:
768 return return_value;
769}
Serhiy Storchaka31913912019-03-14 10:32:22 +0200770/*[clinic end generated code: output=8f972562c8f71e2b input=a9049054013a1b77]*/