Improvements on error handling in the Python API.
authorTamito Kajiyama <rd6t-kjym@asahi-net.or.jp>
Tue, 31 Mar 2009 22:45:11 +0000 (22:45 +0000)
committerTamito Kajiyama <rd6t-kjym@asahi-net.or.jp>
Tue, 31 Mar 2009 22:45:11 +0000 (22:45 +0000)
source/blender/freestyle/intern/python/Interface0D/ViewVertex/BPy_NonTVertex.cpp
source/blender/freestyle/intern/python/Interface0D/ViewVertex/BPy_TVertex.cpp
source/blender/freestyle/intern/python/Interface1D/FEdge/BPy_FEdgeSharp.cpp
source/blender/freestyle/intern/python/Interface1D/FEdge/BPy_FEdgeSmooth.cpp

index 74c91856bbd1633b2f191c2ad8748e00ee911f04..a6f00ba854900186f483dd7920e7372601f4de83 100644 (file)
@@ -133,6 +133,7 @@ int NonTVertex___init__(BPy_NonTVertex *self, PyObject *args, PyObject *kwds)
                self->ntv = new NonTVertex( ((BPy_SVertex *) obj)->sv );
 
        } else {
+               PyErr_SetString(PyExc_TypeError, "invalid argument");
                return -1;
        }
 
@@ -174,10 +175,8 @@ PyObject * NonTVertex_svertex( BPy_NonTVertex *self ) {
 PyObject * NonTVertex_setSVertex( BPy_NonTVertex *self, PyObject *args) {
        PyObject *py_sv;
 
-       if(!( PyArg_ParseTuple(args, "O", &py_sv) && BPy_SVertex_Check(py_sv) )) {
-               cout << "ERROR: NonTVertex_setSVertex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "O!", &SVertex_Type, &py_sv) ))
+               return NULL;
 
        self->ntv->setSVertex( ((BPy_SVertex *) py_sv)->sv );
 
index 83ec27818fa863df14dc6cff6bee4f46fe595f1c..f16bcbaab87e009835e27906ff88f7180c0a1cbf 100644 (file)
@@ -172,10 +172,8 @@ PyObject * TVertex_backSVertex( BPy_TVertex *self ) {
 PyObject * TVertex_setFrontSVertex( BPy_TVertex *self, PyObject *args) {
        PyObject *py_sv;
 
-       if(!( PyArg_ParseTuple(args, "O", &py_sv) && BPy_SVertex_Check(py_sv) )) {
-               cout << "ERROR: TVertex_setFrontSVertex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "O!", &SVertex_Type, &py_sv) ))
+               return NULL;
 
        self->tv->setFrontSVertex( ((BPy_SVertex *) py_sv)->sv );
 
@@ -185,10 +183,8 @@ PyObject * TVertex_setFrontSVertex( BPy_TVertex *self, PyObject *args) {
 PyObject * TVertex_setBackSVertex( BPy_TVertex *self, PyObject *args) {
        PyObject *py_sv;
 
-       if(!( PyArg_ParseTuple(args, "O", &py_sv) && BPy_SVertex_Check(py_sv) )) {
-               cout << "ERROR: TVertex_setBackSVertex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "O", &SVertex_Type, &py_sv) ))
+               return NULL;
 
        self->tv->setBackSVertex( ((BPy_SVertex *) py_sv)->sv );
 
@@ -198,10 +194,8 @@ PyObject * TVertex_setBackSVertex( BPy_TVertex *self, PyObject *args) {
 PyObject * TVertex_setId( BPy_TVertex *self, PyObject *args) {
        PyObject *py_id;
 
-       if(!( PyArg_ParseTuple(args, "O", &py_id) && BPy_Id_Check(py_id) )) {
-               cout << "ERROR: TVertex_setId" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "O!", &Id_Type, &py_id) ))
+               return NULL;
 
        if( ((BPy_Id *) py_id)->id )
                self->tv->setId(*( ((BPy_Id *) py_id)->id ));
@@ -212,10 +206,8 @@ PyObject * TVertex_setId( BPy_TVertex *self, PyObject *args) {
 PyObject * TVertex_getSVertex( BPy_TVertex *self, PyObject *args) {
        PyObject *py_fe;
 
-       if(!( PyArg_ParseTuple(args, "O", &py_fe) && BPy_FEdge_Check(py_fe) )) {
-               cout << "ERROR: TVertex_getSVertex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "O!", &FEdge_Type, &py_fe) ))
+               return NULL;
 
        SVertex *sv = self->tv->getSVertex( ((BPy_FEdge *) py_fe)->fe );
        if( sv ){
@@ -228,10 +220,8 @@ PyObject * TVertex_getSVertex( BPy_TVertex *self, PyObject *args) {
 PyObject * TVertex_mate( BPy_TVertex *self, PyObject *args) {
        PyObject *py_ve;
 
-       if(!( PyArg_ParseTuple(args, "O", &py_ve) && BPy_ViewEdge_Check(py_ve) )) {
-               cout << "ERROR: TVertex_mate" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "O!", &ViewEdge_Type, &py_ve) ))
+               return NULL;
 
        ViewEdge *ve = self->tv->mate( ((BPy_ViewEdge *) py_ve)->ve );
        if( ve ){
index 336331d92d51285328c23fa200593d5edd0ccbe1..c6bc201e37d538faaa0f55803946f1db183740f8 100644 (file)
@@ -136,15 +136,13 @@ int FEdgeSharp___init__(BPy_FEdgeSharp *self, PyObject *args, PyObject *kwds)
                self->fes = new FEdgeSharp();
                
        } else if( BPy_FEdgeSharp_Check(obj1) ) {
-               if( ((BPy_FEdgeSharp *) obj1)->fes )
-                       self->fes = new FEdgeSharp(*( ((BPy_FEdgeSharp *) obj1)->fes ));
-               else
-                       return -1;
+               self->fes = new FEdgeSharp(*( ((BPy_FEdgeSharp *) obj1)->fes ));
                
        } else if( BPy_SVertex_Check(obj1) && BPy_SVertex_Check(obj2) ) {
                self->fes = new FEdgeSharp( ((BPy_SVertex *) obj1)->sv, ((BPy_SVertex *) obj2)->sv );
 
        } else {
+               PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
                return -1;
        }
 
@@ -186,9 +184,13 @@ PyObject * FEdgeSharp_bMaterial( BPy_FEdgeSharp *self ) {
 PyObject * FEdgeSharp_setNormalA( BPy_FEdgeSharp *self, PyObject *args ) {
        PyObject *obj = 0;
 
-       if(!( PyArg_ParseTuple(args, "O", &obj) && PyList_Check(obj) && PyList_Size(obj) > 2 )) {
-               cout << "ERROR: FEdgeSharp_setNormalA" << endl;
-               Py_RETURN_NONE;
+       if(!( PyArg_ParseTuple(args, "O!", &PyList_Type, &obj) ))
+               return NULL;
+       if( PyList_Size(obj) != 3 ) {
+               stringstream msg("FEdgeSharp::setNormalA() accepts a list of 3 elements (");
+               msg << PyList_Size(obj) << " found)";
+               PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+               return NULL;
        }
        
        Vec3r v(        PyFloat_AsDouble( PyList_GetItem(obj,0) ),
@@ -203,9 +205,13 @@ PyObject * FEdgeSharp_setNormalA( BPy_FEdgeSharp *self, PyObject *args ) {
 PyObject * FEdgeSharp_setNormalB( BPy_FEdgeSharp *self, PyObject *args ) {
        PyObject *obj = 0;
 
-       if(!( PyArg_ParseTuple(args, "O", &obj) && PyList_Check(obj) && PyList_Size(obj) > 2 )) {
-               cout << "ERROR: FEdgeSharp_setNormalB" << endl;
-               Py_RETURN_NONE;
+       if(!( PyArg_ParseTuple(args, "O!", &PyList_Type, &obj) ))
+               return NULL;
+       if( PyList_Size(obj) != 3 ) {
+               stringstream msg("FEdgeSharp::setNormalB() accepts a list of 3 elements (");
+               msg << PyList_Size(obj) << " found)";
+               PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+               return NULL;
        }
        
        Vec3r v(        PyFloat_AsDouble( PyList_GetItem(obj,0) ),
@@ -220,10 +226,8 @@ PyObject * FEdgeSharp_setNormalB( BPy_FEdgeSharp *self, PyObject *args ) {
 PyObject * FEdgeSharp_setaMaterialIndex( BPy_FEdgeSharp *self, PyObject *args ) {
        unsigned int i;
 
-       if(!( PyArg_ParseTuple(args, "I", &i) )) {
-               cout << "ERROR: FEdgeSharp_setaMaterialIndex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "I", &i) ))
+               return NULL;
        
        self->fes->setaFrsMaterialIndex( i );
 
@@ -233,10 +237,8 @@ PyObject * FEdgeSharp_setaMaterialIndex( BPy_FEdgeSharp *self, PyObject *args )
 PyObject * FEdgeSharp_setbMaterialIndex( BPy_FEdgeSharp *self, PyObject *args ) {
        unsigned int i;
 
-       if(!( PyArg_ParseTuple(args, "I", &i) )) {
-               cout << "ERROR: FEdgeSharp_setbMaterialIndex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "I", &i) ))
+               return NULL;
        
        self->fes->setbFrsMaterialIndex( i );
 
index 0970e22a72672d6b9620f50fd2a20b0e95bbc2c9..bb0e6eef4e4c5dea79e1573ea587d5f6c3f08866 100644 (file)
@@ -128,15 +128,13 @@ int FEdgeSmooth___init__(BPy_FEdgeSmooth *self, PyObject *args, PyObject *kwds)
                self->fes = new FEdgeSmooth();
                
        } else if( BPy_FEdgeSmooth_Check(obj1) ) {
-               if( ((BPy_FEdgeSmooth *) obj1)->fes )
-                       self->fes = new FEdgeSmooth(*( ((BPy_FEdgeSmooth *) obj1)->fes ));
-               else
-                       return -1;
+               self->fes = new FEdgeSmooth(*( ((BPy_FEdgeSmooth *) obj1)->fes ));
                
        } else if( BPy_SVertex_Check(obj1) && BPy_SVertex_Check(obj2) ) {
                self->fes = new FEdgeSmooth( ((BPy_SVertex *) obj1)->sv, ((BPy_SVertex *) obj2)->sv );
 
        } else {
+               PyErr_SetString(PyExc_TypeError, "invalid argument(s)");
                return -1;
        }
 
@@ -164,9 +162,13 @@ PyObject * FEdgeSmooth_material( BPy_FEdgeSmooth *self ) {
 PyObject * FEdgeSmooth_setNormal( BPy_FEdgeSmooth *self, PyObject *args ) {
        PyObject *obj = 0;
 
-       if(!( PyArg_ParseTuple(args, "O", &obj) && PyList_Check(obj) && PyList_Size(obj) > 2 )) {
-               cout << "ERROR: FEdgeSmooth_setNormal" << endl;
-               Py_RETURN_NONE;
+       if(!( PyArg_ParseTuple(args, "O!", &PyList_Type, &obj) ))
+               return NULL;
+       if( PyList_Size(obj) != 3 ) {
+               stringstream msg("FEdgeSmooth::setNormal() accepts a list of 3 elements (");
+               msg << PyList_Size(obj) << " found)";
+               PyErr_SetString(PyExc_TypeError, msg.str().c_str());
+               return NULL;
        }
        
        Vec3r v(        PyFloat_AsDouble( PyList_GetItem(obj,0) ),
@@ -181,10 +183,8 @@ PyObject * FEdgeSmooth_setNormal( BPy_FEdgeSmooth *self, PyObject *args ) {
 PyObject * FEdgeSmooth_setMaterialIndex( BPy_FEdgeSmooth *self, PyObject *args ) {
        unsigned int i;
 
-       if(!( PyArg_ParseTuple(args, "I", &i) )) {
-               cout << "ERROR: FEdgeSmooth_setMaterialIndex" << endl;
-               Py_RETURN_NONE;
-       }
+       if(!( PyArg_ParseTuple(args, "I", &i) ))
+               return NULL;
        
        self->fes->setFrsMaterialIndex( i );