Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
serhiy-storchaka wants to merge9 commits intopython:main
base:main
Choose a base branch
Loading
fromserhiy-storchaka:importerror-repr
Open
Show file tree
Hide file tree
Changes from1 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
NextNext commit
bpo-29999: repr() of ImportError now contains attributes name and path.
  • Loading branch information
@serhiy-storchaka
serhiy-storchaka committedApr 5, 2017
commit025c9808bc8045e04a7d7441638a9619e955fbaf
30 changes: 30 additions & 0 deletionsLib/test/test_exceptions.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1126,6 +1126,36 @@ def test_non_str_argument(self):
exc = ImportError(arg)
self.assertEqual(str(arg), str(exc))

def test_repr(self):
exc = ImportError()
self.assertEqual(repr(exc), "ImportError()")

exc = ImportError('test')
self.assertEqual(repr(exc), "ImportError('test')")

exc = ImportError('test', 'case')
self.assertEqual(repr(exc), "ImportError('test', 'case')")

exc = ImportError(name='somemodule')
self.assertEqual(repr(exc), "ImportError(name='somemodule')")

exc = ImportError('test', name='somemodule')
self.assertEqual(repr(exc), "ImportError('test', name='somemodule')")

exc = ImportError(path='somepath')
self.assertEqual(repr(exc), "ImportError(path='somepath')")

exc = ImportError('test', path='somepath')
self.assertEqual(repr(exc), "ImportError('test', path='somepath')")

exc = ImportError(name='somename', path='somepath')
self.assertEqual(repr(exc),
"ImportError(name='somename', path='somepath')")

exc = ImportError('test', name='somename', path='somepath')
self.assertEqual(repr(exc),
"ImportError('test', name='somename', path='somepath')")

Copy link
Member

@ezio-melottiezio-melottiMar 5, 2023
edited
Loading

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 theyrepr).

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 theModuleNotFoundError 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.

auvipy reacted with thumbs up emoji

if __name__ == '__main__':
unittest.main()
2 changes: 2 additions & 0 deletionsMisc/NEWS
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -10,6 +10,8 @@ What's New in Python 3.7.0 alpha 1?
Core and Builtins
-----------------

- bpo-29999: repr() of ImportError now contains attributes name and path.

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

- bpo-29935: Fixed error messages in the index() method of tuple, list and deque
Expand Down
53 changes: 46 additions & 7 deletionsObjects/exceptions.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -121,7 +121,11 @@ BaseException_repr(PyBaseExceptionObject *self)
dot = (const char *) strrchr(name, '.');
if (dot != NULL) name = dot+1;

return PyUnicode_FromFormat("%s%R", name, self->args);
if (PyTuple_GET_SIZE(self->args) == 1)
return PyUnicode_FromFormat("%s(%R)", name,
PyTuple_GET_ITEM(self->args, 0));
else
return PyUnicode_FromFormat("%s%R", name, self->args);
}

/* Pickling support */
Expand DownExpand Up@@ -682,6 +686,30 @@ ImportError_str(PyImportErrorObject *self)
}
}

static PyObject *
ImportError_repr(PyImportErrorObject *self)
{
int hasargs = PyTuple_GET_SIZE(((PyBaseExceptionObject *)self)->args) != 0;
PyObject *r = BaseException_repr((PyBaseExceptionObject *)self);
if (r && (self->name || self->path)) {
/* remove ')' */
Py_SETREF(r, PyUnicode_Substring(r, 0, PyUnicode_GET_LENGTH(r) - 1));
if (r && self->name) {
Py_SETREF(r, PyUnicode_FromFormat("%U%sname=%R",
r, hasargs ? ", " : "", self->name));
hasargs = 1;
}
if (r && self->path) {
Py_SETREF(r, PyUnicode_FromFormat("%U%spath=%R",
r, hasargs ? ", " : "", self->path));
}
if (r) {
Py_SETREF(r, PyUnicode_FromFormat("%U)", r));
}
}
return r;
}

static PyMemberDef ImportError_members[] = {
{"msg", T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
PyDoc_STR("exception message")},
Expand All@@ -696,12 +724,23 @@ static PyMethodDef ImportError_methods[] = {
{NULL}
};

ComplexExtendsException(PyExc_Exception, ImportError,
ImportError, 0 /* new */,
ImportError_methods, ImportError_members,
0 /* getset */, ImportError_str,
"Import can't find module, or can't find name in "
"module.");
static PyTypeObject _PyExc_ImportError = {
PyVarObject_HEAD_INIT(NULL, 0)
"ImportError",
sizeof(PyImportErrorObject), 0,
(destructor)ImportError_dealloc, 0, 0, 0, 0,
(reprfunc)ImportError_repr, 0, 0, 0, 0, 0,
(reprfunc)ImportError_str, 0, 0, 0,
Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC,
PyDoc_STR("Import can't find module, or can't find name in "
"module."),
(traverseproc)ImportError_traverse,
(inquiry)ImportError_clear, 0, 0, 0, 0, ImportError_methods,
ImportError_members, 0, &_PyExc_Exception,
0, 0, 0, offsetof(PyImportErrorObject, dict),
(initproc)ImportError_init,
};
PyObject *PyExc_ImportError = (PyObject *)&_PyExc_ImportError;

/*
* ModuleNotFoundError extends ImportError
Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp