Skip to content

Commit cb38aa4

Browse files
check type in _find_incompatible_extension_module and add try-except
1 parent adf2c47 commit cb38aa4

File tree

1 file changed

+20
-17
lines changed

1 file changed

+20
-17
lines changed

Lib/traceback.py

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,26 +1907,29 @@ def _find_incompatible_extension_module(module_name):
19071907
import importlib.machinery
19081908
import importlib.resources.readers
19091909

1910-
if not module_name or not importlib.machinery.EXTENSION_SUFFIXES:
1910+
if not isinstance(module_name, str) or not importlib.machinery.EXTENSION_SUFFIXES:
19111911
return
19121912

19131913
# We assume the last extension is untagged (eg. .so, .pyd)!
19141914
# tests.test_traceback.MiscTest.test_find_incompatible_extension_modules
19151915
# tests that assumption.
1916-
untagged_suffix = importlib.machinery.EXTENSION_SUFFIXES[-1]
1917-
# On Windows the debug tag is part of the module file stem, instead of the
1918-
# extension (eg. foo_d.pyd), so let's remove it and just look for .pyd.
1919-
if os.name == 'nt':
1920-
untagged_suffix = untagged_suffix.removeprefix('_d')
1921-
1922-
parent, _, child = module_name.rpartition('.')
1923-
if parent:
1924-
traversable = importlib.resources.files(parent)
1925-
else:
1926-
traversable = importlib.resources.readers.MultiplexedPath(
1927-
*map(pathlib.Path, filter(os.path.isdir, sys.path))
1928-
)
1916+
try:
1917+
untagged_suffix = importlib.machinery.EXTENSION_SUFFIXES[-1]
1918+
# On Windows the debug tag is part of the module file stem, instead of the
1919+
# extension (eg. foo_d.pyd), so let's remove it and just look for .pyd.
1920+
if os.name == 'nt':
1921+
untagged_suffix = untagged_suffix.removeprefix('_d')
1922+
1923+
parent, _, child = module_name.rpartition('.')
1924+
if parent:
1925+
traversable = importlib.resources.files(parent)
1926+
else:
1927+
traversable = importlib.resources.readers.MultiplexedPath(
1928+
*map(pathlib.Path, filter(os.path.isdir, sys.path))
1929+
)
19291930

1930-
for entry in traversable.iterdir():
1931-
if entry.name.startswith(child + '.') and entry.name.endswith(untagged_suffix):
1932-
return entry.name
1931+
for entry in traversable.iterdir():
1932+
if entry.name.startswith(child + '.') and entry.name.endswith(untagged_suffix):
1933+
return entry.name
1934+
except Exception:
1935+
return

0 commit comments

Comments
 (0)