Relaxed type checking concerning boolean arguments in class constructors

and __call__ methods so that not only True and False but also various
other boolean expressions (e.g., 0, 1, and None) are accepted.
This commit is contained in:
2009-04-03 20:03:09 +00:00
parent 5926ad2db2
commit acfd7c82ab
14 changed files with 82 additions and 34 deletions

View File

@@ -326,7 +326,7 @@ PyObject * BPy_ChainSilhouetteIterator_from_ChainSilhouetteIterator( ChainSilhou
//==============================
bool bool_from_PyBool( PyObject *b ) {
return (b == Py_True || PyInt_AsLong(b) != 0);
return PyObject_IsTrue(b) != 0;
}
IntegrationType IntegrationType_from_BPy_IntegrationType( PyObject* obj ) {

View File

@@ -390,7 +390,7 @@ PyObject * StrokeAttribute_setThickness( BPy_StrokeAttribute *self, PyObject *ar
PyObject * StrokeAttribute_setVisible( BPy_StrokeAttribute *self, PyObject *args ) {
PyObject *py_b;
if(!( PyArg_ParseTuple(args, "O!", &PyBool_Type, &py_b) ))
if(!( PyArg_ParseTuple(args, "O", &py_b) ))
return NULL;
self->sa->setVisible( bool_from_PyBool(py_b) );

View File

@@ -330,7 +330,7 @@ PyObject * FEdge_setViewEdge( BPy_FEdge *self, PyObject *args ) {
PyObject *FEdge_setSmooth( BPy_FEdge *self , PyObject *args) {
PyObject *py_b;
if(!( PyArg_ParseTuple(args, "O!", &PyBool_Type, &py_b) ))
if(!( PyArg_ParseTuple(args, "O", &py_b) ))
return NULL;
self->fe->setSmooth( bool_from_PyBool(py_b) );

View File

@@ -354,7 +354,7 @@ PyObject *Stroke_setTextureId( BPy_Stroke *self , PyObject *args) {
PyObject *Stroke_setTips( BPy_Stroke *self , PyObject *args) {
PyObject *py_b;
if(!( PyArg_ParseTuple(args, "O!", &PyBool_Type, &py_b) ))
if(!( PyArg_ParseTuple(args, "O", &py_b) ))
return NULL;
self->s->setTips( bool_from_PyBool(py_b) );

View File

@@ -145,7 +145,7 @@ int Chain___init__(BPy_Chain *self, PyObject *args, PyObject *kwds)
PyObject * Chain_push_viewedge_back( BPy_Chain *self, PyObject *args ) {
PyObject *obj1 = 0, *obj2 = 0;
if(!( PyArg_ParseTuple(args, "O!O!", &ViewEdge_Type, &obj1, &PyBool_Type, &obj2) ))
if(!( PyArg_ParseTuple(args, "O!O", &ViewEdge_Type, &obj1, &obj2) ))
return NULL;
ViewEdge *ve = ((BPy_ViewEdge *) obj1)->ve;
@@ -158,7 +158,7 @@ PyObject * Chain_push_viewedge_back( BPy_Chain *self, PyObject *args ) {
PyObject * Chain_push_viewedge_front( BPy_Chain *self, PyObject *args ) {
PyObject *obj1 = 0, *obj2 = 0;
if(!( PyArg_ParseTuple(args, "O!O!", &ViewEdge_Type, &obj1, &PyBool_Type, &obj2) ))
if(!( PyArg_ParseTuple(args, "O!O", &ViewEdge_Type, &obj1, &obj2) ))
return NULL;
ViewEdge *ve = ((BPy_ViewEdge *) obj1)->ve;

View File

@@ -124,8 +124,8 @@ int AdjacencyIterator___init__(BPy_AdjacencyIterator *self, PyObject *args )
self->a_it = new AdjacencyIterator(*( ((BPy_AdjacencyIterator *) obj1)->a_it ));
} else if( BPy_ViewVertex_Check(obj1) ) {
bool restrictToSelection = ( obj2 && PyBool_Check(obj2) ) ? bool_from_PyBool(obj2) : true;
bool restrictToUnvisited = ( obj3 && PyBool_Check(obj3) ) ? bool_from_PyBool(obj3) : true;
bool restrictToSelection = ( obj2 ) ? bool_from_PyBool(obj2) : true;
bool restrictToUnvisited = ( obj3 ) ? bool_from_PyBool(obj3) : true;
self->a_it = new AdjacencyIterator( ((BPy_ViewVertex *) obj1)->vv, restrictToSelection, restrictToUnvisited );

View File

@@ -111,29 +111,53 @@ int ChainPredicateIterator___init__(BPy_ChainPredicateIterator *self, PyObject *
{
PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0, *obj4 = 0, *obj5 = 0, *obj6 = 0;
if (!( PyArg_ParseTuple(args, "O|OOOOO", &obj1, &obj2, &obj3, &obj4, &obj5, &obj6) ))
if (!( PyArg_ParseTuple(args, "|OOOOOO", &obj1, &obj2, &obj3, &obj4, &obj5, &obj6) ))
return -1;
if( obj1 && BPy_ChainPredicateIterator_Check(obj1) ) {
self->cp_it = new ChainPredicateIterator(*( ((BPy_ChainPredicateIterator *) obj1)->cp_it ));
} else if( obj1 && BPy_UnaryPredicate1D_Check(obj1) && ((BPy_UnaryPredicate1D *) obj1)->up1D &&
obj2 && BPy_BinaryPredicate1D_Check(obj2) && ((BPy_BinaryPredicate1D *) obj2)->bp1D ) {
} else if( obj1 && BPy_UnaryPredicate1D_Check(obj1) &&
obj2 && BPy_BinaryPredicate1D_Check(obj2) ) {
if (!((BPy_UnaryPredicate1D *) obj1)->up1D) {
PyErr_SetString(PyExc_TypeError, "1st argument: invalid UnaryPredicate1D object");
return -1;
}
if (!((BPy_BinaryPredicate1D *) obj2)->bp1D) {
PyErr_SetString(PyExc_TypeError, "2nd argument: invalid BinaryPredicate1D object");
return -1;
}
UnaryPredicate1D *up1D = ((BPy_UnaryPredicate1D *) obj1)->up1D;
BinaryPredicate1D *bp1D = ((BPy_BinaryPredicate1D *) obj2)->bp1D;
bool restrictToSelection = ( obj3 && PyBool_Check(obj3) ) ? bool_from_PyBool(obj3) : true;
bool restrictToUnvisited = ( obj4 && PyBool_Check(obj4) ) ? bool_from_PyBool(obj4) : true;
ViewEdge *begin = ( obj5 && BPy_ViewEdge_Check(obj5) ) ? ((BPy_ViewEdge *) obj5)->ve : 0;
bool orientation = ( obj6 && PyBool_Check(obj6) ) ? bool_from_PyBool(obj6) : true;
bool restrictToSelection = ( obj3 ) ? bool_from_PyBool(obj3) : true;
bool restrictToUnvisited = ( obj4 ) ? bool_from_PyBool(obj4) : true;
ViewEdge *begin;
if ( !obj5 || obj5 == Py_None )
begin = NULL;
else if ( BPy_ViewEdge_Check(obj5) )
begin = ((BPy_ViewEdge *) obj5)->ve;
else {
PyErr_SetString(PyExc_TypeError, "5th argument must be either a ViewEdge object or None");
return -1;
}
bool orientation = ( obj6 ) ? bool_from_PyBool(obj6) : true;
self->cp_it = new ChainPredicateIterator( *up1D, *bp1D, restrictToSelection, restrictToUnvisited, begin, orientation);
} else {
bool restrictToSelection = ( obj1 && PyBool_Check(obj1) ) ? bool_from_PyBool(obj1) : true;
bool restrictToUnvisited = ( obj2 && PyBool_Check(obj2) ) ? bool_from_PyBool(obj2) : true;
ViewEdge *begin = ( obj3 && BPy_ViewEdge_Check(obj3) ) ? ((BPy_ViewEdge *) obj3)->ve : 0;
bool orientation = ( obj4 && PyBool_Check(obj4) ) ? bool_from_PyBool(obj4) : true;
bool restrictToSelection = ( obj1 ) ? bool_from_PyBool(obj1) : true;
bool restrictToUnvisited = ( obj2 ) ? bool_from_PyBool(obj2) : true;
ViewEdge *begin;
if ( !obj3 || obj3 == Py_None )
begin = NULL;
else if ( BPy_ViewEdge_Check(obj3) )
begin = ((BPy_ViewEdge *) obj3)->ve;
else {
PyErr_SetString(PyExc_TypeError, "3rd argument must be either a ViewEdge object or None");
return -1;
}
bool orientation = ( obj4 ) ? bool_from_PyBool(obj4) : true;
self->cp_it = new ChainPredicateIterator( restrictToSelection, restrictToUnvisited, begin, orientation);
}

View File

@@ -118,9 +118,17 @@ int ChainSilhouetteIterator___init__(BPy_ChainSilhouetteIterator *self, PyObject
self->cs_it = new ChainSilhouetteIterator(*( ((BPy_ChainSilhouetteIterator *) obj1)->cs_it ));
} else {
bool restrictToSelection = ( obj1 && PyBool_Check(obj1) ) ? bool_from_PyBool(obj1) : true;
ViewEdge *begin = ( obj2 && BPy_ViewEdge_Check(obj2) ) ? ((BPy_ViewEdge *) obj2)->ve : 0;
bool orientation = ( obj3 && PyBool_Check(obj3) ) ? bool_from_PyBool(obj3) : true;
bool restrictToSelection = ( obj1 ) ? bool_from_PyBool(obj1) : true;
ViewEdge *begin;
if ( !obj2 || obj2 == Py_None )
begin = NULL;
else if ( BPy_ViewEdge_Check(obj2) )
begin = ((BPy_ViewEdge *) obj2)->ve;
else {
PyErr_SetString(PyExc_TypeError, "2nd argument must be either a ViewEdge object or None");
return -1;
}
bool orientation = ( obj3 ) ? bool_from_PyBool(obj3) : true;
self->cs_it = new ChainSilhouetteIterator( restrictToSelection, begin, orientation);
}

View File

@@ -121,17 +121,25 @@ int ChainingIterator___init__(BPy_ChainingIterator *self, PyObject *args )
{
PyObject *obj1 = 0, *obj2 = 0, *obj3 = 0, *obj4 = 0;
if (!( PyArg_ParseTuple(args, "O|OOO", &obj1, &obj2, &obj3, &obj4) ))
if (!( PyArg_ParseTuple(args, "|OOOO", &obj1, &obj2, &obj3, &obj4) ))
return -1;
if( obj1 && BPy_ChainingIterator_Check(obj1) ) {
self->c_it = new ChainingIterator(*( ((BPy_ChainingIterator *) obj1)->c_it ));
} else {
bool restrictToSelection = ( obj1 && PyBool_Check(obj1) ) ? bool_from_PyBool(obj1) : true;
bool restrictToUnvisited = ( obj2 && PyBool_Check(obj2) ) ? bool_from_PyBool(obj2) : true;
ViewEdge *begin = ( obj3 && BPy_ViewEdge_Check(obj3) ) ? ((BPy_ViewEdge *) obj3)->ve : 0;
bool orientation = ( obj4 && PyBool_Check(obj4) ) ? bool_from_PyBool(obj4) : true;
bool restrictToSelection = ( obj1 ) ? bool_from_PyBool(obj1) : true;
bool restrictToUnvisited = ( obj2 ) ? bool_from_PyBool(obj2) : true;
ViewEdge *begin;
if ( !obj3 || obj3 == Py_None )
begin = NULL;
else if ( BPy_ViewEdge_Check(obj3) )
begin = ((BPy_ViewEdge *) obj3)->ve;
else {
PyErr_SetString(PyExc_TypeError, "3rd argument must be either a ViewEdge object or None");
return -1;
}
bool orientation = ( obj4 ) ? bool_from_PyBool(obj4) : true;
self->c_it = new ChainingIterator( restrictToSelection, restrictToUnvisited, begin, orientation);
}

View File

@@ -135,8 +135,16 @@ int ViewEdgeIterator___init__(BPy_ViewEdgeIterator *self, PyObject *args )
self->ve_it = new ViewEdgeInternal::ViewEdgeIterator(*( ((BPy_ViewEdgeIterator *) obj1)->ve_it ));
} else {
ViewEdge *begin = ( obj1 && BPy_ViewEdge_Check(obj1) ) ? ((BPy_ViewEdge *) obj1)->ve : 0;
bool orientation = ( obj2 && PyBool_Check(obj2) ) ? bool_from_PyBool(obj2) : true;
ViewEdge *begin;
if ( !obj1 || obj1 == Py_None )
begin = NULL;
else if ( BPy_ViewEdge_Check(obj1) )
begin = ((BPy_ViewEdge *) obj1)->ve;
else {
PyErr_SetString(PyExc_TypeError, "1st argument must be either a ViewEdge object or None");
return -1;
}
bool orientation = ( obj2 ) ? bool_from_PyBool(obj2) : true;
self->ve_it = new ViewEdgeInternal::ViewEdgeIterator( begin, orientation);
@@ -192,7 +200,7 @@ PyObject *ViewEdgeIterator_getOrientation( BPy_ViewEdgeIterator *self ) {
PyObject *ViewEdgeIterator_setOrientation( BPy_ViewEdgeIterator *self, PyObject *args ) {
PyObject *py_b;
if(!( PyArg_ParseTuple(args, "O!", &PyBool_Type, &py_b) ))
if(!( PyArg_ParseTuple(args, "O", &py_b) ))
return NULL;
self->ve_it->setOrientation( bool_from_PyBool(py_b) );

View File

@@ -105,7 +105,7 @@ int CalligraphicShader___init__( BPy_CalligraphicShader* self, PyObject *args)
PyObject *obj3 = 0, *obj4 = 0;
if(!( PyArg_ParseTuple(args, "ddO!O!", &d1, &d2, &PyList_Type, &obj3, &PyBool_Type, &obj4) ))
if(!( PyArg_ParseTuple(args, "ddO!O", &d1, &d2, &PyList_Type, &obj3, &obj4) ))
return -1;
if( PyList_Size(obj3) != 2 ) {
stringstream msg("CalligraphicShader() accepts a list of 2 elements (");

View File

@@ -106,7 +106,7 @@ int SpatialNoiseShader___init__( BPy_SpatialNoiseShader* self, PyObject *args)
PyObject *obj4 = 0, *obj5 = 0;
if(!( PyArg_ParseTuple(args, "ffiO!O!", &f1, &f2, &i3, &PyBool_Type, &obj4, &PyBool_Type, &obj5) )) {
if(!( PyArg_ParseTuple(args, "ffiOO", &f1, &f2, &i3, &obj4, &obj5) )) {
cout << "ERROR: SpatialNoiseShader___init__" << endl;
return -1;
}

View File

@@ -105,7 +105,7 @@ int StrokeTextureShader___init__( BPy_StrokeTextureShader* self, PyObject *args)
const char *s1;
PyObject *obj2 = 0, *obj3 = 0;
if(!( PyArg_ParseTuple(args, "s|O!O!", &s1, &MediumType_Type, &obj2, &PyBool_Type, &obj3) )) {
if(!( PyArg_ParseTuple(args, "s|O!O", &s1, &MediumType_Type, &obj2, &obj3) )) {
cout << "ERROR: StrokeTextureShader___init__" << endl;
return -1;
}

View File

@@ -105,7 +105,7 @@ int ThicknessVariationPatternShader___init__( BPy_ThicknessVariationPatternShade
float f2 = 1.0, f3 = 5.0;
PyObject *obj4 = 0;
if(!( PyArg_ParseTuple(args, "s|ffO!", &s1, &f2, &f3, &PyBool_Type, &obj4) )) {
if(!( PyArg_ParseTuple(args, "s|ffO", &s1, &f2, &f3, &obj4) )) {
cout << "ERROR: ThicknessVariationPatternShader___init__" << endl;
return -1;
}