diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2017-04-08 08:25:47 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-04-08 08:25:47 (GMT) |
commit | af685f9050416da8050e0ec11a8dff9afd4130e7 (patch) | |
tree | 6cc7ad8a88bd7458913a0b1501601b230c62f513 /Objects | |
parent | c26b19d5c7aba51b50a4d7fb5f8291036cb9da24 (diff) | |
download | cpython-af685f9050416da8050e0ec11a8dff9afd4130e7.zip cpython-af685f9050416da8050e0ec11a8dff9afd4130e7.tar.gz cpython-af685f9050416da8050e0ec11a8dff9afd4130e7.tar.bz2 |
bpo-29998: Pickling and copying ImportError now preserves name and path (#1010) (#1042)
attributes.
(cherry picked from commit b785396ab451b0c9d6ae9ee5a9e56c810209a6cb)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/exceptions.c | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/Objects/exceptions.c b/Objects/exceptions.c index f63f06a..d158b97 100644 --- a/Objects/exceptions.c +++ b/Objects/exceptions.c @@ -686,6 +686,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")}, @@ -697,6 +744,7 @@ static PyMemberDef ImportError_members[] = { }; static PyMethodDef ImportError_methods[] = { + {"__reduce__", (PyCFunction)ImportError_reduce, METH_NOARGS}, {NULL} }; |