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 fromall commits
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
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@@ -1954,6 +1954,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
def test_copy_pickle(self):
for kwargs in (dict(),
dict(name='somename'),
Expand Down
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
:meth:`repr` of :class:`ImportError` now contains attributes name and path.
Patch by Serhiy Storchaka.
47 changes: 41 additions & 6 deletionsObjects/exceptions.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1592,6 +1592,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 PyObject *
ImportError_getstate(PyImportErrorObject *self)
{
Expand DownExpand Up@@ -1657,12 +1681,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