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#136770

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
Yoav11 wants to merge28 commits intopython:main
base:main
Choose a base branch
Loading
fromYoav11:importerror-repr
Open
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
28 commits
Select commitHold shift + click to select a range
cb764d9
bpo-29999: repr() of ImportError now contains attributes name and path.
serhiy-storchakaApr 5, 2017
db41c7d
Use GitHub Actions version of Bedevere
arhadthedevFeb 12, 2023
1caeb91
Add the news entry back
arhadthedevFeb 13, 2023
7f29270
Remove an accidentally included untracked file
arhadthedevFeb 13, 2023
c80e056
first commit
ynir3Jul 19, 2025
126be20
add test_ModuleNotFoundError_repr_with_failed_import
ynir3Jul 19, 2025
4a97f5c
add test_ModuleNotFoundError_repr_with_failed_import
ynir3Jul 19, 2025
852da8c
add blurb
ynir3Jul 19, 2025
6ab7362
add noqa:F401
ynir3Jul 19, 2025
12c2293
remove whitespace
ynir3Jul 19, 2025
a53da37
fix trailing-whitespace
ynir3Jul 19, 2025
5b6d626
use ComplexExtendsException
ynir3Jul 19, 2025
951e0a1
return to non-macro style
ynir3Jul 19, 2025
8fbfd18
use modern field initializers
ynir3Jul 19, 2025
458bcd0
remove un-necessary casts
ynir3Jul 19, 2025
fa2f884
use PyUnicodeWriter
ynir3Jul 19, 2025
35a6a29
fix trailing whitespace
ynir3Jul 19, 2025
6e416c6
reduce nesting
ynir3Jul 19, 2025
aa75f6a
use PyUnicdodeWriter_Format and if statement braces
ynir3Jul 19, 2025
8b5e4ed
indentation
ynir3Jul 19, 2025
28545e5
move decref
ynir3Jul 19, 2025
4f63193
check baseException_repr failure, add 3.15rst
ynir3Jul 20, 2025
62a479b
Merge branch 'main' into importerror-repr
Yoav11Jul 20, 2025
25f3d42
fix chr c input
ynir3Jul 20, 2025
a45ed75
move tests, edit rst wording
ynir3Jul 20, 2025
69e7305
Merge branch 'main' into importerror-repr
Yoav11Jul 20, 2025
9c09cd0
edit blurb
ynir3Jul 20, 2025
ca0e2a4
edit blurb
ynir3Jul 20, 2025
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@ynir3
serhiy-storchaka authored andynir3 committedJul 19, 2025
commitcb764d9dd9d6ad1e10ad89150c3634e1b567e1f9
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@@ -2595,6 +2595,36 @@ def after_with():
with ExitFails():
1/0
self.lineno_after_raise(after_with, 1, 1)
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')")


if __name__ == '__main__':
unittest.main()
47 changes: 41 additions & 6 deletionsObjects/exceptions.c
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -1864,6 +1864,30 @@ ImportError_reduce(PyObject *self, PyObject *Py_UNUSED(ignored))
return res;
}

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", _Py_T_OBJECT, offsetof(PyImportErrorObject, msg), 0,
PyDoc_STR("exception message")},
Expand All@@ -1881,12 +1905,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