Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Pickle the keyword attributes of AttributeError
  • Loading branch information
csm10495 committed Apr 7, 2023
commit 5e08ff256cd4fa6642546e408742e7eecc41212f
45 changes: 44 additions & 1 deletion Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -2282,14 +2282,57 @@ AttributeError_traverse(PyAttributeErrorObject *self, visitproc visit, void *arg
return BaseException_traverse((PyBaseExceptionObject *)self, visit, arg);
}

/* Pickling support */
static PyObject *
AttributeError_getstate(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *dict = ((PyAttributeErrorObject *)self)->dict;
if (self->name || self->obj || self->args) {
Comment thread
csm10495 marked this conversation as resolved.
Outdated
dict = dict ? PyDict_Copy(dict) : PyDict_New();
if (dict == NULL)
return NULL;
Comment thread
csm10495 marked this conversation as resolved.
Outdated
if (self->name && PyDict_SetItemString(dict, "name", self->name) < 0) {
Py_DECREF(dict);
return NULL;
}
if (self->obj && PyDict_SetItemString(dict, "obj", self->obj) < 0) {
Comment thread
csm10495 marked this conversation as resolved.
Outdated
Py_DECREF(dict);
return NULL;
}
if (self->args && PyDict_SetItemString(dict, "args", self->args) < 0) {
Py_DECREF(dict);
return NULL;
}
return dict;
}
else if (dict) {
return Py_NewRef(dict);
}
else {
Py_RETURN_NONE;
}
Comment thread
csm10495 marked this conversation as resolved.
Outdated
}

static PyObject *
AttributeError_reduce(PyAttributeErrorObject *self, PyObject *Py_UNUSED(ignored))
{
PyObject *state = AttributeError_getstate(self, NULL);
if (state == NULL)
return NULL;
Comment thread
csm10495 marked this conversation as resolved.
Outdated

return PyTuple_Pack(3, Py_TYPE(self), self->args, state);
}

static PyMemberDef AttributeError_members[] = {
{"name", T_OBJECT, offsetof(PyAttributeErrorObject, name), 0, PyDoc_STR("attribute name")},
{"obj", T_OBJECT, offsetof(PyAttributeErrorObject, obj), 0, PyDoc_STR("object")},
{NULL} /* Sentinel */
};

static PyMethodDef AttributeError_methods[] = {
{NULL} /* Sentinel */
{"__getstate__", (PyCFunction)AttributeError_getstate, METH_NOARGS},
{"__reduce__", (PyCFunction)AttributeError_reduce, METH_NOARGS },
{NULL}
};

ComplexExtendsException(PyExc_Exception, AttributeError,
Expand Down