mirror of
https://github.com/python/cpython.git
synced 2025-10-07 07:31:46 +00:00
attributes.
(cherry picked from commit b785396ab4
)
This commit is contained in:
parent
fa25f16a44
commit
e63f8f293a
3 changed files with 71 additions and 0 deletions
|
@ -692,6 +692,53 @@ ImportError_str(PyImportErrorObject *self)
|
|||
}
|
||||
}
|
||||
|
||||
static PyObject *
|
||||
ImportError_getstate(PyImportErrorObject *self)
|
||||
{
|
||||
PyObject *dict = ((PyBaseExceptionObject *)self)->dict;
|
||||
if (self->name || self->path) {
|
||||
_Py_IDENTIFIER(name);
|
||||
_Py_IDENTIFIER(path);
|
||||
dict = dict ? PyDict_Copy(dict) : PyDict_New();
|
||||
if (dict == NULL)
|
||||
return NULL;
|
||||
if (self->name && _PyDict_SetItemId(dict, &PyId_name, self->name) < 0) {
|
||||
Py_DECREF(dict);
|
||||
return NULL;
|
||||
}
|
||||
if (self->path && _PyDict_SetItemId(dict, &PyId_path, self->path) < 0) {
|
||||
Py_DECREF(dict);
|
||||
return NULL;
|
||||
}
|
||||
return dict;
|
||||
}
|
||||
else if (dict) {
|
||||
Py_INCREF(dict);
|
||||
return dict;
|
||||
}
|
||||
else {
|
||||
Py_RETURN_NONE;
|
||||
}
|
||||
}
|
||||
|
||||
/* Pickling support */
|
||||
static PyObject *
|
||||
ImportError_reduce(PyImportErrorObject *self)
|
||||
{
|
||||
PyObject *res;
|
||||
PyObject *args;
|
||||
PyObject *state = ImportError_getstate(self);
|
||||
if (state == NULL)
|
||||
return NULL;
|
||||
args = ((PyBaseExceptionObject *)self)->args;
|
||||
if (state == Py_None)
|
||||
res = PyTuple_Pack(2, Py_TYPE(self), args);
|
||||
else
|
||||
res = PyTuple_Pack(3, Py_TYPE(self), args, state);
|
||||
Py_DECREF(state);
|
||||
return res;
|
||||
}
|
||||
|
||||
static PyMemberDef ImportError_members[] = {
|
||||
{"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
|
||||
PyDoc_STR("exception message")},
|
||||
|
@ -703,6 +750,7 @@ static PyMemberDef ImportError_members[] = {
|
|||
};
|
||||
|
||||
static PyMethodDef ImportError_methods[] = {
|
||||
{"__reduce__", (PyCFunction)ImportError_reduce, METH_NOARGS},
|
||||
{NULL}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue