mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
bpo-26579: Add object.__getstate__(). (GH-2821)
Copying and pickling instances of subclasses of builtin types bytearray, set, frozenset, collections.OrderedDict, collections.deque, weakref.WeakSet, and datetime.tzinfo now copies and pickles instance attributes implemented as slots.
This commit is contained in:
parent
f82f9ce323
commit
884eba3c76
25 changed files with 389 additions and 255 deletions
|
@ -2122,35 +2122,26 @@ bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep)
|
|||
static PyObject *
|
||||
_common_reduce(PyByteArrayObject *self, int proto)
|
||||
{
|
||||
PyObject *dict;
|
||||
char *buf;
|
||||
PyObject *state;
|
||||
const char *buf;
|
||||
|
||||
if (_PyObject_LookupAttr((PyObject *)self, &_Py_ID(__dict__), &dict) < 0) {
|
||||
state = _PyObject_GetState((PyObject *)self);
|
||||
if (state == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
if (dict == NULL) {
|
||||
dict = Py_None;
|
||||
Py_INCREF(dict);
|
||||
}
|
||||
|
||||
if (!Py_SIZE(self)) {
|
||||
return Py_BuildValue("(O()N)", Py_TYPE(self), state);
|
||||
}
|
||||
buf = PyByteArray_AS_STRING(self);
|
||||
if (proto < 3) {
|
||||
/* use str based reduction for backwards compatibility with Python 2.x */
|
||||
PyObject *latin1;
|
||||
if (Py_SIZE(self))
|
||||
latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
|
||||
else
|
||||
latin1 = PyUnicode_FromString("");
|
||||
return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict);
|
||||
PyObject *latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
|
||||
return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", state);
|
||||
}
|
||||
else {
|
||||
/* use more efficient byte based reduction */
|
||||
if (Py_SIZE(self)) {
|
||||
return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
|
||||
}
|
||||
else {
|
||||
return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
|
||||
}
|
||||
return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), state);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue