Skip to content

Commit

Permalink
Move checker initialization and closing into context manager
Browse files Browse the repository at this point in the history
  • Loading branch information
janneronkko committed Jul 23, 2019
1 parent e62ee97 commit ad16357
Showing 1 changed file with 33 additions and 30 deletions.
63 changes: 33 additions & 30 deletions pylint/lint.py
Original file line number Diff line number Diff line change
Expand Up @@ -1102,36 +1102,9 @@ def _check_files(self, get_ast, file_descrs):
- filepath: path of the file
- modname: module name
"""
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)

check_astroid_module = functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)

for name, filepath, modname in file_descrs:
self._check_file(get_ast, check_astroid_module, name, filepath, modname)

# notify global end
self.stats["statement"] = walker.nbstatements
for checker in reversed(_checkers):
checker.close()
with self._astroid_module_checker() as check_astroid_module:
for name, filepath, modname in file_descrs:
self._check_file(get_ast, check_astroid_module, name, filepath, modname)

def _check_file(self, get_ast, check_astroid_module, name, filepath, modname):
self.set_current_module(name, filepath)
Expand Down Expand Up @@ -1201,6 +1174,36 @@ def set_current_module(self, modname, filepath=None):
for msg_cat in MSG_TYPES.values():
self.stats["by_module"][modname][msg_cat] = 0

@contextlib.contextmanager
def _astroid_module_checker(self):
walker = ASTWalker(self)
_checkers = self.prepare_checkers()
tokencheckers = [
c
for c in _checkers
if interfaces.implements(c, interfaces.ITokenChecker) and c is not self
]
rawcheckers = [
c for c in _checkers if interfaces.implements(c, interfaces.IRawChecker)
]
# notify global begin
for checker in _checkers:
checker.open()
if interfaces.implements(checker, interfaces.IAstroidChecker):
walker.add_checker(checker)

yield functools.partial(
self.check_astroid_module,
walker=walker,
tokencheckers=tokencheckers,
rawcheckers=rawcheckers,
)

# notify global end
self.stats["statement"] = walker.nbstatements
for checker in reversed(_checkers):
checker.close()

def get_ast(self, filepath, modname):
"""return an ast(roid) representation for a module"""
try:
Expand Down

0 comments on commit ad16357

Please sign in to comment.