Skip to content

Commit

Permalink
Merge pull request #129 from adamchainz/improve_module_filter
Browse files Browse the repository at this point in the history
Improve ImportableModuleFilter
  • Loading branch information
dhellmann authored Dec 4, 2021
2 parents bb2ce9c + 580888c commit b74dbb4
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
19 changes: 13 additions & 6 deletions sphinxcontrib/spelling/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import builtins
import imp
import subprocess
import sys
from xmlrpc import client as xmlrpc_client

from enchant.tokenize import Filter, get_tokenizer, tokenize, unit_tokenize
Expand Down Expand Up @@ -177,18 +178,24 @@ class ImportableModuleFilter(Filter):
"""
def __init__(self, tokenizer):
super().__init__(tokenizer)
self.found_modules = set()
self.sought_modules = set()
self.found_modules = set(sys.builtin_module_names)
self.sought_modules = self.found_modules.copy()

def _skip(self, word):
valid_module_name = all(n.isidentifier() for n in word.split('.'))
if not valid_module_name:
return False

if word not in self.sought_modules:
self.sought_modules.add(word)
try:
imp.find_module(word)
module_file, _, _ = imp.find_module(word)
except ImportError:
return False
self.found_modules.add(word)
return True
pass
else:
if module_file is not None:
module_file.close()
self.found_modules.add(word)
return word in self.found_modules


Expand Down
1 change: 1 addition & 0 deletions tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ def test_contributors(name):
('os', True),
('os.name', False),
('__main__', False),
("don't", False),
]
)
def test_importable_module_skip(word, expected):
Expand Down

0 comments on commit b74dbb4

Please sign in to comment.