Skip to content

Commit af5c040

Browse files
committed
Try to remove imp, again
1 parent 94061f2 commit af5c040

File tree

2 files changed

+14
-16
lines changed

2 files changed

+14
-16
lines changed

pip_check_reqs/common.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
import ast
44
import fnmatch
5-
import imp
5+
# import imp
6+
import importlib
67
import logging
78
import os
89
import re
@@ -83,14 +84,14 @@ def _add_module(self, modname: str, lineno: int) -> None:
8384
progress = []
8485
modpath = last_modpath = None
8586
for modname_part in modname.split("."):
86-
try:
87-
_, modpath, _ = imp.find_module(modname_part, path)
88-
except ImportError:
89-
# the component specified at this point is not importable
90-
# (is just an attribute of the module)
91-
# *or* it's not actually installed, so we don't care either
87+
find_spec_result = importlib.util.find_spec(name=modname_part, package=path)
88+
89+
if find_spec_result is None:
90+
# The component specified at this point is not installed.
9291
break
9392

93+
modpath = find_spec_result.origin
94+
9495
# success! we found *something*
9596
progress.append(modname_part)
9697

@@ -115,7 +116,7 @@ def _add_module(self, modname: str, lineno: int) -> None:
115116

116117
modname = ".".join(progress)
117118
if modname not in self._modules:
118-
self._modules[modname] = FoundModule(modname, modpath)
119+
self._modules[modname] = FoundModule(modname=modname, filename=Path(modpath).parent)
119120
assert isinstance(self._location, str)
120121
self._modules[modname].locations.append((self._location, lineno))
121122

tests/test_common.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ def test_found_module() -> None:
4747
("import ast, pathlib", ["ast", "pathlib"]),
4848
("from pathlib import Path", ["pathlib"]),
4949
("from string import hexdigits", ["string"]),
50-
("import distutils.command.check", ["distutils.command.check"]),
50+
("import distutils.command.check", ["distutils"]),
5151
("import spam", []), # don't break because bad programmer
5252
("from .foo import bar", []), # don't break on relative imports
5353
("from . import baz", []),
@@ -94,24 +94,21 @@ def test_pyfiles_package(tmp_path: Path) -> None:
9494
]
9595

9696

97-
# Beware - using `sys` or `os` here can have weird results.
98-
# See the comment in the implementation.
99-
# We don't mind so much as we only really use this for third party packages.
10097
@pytest.mark.parametrize(
10198
["ignore_ham", "ignore_hashlib", "expect", "locs"],
10299
[
103100
(
104101
False,
105102
False,
106-
["ast", "pathlib", "hashlib"],
103+
["ast", "pathlib", "hashlib", "sys"],
107104
[
108105
("spam.py", 2),
109106
("ham.py", 2),
110107
],
111108
),
112-
(False, True, ["ast", "pathlib"], [("spam.py", 2), ("ham.py", 2)]),
113-
(True, False, ["ast"], [("spam.py", 2)]),
114-
(True, True, ["ast"], [("spam.py", 2)]),
109+
(False, True, ["ast", "pathlib", "sys"], [("spam.py", 2), ("ham.py", 2)]),
110+
(True, False, ["ast", "sys"], [("spam.py", 2)]),
111+
(True, True, ["ast", "sys"], [("spam.py", 2)]),
115112
],
116113
)
117114
def test_find_imported_modules(

0 commit comments

Comments
 (0)