Skip to content

Commit

Permalink
- fixed get_info attempting to load modules from non-Python files;
Browse files Browse the repository at this point in the history
  • Loading branch information
jaltmayerpizzorno committed Aug 29, 2024
1 parent a9e0d2d commit e8aa40e
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/coverup/codeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ def _resolve_from_import(file: Path, imp: ast.ImportFrom) -> str:
def _load_module(module_name: str) -> ast.Module | None:
try:
if ((spec := importlib.util.find_spec(module_name))
and spec.origin and spec.origin not in ('frozen', 'built-in')):
and spec.origin and spec.origin.endswith('.py')):
_debug(f"Loading {spec.origin}")
return parse_file(Path(spec.origin))

except ModuleNotFoundError:
Expand Down
11 changes: 11 additions & 0 deletions tests/test_codeinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -1129,3 +1129,14 @@ def test_get_info_builtin_module(import_fixture):

tree = codeinfo.parse_file(code)
assert codeinfo.get_info(tree, 'sys.path') == None


def test_get_info_non_py(import_fixture):
"""On Python 3.10 on Linux, this module maps to a .so, not a Python file"""
tmp_path = import_fixture

code = tmp_path / "foo.py"
code.write_text("")

tree = codeinfo.parse_file(code)
assert codeinfo.get_info(tree, 'termios.tcgetattr') == None

0 comments on commit e8aa40e

Please sign in to comment.