Skip to content

Commit 126be20

Browse files
committed
add test_ModuleNotFoundError_repr_with_failed_import
1 parent c80e056 commit 126be20

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

Lib/test/test_exceptions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,6 +2595,7 @@ def after_with():
25952595
with ExitFails():
25962596
1/0
25972597
self.lineno_after_raise(after_with, 1, 1)
2598+
25982599
def test_repr(self):
25992600
exc = ImportError()
26002601
self.assertEqual(repr(exc), "ImportError()")
@@ -2624,6 +2625,22 @@ def test_repr(self):
26242625
exc = ImportError('test', name='somename', path='somepath')
26252626
self.assertEqual(repr(exc),
26262627
"ImportError('test', name='somename', path='somepath')")
2628+
2629+
exc = ModuleNotFoundError('test', name='somename', path='somepath')
2630+
self.assertEqual(repr(exc),
2631+
"ModuleNotFoundError('test', name='somename', path='somepath')")
2632+
2633+
def test_importerror_name_and_path(self):
2634+
try:
2635+
import does_not_exist # noqa: F401
2636+
except ModuleNotFoundError as e:
2637+
self.assertEqual(e.name, "does_not_exist")
2638+
self.assertIsNone(e.path)
2639+
2640+
self.assertEqual(repr(e),
2641+
"ModuleNotFoundError(\"No module named 'does_not_exist'\", name='does_not_exist')")
2642+
else:
2643+
self.fail("Expected ModuleNotFoundError was not raised")
26272644

26282645

26292646
if __name__ == '__main__':

0 commit comments

Comments
 (0)