mirror of
https://github.com/PyCQA/flake8.git
synced 2026-04-12 15:44:17 +00:00
Ensure exceptions are pickleable
This commit is contained in:
parent
9b770f590e
commit
e8de066f94
3 changed files with 74 additions and 27 deletions
|
|
@ -4,14 +4,10 @@
|
||||||
class Flake8Exception(Exception):
|
class Flake8Exception(Exception):
|
||||||
"""Plain Flake8 exception."""
|
"""Plain Flake8 exception."""
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class EarlyQuit(Flake8Exception):
|
class EarlyQuit(Flake8Exception):
|
||||||
"""Except raised when encountering a KeyboardInterrupt."""
|
"""Except raised when encountering a KeyboardInterrupt."""
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class ExecutionError(Flake8Exception):
|
class ExecutionError(Flake8Exception):
|
||||||
"""Exception raised during execution of Flake8."""
|
"""Exception raised during execution of Flake8."""
|
||||||
|
|
@ -22,15 +18,15 @@ class FailedToLoadPlugin(Flake8Exception):
|
||||||
|
|
||||||
FORMAT = 'Flake8 failed to load plugin "%(name)s" due to %(exc)s.'
|
FORMAT = 'Flake8 failed to load plugin "%(name)s" due to %(exc)s.'
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, plugin, exception):
|
||||||
"""Initialize our FailedToLoadPlugin exception."""
|
"""Initialize our FailedToLoadPlugin exception."""
|
||||||
self.plugin = kwargs.pop("plugin")
|
self.plugin = plugin
|
||||||
self.ep_name = self.plugin.name
|
self.ep_name = self.plugin.name
|
||||||
self.original_exception = kwargs.pop("exception")
|
self.original_exception = exception
|
||||||
super(FailedToLoadPlugin, self).__init__(*args, **kwargs)
|
super(FailedToLoadPlugin, self).__init__(plugin, exception)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Return a nice string for our exception."""
|
"""Format our exception message."""
|
||||||
return self.FORMAT % {
|
return self.FORMAT % {
|
||||||
"name": self.ep_name,
|
"name": self.ep_name,
|
||||||
"exc": self.original_exception,
|
"exc": self.original_exception,
|
||||||
|
|
@ -40,9 +36,8 @@ class FailedToLoadPlugin(Flake8Exception):
|
||||||
class InvalidSyntax(Flake8Exception):
|
class InvalidSyntax(Flake8Exception):
|
||||||
"""Exception raised when tokenizing a file fails."""
|
"""Exception raised when tokenizing a file fails."""
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, exception): # type: (Exception) -> None
|
||||||
"""Initialize our InvalidSyntax exception."""
|
"""Initialize our InvalidSyntax exception."""
|
||||||
exception = kwargs.pop("exception", None)
|
|
||||||
self.original_exception = exception
|
self.original_exception = exception
|
||||||
self.error_message = "{0}: {1}".format(
|
self.error_message = "{0}: {1}".format(
|
||||||
exception.__class__.__name__, exception.args[0]
|
exception.__class__.__name__, exception.args[0]
|
||||||
|
|
@ -50,9 +45,11 @@ class InvalidSyntax(Flake8Exception):
|
||||||
self.error_code = "E902"
|
self.error_code = "E902"
|
||||||
self.line_number = 1
|
self.line_number = 1
|
||||||
self.column_number = 0
|
self.column_number = 0
|
||||||
super(InvalidSyntax, self).__init__(
|
super(InvalidSyntax, self).__init__(exception)
|
||||||
self.error_message, *args, **kwargs
|
|
||||||
)
|
def __str__(self):
|
||||||
|
"""Format our exception message."""
|
||||||
|
return self.error_message
|
||||||
|
|
||||||
|
|
||||||
class PluginRequestedUnknownParameters(Flake8Exception):
|
class PluginRequestedUnknownParameters(Flake8Exception):
|
||||||
|
|
@ -60,12 +57,12 @@ class PluginRequestedUnknownParameters(Flake8Exception):
|
||||||
|
|
||||||
FORMAT = '"%(name)s" requested unknown parameters causing %(exc)s'
|
FORMAT = '"%(name)s" requested unknown parameters causing %(exc)s'
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, plugin, exception):
|
||||||
"""Pop certain keyword arguments for initialization."""
|
"""Pop certain keyword arguments for initialization."""
|
||||||
self.original_exception = kwargs.pop("exception")
|
self.plugin = plugin
|
||||||
self.plugin = kwargs.pop("plugin")
|
self.original_exception = exception
|
||||||
super(PluginRequestedUnknownParameters, self).__init__(
|
super(PluginRequestedUnknownParameters, self).__init__(
|
||||||
*args, **kwargs
|
plugin, exception
|
||||||
)
|
)
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
|
|
@ -81,13 +78,11 @@ class PluginExecutionFailed(Flake8Exception):
|
||||||
|
|
||||||
FORMAT = '"%(name)s" failed during execution due to "%(exc)s"'
|
FORMAT = '"%(name)s" failed during execution due to "%(exc)s"'
|
||||||
|
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, plugin, exception):
|
||||||
"""Utilize keyword arguments for message generation."""
|
"""Utilize keyword arguments for message generation."""
|
||||||
self.original_exception = kwargs.pop("exception")
|
self.plugin = plugin
|
||||||
self.plugin = kwargs.pop("plugin")
|
self.original_exception = exception
|
||||||
super(PluginExecutionFailed, self).__init__(
|
super(PluginExecutionFailed, self).__init__(plugin, exception)
|
||||||
str(self), *args, **kwargs
|
|
||||||
)
|
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
"""Format our exception message."""
|
"""Format our exception message."""
|
||||||
|
|
@ -100,8 +95,6 @@ class PluginExecutionFailed(Flake8Exception):
|
||||||
class HookInstallationError(Flake8Exception):
|
class HookInstallationError(Flake8Exception):
|
||||||
"""Parent exception for all hooks errors."""
|
"""Parent exception for all hooks errors."""
|
||||||
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
class GitHookAlreadyExists(HookInstallationError):
|
class GitHookAlreadyExists(HookInstallationError):
|
||||||
"""Exception raised when the git pre-commit hook file already exists."""
|
"""Exception raised when the git pre-commit hook file already exists."""
|
||||||
|
|
|
||||||
|
|
@ -118,7 +118,7 @@ class FileProcessor(object):
|
||||||
tokenize.generate_tokens(lambda: next(line_iter))
|
tokenize.generate_tokens(lambda: next(line_iter))
|
||||||
)
|
)
|
||||||
except tokenize.TokenError as exc:
|
except tokenize.TokenError as exc:
|
||||||
raise exceptions.InvalidSyntax(exc.message, exception=exc)
|
raise exceptions.InvalidSyntax(exception=exc)
|
||||||
|
|
||||||
return self._file_tokens
|
return self._file_tokens
|
||||||
|
|
||||||
|
|
|
||||||
54
tests/unit/test_exceptions.py
Normal file
54
tests/unit/test_exceptions.py
Normal file
|
|
@ -0,0 +1,54 @@
|
||||||
|
"""Tests for the flake8.exceptions module."""
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
import entrypoints
|
||||||
|
|
||||||
|
from flake8 import exceptions
|
||||||
|
from flake8.plugins import manager as plugins_manager
|
||||||
|
|
||||||
|
|
||||||
|
class _ExceptionTest:
|
||||||
|
def test_pickleable(self):
|
||||||
|
"""Test that the exception is round-trip pickleable."""
|
||||||
|
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
|
||||||
|
new_err = pickle.loads(pickle.dumps(self.err, protocol=proto))
|
||||||
|
assert str(self.err) == str(new_err)
|
||||||
|
orig_e = self.err.original_exception
|
||||||
|
new_e = new_err.original_exception
|
||||||
|
assert (type(orig_e), orig_e.args) == (type(new_e), new_e.args)
|
||||||
|
|
||||||
|
|
||||||
|
class TestFailedToLoadPlugin(_ExceptionTest):
|
||||||
|
"""Tests for the FailedToLoadPlugin exception."""
|
||||||
|
|
||||||
|
err = exceptions.FailedToLoadPlugin(
|
||||||
|
plugin=plugins_manager.Plugin(
|
||||||
|
'plugin_name',
|
||||||
|
entrypoints.EntryPoint('plugin_name', 'os.path', None),
|
||||||
|
),
|
||||||
|
exception=ValueError('boom!'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestInvalidSyntax(_ExceptionTest):
|
||||||
|
"""Tests for the InvalidSyntax exception."""
|
||||||
|
|
||||||
|
err = exceptions.InvalidSyntax(exception=ValueError('Unexpected token: $'))
|
||||||
|
|
||||||
|
|
||||||
|
class TestPluginRequestedUnknownParameters(_ExceptionTest):
|
||||||
|
"""Tests for the PluginRequestedUnknownParameters exception."""
|
||||||
|
|
||||||
|
err = exceptions.PluginRequestedUnknownParameters(
|
||||||
|
plugin={'plugin_name': 'plugin_name'},
|
||||||
|
exception=ValueError('boom!'),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
class TestPluginExecutionFailed(_ExceptionTest):
|
||||||
|
"""Tests for the PluginExecutionFailed exception."""
|
||||||
|
|
||||||
|
err = exceptions.PluginExecutionFailed(
|
||||||
|
plugin={'plugin_name': 'plugin_name'},
|
||||||
|
exception=ValueError('boom!'),
|
||||||
|
)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue