From b1fcd5ea8badb52081987148e328d037caf3e122 Mon Sep 17 00:00:00 2001 From: Poruri Sai Rahul Date: Tue, 10 May 2022 09:34:31 +0100 Subject: [PATCH] Return "PyErr_Format" calls in "traits/ctraits.c" (#1640) Also changes the return type of `trait_new` to eliminate the need for the `(newfunc)` cast. (cherry picked from commit 576b6b04a828080f034d191e2cf9e6d0ed8498e1) --- traits/ctraits.c | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/traits/ctraits.c b/traits/ctraits.c index 0bc42ce02..2ee6d40a7 100644 --- a/traits/ctraits.c +++ b/traits/ctraits.c @@ -1885,13 +1885,11 @@ getattr_generic(trait_object *trait, has_traits_object *obj, PyObject *name) static PyObject * getattr_event(trait_object *trait, has_traits_object *obj, PyObject *name) { - PyErr_Format( + return PyErr_Format( PyExc_AttributeError, "The %.400U" " trait of a %.50s instance is an 'event', which is write only.", name, Py_TYPE(obj)->tp_name); - - return NULL; } /*----------------------------------------------------------------------------- @@ -2897,7 +2895,7 @@ static trait_setattr setattr_handlers[] = { NULL}; -trait_object * +PyObject * trait_new(PyTypeObject *trait_type, PyObject *args, PyObject *kw) { int kind = 0; @@ -2916,15 +2914,14 @@ trait_new(PyTypeObject *trait_type, PyObject *args, PyObject *kw) trait = (trait_object *)PyType_GenericNew(trait_type, args, kw); trait->getattr = getattr_handlers[kind]; trait->setattr = setattr_handlers[kind]; - return trait; + return (PyObject *)trait; } - PyErr_Format( + return PyErr_Format( TraitError, "Invalid argument to trait constructor. The argument `kind` " "must be an integer between 0 and 8 but a value of %d was provided.", kind); - return NULL; } /*----------------------------------------------------------------------------- @@ -3057,11 +3054,10 @@ _trait_set_default_value(trait_object *trait, PyObject *args) } if ((value_type < 0) || (value_type > MAXIMUM_DEFAULT_VALUE_TYPE)) { - PyErr_Format( + return PyErr_Format( PyExc_ValueError, "The default value type must be 0..%d, but %d was specified.", MAXIMUM_DEFAULT_VALUE_TYPE, value_type); - return NULL; } /* Validate the value */ @@ -5596,7 +5592,7 @@ static PyTypeObject trait_type = { sizeof(trait_object) - sizeof(PyObject *), /* tp_dictoffset */ 0, /* tp_init */ 0, /* tp_alloc */ - (newfunc)trait_new /* tp_new */ + trait_new /* tp_new */ }; /*-----------------------------------------------------------------------------