Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
14 changes: 14 additions & 0 deletions Lib/test/test_traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -5325,6 +5325,20 @@ def test_windows_only_module_error(self):
else:
self.fail("ModuleNotFoundError was not raised")

def test_module_not_found_without_search_paths(self):
importlib.import_module('importlib.resources.readers')
error = ModuleNotFoundError("No module named 'missing_module'",
name='missing_module')
with os_helper.temp_dir() as tmp:
for paths in ([], [os.path.join(tmp, 'missing')]):
with self.subTest(paths=paths):
with support.swap_attr(sys, 'path', paths):
self.assertEqual(
traceback.format_exception_only(error),
["ModuleNotFoundError: No module named "
"'missing_module'\n"],
)

@unittest.skipIf(not importlib.machinery.EXTENSION_SUFFIXES, 'Platform does not support extension modules')
def test_find_incompatible_extension_modules(self):
"""_find_incompatible_extension_modules assumes the last extension in
Expand Down
7 changes: 4 additions & 3 deletions Lib/traceback.py
Original file line number Diff line number Diff line change
Expand Up @@ -2106,9 +2106,10 @@ def _find_incompatible_extension_module(module_name):
if parent:
traversable = importlib.resources.files(parent)
else:
traversable = importlib.resources.readers.MultiplexedPath(
*map(pathlib.Path, filter(os.path.isdir, sys.path))
)
paths = list(map(pathlib.Path, filter(os.path.isdir, sys.path)))
if not paths:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Can this check run before the importlib.resources import? If a fresh process clears sys.path, that import can fail before we get here.

return
traversable = importlib.resources.readers.MultiplexedPath(*paths)

for entry in traversable.iterdir():
if entry.name.startswith(child + '.') and entry.name.endswith(untagged_suffix):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix formatting of :exc:`ModuleNotFoundError` when :data:`sys.path` contains
no existing directories, including when displaying errors in the REPL.
Loading