10000 gh-74185: repr() of ImportError now contains attributes name and path. by serhiy-storchaka · Pull Request #1011 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-74185: repr() of ImportError now contains attributes name and path. #1011

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Prev Previous commit
Next Next commit
Merge branch 'master' into importerror-repr
  • Loading branch information
serhiy-storchaka authored Apr 13, 2017
commit b2d33e8a63ca025afb64ac2a702aba39c88c11c8
19 changes: 19 additions & 0 deletions Lib/test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1157,6 +1157,25 @@ def test_repr(self):
self.assertEqual(repr(exc),
"ImportError('test', name='somename', path='somepath')")

Copy link
Member
@ezio-melotti ezio-melotti Mar 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are there any tests that ensure that the name of the module and the path are set correctly?
I would add a test tries to import a non-existing module and check those attributes (and/or they repr).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, a test with the ModuleNotFoundError subclass would be nice.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, but we might want a test that actually tries importing some nonexistent module and see if its __repr__ has those required attributes.

def test_copy_pickle(self):
for kwargs in (dict(),
dict(name='somename'),
dict(path='somepath'),
dict(name='somename', path='somepath')):
orig = ImportError('test', **kwargs)
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
exc = pickle.loads(pickle.dumps(orig, proto))
self.assertEqual(exc.args, ('test',))
self.assertEqual(exc.msg, 'test')
self.assertEqual(exc.name, orig.name)
self.assertEqual(exc.path, orig.path)
for c in copy.copy, copy.deepcopy:
exc = c(orig)
self.assertEqual(exc.args, ('test',))
self.assertEqual(exc.msg, 'test')
self.assertEqual(exc.name, orig.name)
self.assertEqual(exc.path, orig.path)


if __name__ == '__main__':
unittest.main()
6 changes: 6 additions & 0 deletions Misc/NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ Core and Builtins
-----------------

- bpo-29999: repr() of ImportError now contains attributes name and path.
Standard repr() of BaseException with single argument no longer contains
trailing comma.

- bpo-29914: Fixed default implementations of __reduce__ and __reduce_ex__().
object.__reduce__() no longer takes arguments, object.__reduce_ex__() now
requires one argument.

- bpo-29949: Fix memory usage regression of set and frozenset object.

Expand Down
47 changes: 47 additions & 0 deletions Objects/exceptions.c
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,53 @@ ImportError_repr(PyImportErrorObject *self)
return r;
}

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")},
Expand Down
You are viewing a condensed version of this merge commit. You can view the full changes here.
0