blob: 6d9072832ceaa8610d88ae35e90a44d95f2e8522 [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};
97 static _PyArg_Parser _parser = {"O|Op:Pickler", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +020098 PyObject *file;
99 PyObject *protocol = NULL;
100 int fix_imports = 1;
101
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300102 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300103 &file, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200104 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300105 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200106 return_value = _pickle_Pickler___init___impl((PicklerObject *)self, file, protocol, fix_imports);
107
108exit:
109 return return_value;
110}
111
112PyDoc_STRVAR(_pickle_PicklerMemoProxy_clear__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800113"clear($self, /)\n"
114"--\n"
115"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200116"Remove all items from memo.");
117
118#define _PICKLE_PICKLERMEMOPROXY_CLEAR_METHODDEF \
119 {"clear", (PyCFunction)_pickle_PicklerMemoProxy_clear, METH_NOARGS, _pickle_PicklerMemoProxy_clear__doc__},
120
121static PyObject *
122_pickle_PicklerMemoProxy_clear_impl(PicklerMemoProxyObject *self);
123
124static PyObject *
125_pickle_PicklerMemoProxy_clear(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
126{
127 return _pickle_PicklerMemoProxy_clear_impl(self);
128}
129
130PyDoc_STRVAR(_pickle_PicklerMemoProxy_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800131"copy($self, /)\n"
132"--\n"
133"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200134"Copy the memo to a new object.");
135
136#define _PICKLE_PICKLERMEMOPROXY_COPY_METHODDEF \
137 {"copy", (PyCFunction)_pickle_PicklerMemoProxy_copy, METH_NOARGS, _pickle_PicklerMemoProxy_copy__doc__},
138
139static PyObject *
140_pickle_PicklerMemoProxy_copy_impl(PicklerMemoProxyObject *self);
141
142static PyObject *
143_pickle_PicklerMemoProxy_copy(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
144{
145 return _pickle_PicklerMemoProxy_copy_impl(self);
146}
147
148PyDoc_STRVAR(_pickle_PicklerMemoProxy___reduce____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800149"__reduce__($self, /)\n"
150"--\n"
151"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200152"Implement pickle support.");
153
154#define _PICKLE_PICKLERMEMOPROXY___REDUCE___METHODDEF \
155 {"__reduce__", (PyCFunction)_pickle_PicklerMemoProxy___reduce__, METH_NOARGS, _pickle_PicklerMemoProxy___reduce____doc__},
156
157static PyObject *
158_pickle_PicklerMemoProxy___reduce___impl(PicklerMemoProxyObject *self);
159
160static PyObject *
161_pickle_PicklerMemoProxy___reduce__(PicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
162{
163 return _pickle_PicklerMemoProxy___reduce___impl(self);
164}
165
166PyDoc_STRVAR(_pickle_Unpickler_load__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800167"load($self, /)\n"
168"--\n"
169"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200170"Load a pickle.\n"
171"\n"
172"Read a pickled object representation from the open file object given\n"
173"in the constructor, and return the reconstituted object hierarchy\n"
174"specified therein.");
175
176#define _PICKLE_UNPICKLER_LOAD_METHODDEF \
177 {"load", (PyCFunction)_pickle_Unpickler_load, METH_NOARGS, _pickle_Unpickler_load__doc__},
178
179static PyObject *
180_pickle_Unpickler_load_impl(UnpicklerObject *self);
181
182static PyObject *
183_pickle_Unpickler_load(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
184{
185 return _pickle_Unpickler_load_impl(self);
186}
187
188PyDoc_STRVAR(_pickle_Unpickler_find_class__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800189"find_class($self, module_name, global_name, /)\n"
190"--\n"
191"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200192"Return an object from a specified module.\n"
193"\n"
194"If necessary, the module will be imported. Subclasses may override\n"
195"this method (e.g. to restrict unpickling of arbitrary classes and\n"
196"functions).\n"
197"\n"
198"This method is called whenever a class or a function object is\n"
199"needed. Both arguments passed are str objects.");
200
201#define _PICKLE_UNPICKLER_FIND_CLASS_METHODDEF \
Victor Stinner0c4a8282017-01-17 02:21:47 +0100202 {"find_class", (PyCFunction)_pickle_Unpickler_find_class, METH_FASTCALL, _pickle_Unpickler_find_class__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200203
204static PyObject *
Larry Hastings89964c42015-04-14 18:07:59 -0400205_pickle_Unpickler_find_class_impl(UnpicklerObject *self,
206 PyObject *module_name,
207 PyObject *global_name);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200208
209static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200210_pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200211{
212 PyObject *return_value = NULL;
213 PyObject *module_name;
214 PyObject *global_name;
215
Sylvain74453812017-06-10 06:51:48 +0200216 if (!_PyArg_UnpackStack(args, nargs, "find_class",
217 2, 2,
218 &module_name, &global_name)) {
Victor Stinner0c4a8282017-01-17 02:21:47 +0100219 goto exit;
220 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200221 return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name);
222
223exit:
224 return return_value;
225}
226
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200227PyDoc_STRVAR(_pickle_Unpickler___sizeof____doc__,
228"__sizeof__($self, /)\n"
229"--\n"
230"\n"
231"Returns size in memory, in bytes.");
232
233#define _PICKLE_UNPICKLER___SIZEOF___METHODDEF \
234 {"__sizeof__", (PyCFunction)_pickle_Unpickler___sizeof__, METH_NOARGS, _pickle_Unpickler___sizeof____doc__},
235
236static Py_ssize_t
237_pickle_Unpickler___sizeof___impl(UnpicklerObject *self);
238
239static PyObject *
240_pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
241{
242 PyObject *return_value = NULL;
243 Py_ssize_t _return_value;
244
245 _return_value = _pickle_Unpickler___sizeof___impl(self);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300246 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200247 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300248 }
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200249 return_value = PyLong_FromSsize_t(_return_value);
250
251exit:
252 return return_value;
253}
254
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200255PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800256"Unpickler(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
257"--\n"
258"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200259"This takes a binary file for reading a pickle data stream.\n"
260"\n"
261"The protocol version of the pickle is detected automatically, so no\n"
262"protocol argument is needed. Bytes past the pickled object\'s\n"
263"representation are ignored.\n"
264"\n"
265"The argument *file* must have two methods, a read() method that takes\n"
266"an integer argument, and a readline() method that requires no\n"
267"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000268"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200269"other custom object that meets this interface.\n"
270"\n"
271"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000272"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200273"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
274"map the old Python 2 names to the new names used in Python 3. The\n"
275"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
276"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
277"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
278"string instances as bytes objects.");
279
280static int
Larry Hastings89964c42015-04-14 18:07:59 -0400281_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
282 int fix_imports, const char *encoding,
283 const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200284
285static int
286_pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
287{
288 int return_value = -1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300289 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
290 static _PyArg_Parser _parser = {"O|$pss:Unpickler", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200291 PyObject *file;
292 int fix_imports = 1;
293 const char *encoding = "ASCII";
294 const char *errors = "strict";
295
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300296 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300297 &file, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200298 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300299 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200300 return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
301
302exit:
303 return return_value;
304}
305
306PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800307"clear($self, /)\n"
308"--\n"
309"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200310"Remove all items from memo.");
311
312#define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \
313 {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
314
315static PyObject *
316_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self);
317
318static PyObject *
319_pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
320{
321 return _pickle_UnpicklerMemoProxy_clear_impl(self);
322}
323
324PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800325"copy($self, /)\n"
326"--\n"
327"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200328"Copy the memo to a new object.");
329
330#define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \
331 {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
332
333static PyObject *
334_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self);
335
336static PyObject *
337_pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
338{
339 return _pickle_UnpicklerMemoProxy_copy_impl(self);
340}
341
342PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800343"__reduce__($self, /)\n"
344"--\n"
345"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200346"Implement pickling support.");
347
348#define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \
349 {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
350
351static PyObject *
352_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self);
353
354static PyObject *
355_pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
356{
357 return _pickle_UnpicklerMemoProxy___reduce___impl(self);
358}
359
360PyDoc_STRVAR(_pickle_dump__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800361"dump($module, /, obj, file, protocol=None, *, fix_imports=True)\n"
362"--\n"
363"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200364"Write a pickled representation of obj to the open file object file.\n"
365"\n"
366"This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n"
367"be more efficient.\n"
368"\n"
369"The optional *protocol* argument tells the pickler to use the given\n"
Ɓukasz Langac51d8c92018-04-03 23:06:53 -0700370"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
371"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
372"with previous versions.\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200373"\n"
374"Specifying a negative protocol version selects the highest protocol\n"
375"version supported. The higher the protocol used, the more recent the\n"
376"version of Python needed to read the pickle produced.\n"
377"\n"
378"The *file* argument must have a write() method that accepts a single\n"
379"bytes argument. It can thus be a file object opened for binary\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000380"writing, an io.BytesIO instance, or any other custom object that meets\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200381"this interface.\n"
382"\n"
383"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
384"to map the new Python 3 names to the old module names used in Python\n"
385"2, so that the pickle data stream is readable with Python 2.");
386
387#define _PICKLE_DUMP_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300388 {"dump", (PyCFunction)_pickle_dump, METH_FASTCALL|METH_KEYWORDS, _pickle_dump__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200389
390static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300391_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -0400392 PyObject *protocol, int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200393
394static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200395_pickle_dump(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200396{
397 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300398 static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
399 static _PyArg_Parser _parser = {"OO|O$p:dump", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200400 PyObject *obj;
401 PyObject *file;
402 PyObject *protocol = NULL;
403 int fix_imports = 1;
404
Victor Stinner3e1fad62017-01-17 01:29:01 +0100405 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300406 &obj, &file, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200407 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300408 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200409 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
410
411exit:
412 return return_value;
413}
414
415PyDoc_STRVAR(_pickle_dumps__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800416"dumps($module, /, obj, protocol=None, *, fix_imports=True)\n"
417"--\n"
418"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200419"Return the pickled representation of the object as a bytes object.\n"
420"\n"
421"The optional *protocol* argument tells the pickler to use the given\n"
422"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
Ɓukasz Langac51d8c92018-04-03 23:06:53 -0700423"protocol is 4. It was introduced in Python 3.4, it is incompatible\n"
424"with previous versions.\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200425"\n"
426"Specifying a negative protocol version selects the highest protocol\n"
427"version supported. The higher the protocol used, the more recent the\n"
428"version of Python needed to read the pickle produced.\n"
429"\n"
430"If *fix_imports* is True and *protocol* is less than 3, pickle will\n"
431"try to map the new Python 3 names to the old module names used in\n"
432"Python 2, so that the pickle data stream is readable with Python 2.");
433
434#define _PICKLE_DUMPS_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300435 {"dumps", (PyCFunction)_pickle_dumps, METH_FASTCALL|METH_KEYWORDS, _pickle_dumps__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200436
437static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300438_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -0400439 int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200440
441static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200442_pickle_dumps(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200443{
444 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300445 static const char * const _keywords[] = {"obj", "protocol", "fix_imports", NULL};
446 static _PyArg_Parser _parser = {"O|O$p:dumps", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200447 PyObject *obj;
448 PyObject *protocol = NULL;
449 int fix_imports = 1;
450
Victor Stinner3e1fad62017-01-17 01:29:01 +0100451 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300452 &obj, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200453 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300454 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200455 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
456
457exit:
458 return return_value;
459}
460
461PyDoc_STRVAR(_pickle_load__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800462"load($module, /, file, *, fix_imports=True, encoding=\'ASCII\',\n"
463" errors=\'strict\')\n"
464"--\n"
465"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200466"Read and return an object from the pickle data stored in a file.\n"
467"\n"
468"This is equivalent to ``Unpickler(file).load()``, but may be more\n"
469"efficient.\n"
470"\n"
471"The protocol version of the pickle is detected automatically, so no\n"
472"protocol argument is needed. Bytes past the pickled object\'s\n"
473"representation are ignored.\n"
474"\n"
475"The argument *file* must have two methods, a read() method that takes\n"
476"an integer argument, and a readline() method that requires no\n"
477"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000478"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200479"other custom object that meets this interface.\n"
480"\n"
481"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000482"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200483"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
484"map the old Python 2 names to the new names used in Python 3. The\n"
485"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
486"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
487"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
488"string instances as bytes objects.");
489
490#define _PICKLE_LOAD_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300491 {"load", (PyCFunction)_pickle_load, METH_FASTCALL|METH_KEYWORDS, _pickle_load__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200492
493static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300494_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400495 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200496
497static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200498_pickle_load(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200499{
500 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300501 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
502 static _PyArg_Parser _parser = {"O|$pss:load", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200503 PyObject *file;
504 int fix_imports = 1;
505 const char *encoding = "ASCII";
506 const char *errors = "strict";
507
Victor Stinner3e1fad62017-01-17 01:29:01 +0100508 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300509 &file, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200510 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300511 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200512 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
513
514exit:
515 return return_value;
516}
517
518PyDoc_STRVAR(_pickle_loads__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800519"loads($module, /, data, *, fix_imports=True, encoding=\'ASCII\',\n"
520" errors=\'strict\')\n"
521"--\n"
522"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200523"Read and return an object from the given pickle data.\n"
524"\n"
525"The protocol version of the pickle is detected automatically, so no\n"
526"protocol argument is needed. Bytes past the pickled object\'s\n"
527"representation are ignored.\n"
528"\n"
529"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000530"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200531"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
532"map the old Python 2 names to the new names used in Python 3. The\n"
533"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
534"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
535"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
536"string instances as bytes objects.");
537
538#define _PICKLE_LOADS_METHODDEF \
Serhiy Storchaka6969eaf2017-07-03 21:20:15 +0300539 {"loads", (PyCFunction)_pickle_loads, METH_FASTCALL|METH_KEYWORDS, _pickle_loads__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200540
541static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300542_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400543 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200544
545static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200546_pickle_loads(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200547{
548 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300549 static const char * const _keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
550 static _PyArg_Parser _parser = {"O|$pss:loads", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200551 PyObject *data;
552 int fix_imports = 1;
553 const char *encoding = "ASCII";
554 const char *errors = "strict";
555
Victor Stinner3e1fad62017-01-17 01:29:01 +0100556 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300557 &data, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200558 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300559 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200560 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
561
562exit:
563 return return_value;
564}
Ɓukasz Langac51d8c92018-04-03 23:06:53 -0700565/*[clinic end generated code: output=6fc104b8299c82dd input=a9049054013a1b77]*/