Skip to content
Open
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
18 changes: 16 additions & 2 deletions atom/src/validatebehavior.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -881,23 +881,37 @@ range_handler( Member* member, CAtom* atom, PyObject* oldvalue, PyObject* newval
PyObject* high = PyTuple_GET_ITEM( member->validate_context, 1 );
if( low != Py_None )
{
if( PyObject_RichCompareBool( low , newvalue, Py_GT ) )
switch (PyObject_RichCompareBool( low , newvalue, Py_GT ))
{
case 0:
break;
case 1:
return PyErr_Format(
PyExc_ValueError,
"range value for '%s' of '%s' too small",
PyUnicode_AsUTF8( member->name ),
Py_TYPE( pyobject_cast( atom ) )->tp_name
);
default:
return 0;
}
}
if( high != Py_None )
{
if( PyObject_RichCompareBool( high , newvalue, Py_LT ) )
switch( PyObject_RichCompareBool( high , newvalue, Py_LT ) )
{
case 0:
break;
case 1:
return PyErr_Format(
PyExc_ValueError,
"range value for '%s' of '%s' too large",
PyUnicode_AsUTF8( member->name ),
Py_TYPE( pyobject_cast( atom ) )->tp_name
);
default:
return 0;
}
}
return cppy::incref( newvalue );
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_default_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ class SetTest(Atom):
5,
DefaultValue.MemberMethod_Object,
),
(ForwardTyped(lambda: int, factory=lambda: (5)), 5, DefaultValue.CallObject),
(ForwardTyped(lambda: int, factory=lambda: 5), 5, DefaultValue.CallObject),
(Instance(int, ("101",), {"base": 2}), 5, DefaultValue.CallObject),
(Instance(int, factory=lambda: 5), 5, DefaultValue.CallObject),
(
Expand Down
25 changes: 25 additions & 0 deletions tests/test_validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -434,3 +434,28 @@ def test_custom_validate(mode, factory):
with pytest.raises(TypeError) as excinfo:
type(v).v.set_validate_mode(getattr(Validate, mode), 1)
assert "str" in excinfo.exconly()


def test_validate_range_error():
class CustomInt(int):
def __gt__(self, other):
raise TypeError("Cannot be compared")

def __lt__(self, other):
raise TypeError("Cannot be compared")

class Obj(Atom):
x = Range(low=0)
y = Range(high=10)

o = Obj()
o.x = 1
o.y = 9
with pytest.raises(ValueError):
o.x = -1
with pytest.raises(ValueError):
o.y = 11
with pytest.raises(TypeError):
o.x = CustomInt(-1)
with pytest.raises(TypeError):
o.y = CustomInt(11)
Loading