Skip to content

Commit 9d4c68e

Browse files
committed
Implement __cause__ for Py3.
Closes: #93
1 parent 4f3262d commit 9d4c68e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

jsonschema.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def __init__(
7676
self.path = collections.deque(path)
7777
self.schema_path = collections.deque(schema_path)
7878
self.context = list(context)
79-
self.cause = cause
79+
self.cause = self.__cause__ = cause
8080
self.validator = validator
8181
self.validator_value = validator_value
8282
self.instance = instance
@@ -118,7 +118,7 @@ class FormatError(Exception):
118118
def __init__(self, message, cause=None):
119119
super(FormatError, self).__init__(message, cause)
120120
self.message = message
121-
self.cause = cause
121+
self.cause = self.__cause__ = cause
122122

123123
def __str__(self):
124124
return self.message.encode("utf-8")
@@ -423,8 +423,8 @@ def validate_format(self, format, instance, schema):
423423
):
424424
try:
425425
self.format_checker.check(instance, format)
426-
except FormatError as e:
427-
yield ValidationError(unicode(e), cause=e.cause)
426+
except FormatError as error:
427+
yield ValidationError(error.message, cause=error.cause)
428428

429429
def validate_minLength(self, mL, instance, schema):
430430
if self.is_type(instance, "string") and len(instance) < mL:
@@ -944,7 +944,8 @@ def check(self, instance, format):
944944
cause = e
945945
if not result:
946946
raise FormatError(
947-
"%r is not a %r" % (instance, format), cause=cause)
947+
"%r is not a %r" % (instance, format), cause=cause,
948+
)
948949

949950
def conforms(self, instance, format):
950951
"""

tests.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -860,19 +860,31 @@ def test_it_can_register_checkers(self):
860860

861861
def test_it_catches_registered_errors(self):
862862
checker = FormatChecker()
863+
cause = self.fn.side_effect = ValueError()
864+
863865
checker.checks("foo", raises=ValueError)(self.fn)
864-
# Registered errors should be caught and turned into FormatErrors
865-
cause = ValueError()
866-
self.fn.side_effect = cause
866+
867867
with self.assertRaises(FormatError) as cm:
868868
checker.check("bar", "foo")
869-
# Original exception should be attached to cause attribute
869+
870870
self.assertIs(cm.exception.cause, cause)
871+
self.assertIs(cm.exception.__cause__, cause)
872+
871873
# Unregistered errors should not be caught
872874
self.fn.side_effect = AttributeError
873875
with self.assertRaises(AttributeError):
874876
checker.check("bar", "foo")
875877

878+
def test_format_error_causes_become_validation_error_causes(self):
879+
checker = FormatChecker()
880+
checker.checks("foo", raises=ValueError)(self.fn)
881+
cause = self.fn.side_effect = ValueError()
882+
validator = Draft4Validator({"format" : "foo"}, format_checker=checker)
883+
884+
with self.assertRaises(ValidationError) as cm:
885+
validator.validate("bar")
886+
887+
self.assertIs(cm.exception.__cause__, cause)
876888

877889

878890
def sorted_errors(errors):

0 commit comments

Comments
 (0)