Skip to content

Commit

Permalink
Handle AttributeErrors during parameter aggregation
Browse files Browse the repository at this point in the history
Plugins do not have their parameters checked in advance, so when we
try to aggregate parameters for a plugin, there's a chance it may
request an attribute of the FileProcessor that simply does not exist.

We catch this and re-raise it but we should also capture it in the
checker manager and handle it appropriately there as well.
  • Loading branch information
sigmavirus24 committed Jul 27, 2016
1 parent 846bfaf commit 47e3e65
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/flake8/checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@

def _run_checks_from_queue(process_queue, results_queue, statistics_queue):
LOG.info('Running checks in parallel')
for checker in iter(process_queue.get, 'DONE'):
LOG.info('Checking "%s"', checker.filename)
checker.run_checks(results_queue, statistics_queue)
results_queue.put('DONE')
try:
for checker in iter(process_queue.get, 'DONE'):
LOG.info('Checking "%s"', checker.filename)
checker.run_checks(results_queue, statistics_queue)
except exceptions.PluginRequestedUnknownParameters as exc:
print(str(exc))
except Exception as exc:
LOG.error('Unhandled exception occurred')
raise
finally:
results_queue.put('DONE')


class Manager(object):
Expand Down Expand Up @@ -356,6 +363,8 @@ def run(self):
except KeyboardInterrupt:
LOG.warning('Flake8 was interrupted by the user')
raise exceptions.EarlyQuit('Early quit while running checks')
finally:
self._force_cleanup()

def start(self, paths=None):
"""Start checking files.
Expand Down Expand Up @@ -447,7 +456,14 @@ def report(self, error_code, line_number, column, text, line=None):
def run_check(self, plugin, **arguments):
"""Run the check in a single plugin."""
LOG.debug('Running %r with %r', plugin, arguments)
self.processor.keyword_arguments_for(plugin.parameters, arguments)
try:
self.processor.keyword_arguments_for(plugin.parameters, arguments)
except AttributeError as ae:
LOG.error('Plugin requested unknown parameters.')
raise exceptions.PluginRequestedUnknownParameters(
plugin=plugin,
exception=ae,
)
return plugin.execute(**arguments)

def run_ast_checks(self):
Expand Down
20 changes: 20 additions & 0 deletions src/flake8/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,26 @@ def __init__(self, *args, **kwargs):
super(InvalidSyntax, self).__init__(*args, **kwargs)


class PluginRequestedUnknownParameters(Flake8Exception):
"""The plugin requested unknown parameters."""

FORMAT = '"%(name)s" requested unknown parameters causing %(exc)s'

def __init__(self, *args, **kwargs):
"""Pop certain keyword arguments for initialization."""
self.original_exception = kwargs.pop('exception')
self.plugin = kwargs.pop('plugin')
super(PluginRequestedUnknownParameters, self).__init__(
*args,
**kwargs
)

def __str__(self):
"""Format our exception message."""
return self.FORMAT % {'name': self.plugin.plugin_name,
'exc': self.original_exception}


class HookInstallationError(Flake8Exception):
"""Parent exception for all hooks errors."""

Expand Down

0 comments on commit 47e3e65

Please sign in to comment.