Skip to content

Commit

Permalink
[3.6] Make exceptions pickleable. (#4095) (#4098)
Browse files Browse the repository at this point in the history
Also change the repr of some exceptions..
(cherry picked from commit dd9db9a)

Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
  • Loading branch information
asvetlov and serhiy-storchaka authored Sep 25, 2019
1 parent e1689c8 commit 83c839f
Show file tree
Hide file tree
Showing 8 changed files with 456 additions and 37 deletions.
1 change: 1 addition & 0 deletions CHANGES/4077.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Made exceptions pickleable. Also changed the repr of some exceptions.
28 changes: 25 additions & 3 deletions aiohttp/client_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,21 @@ def __init__(self, request_info: RequestInfo,
self.message = message
self.headers = headers
self.history = history
self.args = (request_info, history)

super().__init__("%s, message='%s', url='%s" %
(self.status, message, request_info.real_url))
def __str__(self) -> str:
return ("%s, message=%r, url=%r" %
(self.status, self.message, self.request_info.real_url))

def __repr__(self) -> str:
args = "%r, %r" % (self.request_info, self.history)
if self.status != 0:
args += ", status=%r" % (self.status,)
if self.message != '':
args += ", message=%r" % (self.message,)
if self.headers is not None:
args += ", headers=%r" % (self.headers,)
return "%s(%s)" % (type(self).__name__, args)

@property
def code(self) -> int:
Expand Down Expand Up @@ -131,6 +143,7 @@ def __init__(self, connection_key: ConnectionKey,
self._conn_key = connection_key
self._os_error = os_error
super().__init__(os_error.errno, os_error.strerror)
self.args = (connection_key, os_error)

@property
def os_error(self) -> OSError:
Expand All @@ -152,6 +165,9 @@ def __str__(self) -> str:
return ('Cannot connect to host {0.host}:{0.port} ssl:{0.ssl} [{1}]'
.format(self, self.strerror))

# OSError.__reduce__ does too much black magick
__reduce__ = BaseException.__reduce__


class ClientProxyConnectionError(ClientConnectorError):
"""Proxy connection error.
Expand All @@ -170,6 +186,10 @@ class ServerDisconnectedError(ServerConnectionError):

def __init__(self, message: Optional[str]=None) -> None:
self.message = message
if message is None:
self.args = ()
else:
self.args = (message,)


class ServerTimeoutError(ServerConnectionError, asyncio.TimeoutError):
Expand All @@ -185,9 +205,10 @@ def __init__(self, expected: bytes, got: bytes,
self.got = got
self.host = host
self.port = port
self.args = (expected, got, host, port)

def __repr__(self) -> str:
return '<{} expected={} got={} host={} port={}>'.format(
return '<{} expected={!r} got={!r} host={!r} port={!r}>'.format(
self.__class__.__name__, self.expected, self.got,
self.host, self.port)

Expand Down Expand Up @@ -246,6 +267,7 @@ def __init__(self, connection_key:
ConnectionKey, certificate_error: Exception) -> None:
self._conn_key = connection_key
self._certificate_error = certificate_error
self.args = (connection_key, certificate_error)

@property
def certificate_error(self) -> Exception:
Expand Down
16 changes: 13 additions & 3 deletions aiohttp/http_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@ def __init__(self, *,
self.headers = headers
self.message = message

super().__init__("%s, message='%s'" % (self.code, message))
def __str__(self) -> str:
return "%s, message=%r" % (self.code, self.message)

def __repr__(self) -> str:
return "<%s: %s>" % (self.__class__.__name__, self)


class BadHttpMessage(HttpProcessingError):
Expand All @@ -42,6 +46,7 @@ class BadHttpMessage(HttpProcessingError):
def __init__(self, message: str, *,
headers: Optional[_CIMultiDict]=None) -> None:
super().__init__(message=message, headers=headers)
self.args = (message,)


class HttpBadRequest(BadHttpMessage):
Expand Down Expand Up @@ -74,6 +79,7 @@ def __init__(self, line: str,
super().__init__(
"Got more than %s bytes (%s) when reading %s." % (
limit, actual_size, line))
self.args = (line, limit, actual_size)


class InvalidHeader(BadHttpMessage):
Expand All @@ -83,16 +89,20 @@ def __init__(self, hdr: Union[bytes, str]) -> None:
hdr = hdr.decode('utf-8', 'surrogateescape')
super().__init__('Invalid HTTP Header: {}'.format(hdr))
self.hdr = hdr
self.args = (hdr,)


class BadStatusLine(BadHttpMessage):

def __init__(self, line: str='') -> None:
if not line:
if not isinstance(line, str):
line = repr(line)
self.args = line,
self.args = (line,)
self.line = line

__str__ = Exception.__str__
__repr__ = Exception.__repr__


class InvalidURLError(BadHttpMessage):
pass
5 changes: 4 additions & 1 deletion aiohttp/http_websocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ class WebSocketError(Exception):

def __init__(self, code: int, message: str) -> None:
self.code = code
super().__init__(message)
super().__init__(code, message)

def __str__(self) -> str:
return self.args[1]


class WSHandshakeError(Exception):
Expand Down
Loading

0 comments on commit 83c839f

Please sign in to comment.