Skip to content

Commit e29db82

Browse files
authored
Update exceptions to have more of their attributes pickled (#3572)
1 parent f8a0c43 commit e29db82

3 files changed

Lines changed: 31 additions & 4 deletions

File tree

‎changelog/3567.feature.rst‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Updated exceptions to save and restore more properties
2+
during the pickle/serialization process.

‎src/urllib3/exceptions.py‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,11 +31,12 @@ class PoolError(HTTPError):
3131

3232
def __init__(self, pool: ConnectionPool, message: str) -> None:
3333
self.pool = pool
34+
self._message = message
3435
super().__init__(f"{pool}: {message}")
3536

3637
def __reduce__(self) -> _TYPE_REDUCE_RESULT:
3738
# For pickling purposes.
38-
return self.__class__, (None, None)
39+
return self.__class__, (None, self._message)
3940

4041

4142
class RequestError(PoolError):
@@ -47,7 +48,7 @@ def __init__(self, pool: ConnectionPool, url: str, message: str) -> None:
4748

4849
def __reduce__(self) -> _TYPE_REDUCE_RESULT:
4950
# For pickling purposes.
50-
return self.__class__, (None, self.url, None)
51+
return self.__class__, (None, self.url, self._message)
5152

5253

5354
class SSLError(HTTPError):
@@ -100,6 +101,10 @@ def __init__(
100101

101102
super().__init__(pool, url, message)
102103

104+
def __reduce__(self) -> _TYPE_REDUCE_RESULT:
105+
# For pickling purposes.
106+
return self.__class__, (None, self.url, self.reason)
107+
103108

104109
class HostChangedError(RequestError):
105110
"""Raised when an existing pool gets a request for a foreign host."""
@@ -139,11 +144,12 @@ class NewConnectionError(ConnectTimeoutError, HTTPError):
139144

140145
def __init__(self, conn: HTTPConnection, message: str) -> None:
141146
self.conn = conn
147+
self._message = message
142148
super().__init__(f"{conn}: {message}")
143149

144150
def __reduce__(self) -> _TYPE_REDUCE_RESULT:
145151
# For pickling purposes.
146-
return self.__class__, (None, None)
152+
return self.__class__, (None, self._message)
147153

148154
@property
149155
def pool(self) -> HTTPConnection:
@@ -162,11 +168,13 @@ class NameResolutionError(NewConnectionError):
162168

163169
def __init__(self, host: str, conn: HTTPConnection, reason: socket.gaierror):
164170
message = f"Failed to resolve '{host}' ({reason})"
171+
self._host = host
172+
self._reason = reason
165173
super().__init__(conn, message)
166174

167175
def __reduce__(self) -> _TYPE_REDUCE_RESULT:
168176
# For pickling purposes.
169-
return self.__class__, (None, None, None)
177+
return self.__class__, (self._host, None, self._reason)
170178

171179

172180
class EmptyPoolError(PoolError):

‎test/test_exceptions.py‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ class TestPickle:
3030
[
3131
HTTPError(None),
3232
MaxRetryError(DUMMY_POOL, "", None),
33+
MaxRetryError(DUMMY_POOL, "", Exception("Error occured")),
3334
LocationParseError(""),
3435
ConnectTimeoutError(None),
3536
HTTPError("foo"),
@@ -40,14 +41,30 @@ class TestPickle:
4041
EmptyPoolError(HTTPConnectionPool("localhost"), ""),
4142
HostChangedError(HTTPConnectionPool("localhost"), "/", 0),
4243
ReadTimeoutError(HTTPConnectionPool("localhost"), "/", ""),
44+
ReadTimeoutError(HTTPConnectionPool("localhost"), "/", "message"),
4345
NewConnectionError(HTTPConnection("localhost"), ""),
4446
NameResolutionError("", HTTPConnection("localhost"), socket.gaierror()),
47+
NameResolutionError(
48+
"host", HTTPConnection("localhost"), socket.gaierror("error")
49+
),
4550
],
4651
)
4752
def test_exceptions(self, exception: Exception) -> None:
4853
result = pickle.loads(pickle.dumps(exception))
4954
assert isinstance(result, type(exception))
5055

56+
if hasattr(exception, "_message"):
57+
assert exception._message == result._message # type: ignore[attr-defined]
58+
assert exception._message in str(result)
59+
60+
if hasattr(exception, "_host"):
61+
# host is likely a string so directly comparable
62+
assert exception._host == result._host # type: ignore[attr-defined]
63+
64+
if hasattr(exception, "_reason"):
65+
# reason is likely an exception so do string comparison instead
66+
assert str(exception._reason) == str(result._reason) # type: ignore[attr-defined]
67+
5168

5269
class TestFormat:
5370
def test_header_parsing_errors(self) -> None:

0 commit comments

Comments
 (0)