blob: 9ad4c37f4765f7d753d8523540025b3cf226ad8d [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 \
202 {"find_class", (PyCFunction)_pickle_Unpickler_find_class, METH_VARARGS, _pickle_Unpickler_find_class__doc__},
203
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 *
210_pickle_Unpickler_find_class(UnpicklerObject *self, PyObject *args)
211{
212 PyObject *return_value = NULL;
213 PyObject *module_name;
214 PyObject *global_name;
215
216 if (!PyArg_UnpackTuple(args, "find_class",
217 2, 2,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300218 &module_name, &global_name)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200219 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300220 }
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"
370"protocol supported protocols are 0, 1, 2, 3 and 4. The default\n"
371"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
372"\n"
373"Specifying a negative protocol version selects the highest protocol\n"
374"version supported. The higher the protocol used, the more recent the\n"
375"version of Python needed to read the pickle produced.\n"
376"\n"
377"The *file* argument must have a write() method that accepts a single\n"
378"bytes argument. It can thus be a file object opened for binary\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000379"writing, an io.BytesIO instance, or any other custom object that meets\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200380"this interface.\n"
381"\n"
382"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
383"to map the new Python 3 names to the old module names used in Python\n"
384"2, so that the pickle data stream is readable with Python 2.");
385
386#define _PICKLE_DUMP_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700387 {"dump", (PyCFunction)_pickle_dump, METH_FASTCALL, _pickle_dump__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200388
389static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300390_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -0400391 PyObject *protocol, int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200392
393static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700394_pickle_dump(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200395{
396 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300397 static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
398 static _PyArg_Parser _parser = {"OO|O$p:dump", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200399 PyObject *obj;
400 PyObject *file;
401 PyObject *protocol = NULL;
402 int fix_imports = 1;
403
Victor Stinner37e4ef72016-09-09 20:00:13 -0700404 if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300405 &obj, &file, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200406 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300407 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200408 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
409
410exit:
411 return return_value;
412}
413
414PyDoc_STRVAR(_pickle_dumps__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800415"dumps($module, /, obj, protocol=None, *, fix_imports=True)\n"
416"--\n"
417"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200418"Return the pickled representation of the object as a bytes object.\n"
419"\n"
420"The optional *protocol* argument tells the pickler to use the given\n"
421"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
422"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
423"\n"
424"Specifying a negative protocol version selects the highest protocol\n"
425"version supported. The higher the protocol used, the more recent the\n"
426"version of Python needed to read the pickle produced.\n"
427"\n"
428"If *fix_imports* is True and *protocol* is less than 3, pickle will\n"
429"try to map the new Python 3 names to the old module names used in\n"
430"Python 2, so that the pickle data stream is readable with Python 2.");
431
432#define _PICKLE_DUMPS_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700433 {"dumps", (PyCFunction)_pickle_dumps, METH_FASTCALL, _pickle_dumps__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200434
435static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300436_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -0400437 int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200438
439static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700440_pickle_dumps(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200441{
442 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300443 static const char * const _keywords[] = {"obj", "protocol", "fix_imports", NULL};
444 static _PyArg_Parser _parser = {"O|O$p:dumps", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200445 PyObject *obj;
446 PyObject *protocol = NULL;
447 int fix_imports = 1;
448
Victor Stinner37e4ef72016-09-09 20:00:13 -0700449 if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300450 &obj, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200451 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300452 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200453 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
454
455exit:
456 return return_value;
457}
458
459PyDoc_STRVAR(_pickle_load__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800460"load($module, /, file, *, fix_imports=True, encoding=\'ASCII\',\n"
461" errors=\'strict\')\n"
462"--\n"
463"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200464"Read and return an object from the pickle data stored in a file.\n"
465"\n"
466"This is equivalent to ``Unpickler(file).load()``, but may be more\n"
467"efficient.\n"
468"\n"
469"The protocol version of the pickle is detected automatically, so no\n"
470"protocol argument is needed. Bytes past the pickled object\'s\n"
471"representation are ignored.\n"
472"\n"
473"The argument *file* must have two methods, a read() method that takes\n"
474"an integer argument, and a readline() method that requires no\n"
475"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000476"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200477"other custom object that meets this interface.\n"
478"\n"
479"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000480"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200481"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
482"map the old Python 2 names to the new names used in Python 3. The\n"
483"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
484"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
485"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
486"string instances as bytes objects.");
487
488#define _PICKLE_LOAD_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700489 {"load", (PyCFunction)_pickle_load, METH_FASTCALL, _pickle_load__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200490
491static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300492_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400493 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200494
495static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700496_pickle_load(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200497{
498 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300499 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
500 static _PyArg_Parser _parser = {"O|$pss:load", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200501 PyObject *file;
502 int fix_imports = 1;
503 const char *encoding = "ASCII";
504 const char *errors = "strict";
505
Victor Stinner37e4ef72016-09-09 20:00:13 -0700506 if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300507 &file, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200508 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300509 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200510 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
511
512exit:
513 return return_value;
514}
515
516PyDoc_STRVAR(_pickle_loads__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800517"loads($module, /, data, *, fix_imports=True, encoding=\'ASCII\',\n"
518" errors=\'strict\')\n"
519"--\n"
520"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200521"Read and return an object from the given pickle data.\n"
522"\n"
523"The protocol version of the pickle is detected automatically, so no\n"
524"protocol argument is needed. Bytes past the pickled object\'s\n"
525"representation are ignored.\n"
526"\n"
527"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000528"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200529"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
530"map the old Python 2 names to the new names used in Python 3. The\n"
531"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
532"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
533"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
534"string instances as bytes objects.");
535
536#define _PICKLE_LOADS_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700537 {"loads", (PyCFunction)_pickle_loads, METH_FASTCALL, _pickle_loads__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200538
539static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300540_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400541 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200542
543static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700544_pickle_loads(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200545{
546 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300547 static const char * const _keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
548 static _PyArg_Parser _parser = {"O|$pss:loads", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200549 PyObject *data;
550 int fix_imports = 1;
551 const char *encoding = "ASCII";
552 const char *errors = "strict";
553
Victor Stinner37e4ef72016-09-09 20:00:13 -0700554 if (!_PyArg_ParseStack(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300555 &data, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200556 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300557 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200558 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
559
560exit:
561 return return_value;
562}
Victor Stinner37e4ef72016-09-09 20:00:13 -0700563/*[clinic end generated code: output=82be137b3c09cb9f input=a9049054013a1b77]*/