blob: e10730a6de8b1af09b4e476193d7c215a4aa327b [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 *
Victor Stinner0c4a8282017-01-17 02:21:47 +0100210_pickle_Unpickler_find_class(UnpicklerObject *self, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200211{
212 PyObject *return_value = NULL;
213 PyObject *module_name;
214 PyObject *global_name;
215
Victor Stinner0c4a8282017-01-17 02:21:47 +0100216 if (!_PyArg_UnpackStack(args, nargs, "find_class",
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200217 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 }
Victor Stinner0c4a8282017-01-17 02:21:47 +0100221
222 if (!_PyArg_NoStackKeywords("find_class", kwnames)) {
223 goto exit;
224 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200225 return_value = _pickle_Unpickler_find_class_impl(self, module_name, global_name);
226
227exit:
228 return return_value;
229}
230
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200231PyDoc_STRVAR(_pickle_Unpickler___sizeof____doc__,
232"__sizeof__($self, /)\n"
233"--\n"
234"\n"
235"Returns size in memory, in bytes.");
236
237#define _PICKLE_UNPICKLER___SIZEOF___METHODDEF \
238 {"__sizeof__", (PyCFunction)_pickle_Unpickler___sizeof__, METH_NOARGS, _pickle_Unpickler___sizeof____doc__},
239
240static Py_ssize_t
241_pickle_Unpickler___sizeof___impl(UnpicklerObject *self);
242
243static PyObject *
244_pickle_Unpickler___sizeof__(UnpicklerObject *self, PyObject *Py_UNUSED(ignored))
245{
246 PyObject *return_value = NULL;
247 Py_ssize_t _return_value;
248
249 _return_value = _pickle_Unpickler___sizeof___impl(self);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300250 if ((_return_value == -1) && PyErr_Occurred()) {
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200251 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300252 }
Serhiy Storchaka5bbd2312014-12-16 19:39:08 +0200253 return_value = PyLong_FromSsize_t(_return_value);
254
255exit:
256 return return_value;
257}
258
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200259PyDoc_STRVAR(_pickle_Unpickler___init____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800260"Unpickler(file, *, fix_imports=True, encoding=\'ASCII\', errors=\'strict\')\n"
261"--\n"
262"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200263"This takes a binary file for reading a pickle data stream.\n"
264"\n"
265"The protocol version of the pickle is detected automatically, so no\n"
266"protocol argument is needed. Bytes past the pickled object\'s\n"
267"representation are ignored.\n"
268"\n"
269"The argument *file* must have two methods, a read() method that takes\n"
270"an integer argument, and a readline() method that requires no\n"
271"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000272"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200273"other custom object that meets this interface.\n"
274"\n"
275"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000276"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200277"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
278"map the old Python 2 names to the new names used in Python 3. The\n"
279"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
280"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
281"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
282"string instances as bytes objects.");
283
284static int
Larry Hastings89964c42015-04-14 18:07:59 -0400285_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
286 int fix_imports, const char *encoding,
287 const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200288
289static int
290_pickle_Unpickler___init__(PyObject *self, PyObject *args, PyObject *kwargs)
291{
292 int return_value = -1;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300293 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
294 static _PyArg_Parser _parser = {"O|$pss:Unpickler", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200295 PyObject *file;
296 int fix_imports = 1;
297 const char *encoding = "ASCII";
298 const char *errors = "strict";
299
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300300 if (!_PyArg_ParseTupleAndKeywordsFast(args, kwargs, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300301 &file, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200302 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300303 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200304 return_value = _pickle_Unpickler___init___impl((UnpicklerObject *)self, file, fix_imports, encoding, errors);
305
306exit:
307 return return_value;
308}
309
310PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_clear__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800311"clear($self, /)\n"
312"--\n"
313"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200314"Remove all items from memo.");
315
316#define _PICKLE_UNPICKLERMEMOPROXY_CLEAR_METHODDEF \
317 {"clear", (PyCFunction)_pickle_UnpicklerMemoProxy_clear, METH_NOARGS, _pickle_UnpicklerMemoProxy_clear__doc__},
318
319static PyObject *
320_pickle_UnpicklerMemoProxy_clear_impl(UnpicklerMemoProxyObject *self);
321
322static PyObject *
323_pickle_UnpicklerMemoProxy_clear(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
324{
325 return _pickle_UnpicklerMemoProxy_clear_impl(self);
326}
327
328PyDoc_STRVAR(_pickle_UnpicklerMemoProxy_copy__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800329"copy($self, /)\n"
330"--\n"
331"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200332"Copy the memo to a new object.");
333
334#define _PICKLE_UNPICKLERMEMOPROXY_COPY_METHODDEF \
335 {"copy", (PyCFunction)_pickle_UnpicklerMemoProxy_copy, METH_NOARGS, _pickle_UnpicklerMemoProxy_copy__doc__},
336
337static PyObject *
338_pickle_UnpicklerMemoProxy_copy_impl(UnpicklerMemoProxyObject *self);
339
340static PyObject *
341_pickle_UnpicklerMemoProxy_copy(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
342{
343 return _pickle_UnpicklerMemoProxy_copy_impl(self);
344}
345
346PyDoc_STRVAR(_pickle_UnpicklerMemoProxy___reduce____doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800347"__reduce__($self, /)\n"
348"--\n"
349"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200350"Implement pickling support.");
351
352#define _PICKLE_UNPICKLERMEMOPROXY___REDUCE___METHODDEF \
353 {"__reduce__", (PyCFunction)_pickle_UnpicklerMemoProxy___reduce__, METH_NOARGS, _pickle_UnpicklerMemoProxy___reduce____doc__},
354
355static PyObject *
356_pickle_UnpicklerMemoProxy___reduce___impl(UnpicklerMemoProxyObject *self);
357
358static PyObject *
359_pickle_UnpicklerMemoProxy___reduce__(UnpicklerMemoProxyObject *self, PyObject *Py_UNUSED(ignored))
360{
361 return _pickle_UnpicklerMemoProxy___reduce___impl(self);
362}
363
364PyDoc_STRVAR(_pickle_dump__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800365"dump($module, /, obj, file, protocol=None, *, fix_imports=True)\n"
366"--\n"
367"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200368"Write a pickled representation of obj to the open file object file.\n"
369"\n"
370"This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may\n"
371"be more efficient.\n"
372"\n"
373"The optional *protocol* argument tells the pickler to use the given\n"
374"protocol supported protocols are 0, 1, 2, 3 and 4. The default\n"
375"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
376"\n"
377"Specifying a negative protocol version selects the highest protocol\n"
378"version supported. The higher the protocol used, the more recent the\n"
379"version of Python needed to read the pickle produced.\n"
380"\n"
381"The *file* argument must have a write() method that accepts a single\n"
382"bytes argument. It can thus be a file object opened for binary\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000383"writing, an io.BytesIO instance, or any other custom object that meets\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200384"this interface.\n"
385"\n"
386"If *fix_imports* is True and protocol is less than 3, pickle will try\n"
387"to map the new Python 3 names to the old module names used in Python\n"
388"2, so that the pickle data stream is readable with Python 2.");
389
390#define _PICKLE_DUMP_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700391 {"dump", (PyCFunction)_pickle_dump, METH_FASTCALL, _pickle_dump__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200392
393static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300394_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
Larry Hastings89964c42015-04-14 18:07:59 -0400395 PyObject *protocol, int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200396
397static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700398_pickle_dump(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200399{
400 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300401 static const char * const _keywords[] = {"obj", "file", "protocol", "fix_imports", NULL};
402 static _PyArg_Parser _parser = {"OO|O$p:dump", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200403 PyObject *obj;
404 PyObject *file;
405 PyObject *protocol = NULL;
406 int fix_imports = 1;
407
Victor Stinner3e1fad62017-01-17 01:29:01 +0100408 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300409 &obj, &file, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200410 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300411 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200412 return_value = _pickle_dump_impl(module, obj, file, protocol, fix_imports);
413
414exit:
415 return return_value;
416}
417
418PyDoc_STRVAR(_pickle_dumps__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800419"dumps($module, /, obj, protocol=None, *, fix_imports=True)\n"
420"--\n"
421"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200422"Return the pickled representation of the object as a bytes object.\n"
423"\n"
424"The optional *protocol* argument tells the pickler to use the given\n"
425"protocol; supported protocols are 0, 1, 2, 3 and 4. The default\n"
426"protocol is 3; a backward-incompatible protocol designed for Python 3.\n"
427"\n"
428"Specifying a negative protocol version selects the highest protocol\n"
429"version supported. The higher the protocol used, the more recent the\n"
430"version of Python needed to read the pickle produced.\n"
431"\n"
432"If *fix_imports* is True and *protocol* is less than 3, pickle will\n"
433"try to map the new Python 3 names to the old module names used in\n"
434"Python 2, so that the pickle data stream is readable with Python 2.");
435
436#define _PICKLE_DUMPS_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700437 {"dumps", (PyCFunction)_pickle_dumps, METH_FASTCALL, _pickle_dumps__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200438
439static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300440_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
Larry Hastings89964c42015-04-14 18:07:59 -0400441 int fix_imports);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200442
443static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700444_pickle_dumps(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200445{
446 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300447 static const char * const _keywords[] = {"obj", "protocol", "fix_imports", NULL};
448 static _PyArg_Parser _parser = {"O|O$p:dumps", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200449 PyObject *obj;
450 PyObject *protocol = NULL;
451 int fix_imports = 1;
452
Victor Stinner3e1fad62017-01-17 01:29:01 +0100453 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300454 &obj, &protocol, &fix_imports)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200455 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300456 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200457 return_value = _pickle_dumps_impl(module, obj, protocol, fix_imports);
458
459exit:
460 return return_value;
461}
462
463PyDoc_STRVAR(_pickle_load__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800464"load($module, /, file, *, fix_imports=True, encoding=\'ASCII\',\n"
465" errors=\'strict\')\n"
466"--\n"
467"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200468"Read and return an object from the pickle data stored in a file.\n"
469"\n"
470"This is equivalent to ``Unpickler(file).load()``, but may be more\n"
471"efficient.\n"
472"\n"
473"The protocol version of the pickle is detected automatically, so no\n"
474"protocol argument is needed. Bytes past the pickled object\'s\n"
475"representation are ignored.\n"
476"\n"
477"The argument *file* must have two methods, a read() method that takes\n"
478"an integer argument, and a readline() method that requires no\n"
479"arguments. Both methods should return bytes. Thus *file* can be a\n"
Martin Panter7462b6492015-11-02 03:37:02 +0000480"binary file object opened for reading, an io.BytesIO object, or any\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200481"other custom object that meets this interface.\n"
482"\n"
483"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000484"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200485"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
486"map the old Python 2 names to the new names used in Python 3. The\n"
487"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
488"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
489"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
490"string instances as bytes objects.");
491
492#define _PICKLE_LOAD_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700493 {"load", (PyCFunction)_pickle_load, METH_FASTCALL, _pickle_load__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200494
495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300496_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400497 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200498
499static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700500_pickle_load(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200501{
502 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300503 static const char * const _keywords[] = {"file", "fix_imports", "encoding", "errors", NULL};
504 static _PyArg_Parser _parser = {"O|$pss:load", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200505 PyObject *file;
506 int fix_imports = 1;
507 const char *encoding = "ASCII";
508 const char *errors = "strict";
509
Victor Stinner3e1fad62017-01-17 01:29:01 +0100510 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300511 &file, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200512 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300513 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200514 return_value = _pickle_load_impl(module, file, fix_imports, encoding, errors);
515
516exit:
517 return return_value;
518}
519
520PyDoc_STRVAR(_pickle_loads__doc__,
Larry Hastings2623c8c2014-02-08 22:15:29 -0800521"loads($module, /, data, *, fix_imports=True, encoding=\'ASCII\',\n"
522" errors=\'strict\')\n"
523"--\n"
524"\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200525"Read and return an object from the given pickle data.\n"
526"\n"
527"The protocol version of the pickle is detected automatically, so no\n"
528"protocol argument is needed. Bytes past the pickled object\'s\n"
529"representation are ignored.\n"
530"\n"
531"Optional keyword arguments are *fix_imports*, *encoding* and *errors*,\n"
Martin Panter46f50722016-05-26 05:35:26 +0000532"which are used to control compatibility support for pickle stream\n"
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200533"generated by Python 2. If *fix_imports* is True, pickle will try to\n"
534"map the old Python 2 names to the new names used in Python 3. The\n"
535"*encoding* and *errors* tell pickle how to decode 8-bit string\n"
536"instances pickled by Python 2; these default to \'ASCII\' and \'strict\',\n"
537"respectively. The *encoding* can be \'bytes\' to read these 8-bit\n"
538"string instances as bytes objects.");
539
540#define _PICKLE_LOADS_METHODDEF \
Victor Stinner37e4ef72016-09-09 20:00:13 -0700541 {"loads", (PyCFunction)_pickle_loads, METH_FASTCALL, _pickle_loads__doc__},
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200542
543static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300544_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
Larry Hastings89964c42015-04-14 18:07:59 -0400545 const char *encoding, const char *errors);
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200546
547static PyObject *
Victor Stinner37e4ef72016-09-09 20:00:13 -0700548_pickle_loads(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200549{
550 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300551 static const char * const _keywords[] = {"data", "fix_imports", "encoding", "errors", NULL};
552 static _PyArg_Parser _parser = {"O|$pss:loads", _keywords, 0};
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200553 PyObject *data;
554 int fix_imports = 1;
555 const char *encoding = "ASCII";
556 const char *errors = "strict";
557
Victor Stinner3e1fad62017-01-17 01:29:01 +0100558 if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser,
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300559 &data, &fix_imports, &encoding, &errors)) {
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200560 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300561 }
Serhiy Storchaka3c1f0f12014-01-27 10:34:22 +0200562 return_value = _pickle_loads_impl(module, data, fix_imports, encoding, errors);
563
564exit:
565 return return_value;
566}
Victor Stinner0c4a8282017-01-17 02:21:47 +0100567/*[clinic end generated code: output=b921d325b2f7a096 input=a9049054013a1b77]*/