aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/tools/python3/src/Modules/_pickle.c
diff options
context:
space:
mode:
authorshadchin <shadchin@yandex-team.ru>2022-02-10 16:44:39 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:44:39 +0300
commite9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch)
tree64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Modules/_pickle.c
parent2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff)
downloadydb-e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0.tar.gz
Restoring authorship annotation for <shadchin@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Modules/_pickle.c')
-rw-r--r--contrib/tools/python3/src/Modules/_pickle.c1490
1 files changed, 745 insertions, 745 deletions
diff --git a/contrib/tools/python3/src/Modules/_pickle.c b/contrib/tools/python3/src/Modules/_pickle.c
index 6577e77112..30bb5c0d75 100644
--- a/contrib/tools/python3/src/Modules/_pickle.c
+++ b/contrib/tools/python3/src/Modules/_pickle.c
@@ -1,15 +1,15 @@
-/* pickle accelerator C extensor: _pickle module.
- *
- * It is built as a built-in module (Py_BUILD_CORE_BUILTIN define) on Windows
- * and as an extension module (Py_BUILD_CORE_MODULE define) on other
- * platforms. */
-
-#if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE)
-# error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined"
+/* pickle accelerator C extensor: _pickle module.
+ *
+ * It is built as a built-in module (Py_BUILD_CORE_BUILTIN define) on Windows
+ * and as an extension module (Py_BUILD_CORE_MODULE define) on other
+ * platforms. */
+
+#if !defined(Py_BUILD_CORE_BUILTIN) && !defined(Py_BUILD_CORE_MODULE)
+# error "Py_BUILD_CORE_BUILTIN or Py_BUILD_CORE_MODULE must be defined"
#endif
#include "Python.h"
-#include "structmember.h" // PyMemberDef
+#include "structmember.h" // PyMemberDef
PyDoc_STRVAR(pickle_module_doc,
"Optimized C implementation for the Python pickle module.");
@@ -23,12 +23,12 @@ class _pickle.UnpicklerMemoProxy "UnpicklerMemoProxyObject *" "&UnpicklerMemoPro
[clinic start generated code]*/
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b3e113468a58e6c]*/
-/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
- Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
- already includes it. */
+/* Bump HIGHEST_PROTOCOL when new opcodes are added to the pickle protocol.
+ Bump DEFAULT_PROTOCOL only when the oldest still supported version of Python
+ already includes it. */
enum {
- HIGHEST_PROTOCOL = 5,
- DEFAULT_PROTOCOL = 4
+ HIGHEST_PROTOCOL = 5,
+ DEFAULT_PROTOCOL = 4
};
/* Pickle opcodes. These must be kept updated with pickle.py.
@@ -104,12 +104,12 @@ enum opcode {
NEWOBJ_EX = '\x92',
STACK_GLOBAL = '\x93',
MEMOIZE = '\x94',
- FRAME = '\x95',
-
- /* Protocol 5 */
- BYTEARRAY8 = '\x96',
- NEXT_BUFFER = '\x97',
- READONLY_BUFFER = '\x98'
+ FRAME = '\x95',
+
+ /* Protocol 5 */
+ BYTEARRAY8 = '\x96',
+ NEXT_BUFFER = '\x97',
+ READONLY_BUFFER = '\x98'
};
enum {
@@ -359,7 +359,7 @@ _Pickle_FastCall(PyObject *func, PyObject *obj)
{
PyObject *result;
- result = PyObject_CallOneArg(func, obj);
+ result = PyObject_CallOneArg(func, obj);
Py_DECREF(obj);
return result;
}
@@ -420,7 +420,7 @@ call_method(PyObject *func, PyObject *self, PyObject *obj)
return PyObject_CallFunctionObjArgs(func, self, obj, NULL);
}
else {
- return PyObject_CallOneArg(func, obj);
+ return PyObject_CallOneArg(func, obj);
}
}
@@ -461,7 +461,7 @@ Pdata_New(void)
if (!(self = PyObject_New(Pdata, &Pdata_Type)))
return NULL;
- Py_SET_SIZE(self, 0);
+ Py_SET_SIZE(self, 0);
self->mark_set = 0;
self->fence = 0;
self->allocated = 8;
@@ -488,7 +488,7 @@ Pdata_clear(Pdata *self, Py_ssize_t clearto)
while (--i >= clearto) {
Py_CLEAR(self->data[i]);
}
- Py_SET_SIZE(self, clearto);
+ Py_SET_SIZE(self, clearto);
return 0;
}
@@ -539,8 +539,8 @@ Pdata_pop(Pdata *self)
Pdata_stack_underflow(self);
return NULL;
}
- Py_SET_SIZE(self, Py_SIZE(self) - 1);
- return self->data[Py_SIZE(self)];
+ Py_SET_SIZE(self, Py_SIZE(self) - 1);
+ return self->data[Py_SIZE(self)];
}
#define PDATA_POP(D, V) do { (V) = Pdata_pop((D)); } while (0)
@@ -550,8 +550,8 @@ Pdata_push(Pdata *self, PyObject *obj)
if (Py_SIZE(self) == self->allocated && Pdata_grow(self) < 0) {
return -1;
}
- self->data[Py_SIZE(self)] = obj;
- Py_SET_SIZE(self, Py_SIZE(self) + 1);
+ self->data[Py_SIZE(self)] = obj;
+ Py_SET_SIZE(self, Py_SIZE(self) + 1);
return 0;
}
@@ -581,7 +581,7 @@ Pdata_poptuple(Pdata *self, Py_ssize_t start)
for (i = start, j = 0; j < len; i++, j++)
PyTuple_SET_ITEM(tuple, j, self->data[i]);
- Py_SET_SIZE(self, start);
+ Py_SET_SIZE(self, start);
return tuple;
}
@@ -598,7 +598,7 @@ Pdata_poplist(Pdata *self, Py_ssize_t start)
for (i = start, j = 0; j < len; i++, j++)
PyList_SET_ITEM(list, j, self->data[i]);
- Py_SET_SIZE(self, start);
+ Py_SET_SIZE(self, start);
return list;
}
@@ -623,9 +623,9 @@ typedef struct PicklerObject {
PyObject *pers_func_self; /* borrowed reference to self if pers_func
is an unbound method, NULL otherwise */
PyObject *dispatch_table; /* private dispatch_table, can be NULL */
- PyObject *reducer_override; /* hook for invoking user-defined callbacks
- instead of save_global when pickling
- functions and classes*/
+ PyObject *reducer_override; /* hook for invoking user-defined callbacks
+ instead of save_global when pickling
+ functions and classes*/
PyObject *write; /* write() method of the output stream. */
PyObject *output_buffer; /* Write into a local bytearray buffer before
@@ -650,7 +650,7 @@ typedef struct PicklerObject {
int fix_imports; /* Indicate whether Pickler should fix
the name of globals for Python 2.x. */
PyObject *fast_memo;
- PyObject *buffer_callback; /* Callback for out-of-band buffers, or NULL */
+ PyObject *buffer_callback; /* Callback for out-of-band buffers, or NULL */
} PicklerObject;
typedef struct UnpicklerObject {
@@ -675,10 +675,10 @@ typedef struct UnpicklerObject {
Py_ssize_t prefetched_idx; /* index of first prefetched byte */
PyObject *read; /* read() method of the input stream. */
- PyObject *readinto; /* readinto() method of the input stream. */
+ PyObject *readinto; /* readinto() method of the input stream. */
PyObject *readline; /* readline() method of the input stream. */
PyObject *peek; /* peek() method of the input stream, or NULL */
- PyObject *buffers; /* iterable of out-of-band buffers, or NULL */
+ PyObject *buffers; /* iterable of out-of-band buffers, or NULL */
char *encoding; /* Name of the encoding to be used for
decoding strings pickled using Python
@@ -1112,7 +1112,7 @@ _Pickler_New(void)
self->pers_func = NULL;
self->dispatch_table = NULL;
- self->buffer_callback = NULL;
+ self->buffer_callback = NULL;
self->write = NULL;
self->proto = 0;
self->bin = 0;
@@ -1124,7 +1124,7 @@ _Pickler_New(void)
self->fast_memo = NULL;
self->max_output_len = WRITE_BUF_SIZE;
self->output_len = 0;
- self->reducer_override = NULL;
+ self->reducer_override = NULL;
self->memo = PyMemoTable_New();
self->output_buffer = PyBytes_FromStringAndSize(NULL,
@@ -1134,8 +1134,8 @@ _Pickler_New(void)
Py_DECREF(self);
return NULL;
}
-
- PyObject_GC_Track(self);
+
+ PyObject_GC_Track(self);
return self;
}
@@ -1144,7 +1144,7 @@ _Pickler_SetProtocol(PicklerObject *self, PyObject *protocol, int fix_imports)
{
long proto;
- if (protocol == Py_None) {
+ if (protocol == Py_None) {
proto = DEFAULT_PROTOCOL;
}
else {
@@ -1185,23 +1185,23 @@ _Pickler_SetOutputStream(PicklerObject *self, PyObject *file)
return 0;
}
-static int
-_Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback)
-{
- if (buffer_callback == Py_None) {
- buffer_callback = NULL;
- }
- if (buffer_callback != NULL && self->proto < 5) {
- PyErr_SetString(PyExc_ValueError,
- "buffer_callback needs protocol >= 5");
- return -1;
- }
-
- Py_XINCREF(buffer_callback);
- self->buffer_callback = buffer_callback;
- return 0;
-}
-
+static int
+_Pickler_SetBufferCallback(PicklerObject *self, PyObject *buffer_callback)
+{
+ if (buffer_callback == Py_None) {
+ buffer_callback = NULL;
+ }
+ if (buffer_callback != NULL && self->proto < 5) {
+ PyErr_SetString(PyExc_ValueError,
+ "buffer_callback needs protocol >= 5");
+ return -1;
+ }
+
+ Py_XINCREF(buffer_callback);
+ self->buffer_callback = buffer_callback;
+ return 0;
+}
+
/* Returns the size of the input on success, -1 on failure. This takes its
own reference to `input`. */
static Py_ssize_t
@@ -1226,7 +1226,7 @@ bad_readline(void)
return -1;
}
-/* Skip any consumed data that was only prefetched using peek() */
+/* Skip any consumed data that was only prefetched using peek() */
static int
_Unpickler_SkipConsumed(UnpicklerObject *self)
{
@@ -1276,7 +1276,7 @@ _Unpickler_ReadFromFile(UnpicklerObject *self, Py_ssize_t n)
return -1;
if (n == READ_WHOLE_LINE) {
- data = PyObject_CallNoArgs(self->readline);
+ data = PyObject_CallNoArgs(self->readline);
}
else {
PyObject *len;
@@ -1334,7 +1334,7 @@ _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
if (!self->read)
return bad_readline();
- /* Extend the buffer to satisfy desired size */
+ /* Extend the buffer to satisfy desired size */
num_read = _Unpickler_ReadFromFile(self, n);
if (num_read < 0)
return -1;
@@ -1345,95 +1345,95 @@ _Unpickler_ReadImpl(UnpicklerObject *self, char **s, Py_ssize_t n)
return n;
}
-/* Read `n` bytes from the unpickler's data source, storing the result in `buf`.
- *
- * This should only be used for non-small data reads where potentially
- * avoiding a copy is beneficial. This method does not try to prefetch
- * more data into the input buffer.
- *
- * _Unpickler_Read() is recommended in most cases.
- */
-static Py_ssize_t
-_Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n)
-{
- assert(n != READ_WHOLE_LINE);
-
- /* Read from available buffer data, if any */
- Py_ssize_t in_buffer = self->input_len - self->next_read_idx;
- if (in_buffer > 0) {
- Py_ssize_t to_read = Py_MIN(in_buffer, n);
- memcpy(buf, self->input_buffer + self->next_read_idx, to_read);
- self->next_read_idx += to_read;
- buf += to_read;
- n -= to_read;
- if (n == 0) {
- /* Entire read was satisfied from buffer */
- return n;
- }
- }
-
- /* Read from file */
- if (!self->read) {
- /* We're unpickling memory, this means the input is truncated */
- return bad_readline();
- }
- if (_Unpickler_SkipConsumed(self) < 0) {
- return -1;
- }
-
- if (!self->readinto) {
- /* readinto() not supported on file-like object, fall back to read()
- * and copy into destination buffer (bpo-39681) */
- PyObject* len = PyLong_FromSsize_t(n);
- if (len == NULL) {
- return -1;
- }
- PyObject* data = _Pickle_FastCall(self->read, len);
- if (data == NULL) {
- return -1;
- }
- if (!PyBytes_Check(data)) {
- PyErr_Format(PyExc_ValueError,
- "read() returned non-bytes object (%R)",
- Py_TYPE(data));
- Py_DECREF(data);
- return -1;
- }
- Py_ssize_t read_size = PyBytes_GET_SIZE(data);
- if (read_size < n) {
- Py_DECREF(data);
- return bad_readline();
- }
- memcpy(buf, PyBytes_AS_STRING(data), n);
- Py_DECREF(data);
- return n;
- }
-
- /* Call readinto() into user buffer */
- PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE);
- if (buf_obj == NULL) {
- return -1;
- }
- PyObject *read_size_obj = _Pickle_FastCall(self->readinto, buf_obj);
- if (read_size_obj == NULL) {
- return -1;
- }
- Py_ssize_t read_size = PyLong_AsSsize_t(read_size_obj);
- Py_DECREF(read_size_obj);
-
- if (read_size < 0) {
- if (!PyErr_Occurred()) {
- PyErr_SetString(PyExc_ValueError,
- "readinto() returned negative size");
- }
- return -1;
- }
- if (read_size < n) {
- return bad_readline();
- }
- return n;
-}
-
+/* Read `n` bytes from the unpickler's data source, storing the result in `buf`.
+ *
+ * This should only be used for non-small data reads where potentially
+ * avoiding a copy is beneficial. This method does not try to prefetch
+ * more data into the input buffer.
+ *
+ * _Unpickler_Read() is recommended in most cases.
+ */
+static Py_ssize_t
+_Unpickler_ReadInto(UnpicklerObject *self, char *buf, Py_ssize_t n)
+{
+ assert(n != READ_WHOLE_LINE);
+
+ /* Read from available buffer data, if any */
+ Py_ssize_t in_buffer = self->input_len - self->next_read_idx;
+ if (in_buffer > 0) {
+ Py_ssize_t to_read = Py_MIN(in_buffer, n);
+ memcpy(buf, self->input_buffer + self->next_read_idx, to_read);
+ self->next_read_idx += to_read;
+ buf += to_read;
+ n -= to_read;
+ if (n == 0) {
+ /* Entire read was satisfied from buffer */
+ return n;
+ }
+ }
+
+ /* Read from file */
+ if (!self->read) {
+ /* We're unpickling memory, this means the input is truncated */
+ return bad_readline();
+ }
+ if (_Unpickler_SkipConsumed(self) < 0) {
+ return -1;
+ }
+
+ if (!self->readinto) {
+ /* readinto() not supported on file-like object, fall back to read()
+ * and copy into destination buffer (bpo-39681) */
+ PyObject* len = PyLong_FromSsize_t(n);
+ if (len == NULL) {
+ return -1;
+ }
+ PyObject* data = _Pickle_FastCall(self->read, len);
+ if (data == NULL) {
+ return -1;
+ }
+ if (!PyBytes_Check(data)) {
+ PyErr_Format(PyExc_ValueError,
+ "read() returned non-bytes object (%R)",
+ Py_TYPE(data));
+ Py_DECREF(data);
+ return -1;
+ }
+ Py_ssize_t read_size = PyBytes_GET_SIZE(data);
+ if (read_size < n) {
+ Py_DECREF(data);
+ return bad_readline();
+ }
+ memcpy(buf, PyBytes_AS_STRING(data), n);
+ Py_DECREF(data);
+ return n;
+ }
+
+ /* Call readinto() into user buffer */
+ PyObject *buf_obj = PyMemoryView_FromMemory(buf, n, PyBUF_WRITE);
+ if (buf_obj == NULL) {
+ return -1;
+ }
+ PyObject *read_size_obj = _Pickle_FastCall(self->readinto, buf_obj);
+ if (read_size_obj == NULL) {
+ return -1;
+ }
+ Py_ssize_t read_size = PyLong_AsSsize_t(read_size_obj);
+ Py_DECREF(read_size_obj);
+
+ if (read_size < 0) {
+ if (!PyErr_Occurred()) {
+ PyErr_SetString(PyExc_ValueError,
+ "readinto() returned negative size");
+ }
+ return -1;
+ }
+ if (read_size < n) {
+ return bad_readline();
+ }
+ return n;
+}
+
/* Read `n` bytes from the unpickler's data source, storing the result in `*s`.
This should be used for all data reads, rather than accessing the unpickler's
@@ -1601,10 +1601,10 @@ _Unpickler_New(void)
self->next_read_idx = 0;
self->prefetched_idx = 0;
self->read = NULL;
- self->readinto = NULL;
+ self->readinto = NULL;
self->readline = NULL;
self->peek = NULL;
- self->buffers = NULL;
+ self->buffers = NULL;
self->encoding = NULL;
self->errors = NULL;
self->marks = NULL;
@@ -1623,36 +1623,36 @@ _Unpickler_New(void)
return NULL;
}
- PyObject_GC_Track(self);
+ PyObject_GC_Track(self);
return self;
}
/* Returns -1 (with an exception set) on failure, 0 on success. This may
- be called once on a freshly created Unpickler. */
+ be called once on a freshly created Unpickler. */
static int
_Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
{
_Py_IDENTIFIER(peek);
_Py_IDENTIFIER(read);
- _Py_IDENTIFIER(readinto);
+ _Py_IDENTIFIER(readinto);
_Py_IDENTIFIER(readline);
- /* Optional file methods */
+ /* Optional file methods */
if (_PyObject_LookupAttrId(file, &PyId_peek, &self->peek) < 0) {
return -1;
}
- if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) < 0) {
- return -1;
- }
+ if (_PyObject_LookupAttrId(file, &PyId_readinto, &self->readinto) < 0) {
+ return -1;
+ }
(void)_PyObject_LookupAttrId(file, &PyId_read, &self->read);
(void)_PyObject_LookupAttrId(file, &PyId_readline, &self->readline);
- if (!self->readline || !self->read) {
+ if (!self->readline || !self->read) {
if (!PyErr_Occurred()) {
PyErr_SetString(PyExc_TypeError,
"file must have 'read' and 'readline' attributes");
}
Py_CLEAR(self->read);
- Py_CLEAR(self->readinto);
+ Py_CLEAR(self->readinto);
Py_CLEAR(self->readline);
Py_CLEAR(self->peek);
return -1;
@@ -1661,7 +1661,7 @@ _Unpickler_SetInputStream(UnpicklerObject *self, PyObject *file)
}
/* Returns -1 (with an exception set) on failure, 0 on success. This may
- be called once on a freshly created Unpickler. */
+ be called once on a freshly created Unpickler. */
static int
_Unpickler_SetInputEncoding(UnpicklerObject *self,
const char *encoding,
@@ -1681,23 +1681,23 @@ _Unpickler_SetInputEncoding(UnpicklerObject *self,
return 0;
}
-/* Returns -1 (with an exception set) on failure, 0 on success. This may
- be called once on a freshly created Unpickler. */
-static int
-_Unpickler_SetBuffers(UnpicklerObject *self, PyObject *buffers)
-{
- if (buffers == NULL || buffers == Py_None) {
- self->buffers = NULL;
- }
- else {
- self->buffers = PyObject_GetIter(buffers);
- if (self->buffers == NULL) {
- return -1;
- }
- }
- return 0;
-}
-
+/* Returns -1 (with an exception set) on failure, 0 on success. This may
+ be called once on a freshly created Unpickler. */
+static int
+_Unpickler_SetBuffers(UnpicklerObject *self, PyObject *buffers)
+{
+ if (buffers == NULL || buffers == Py_None) {
+ self->buffers = NULL;
+ }
+ else {
+ self->buffers = PyObject_GetIter(buffers);
+ if (self->buffers == NULL) {
+ return -1;
+ }
+ }
+ return 0;
+}
+
/* Generate a GET opcode for an object stored in the memo. */
static int
memo_get(PicklerObject *self, PyObject *key)
@@ -2009,7 +2009,7 @@ fast_save_enter(PicklerObject *self, PyObject *obj)
PyErr_Format(PyExc_ValueError,
"fast mode: can't pickle cyclic objects "
"including object type %.200s at %p",
- Py_TYPE(obj)->tp_name, obj);
+ Py_TYPE(obj)->tp_name, obj);
self->fast_nesting = -1;
return 0;
}
@@ -2152,7 +2152,7 @@ save_long(PicklerObject *self, PyObject *obj)
/* How many bytes do we need? There are nbits >> 3 full
* bytes of data, and nbits & 7 leftover bits. If there
* are any leftover bits, then we clearly need another
- * byte. What's not so obvious is that we *probably*
+ * byte. What's not so obvious is that we *probably*
* need another byte even if there aren't any leftovers:
* the most-significant bit of the most-significant byte
* acts like a sign bit, and it's usually got a sense
@@ -2329,7 +2329,7 @@ _Pickler_write_bytes(PicklerObject *self,
return -1;
}
}
- result = PyObject_CallOneArg(self->write, payload);
+ result = PyObject_CallOneArg(self->write, payload);
Py_XDECREF(mem);
if (result == NULL) {
return -1;
@@ -2354,54 +2354,54 @@ _Pickler_write_bytes(PicklerObject *self,
}
static int
-_save_bytes_data(PicklerObject *self, PyObject *obj, const char *data,
- Py_ssize_t size)
-{
- assert(self->proto >= 3);
-
- char header[9];
- Py_ssize_t len;
-
- if (size < 0)
- return -1;
-
- if (size <= 0xff) {
- header[0] = SHORT_BINBYTES;
- header[1] = (unsigned char)size;
- len = 2;
- }
- else if ((size_t)size <= 0xffffffffUL) {
- header[0] = BINBYTES;
- header[1] = (unsigned char)(size & 0xff);
- header[2] = (unsigned char)((size >> 8) & 0xff);
- header[3] = (unsigned char)((size >> 16) & 0xff);
- header[4] = (unsigned char)((size >> 24) & 0xff);
- len = 5;
- }
- else if (self->proto >= 4) {
- header[0] = BINBYTES8;
- _write_size64(header + 1, size);
- len = 9;
- }
- else {
- PyErr_SetString(PyExc_OverflowError,
- "serializing a bytes object larger than 4 GiB "
- "requires pickle protocol 4 or higher");
- return -1;
- }
-
- if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
- return -1;
- }
-
- if (memo_put(self, obj) < 0) {
- return -1;
- }
-
- return 0;
-}
-
-static int
+_save_bytes_data(PicklerObject *self, PyObject *obj, const char *data,
+ Py_ssize_t size)
+{
+ assert(self->proto >= 3);
+
+ char header[9];
+ Py_ssize_t len;
+
+ if (size < 0)
+ return -1;
+
+ if (size <= 0xff) {
+ header[0] = SHORT_BINBYTES;
+ header[1] = (unsigned char)size;
+ len = 2;
+ }
+ else if ((size_t)size <= 0xffffffffUL) {
+ header[0] = BINBYTES;
+ header[1] = (unsigned char)(size & 0xff);
+ header[2] = (unsigned char)((size >> 8) & 0xff);
+ header[3] = (unsigned char)((size >> 16) & 0xff);
+ header[4] = (unsigned char)((size >> 24) & 0xff);
+ len = 5;
+ }
+ else if (self->proto >= 4) {
+ header[0] = BINBYTES8;
+ _write_size64(header + 1, size);
+ len = 9;
+ }
+ else {
+ PyErr_SetString(PyExc_OverflowError,
+ "serializing a bytes object larger than 4 GiB "
+ "requires pickle protocol 4 or higher");
+ return -1;
+ }
+
+ if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
+ return -1;
+ }
+
+ if (memo_put(self, obj) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
save_bytes(PicklerObject *self, PyObject *obj)
{
if (self->proto < 3) {
@@ -2416,7 +2416,7 @@ save_bytes(PicklerObject *self, PyObject *obj)
Python 2 *and* the appropriate 'bytes' object when unpickled
using Python 3. Again this is a hack and we don't need to do this
with newer protocols. */
- PyObject *reduce_value;
+ PyObject *reduce_value;
int status;
if (PyBytes_GET_SIZE(obj) == 0) {
@@ -2447,131 +2447,131 @@ save_bytes(PicklerObject *self, PyObject *obj)
return status;
}
else {
- return _save_bytes_data(self, obj, PyBytes_AS_STRING(obj),
- PyBytes_GET_SIZE(obj));
- }
-}
-
-static int
-_save_bytearray_data(PicklerObject *self, PyObject *obj, const char *data,
- Py_ssize_t size)
-{
- assert(self->proto >= 5);
-
- char header[9];
- Py_ssize_t len;
-
- if (size < 0)
- return -1;
-
- header[0] = BYTEARRAY8;
- _write_size64(header + 1, size);
- len = 9;
-
- if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
- return -1;
- }
-
- if (memo_put(self, obj) < 0) {
- return -1;
- }
-
- return 0;
-}
-
-static int
-save_bytearray(PicklerObject *self, PyObject *obj)
-{
- if (self->proto < 5) {
- /* Older pickle protocols do not have an opcode for pickling
- * bytearrays. */
- PyObject *reduce_value = NULL;
- int status;
-
- if (PyByteArray_GET_SIZE(obj) == 0) {
- reduce_value = Py_BuildValue("(O())",
- (PyObject *) &PyByteArray_Type);
- }
- else {
- PyObject *bytes_obj = PyBytes_FromObject(obj);
- if (bytes_obj != NULL) {
- reduce_value = Py_BuildValue("(O(O))",
- (PyObject *) &PyByteArray_Type,
- bytes_obj);
- Py_DECREF(bytes_obj);
- }
- }
- if (reduce_value == NULL)
+ return _save_bytes_data(self, obj, PyBytes_AS_STRING(obj),
+ PyBytes_GET_SIZE(obj));
+ }
+}
+
+static int
+_save_bytearray_data(PicklerObject *self, PyObject *obj, const char *data,
+ Py_ssize_t size)
+{
+ assert(self->proto >= 5);
+
+ char header[9];
+ Py_ssize_t len;
+
+ if (size < 0)
+ return -1;
+
+ header[0] = BYTEARRAY8;
+ _write_size64(header + 1, size);
+ len = 9;
+
+ if (_Pickler_write_bytes(self, header, len, data, size, obj) < 0) {
+ return -1;
+ }
+
+ if (memo_put(self, obj) < 0) {
+ return -1;
+ }
+
+ return 0;
+}
+
+static int
+save_bytearray(PicklerObject *self, PyObject *obj)
+{
+ if (self->proto < 5) {
+ /* Older pickle protocols do not have an opcode for pickling
+ * bytearrays. */
+ PyObject *reduce_value = NULL;
+ int status;
+
+ if (PyByteArray_GET_SIZE(obj) == 0) {
+ reduce_value = Py_BuildValue("(O())",
+ (PyObject *) &PyByteArray_Type);
+ }
+ else {
+ PyObject *bytes_obj = PyBytes_FromObject(obj);
+ if (bytes_obj != NULL) {
+ reduce_value = Py_BuildValue("(O(O))",
+ (PyObject *) &PyByteArray_Type,
+ bytes_obj);
+ Py_DECREF(bytes_obj);
+ }
+ }
+ if (reduce_value == NULL)
return -1;
- /* save_reduce() will memoize the object automatically. */
- status = save_reduce(self, reduce_value, obj);
- Py_DECREF(reduce_value);
- return status;
- }
- else {
- return _save_bytearray_data(self, obj, PyByteArray_AS_STRING(obj),
- PyByteArray_GET_SIZE(obj));
- }
-}
-
-static int
-save_picklebuffer(PicklerObject *self, PyObject *obj)
-{
- if (self->proto < 5) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_SetString(st->PicklingError,
- "PickleBuffer can only pickled with protocol >= 5");
- return -1;
- }
- const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);
- if (view == NULL) {
- return -1;
- }
- if (view->suboffsets != NULL || !PyBuffer_IsContiguous(view, 'A')) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_SetString(st->PicklingError,
- "PickleBuffer can not be pickled when "
- "pointing to a non-contiguous buffer");
- return -1;
- }
- int in_band = 1;
- if (self->buffer_callback != NULL) {
- PyObject *ret = PyObject_CallOneArg(self->buffer_callback, obj);
- if (ret == NULL) {
- return -1;
- }
- in_band = PyObject_IsTrue(ret);
- Py_DECREF(ret);
- if (in_band == -1) {
- return -1;
- }
- }
- if (in_band) {
- /* Write data in-band */
- if (view->readonly) {
- return _save_bytes_data(self, obj, (const char*) view->buf,
- view->len);
+ /* save_reduce() will memoize the object automatically. */
+ status = save_reduce(self, reduce_value, obj);
+ Py_DECREF(reduce_value);
+ return status;
+ }
+ else {
+ return _save_bytearray_data(self, obj, PyByteArray_AS_STRING(obj),
+ PyByteArray_GET_SIZE(obj));
+ }
+}
+
+static int
+save_picklebuffer(PicklerObject *self, PyObject *obj)
+{
+ if (self->proto < 5) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_SetString(st->PicklingError,
+ "PickleBuffer can only pickled with protocol >= 5");
+ return -1;
+ }
+ const Py_buffer* view = PyPickleBuffer_GetBuffer(obj);
+ if (view == NULL) {
+ return -1;
+ }
+ if (view->suboffsets != NULL || !PyBuffer_IsContiguous(view, 'A')) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_SetString(st->PicklingError,
+ "PickleBuffer can not be pickled when "
+ "pointing to a non-contiguous buffer");
+ return -1;
+ }
+ int in_band = 1;
+ if (self->buffer_callback != NULL) {
+ PyObject *ret = PyObject_CallOneArg(self->buffer_callback, obj);
+ if (ret == NULL) {
+ return -1;
+ }
+ in_band = PyObject_IsTrue(ret);
+ Py_DECREF(ret);
+ if (in_band == -1) {
+ return -1;
+ }
+ }
+ if (in_band) {
+ /* Write data in-band */
+ if (view->readonly) {
+ return _save_bytes_data(self, obj, (const char*) view->buf,
+ view->len);
}
else {
- return _save_bytearray_data(self, obj, (const char*) view->buf,
- view->len);
- }
- }
- else {
- /* Write data out-of-band */
- const char next_buffer_op = NEXT_BUFFER;
- if (_Pickler_Write(self, &next_buffer_op, 1) < 0) {
+ return _save_bytearray_data(self, obj, (const char*) view->buf,
+ view->len);
+ }
+ }
+ else {
+ /* Write data out-of-band */
+ const char next_buffer_op = NEXT_BUFFER;
+ if (_Pickler_Write(self, &next_buffer_op, 1) < 0) {
return -1;
}
- if (view->readonly) {
- const char readonly_buffer_op = READONLY_BUFFER;
- if (_Pickler_Write(self, &readonly_buffer_op, 1) < 0) {
- return -1;
- }
- }
+ if (view->readonly) {
+ const char readonly_buffer_op = READONLY_BUFFER;
+ if (_Pickler_Write(self, &readonly_buffer_op, 1) < 0) {
+ return -1;
+ }
+ }
}
- return 0;
+ return 0;
}
/* A copy of PyUnicode_EncodeRawUnicodeEscape() that also translates
@@ -2581,7 +2581,7 @@ raw_unicode_escape(PyObject *obj)
{
char *p;
Py_ssize_t i, size;
- const void *data;
+ const void *data;
unsigned int kind;
_PyBytesWriter writer;
@@ -2620,10 +2620,10 @@ raw_unicode_escape(PyObject *obj)
*p++ = Py_hexdigits[ch & 15];
}
/* Map 16-bit characters, '\\' and '\n' to '\uxxxx' */
- else if (ch >= 256 ||
- ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' ||
- ch == 0x1a)
- {
+ else if (ch >= 256 ||
+ ch == '\\' || ch == 0 || ch == '\n' || ch == '\r' ||
+ ch == 0x1a)
+ {
/* -1: subtract 1 preallocated byte */
p = _PyBytesWriter_Prepare(&writer, p, 6-1);
if (p == NULL)
@@ -2694,8 +2694,8 @@ write_unicode_binary(PicklerObject *self, PyObject *obj)
}
else {
PyErr_SetString(PyExc_OverflowError,
- "serializing a string larger than 4 GiB "
- "requires pickle protocol 4 or higher");
+ "serializing a string larger than 4 GiB "
+ "requires pickle protocol 4 or higher");
Py_XDECREF(encoded);
return -1;
}
@@ -3338,7 +3338,7 @@ save_dict(PicklerObject *self, PyObject *obj)
} else {
_Py_IDENTIFIER(items);
- items = _PyObject_CallMethodIdNoArgs(obj, &PyId_items);
+ items = _PyObject_CallMethodIdNoArgs(obj, &PyId_items);
if (items == NULL)
goto error;
iter = PyObject_GetIter(items);
@@ -3944,7 +3944,7 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
PyObject *state = NULL;
PyObject *listitems = Py_None;
PyObject *dictitems = Py_None;
- PyObject *state_setter = Py_None;
+ PyObject *state_setter = Py_None;
PickleState *st = _Pickle_GetGlobalState();
Py_ssize_t size;
int use_newobj = 0, use_newobj_ex = 0;
@@ -3955,15 +3955,15 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
const char newobj_ex_op = NEWOBJ_EX;
size = PyTuple_Size(args);
- if (size < 2 || size > 6) {
+ if (size < 2 || size > 6) {
PyErr_SetString(st->PicklingError, "tuple returned by "
- "__reduce__ must contain 2 through 6 elements");
+ "__reduce__ must contain 2 through 6 elements");
return -1;
}
- if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6,
- &callable, &argtup, &state, &listitems, &dictitems,
- &state_setter))
+ if (!PyArg_UnpackTuple(args, "save_reduce", 2, 6,
+ &callable, &argtup, &state, &listitems, &dictitems,
+ &state_setter))
return -1;
if (!PyCallable_Check(callable)) {
@@ -3998,15 +3998,15 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
return -1;
}
- if (state_setter == Py_None)
- state_setter = NULL;
- else if (!PyCallable_Check(state_setter)) {
- PyErr_Format(st->PicklingError, "sixth element of the tuple "
- "returned by __reduce__ must be a function, not %s",
- Py_TYPE(state_setter)->tp_name);
- return -1;
- }
-
+ if (state_setter == Py_None)
+ state_setter = NULL;
+ else if (!PyCallable_Check(state_setter)) {
+ PyErr_Format(st->PicklingError, "sixth element of the tuple "
+ "returned by __reduce__ must be a function, not %s",
+ Py_TYPE(state_setter)->tp_name);
+ return -1;
+ }
+
if (self->proto >= 2) {
PyObject *name;
_Py_IDENTIFIER(__name__);
@@ -4226,31 +4226,31 @@ save_reduce(PicklerObject *self, PyObject *args, PyObject *obj)
return -1;
if (state) {
- if (state_setter == NULL) {
- if (save(self, state, 0) < 0 ||
- _Pickler_Write(self, &build_op, 1) < 0)
- return -1;
- }
- else {
-
- /* If a state_setter is specified, call it instead of load_build to
- * update obj's with its previous state.
- * The first 4 save/write instructions push state_setter and its
- * tuple of expected arguments (obj, state) onto the stack. The
- * REDUCE opcode triggers the state_setter(obj, state) function
- * call. Finally, because state-updating routines only do in-place
- * modification, the whole operation has to be stack-transparent.
- * Thus, we finally pop the call's output from the stack.*/
-
- const char tupletwo_op = TUPLE2;
- const char pop_op = POP;
- if (save(self, state_setter, 0) < 0 ||
- save(self, obj, 0) < 0 || save(self, state, 0) < 0 ||
- _Pickler_Write(self, &tupletwo_op, 1) < 0 ||
- _Pickler_Write(self, &reduce_op, 1) < 0 ||
- _Pickler_Write(self, &pop_op, 1) < 0)
- return -1;
- }
+ if (state_setter == NULL) {
+ if (save(self, state, 0) < 0 ||
+ _Pickler_Write(self, &build_op, 1) < 0)
+ return -1;
+ }
+ else {
+
+ /* If a state_setter is specified, call it instead of load_build to
+ * update obj's with its previous state.
+ * The first 4 save/write instructions push state_setter and its
+ * tuple of expected arguments (obj, state) onto the stack. The
+ * REDUCE opcode triggers the state_setter(obj, state) function
+ * call. Finally, because state-updating routines only do in-place
+ * modification, the whole operation has to be stack-transparent.
+ * Thus, we finally pop the call's output from the stack.*/
+
+ const char tupletwo_op = TUPLE2;
+ const char pop_op = POP;
+ if (save(self, state_setter, 0) < 0 ||
+ save(self, obj, 0) < 0 || save(self, state, 0) < 0 ||
+ _Pickler_Write(self, &tupletwo_op, 1) < 0 ||
+ _Pickler_Write(self, &reduce_op, 1) < 0 ||
+ _Pickler_Write(self, &pop_op, 1) < 0)
+ return -1;
+ }
}
return 0;
}
@@ -4275,7 +4275,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
1 if a persistent id was saved.
*/
if ((status = save_pers(self, obj)) != 0)
- return status;
+ return status;
}
type = Py_TYPE(obj);
@@ -4288,39 +4288,39 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
/* Atom types; these aren't memoized, so don't check the memo. */
if (obj == Py_None) {
- return save_none(self, obj);
+ return save_none(self, obj);
}
else if (obj == Py_False || obj == Py_True) {
- return save_bool(self, obj);
+ return save_bool(self, obj);
}
else if (type == &PyLong_Type) {
- return save_long(self, obj);
+ return save_long(self, obj);
}
else if (type == &PyFloat_Type) {
- return save_float(self, obj);
+ return save_float(self, obj);
}
/* Check the memo to see if it has the object. If so, generate
a GET (or BINGET) opcode, instead of pickling the object
once again. */
if (PyMemoTable_Get(self->memo, obj)) {
- return memo_get(self, obj);
+ return memo_get(self, obj);
}
if (type == &PyBytes_Type) {
- return save_bytes(self, obj);
+ return save_bytes(self, obj);
}
else if (type == &PyUnicode_Type) {
- return save_unicode(self, obj);
- }
-
- /* We're only calling Py_EnterRecursiveCall here so that atomic
- types above are pickled faster. */
- if (Py_EnterRecursiveCall(" while pickling an object")) {
- return -1;
- }
-
- if (type == &PyDict_Type) {
+ return save_unicode(self, obj);
+ }
+
+ /* We're only calling Py_EnterRecursiveCall here so that atomic
+ types above are pickled faster. */
+ if (Py_EnterRecursiveCall(" while pickling an object")) {
+ return -1;
+ }
+
+ if (type == &PyDict_Type) {
status = save_dict(self, obj);
goto done;
}
@@ -4340,32 +4340,32 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
status = save_tuple(self, obj);
goto done;
}
- else if (type == &PyByteArray_Type) {
- status = save_bytearray(self, obj);
- goto done;
- }
- else if (type == &PyPickleBuffer_Type) {
- status = save_picklebuffer(self, obj);
- goto done;
- }
-
- /* Now, check reducer_override. If it returns NotImplemented,
- * fallback to save_type or save_global, and then perhaps to the
- * regular reduction mechanism.
- */
- if (self->reducer_override != NULL) {
- reduce_value = PyObject_CallOneArg(self->reducer_override, obj);
- if (reduce_value == NULL) {
- goto error;
- }
- if (reduce_value != Py_NotImplemented) {
- goto reduce;
- }
- Py_DECREF(reduce_value);
- reduce_value = NULL;
- }
-
- if (type == &PyType_Type) {
+ else if (type == &PyByteArray_Type) {
+ status = save_bytearray(self, obj);
+ goto done;
+ }
+ else if (type == &PyPickleBuffer_Type) {
+ status = save_picklebuffer(self, obj);
+ goto done;
+ }
+
+ /* Now, check reducer_override. If it returns NotImplemented,
+ * fallback to save_type or save_global, and then perhaps to the
+ * regular reduction mechanism.
+ */
+ if (self->reducer_override != NULL) {
+ reduce_value = PyObject_CallOneArg(self->reducer_override, obj);
+ if (reduce_value == NULL) {
+ goto error;
+ }
+ if (reduce_value != Py_NotImplemented) {
+ goto reduce;
+ }
+ Py_DECREF(reduce_value);
+ reduce_value = NULL;
+ }
+
+ if (type == &PyType_Type) {
status = save_type(self, obj);
goto done;
}
@@ -4437,14 +4437,14 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
}
else {
/* Check for a __reduce__ method. */
- if (_PyObject_LookupAttrId(obj, &PyId___reduce__, &reduce_func) < 0) {
- goto error;
- }
+ if (_PyObject_LookupAttrId(obj, &PyId___reduce__, &reduce_func) < 0) {
+ goto error;
+ }
if (reduce_func != NULL) {
- reduce_value = PyObject_CallNoArgs(reduce_func);
+ reduce_value = PyObject_CallNoArgs(reduce_func);
}
else {
- PickleState *st = _Pickle_GetGlobalState();
+ PickleState *st = _Pickle_GetGlobalState();
PyErr_Format(st->PicklingError,
"can't pickle '%.200s' object: %R",
type->tp_name, obj);
@@ -4456,7 +4456,7 @@ save(PicklerObject *self, PyObject *obj, int pers_save)
if (reduce_value == NULL)
goto error;
- reduce:
+ reduce:
if (PyUnicode_Check(reduce_value)) {
status = save_global(self, obj, reduce_value);
goto done;
@@ -4488,22 +4488,22 @@ static int
dump(PicklerObject *self, PyObject *obj)
{
const char stop_op = STOP;
- int status = -1;
- PyObject *tmp;
- _Py_IDENTIFIER(reducer_override);
-
- if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override,
- &tmp) < 0) {
- goto error;
- }
- /* Cache the reducer_override method, if it exists. */
- if (tmp != NULL) {
- Py_XSETREF(self->reducer_override, tmp);
- }
- else {
- Py_CLEAR(self->reducer_override);
- }
-
+ int status = -1;
+ PyObject *tmp;
+ _Py_IDENTIFIER(reducer_override);
+
+ if (_PyObject_LookupAttrId((PyObject *)self, &PyId_reducer_override,
+ &tmp) < 0) {
+ goto error;
+ }
+ /* Cache the reducer_override method, if it exists. */
+ if (tmp != NULL) {
+ Py_XSETREF(self->reducer_override, tmp);
+ }
+ else {
+ Py_CLEAR(self->reducer_override);
+ }
+
if (self->proto >= 2) {
char header[2];
@@ -4511,7 +4511,7 @@ dump(PicklerObject *self, PyObject *obj)
assert(self->proto >= 0 && self->proto < 256);
header[1] = (unsigned char)self->proto;
if (_Pickler_Write(self, header, 2) < 0)
- goto error;
+ goto error;
if (self->proto >= 4)
self->framing = 1;
}
@@ -4519,22 +4519,22 @@ dump(PicklerObject *self, PyObject *obj)
if (save(self, obj, 0) < 0 ||
_Pickler_Write(self, &stop_op, 1) < 0 ||
_Pickler_CommitFrame(self) < 0)
- goto error;
-
- // Success
- status = 0;
-
- error:
+ goto error;
+
+ // Success
+ status = 0;
+
+ error:
self->framing = 0;
-
- /* Break the reference cycle we generated at the beginning this function
- * call when setting the reducer_override attribute of the Pickler instance
- * to a bound method of the same instance. This is important as the Pickler
- * instance holds a reference to each object it has pickled (through its
- * memo): thus, these objects won't be garbage-collected as long as the
- * Pickler itself is not collected. */
- Py_CLEAR(self->reducer_override);
- return status;
+
+ /* Break the reference cycle we generated at the beginning this function
+ * call when setting the reducer_override attribute of the Pickler instance
+ * to a bound method of the same instance. This is important as the Pickler
+ * instance holds a reference to each object it has pickled (through its
+ * memo): thus, these objects won't be garbage-collected as long as the
+ * Pickler itself is not collected. */
+ Py_CLEAR(self->reducer_override);
+ return status;
}
/*[clinic input]
@@ -4640,8 +4640,8 @@ Pickler_dealloc(PicklerObject *self)
Py_XDECREF(self->pers_func);
Py_XDECREF(self->dispatch_table);
Py_XDECREF(self->fast_memo);
- Py_XDECREF(self->reducer_override);
- Py_XDECREF(self->buffer_callback);
+ Py_XDECREF(self->reducer_override);
+ Py_XDECREF(self->buffer_callback);
PyMemoTable_Del(self->memo);
@@ -4655,8 +4655,8 @@ Pickler_traverse(PicklerObject *self, visitproc visit, void *arg)
Py_VISIT(self->pers_func);
Py_VISIT(self->dispatch_table);
Py_VISIT(self->fast_memo);
- Py_VISIT(self->reducer_override);
- Py_VISIT(self->buffer_callback);
+ Py_VISIT(self->reducer_override);
+ Py_VISIT(self->buffer_callback);
return 0;
}
@@ -4668,8 +4668,8 @@ Pickler_clear(PicklerObject *self)
Py_CLEAR(self->pers_func);
Py_CLEAR(self->dispatch_table);
Py_CLEAR(self->fast_memo);
- Py_CLEAR(self->reducer_override);
- Py_CLEAR(self->buffer_callback);
+ Py_CLEAR(self->reducer_override);
+ Py_CLEAR(self->buffer_callback);
if (self->memo != NULL) {
PyMemoTable *memo = self->memo;
@@ -4685,16 +4685,16 @@ Pickler_clear(PicklerObject *self)
_pickle.Pickler.__init__
file: object
- protocol: object = None
+ protocol: object = None
fix_imports: bool = True
- buffer_callback: object = None
+ buffer_callback: object = None
This takes a binary file for writing a pickle data stream.
The optional *protocol* argument tells the pickler to use the given
-protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
-protocol is 4. It was introduced in Python 3.4, and is incompatible
-with previous versions.
+protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
+protocol is 4. It was introduced in Python 3.4, and is incompatible
+with previous versions.
Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the
@@ -4708,25 +4708,25 @@ this interface.
If *fix_imports* is True and protocol is less than 3, pickle will try
to map the new Python 3 names to the old module names used in Python
2, so that the pickle data stream is readable with Python 2.
-
-If *buffer_callback* is None (the default), buffer views are
-serialized into *file* as part of the pickle stream.
-
-If *buffer_callback* is not None, then it can be called any number
-of times with a buffer view. If the callback returns a false value
-(such as None), the given buffer is out-of-band; otherwise the
-buffer is serialized in-band, i.e. inside the pickle stream.
-
-It is an error if *buffer_callback* is not None and *protocol*
-is None or smaller than 5.
-
+
+If *buffer_callback* is None (the default), buffer views are
+serialized into *file* as part of the pickle stream.
+
+If *buffer_callback* is not None, then it can be called any number
+of times with a buffer view. If the callback returns a false value
+(such as None), the given buffer is out-of-band; otherwise the
+buffer is serialized in-band, i.e. inside the pickle stream.
+
+It is an error if *buffer_callback* is not None and *protocol*
+is None or smaller than 5.
+
[clinic start generated code]*/
static int
_pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
- PyObject *protocol, int fix_imports,
- PyObject *buffer_callback)
-/*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/
+ PyObject *protocol, int fix_imports,
+ PyObject *buffer_callback)
+/*[clinic end generated code: output=0abedc50590d259b input=a7c969699bf5dad3]*/
{
_Py_IDENTIFIER(persistent_id);
_Py_IDENTIFIER(dispatch_table);
@@ -4741,9 +4741,9 @@ _pickle_Pickler___init___impl(PicklerObject *self, PyObject *file,
if (_Pickler_SetOutputStream(self, file) < 0)
return -1;
- if (_Pickler_SetBufferCallback(self, buffer_callback) < 0)
- return -1;
-
+ if (_Pickler_SetBufferCallback(self, buffer_callback) < 0)
+ return -1;
+
/* memo and output_buffer may have already been created in _Pickler_New */
if (self->memo == NULL) {
self->memo = PyMemoTable_New();
@@ -4916,10 +4916,10 @@ static PyTypeObject PicklerMemoProxyType = {
sizeof(PicklerMemoProxyObject), /*tp_basicsize*/
0,
(destructor)PicklerMemoProxy_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ 0, /* tp_as_async */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -4974,7 +4974,7 @@ Pickler_set_memo(PicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored))
return -1;
}
- if (Py_IS_TYPE(obj, &PicklerMemoProxyType)) {
+ if (Py_IS_TYPE(obj, &PicklerMemoProxyType)) {
PicklerObject *pickler =
((PicklerMemoProxyObject *)obj)->pickler;
@@ -5077,10 +5077,10 @@ static PyTypeObject Pickler_Type = {
sizeof(PicklerObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Pickler_dealloc, /*tp_dealloc*/
- 0, /*tp_vectorcall_offset*/
+ 0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
- 0, /*tp_as_async*/
+ 0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
@@ -5532,101 +5532,101 @@ load_counted_binbytes(UnpicklerObject *self, int nbytes)
return -1;
}
- bytes = PyBytes_FromStringAndSize(NULL, size);
+ bytes = PyBytes_FromStringAndSize(NULL, size);
if (bytes == NULL)
return -1;
- if (_Unpickler_ReadInto(self, PyBytes_AS_STRING(bytes), size) < 0) {
- Py_DECREF(bytes);
- return -1;
- }
+ if (_Unpickler_ReadInto(self, PyBytes_AS_STRING(bytes), size) < 0) {
+ Py_DECREF(bytes);
+ return -1;
+ }
PDATA_PUSH(self->stack, bytes, -1);
return 0;
}
static int
-load_counted_bytearray(UnpicklerObject *self)
-{
- PyObject *bytearray;
- Py_ssize_t size;
- char *s;
-
- if (_Unpickler_Read(self, &s, 8) < 0) {
- return -1;
- }
-
- size = calc_binsize(s, 8);
- if (size < 0) {
- PyErr_Format(PyExc_OverflowError,
- "BYTEARRAY8 exceeds system's maximum size of %zd bytes",
- PY_SSIZE_T_MAX);
- return -1;
- }
-
- bytearray = PyByteArray_FromStringAndSize(NULL, size);
- if (bytearray == NULL) {
- return -1;
- }
- if (_Unpickler_ReadInto(self, PyByteArray_AS_STRING(bytearray), size) < 0) {
- Py_DECREF(bytearray);
- return -1;
- }
-
- PDATA_PUSH(self->stack, bytearray, -1);
- return 0;
-}
-
-static int
-load_next_buffer(UnpicklerObject *self)
-{
- if (self->buffers == NULL) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_SetString(st->UnpicklingError,
- "pickle stream refers to out-of-band data "
- "but no *buffers* argument was given");
- return -1;
- }
- PyObject *buf = PyIter_Next(self->buffers);
- if (buf == NULL) {
- if (!PyErr_Occurred()) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_SetString(st->UnpicklingError,
- "not enough out-of-band buffers");
- }
- return -1;
- }
-
- PDATA_PUSH(self->stack, buf, -1);
- return 0;
-}
-
-static int
-load_readonly_buffer(UnpicklerObject *self)
-{
- Py_ssize_t len = Py_SIZE(self->stack);
- if (len <= self->stack->fence) {
- return Pdata_stack_underflow(self->stack);
- }
-
- PyObject *obj = self->stack->data[len - 1];
- PyObject *view = PyMemoryView_FromObject(obj);
- if (view == NULL) {
- return -1;
- }
- if (!PyMemoryView_GET_BUFFER(view)->readonly) {
- /* Original object is writable */
- PyMemoryView_GET_BUFFER(view)->readonly = 1;
- self->stack->data[len - 1] = view;
- Py_DECREF(obj);
- }
- else {
- /* Original object is read-only, no need to replace it */
- Py_DECREF(view);
- }
- return 0;
-}
-
-static int
+load_counted_bytearray(UnpicklerObject *self)
+{
+ PyObject *bytearray;
+ Py_ssize_t size;
+ char *s;
+
+ if (_Unpickler_Read(self, &s, 8) < 0) {
+ return -1;
+ }
+
+ size = calc_binsize(s, 8);
+ if (size < 0) {
+ PyErr_Format(PyExc_OverflowError,
+ "BYTEARRAY8 exceeds system's maximum size of %zd bytes",
+ PY_SSIZE_T_MAX);
+ return -1;
+ }
+
+ bytearray = PyByteArray_FromStringAndSize(NULL, size);
+ if (bytearray == NULL) {
+ return -1;
+ }
+ if (_Unpickler_ReadInto(self, PyByteArray_AS_STRING(bytearray), size) < 0) {
+ Py_DECREF(bytearray);
+ return -1;
+ }
+
+ PDATA_PUSH(self->stack, bytearray, -1);
+ return 0;
+}
+
+static int
+load_next_buffer(UnpicklerObject *self)
+{
+ if (self->buffers == NULL) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_SetString(st->UnpicklingError,
+ "pickle stream refers to out-of-band data "
+ "but no *buffers* argument was given");
+ return -1;
+ }
+ PyObject *buf = PyIter_Next(self->buffers);
+ if (buf == NULL) {
+ if (!PyErr_Occurred()) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_SetString(st->UnpicklingError,
+ "not enough out-of-band buffers");
+ }
+ return -1;
+ }
+
+ PDATA_PUSH(self->stack, buf, -1);
+ return 0;
+}
+
+static int
+load_readonly_buffer(UnpicklerObject *self)
+{
+ Py_ssize_t len = Py_SIZE(self->stack);
+ if (len <= self->stack->fence) {
+ return Pdata_stack_underflow(self->stack);
+ }
+
+ PyObject *obj = self->stack->data[len - 1];
+ PyObject *view = PyMemoryView_FromObject(obj);
+ if (view == NULL) {
+ return -1;
+ }
+ if (!PyMemoryView_GET_BUFFER(view)->readonly) {
+ /* Original object is writable */
+ PyMemoryView_GET_BUFFER(view)->readonly = 1;
+ self->stack->data[len - 1] = view;
+ Py_DECREF(obj);
+ }
+ else {
+ /* Original object is read-only, no need to replace it */
+ Py_DECREF(view);
+ }
+ return 0;
+}
+
+static int
load_unicode(UnpicklerObject *self)
{
PyObject *str;
@@ -5821,7 +5821,7 @@ instantiate(PyObject *cls, PyObject *args)
return NULL;
}
if (func == NULL) {
- return _PyObject_CallMethodIdOneArg(cls, &PyId___new__, cls);
+ return _PyObject_CallMethodIdOneArg(cls, &PyId___new__, cls);
}
Py_DECREF(func);
}
@@ -5991,27 +5991,27 @@ load_newobj_ex(UnpicklerObject *self)
PyErr_Format(st->UnpicklingError,
"NEWOBJ_EX class argument must be a type, not %.200s",
Py_TYPE(cls)->tp_name);
- goto error;
+ goto error;
}
if (((PyTypeObject *)cls)->tp_new == NULL) {
PyErr_SetString(st->UnpicklingError,
"NEWOBJ_EX class argument doesn't have __new__");
- goto error;
- }
- if (!PyTuple_Check(args)) {
- PyErr_Format(st->UnpicklingError,
- "NEWOBJ_EX args argument must be a tuple, not %.200s",
- Py_TYPE(args)->tp_name);
- goto error;
- }
- if (!PyDict_Check(kwargs)) {
- PyErr_Format(st->UnpicklingError,
- "NEWOBJ_EX kwargs argument must be a dict, not %.200s",
- Py_TYPE(kwargs)->tp_name);
- goto error;
- }
-
+ goto error;
+ }
+ if (!PyTuple_Check(args)) {
+ PyErr_Format(st->UnpicklingError,
+ "NEWOBJ_EX args argument must be a tuple, not %.200s",
+ Py_TYPE(args)->tp_name);
+ goto error;
+ }
+ if (!PyDict_Check(kwargs)) {
+ PyErr_Format(st->UnpicklingError,
+ "NEWOBJ_EX kwargs argument must be a dict, not %.200s",
+ Py_TYPE(kwargs)->tp_name);
+ goto error;
+ }
+
obj = ((PyTypeObject *)cls)->tp_new((PyTypeObject *)cls, args, kwargs);
Py_DECREF(kwargs);
Py_DECREF(args);
@@ -6021,12 +6021,12 @@ load_newobj_ex(UnpicklerObject *self)
}
PDATA_PUSH(self->stack, obj, -1);
return 0;
-
-error:
- Py_DECREF(kwargs);
- Py_DECREF(args);
- Py_DECREF(cls);
- return -1;
+
+error:
+ Py_DECREF(kwargs);
+ Py_DECREF(args);
+ Py_DECREF(cls);
+ return -1;
}
static int
@@ -6180,7 +6180,7 @@ load_pop(UnpicklerObject *self)
else {
len--;
Py_DECREF(self->stack->data[len]);
- Py_SET_SIZE(self->stack, len);
+ Py_SET_SIZE(self->stack, len);
}
return 0;
}
@@ -6235,10 +6235,10 @@ load_get(UnpicklerObject *self)
value = _Unpickler_MemoGet(self, idx);
if (value == NULL) {
- if (!PyErr_Occurred()) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
- }
+ if (!PyErr_Occurred()) {
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
+ }
Py_DECREF(key);
return -1;
}
@@ -6264,8 +6264,8 @@ load_binget(UnpicklerObject *self)
if (value == NULL) {
PyObject *key = PyLong_FromSsize_t(idx);
if (key != NULL) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
Py_DECREF(key);
}
return -1;
@@ -6291,8 +6291,8 @@ load_long_binget(UnpicklerObject *self)
if (value == NULL) {
PyObject *key = PyLong_FromSsize_t(idx);
if (key != NULL) {
- PickleState *st = _Pickle_GetGlobalState();
- PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
+ PickleState *st = _Pickle_GetGlobalState();
+ PyErr_Format(st->UnpicklingError, "Memo value not found at index %ld", idx);
Py_DECREF(key);
}
return -1;
@@ -6355,20 +6355,20 @@ load_extension(UnpicklerObject *self, int nbytes)
/* Since the extension registry is manipulable via Python code,
* confirm that pair is really a 2-tuple of strings.
*/
- if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
- goto error;
- }
-
- module_name = PyTuple_GET_ITEM(pair, 0);
- if (!PyUnicode_Check(module_name)) {
- goto error;
- }
-
- class_name = PyTuple_GET_ITEM(pair, 1);
- if (!PyUnicode_Check(class_name)) {
- goto error;
- }
-
+ if (!PyTuple_Check(pair) || PyTuple_Size(pair) != 2) {
+ goto error;
+ }
+
+ module_name = PyTuple_GET_ITEM(pair, 0);
+ if (!PyUnicode_Check(module_name)) {
+ goto error;
+ }
+
+ class_name = PyTuple_GET_ITEM(pair, 1);
+ if (!PyUnicode_Check(class_name)) {
+ goto error;
+ }
+
/* Load the object. */
obj = find_class(self, module_name, class_name);
if (obj == NULL) {
@@ -6384,12 +6384,12 @@ load_extension(UnpicklerObject *self, int nbytes)
}
PDATA_PUSH(self->stack, obj, -1);
return 0;
-
-error:
- Py_DECREF(py_code);
- PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
- "isn't a 2-tuple of strings", code);
- return -1;
+
+error:
+ Py_DECREF(py_code);
+ PyErr_Format(PyExc_ValueError, "_inverted_registry[%ld] "
+ "isn't a 2-tuple of strings", code);
+ return -1;
}
static int
@@ -6511,9 +6511,9 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
PyObject *extend_func;
_Py_IDENTIFIER(extend);
- if (_PyObject_LookupAttrId(list, &PyId_extend, &extend_func) < 0) {
- return -1;
- }
+ if (_PyObject_LookupAttrId(list, &PyId_extend, &extend_func) < 0) {
+ return -1;
+ }
if (extend_func != NULL) {
slice = Pdata_poplist(self->stack, x);
if (!slice) {
@@ -6541,13 +6541,13 @@ do_append(UnpicklerObject *self, Py_ssize_t x)
result = _Pickle_FastCall(append_func, value);
if (result == NULL) {
Pdata_clear(self->stack, i + 1);
- Py_SET_SIZE(self->stack, x);
+ Py_SET_SIZE(self->stack, x);
Py_DECREF(append_func);
return -1;
}
Py_DECREF(result);
}
- Py_SET_SIZE(self->stack, x);
+ Py_SET_SIZE(self->stack, x);
Py_DECREF(append_func);
}
}
@@ -6587,7 +6587,7 @@ do_setitems(UnpicklerObject *self, Py_ssize_t x)
return 0;
if ((len - x) % 2 != 0) {
PickleState *st = _Pickle_GetGlobalState();
- /* Corrupt or hostile pickle -- we never write one like this. */
+ /* Corrupt or hostile pickle -- we never write one like this. */
PyErr_SetString(st->UnpicklingError,
"odd number of items for SETITEMS");
return -1;
@@ -6669,12 +6669,12 @@ load_additems(UnpicklerObject *self)
result = _Pickle_FastCall(add_func, item);
if (result == NULL) {
Pdata_clear(self->stack, i + 1);
- Py_SET_SIZE(self->stack, mark);
+ Py_SET_SIZE(self->stack, mark);
return -1;
}
Py_DECREF(result);
}
- Py_SET_SIZE(self->stack, mark);
+ Py_SET_SIZE(self->stack, mark);
}
return 0;
@@ -6800,15 +6800,15 @@ load_mark(UnpicklerObject *self)
* mark stack.
*/
- if (self->num_marks >= self->marks_size) {
- size_t alloc = ((size_t)self->num_marks << 1) + 20;
- Py_ssize_t *marks_new = self->marks;
- PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
- if (marks_new == NULL) {
+ if (self->num_marks >= self->marks_size) {
+ size_t alloc = ((size_t)self->num_marks << 1) + 20;
+ Py_ssize_t *marks_new = self->marks;
+ PyMem_RESIZE(marks_new, Py_ssize_t, alloc);
+ if (marks_new == NULL) {
PyErr_NoMemory();
return -1;
}
- self->marks = marks_new;
+ self->marks = marks_new;
self->marks_size = (Py_ssize_t)alloc;
}
@@ -6932,9 +6932,9 @@ load(UnpicklerObject *self)
OP_ARG(SHORT_BINBYTES, load_counted_binbytes, 1)
OP_ARG(BINBYTES, load_counted_binbytes, 4)
OP_ARG(BINBYTES8, load_counted_binbytes, 8)
- OP(BYTEARRAY8, load_counted_bytearray)
- OP(NEXT_BUFFER, load_next_buffer)
- OP(READONLY_BUFFER, load_readonly_buffer)
+ OP(BYTEARRAY8, load_counted_bytearray)
+ OP(NEXT_BUFFER, load_next_buffer)
+ OP(READONLY_BUFFER, load_readonly_buffer)
OP_ARG(SHORT_BINSTRING, load_counted_binstring, 1)
OP_ARG(BINSTRING, load_counted_binstring, 4)
OP(STRING, load_string)
@@ -7083,11 +7083,11 @@ _pickle_Unpickler_find_class_impl(UnpicklerObject *self,
PyObject *global;
PyObject *module;
- if (PySys_Audit("pickle.find_class", "OO",
- module_name, global_name) < 0) {
- return NULL;
- }
-
+ if (PySys_Audit("pickle.find_class", "OO",
+ module_name, global_name) < 0) {
+ return NULL;
+ }
+
/* Try to map the old names used in Python 2.x to the new ones used in
Python 3.x. We do this only with old pickle protocols and when the
user has not disabled the feature. */
@@ -7195,12 +7195,12 @@ Unpickler_dealloc(UnpicklerObject *self)
{
PyObject_GC_UnTrack((PyObject *)self);
Py_XDECREF(self->readline);
- Py_XDECREF(self->readinto);
+ Py_XDECREF(self->readinto);
Py_XDECREF(self->read);
Py_XDECREF(self->peek);
Py_XDECREF(self->stack);
Py_XDECREF(self->pers_func);
- Py_XDECREF(self->buffers);
+ Py_XDECREF(self->buffers);
if (self->buffer.buf != NULL) {
PyBuffer_Release(&self->buffer);
self->buffer.buf = NULL;
@@ -7219,12 +7219,12 @@ static int
Unpickler_traverse(UnpicklerObject *self, visitproc visit, void *arg)
{
Py_VISIT(self->readline);
- Py_VISIT(self->readinto);
+ Py_VISIT(self->readinto);
Py_VISIT(self->read);
Py_VISIT(self->peek);
Py_VISIT(self->stack);
Py_VISIT(self->pers_func);
- Py_VISIT(self->buffers);
+ Py_VISIT(self->buffers);
return 0;
}
@@ -7232,12 +7232,12 @@ static int
Unpickler_clear(UnpicklerObject *self)
{
Py_CLEAR(self->readline);
- Py_CLEAR(self->readinto);
+ Py_CLEAR(self->readinto);
Py_CLEAR(self->read);
Py_CLEAR(self->peek);
Py_CLEAR(self->stack);
Py_CLEAR(self->pers_func);
- Py_CLEAR(self->buffers);
+ Py_CLEAR(self->buffers);
if (self->buffer.buf != NULL) {
PyBuffer_Release(&self->buffer);
self->buffer.buf = NULL;
@@ -7265,7 +7265,7 @@ _pickle.Unpickler.__init__
fix_imports: bool = True
encoding: str = 'ASCII'
errors: str = 'strict'
- buffers: object(c_default="NULL") = ()
+ buffers: object(c_default="NULL") = ()
This takes a binary file for reading a pickle data stream.
@@ -7292,8 +7292,8 @@ string instances as bytes objects.
static int
_pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
int fix_imports, const char *encoding,
- const char *errors, PyObject *buffers)
-/*[clinic end generated code: output=09f0192649ea3f85 input=ca4c1faea9553121]*/
+ const char *errors, PyObject *buffers)
+/*[clinic end generated code: output=09f0192649ea3f85 input=ca4c1faea9553121]*/
{
_Py_IDENTIFIER(persistent_load);
@@ -7307,9 +7307,9 @@ _pickle_Unpickler___init___impl(UnpicklerObject *self, PyObject *file,
if (_Unpickler_SetInputEncoding(self, encoding, errors) < 0)
return -1;
- if (_Unpickler_SetBuffers(self, buffers) < 0)
- return -1;
-
+ if (_Unpickler_SetBuffers(self, buffers) < 0)
+ return -1;
+
self->fix_imports = fix_imports;
if (init_method_ref((PyObject *)self, &PyId_persistent_load,
@@ -7471,10 +7471,10 @@ static PyTypeObject UnpicklerMemoProxyType = {
sizeof(UnpicklerMemoProxyObject), /*tp_basicsize*/
0,
(destructor)UnpicklerMemoProxy_dealloc, /* tp_dealloc */
- 0, /* tp_vectorcall_offset */
+ 0, /* tp_vectorcall_offset */
0, /* tp_getattr */
0, /* tp_setattr */
- 0, /* tp_as_async */
+ 0, /* tp_as_async */
0, /* tp_repr */
0, /* tp_as_number */
0, /* tp_as_sequence */
@@ -7532,7 +7532,7 @@ Unpickler_set_memo(UnpicklerObject *self, PyObject *obj, void *Py_UNUSED(ignored
return -1;
}
- if (Py_IS_TYPE(obj, &UnpicklerMemoProxyType)) {
+ if (Py_IS_TYPE(obj, &UnpicklerMemoProxyType)) {
UnpicklerObject *unpickler =
((UnpicklerMemoProxyObject *)obj)->unpickler;
@@ -7642,10 +7642,10 @@ static PyTypeObject Unpickler_Type = {
sizeof(UnpicklerObject), /*tp_basicsize*/
0, /*tp_itemsize*/
(destructor)Unpickler_dealloc, /*tp_dealloc*/
- 0, /*tp_vectorcall_offset*/
+ 0, /*tp_vectorcall_offset*/
0, /*tp_getattr*/
0, /*tp_setattr*/
- 0, /*tp_as_async*/
+ 0, /*tp_as_async*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
@@ -7685,10 +7685,10 @@ _pickle.dump
obj: object
file: object
- protocol: object = None
+ protocol: object = None
*
fix_imports: bool = True
- buffer_callback: object = None
+ buffer_callback: object = None
Write a pickled representation of obj to the open file object file.
@@ -7696,9 +7696,9 @@ This is equivalent to ``Pickler(file, protocol).dump(obj)``, but may
be more efficient.
The optional *protocol* argument tells the pickler to use the given
-protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
-protocol is 4. It was introduced in Python 3.4, and is incompatible
-with previous versions.
+protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
+protocol is 4. It was introduced in Python 3.4, and is incompatible
+with previous versions.
Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the
@@ -7712,18 +7712,18 @@ this interface.
If *fix_imports* is True and protocol is less than 3, pickle will try
to map the new Python 3 names to the old module names used in Python
2, so that the pickle data stream is readable with Python 2.
-
-If *buffer_callback* is None (the default), buffer views are serialized
-into *file* as part of the pickle stream. It is an error if
-*buffer_callback* is not None and *protocol* is None or smaller than 5.
-
+
+If *buffer_callback* is None (the default), buffer views are serialized
+into *file* as part of the pickle stream. It is an error if
+*buffer_callback* is not None and *protocol* is None or smaller than 5.
+
[clinic start generated code]*/
static PyObject *
_pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
- PyObject *protocol, int fix_imports,
- PyObject *buffer_callback)
-/*[clinic end generated code: output=706186dba996490c input=5ed6653da99cd97c]*/
+ PyObject *protocol, int fix_imports,
+ PyObject *buffer_callback)
+/*[clinic end generated code: output=706186dba996490c input=5ed6653da99cd97c]*/
{
PicklerObject *pickler = _Pickler_New();
@@ -7736,9 +7736,9 @@ _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
if (_Pickler_SetOutputStream(pickler, file) < 0)
goto error;
- if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
- goto error;
-
+ if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
+ goto error;
+
if (dump(pickler, obj) < 0)
goto error;
@@ -7758,17 +7758,17 @@ _pickle_dump_impl(PyObject *module, PyObject *obj, PyObject *file,
_pickle.dumps
obj: object
- protocol: object = None
+ protocol: object = None
*
fix_imports: bool = True
- buffer_callback: object = None
+ buffer_callback: object = None
Return the pickled representation of the object as a bytes object.
The optional *protocol* argument tells the pickler to use the given
-protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
-protocol is 4. It was introduced in Python 3.4, and is incompatible
-with previous versions.
+protocol; supported protocols are 0, 1, 2, 3, 4 and 5. The default
+protocol is 4. It was introduced in Python 3.4, and is incompatible
+with previous versions.
Specifying a negative protocol version selects the highest protocol
version supported. The higher the protocol used, the more recent the
@@ -7777,17 +7777,17 @@ version of Python needed to read the pickle produced.
If *fix_imports* is True and *protocol* is less than 3, pickle will
try to map the new Python 3 names to the old module names used in
Python 2, so that the pickle data stream is readable with Python 2.
-
-If *buffer_callback* is None (the default), buffer views are serialized
-into *file* as part of the pickle stream. It is an error if
-*buffer_callback* is not None and *protocol* is None or smaller than 5.
-
+
+If *buffer_callback* is None (the default), buffer views are serialized
+into *file* as part of the pickle stream. It is an error if
+*buffer_callback* is not None and *protocol* is None or smaller than 5.
+
[clinic start generated code]*/
static PyObject *
_pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
- int fix_imports, PyObject *buffer_callback)
-/*[clinic end generated code: output=fbab0093a5580fdf input=e543272436c6f987]*/
+ int fix_imports, PyObject *buffer_callback)
+/*[clinic end generated code: output=fbab0093a5580fdf input=e543272436c6f987]*/
{
PyObject *result;
PicklerObject *pickler = _Pickler_New();
@@ -7798,9 +7798,9 @@ _pickle_dumps_impl(PyObject *module, PyObject *obj, PyObject *protocol,
if (_Pickler_SetProtocol(pickler, protocol, fix_imports) < 0)
goto error;
- if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
- goto error;
-
+ if (_Pickler_SetBufferCallback(pickler, buffer_callback) < 0)
+ goto error;
+
if (dump(pickler, obj) < 0)
goto error;
@@ -7822,7 +7822,7 @@ _pickle.load
fix_imports: bool = True
encoding: str = 'ASCII'
errors: str = 'strict'
- buffers: object(c_default="NULL") = ()
+ buffers: object(c_default="NULL") = ()
Read and return an object from the pickle data stored in a file.
@@ -7851,9 +7851,9 @@ string instances as bytes objects.
static PyObject *
_pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
- const char *encoding, const char *errors,
- PyObject *buffers)
-/*[clinic end generated code: output=250452d141c23e76 input=46c7c31c92f4f371]*/
+ const char *encoding, const char *errors,
+ PyObject *buffers)
+/*[clinic end generated code: output=250452d141c23e76 input=46c7c31c92f4f371]*/
{
PyObject *result;
UnpicklerObject *unpickler = _Unpickler_New();
@@ -7867,9 +7867,9 @@ _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
goto error;
- if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
- goto error;
-
+ if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
+ goto error;
+
unpickler->fix_imports = fix_imports;
result = load(unpickler);
@@ -7886,12 +7886,12 @@ _pickle_load_impl(PyObject *module, PyObject *file, int fix_imports,
_pickle.loads
data: object
- /
+ /
*
fix_imports: bool = True
encoding: str = 'ASCII'
errors: str = 'strict'
- buffers: object(c_default="NULL") = ()
+ buffers: object(c_default="NULL") = ()
Read and return an object from the given pickle data.
@@ -7911,9 +7911,9 @@ string instances as bytes objects.
static PyObject *
_pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
- const char *encoding, const char *errors,
- PyObject *buffers)
-/*[clinic end generated code: output=82ac1e6b588e6d02 input=b3615540d0535087]*/
+ const char *encoding, const char *errors,
+ PyObject *buffers)
+/*[clinic end generated code: output=82ac1e6b588e6d02 input=b3615540d0535087]*/
{
PyObject *result;
UnpicklerObject *unpickler = _Unpickler_New();
@@ -7927,9 +7927,9 @@ _pickle_loads_impl(PyObject *module, PyObject *data, int fix_imports,
if (_Unpickler_SetInputEncoding(unpickler, encoding, errors) < 0)
goto error;
- if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
- goto error;
-
+ if (_Unpickler_SetBuffers(unpickler, buffers) < 0)
+ goto error;
+
unpickler->fix_imports = fix_imports;
result = load(unpickler);
@@ -7979,7 +7979,7 @@ pickle_traverse(PyObject *m, visitproc visit, void *arg)
Py_VISIT(st->import_mapping_3to2);
Py_VISIT(st->codecs_encode);
Py_VISIT(st->getattr);
- Py_VISIT(st->partial);
+ Py_VISIT(st->partial);
return 0;
}
@@ -8019,16 +8019,16 @@ PyInit__pickle(void)
if (m == NULL)
return NULL;
- /* Add types */
- if (PyModule_AddType(m, &Pickler_Type) < 0) {
+ /* Add types */
+ if (PyModule_AddType(m, &Pickler_Type) < 0) {
return NULL;
- }
- if (PyModule_AddType(m, &Unpickler_Type) < 0) {
+ }
+ if (PyModule_AddType(m, &Unpickler_Type) < 0) {
return NULL;
- }
- if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) {
- return NULL;
- }
+ }
+ if (PyModule_AddType(m, &PyPickleBuffer_Type) < 0) {
+ return NULL;
+ }
st = _Pickle_GetState(m);