Skip to content

Commit 382fc2c

Browse files
authored
Merge pull request #6629 from Tarty/fix-6628-jsondecode-error-not-deserializable
Fix #6628 - JSONDecodeError are not deserializable
2 parents 7a13c04 + 3ff3ff2 commit 382fc2c

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/requests/exceptions.py

+10
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,16 @@ def __init__(self, *args, **kwargs):
4141
CompatJSONDecodeError.__init__(self, *args)
4242
InvalidJSONError.__init__(self, *self.args, **kwargs)
4343

44+
def __reduce__(self):
45+
"""
46+
The __reduce__ method called when pickling the object must
47+
be the one from the JSONDecodeError (be it json/simplejson)
48+
as it expects all the arguments for instantiation, not just
49+
one like the IOError, and the MRO would by default call the
50+
__reduce__ method from the IOError due to the inheritance order.
51+
"""
52+
return CompatJSONDecodeError.__reduce__(self)
53+
4454

4555
class HTTPError(RequestException):
4656
"""An HTTP error occurred."""

tests/test_requests.py

+10
Original file line numberDiff line numberDiff line change
@@ -2810,3 +2810,13 @@ def test_status_code_425(self):
28102810
assert r4 == 425
28112811
assert r5 == 425
28122812
assert r6 == 425
2813+
2814+
2815+
def test_json_decode_errors_are_serializable_deserializable():
2816+
json_decode_error = requests.exceptions.JSONDecodeError(
2817+
"Extra data",
2818+
'{"responseCode":["706"],"data":null}{"responseCode":["706"],"data":null}',
2819+
36,
2820+
)
2821+
deserialized_error = pickle.loads(pickle.dumps(json_decode_error))
2822+
assert repr(json_decode_error) == repr(deserialized_error)

0 commit comments

Comments
 (0)