Made predicate and function types callable in the sense that

callable(I, T) returns True when I is an object of a type T or
of a subtype of T.  Also implemented a measure to avoid an
infinite loop when user-defined predicate and function classes
do not properly overload the __call__ method (including the
cases of directly instantiating the base classes such as
UnaryPredicate0D and BinaryPredicate1D).
This commit is contained in:
2009-07-26 16:15:28 +00:00
parent 78ce17fce8
commit 730fb1021c
22 changed files with 243 additions and 88 deletions

View File

@@ -25,12 +25,11 @@ static void UnaryPredicate1D___dealloc__(BPy_UnaryPredicate1D *self);
static PyObject * UnaryPredicate1D___repr__(BPy_UnaryPredicate1D *self);
static PyObject * UnaryPredicate1D_getName( BPy_UnaryPredicate1D *self, PyObject *args);
static PyObject * UnaryPredicate1D___call__( BPy_UnaryPredicate1D *self, PyObject *args);
static PyObject * UnaryPredicate1D___call__( BPy_UnaryPredicate1D *self, PyObject *args, PyObject *kwds);
/*----------------------UnaryPredicate1D instance definitions ----------------------------*/
static PyMethodDef BPy_UnaryPredicate1D_methods[] = {
{"getName", ( PyCFunction ) UnaryPredicate1D_getName, METH_NOARGS, ""},
{"__call__", ( PyCFunction ) UnaryPredicate1D___call__, METH_VARARGS, "" },
{NULL, NULL, 0, NULL}
};
@@ -60,7 +59,7 @@ PyTypeObject UnaryPredicate1D_Type = {
/* More standard operations (here for binary compatibility) */
NULL, /* hashfunc tp_hash; */
NULL, /* ternaryfunc tp_call; */
(ternaryfunc)UnaryPredicate1D___call__, /* ternaryfunc tp_call; */
NULL, /* reprfunc tp_str; */
NULL, /* getattrofunc tp_getattro; */
NULL, /* setattrofunc tp_setattro; */
@@ -203,10 +202,14 @@ PyObject * UnaryPredicate1D_getName( BPy_UnaryPredicate1D *self, PyObject *args)
return PyString_FromString( self->up1D->getName().c_str() );
}
PyObject * UnaryPredicate1D___call__( BPy_UnaryPredicate1D *self, PyObject *args)
static PyObject * UnaryPredicate1D___call__( BPy_UnaryPredicate1D *self, PyObject *args, PyObject *kwds)
{
PyObject *py_if1D;
if( kwds != NULL ) {
PyErr_SetString(PyExc_TypeError, "keyword argument(s) not supported");
return NULL;
}
if( !PyArg_ParseTuple(args, "O!", &Interface1D_Type, &py_if1D) )
return NULL;
@@ -217,6 +220,10 @@ PyObject * UnaryPredicate1D___call__( BPy_UnaryPredicate1D *self, PyObject *args
PyErr_SetString(PyExc_RuntimeError, msg.c_str());
return NULL;
}
if( typeid(*(self->up1D)) == typeid(UnaryPredicate1D) ) {
PyErr_SetString(PyExc_TypeError, "__call__ method must be overloaded");
return NULL;
}
if( self->up1D->operator()(*if1D) < 0 ) {
if (!PyErr_Occurred()) {
string msg(self->up1D->getName() + " __call__ method failed");