Skip to content

Commit 2130007

Browse files
committed
Move str->Path converstion to __init__
1 parent 08d3283 commit 2130007

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

bpython/importcompletion.py

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
import sys
2727
import warnings
2828
from pathlib import Path
29-
from typing import Optional, Set, Generator, Tuple, List
29+
from typing import Optional, Set, Generator, Tuple, Sequence, Iterable, Union
3030

3131
from .line import (
3232
current_word,
@@ -49,15 +49,31 @@
4949

5050

5151
class ModuleGatherer:
52-
def __init__(self, path: Optional[Path] = None, skiplist=None) -> None:
53-
# The cached list of all known modules
52+
def __init__(
53+
self,
54+
paths: Optional[Iterable[Union[str, Path]]] = None,
55+
skiplist: Optional[Sequence[str]] = None,
56+
) -> None:
57+
"""Initialize module gatherer with all modules in `paths`, which should be a list of
58+
directory names. If `paths` is not given, `sys.path` will be used."""
59+
60+
# Cached list of all known modules
5461
self.modules: Set[str] = set()
55-
# List of (st_dev, st_ino) to compare against so that paths are not repeated
62+
# Set of (st_dev, st_ino) to compare against so that paths are not repeated
5663
self.paths: Set[Tuple[int, int]] = set()
5764
# Patterns to skip
58-
self.skiplist = skiplist if skiplist is not None else tuple()
65+
self.skiplist: Sequence[str] = (
66+
skiplist if skiplist is not None else tuple()
67+
)
5968
self.fully_loaded = False
60-
self.find_iterator = self.find_all_modules(path)
69+
70+
if paths is None:
71+
self.modules.update(sys.builtin_module_names)
72+
paths = sys.path
73+
74+
self.find_iterator = self.find_all_modules(
75+
(Path(p).resolve() if p else Path.cwd() for p in sys.path)
76+
)
6177

6278
def module_matches(self, cw: str, prefix: str = "") -> Set[str]:
6379
"""Modules names to replace cw with"""
@@ -191,8 +207,7 @@ def find_modules(self, path: Path) -> Generator[str, None, None]:
191207
except (ImportError, OSError, SyntaxError):
192208
continue
193209
except UnicodeEncodeError:
194-
# Happens with Python 3 when there is a filename in some
195-
# invalid encoding
210+
# Happens with Python 3 when there is a filename in some invalid encoding
196211
continue
197212
else:
198213
if is_package:
@@ -205,16 +220,13 @@ def find_modules(self, path: Path) -> Generator[str, None, None]:
205220
yield f"{name}.{subname}"
206221
yield name
207222

208-
def find_all_modules(self, path=None):
223+
def find_all_modules(
224+
self, paths: Iterable[Path]
225+
) -> Generator[None, None, None]:
209226
"""Return a list with all modules in `path`, which should be a list of
210227
directory names. If path is not given, sys.path will be used."""
211228

212-
if path is None:
213-
self.modules.update(sys.builtin_module_names)
214-
path = sys.path
215-
216-
for p in path:
217-
p = Path(p).resolve() if p else Path.cwd()
229+
for p in paths:
218230
for module in self.find_modules(p):
219231
self.modules.add(module)
220232
yield

bpython/test/test_importcompletion.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -162,9 +162,7 @@ def setUp(self):
162162
base_path / "Right", target_is_directory=True
163163
)
164164

165-
self.module_gatherer = ModuleGatherer(
166-
[os.path.abspath(import_test_folder)]
167-
)
165+
self.module_gatherer = ModuleGatherer((base_path.absolute(),))
168166
while self.module_gatherer.find_coroutine():
169167
pass
170168

@@ -211,7 +209,7 @@ def test_issue_847(self):
211209
(base_path / "xyzzy" / "plugh" / "bar.py").touch()
212210
(base_path / "xyzzy" / "plugh" / "foo.py").touch()
213211

214-
module_gatherer = ModuleGatherer([base_path.absolute()])
212+
module_gatherer = ModuleGatherer((base_path.absolute(),))
215213
while module_gatherer.find_coroutine():
216214
pass
217215

0 commit comments

Comments
 (0)