diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2022-04-06 17:00:14 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-06 17:00:14 (GMT) |
commit | 884eba3c76916889fd6bff3b37b8552bfb4f9566 (patch) | |
tree | 51fd55d6170cdff327ac11d70f1e5ff1aa7e735a /Modules | |
parent | f82f9ce3239b9a7e6ffa278658dd9858f64a3c14 (diff) | |
download | cpython-884eba3c76916889fd6bff3b37b8552bfb4f9566.zip cpython-884eba3c76916889fd6bff3b37b8552bfb4f9566.tar.gz cpython-884eba3c76916889fd6bff3b37b8552bfb4f9566.tar.bz2 |
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.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/_collectionsmodule.c | 15 | ||||
-rw-r--r-- | Modules/_datetimemodule.c | 30 |
2 files changed, 10 insertions, 35 deletions
diff --git a/Modules/_collectionsmodule.c b/Modules/_collectionsmodule.c index b47f977..f78e261 100644 --- a/Modules/_collectionsmodule.c +++ b/Modules/_collectionsmodule.c @@ -1347,27 +1347,24 @@ deque_traverse(dequeobject *deque, visitproc visit, void *arg) static PyObject * deque_reduce(dequeobject *deque, PyObject *Py_UNUSED(ignored)) { - PyObject *dict, *it; + PyObject *state, *it; - if (_PyObject_LookupAttr((PyObject *)deque, &_Py_ID(__dict__), &dict) < 0) { + state = _PyObject_GetState((PyObject *)deque); + if (state == NULL) { return NULL; } - if (dict == NULL) { - dict = Py_None; - Py_INCREF(dict); - } it = PyObject_GetIter((PyObject *)deque); if (it == NULL) { - Py_DECREF(dict); + Py_DECREF(state); return NULL; } if (deque->maxlen < 0) { - return Py_BuildValue("O()NN", Py_TYPE(deque), dict, it); + return Py_BuildValue("O()NN", Py_TYPE(deque), state, it); } else { - return Py_BuildValue("O(()n)NN", Py_TYPE(deque), deque->maxlen, dict, it); + return Py_BuildValue("O(()n)NN", Py_TYPE(deque), deque->maxlen, state, it); } } diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c index ae97190..fc766c3 100644 --- a/Modules/_datetimemodule.c +++ b/Modules/_datetimemodule.c @@ -3736,9 +3736,8 @@ static PyObject * tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) { PyObject *args, *state; - PyObject *getinitargs, *getstate; + PyObject *getinitargs; _Py_IDENTIFIER(__getinitargs__); - _Py_IDENTIFIER(__getstate__); if (_PyObject_LookupAttrId(self, &PyId___getinitargs__, &getinitargs) < 0) { return NULL; @@ -3754,34 +3753,13 @@ tzinfo_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) return NULL; } - if (_PyObject_LookupAttrId(self, &PyId___getstate__, &getstate) < 0) { + state = _PyObject_GetState(self); + if (state == NULL) { Py_DECREF(args); return NULL; } - if (getstate != NULL) { - state = PyObject_CallNoArgs(getstate); - Py_DECREF(getstate); - if (state == NULL) { - Py_DECREF(args); - return NULL; - } - } - else { - PyObject **dictptr; - state = Py_None; - dictptr = _PyObject_GetDictPtr(self); - if (dictptr && *dictptr && PyDict_GET_SIZE(*dictptr)) { - state = *dictptr; - } - Py_INCREF(state); - } - if (state == Py_None) { - Py_DECREF(state); - return Py_BuildValue("(ON)", Py_TYPE(self), args); - } - else - return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); + return Py_BuildValue("(ONN)", Py_TYPE(self), args, state); } static PyMethodDef tzinfo_methods[] = { |