Skip to content

Commit 7e80f7b

Browse files
committed
Make exceptions picklable on Python 2.X
1 parent 4baff21 commit 7e80f7b

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

jsonschema/exceptions.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ def __init__(
4141
for error in context:
4242
error.parent = self
4343

44+
# Initialize exception args or otherwise __reduce__ will return empty
45+
# args tuple on Py2.7 and unpickling will fail because init requires at
46+
# least one argument.
47+
self.args = (message,)
48+
4449
def __repr__(self):
4550
return "<%s: %r>" % (self.__class__.__name__, self.message)
4651

jsonschema/tests/test_exceptions.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import pickle
12
import textwrap
23

34
from jsonschema import Draft4Validator, exceptions
@@ -380,3 +381,20 @@ def test_str_works_with_instances_having_overriden_eq_operator(self):
380381
)
381382
str(error)
382383
self.assertFalse(instance.__eq__.called)
384+
385+
386+
class TestErrorsArePicklable(unittest.TestCase):
387+
def test_validation_error_is_picklable(self):
388+
err = exceptions.ValidationError(
389+
"a message",
390+
validator="foo",
391+
instance="bar",
392+
validator_value="some",
393+
schema="schema",
394+
)
395+
unpickled = pickle.loads(pickle.dumps(err))
396+
self.assertEqual(unpickled.message, "a message")
397+
self.assertEqual(unpickled.validator, "foo")
398+
self.assertEqual(unpickled.instance, "bar")
399+
self.assertEqual(unpickled.validator_value, "some")
400+
self.assertEqual(unpickled.schema, "schema")

0 commit comments

Comments
 (0)