Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: propagate exceptions from slow_validate #581

Merged
merged 3 commits into from
Nov 25, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions traits/ctraits.c
Original file line number Diff line number Diff line change
Expand Up @@ -3915,11 +3915,12 @@ validate_trait_complex ( trait_object * trait, has_traits_object * obj,
case 8: /* Perform 'slow' validate check: */
result = PyObject_CallMethod( PyTuple_GET_ITEM( type_info, 1 ),
"slow_validate", "(OOO)", obj, name, value );
if ( result != NULL )
return result;

PyErr_Clear();
break;
if (result == NULL && PyErr_ExceptionMatches(TraitError)) {
PyErr_Clear();
break;
}
return result;

case 9: /* Tuple item check: */
result = validate_trait_tuple_check(
Expand Down
30 changes: 29 additions & 1 deletion traits/tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
Property,
on_trait_change,
)
from traits.testing.optional_dependencies import numpy, requires_numpy
from traits.trait_errors import TraitError
from traits.trait_handlers import TraitType
from traits.trait_types import Bool, DelegatesTo, Either, Instance, Int, List
from traits.testing.optional_dependencies import numpy, requires_numpy

if numpy is not None:
from traits.trait_numeric import Array
Expand Down Expand Up @@ -108,6 +109,20 @@ def set_changed(self):
self.changed = True


class RaisingValidator(TraitType):
""" Trait type whose ``validate`` method raises an inappropriate exception.

Used for testing propagation of that exception.
"""
info_text = "bogus"

#: The default value for the trait:
default_value = None

def validate(self, object, name, value):
raise ZeroDivisionError("Just testing")


class TestRegression(unittest.TestCase):
def test_default_value_for_no_cache(self):
""" Make sure that CTrait.default_value_for() does not cache the
Expand Down Expand Up @@ -258,3 +273,16 @@ class StrictDummy(HasStrictTraits):
a = StrictDummy()
with self.assertRaises(TraitError):
setattr(a, u"forbidden", 53)

def test_validate_exception_propagates(self):
class A(HasTraits):
foo = RaisingValidator()

bar = Either(None, RaisingValidator())

a = A()
with self.assertRaises(ZeroDivisionError):
a.foo = "foo"

with self.assertRaises(ZeroDivisionError):
a.bar = "foo"