[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19338] branches/soc-2008-mxcurioni/source /blender/freestyle/intern/python: Improvements in error handling at Python-C++ boundaries.

Tamito Kajiyama rd6t-kjym at asahi-net.or.jp
Fri Mar 20 23:41:27 CET 2009


Revision: 19338
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=19338
Author:   kjym3
Date:     2009-03-20 23:41:27 +0100 (Fri, 20 Mar 2009)

Log Message:
-----------
Improvements in error handling at Python-C++ boundaries.

Modified Paths:
--------------
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate0D.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate1D.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate0D.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate1D.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.h
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DDouble.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DEdgeNature.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DFloat.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DId.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DMaterial.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DUnsigned.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DVec2f.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DVec3f.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DVectorViewShape.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction0D/BPy_UnaryFunction0DViewShape.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DDouble.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DEdgeNature.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DFloat.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DUnsigned.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DVec2f.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DVec3f.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DVectorViewShape.cpp
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/UnaryFunction1D/BPy_UnaryFunction1DVoid.cpp

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate0D.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate0D.cpp	2009-03-20 18:00:51 UTC (rev 19337)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate0D.cpp	2009-03-20 22:41:27 UTC (rev 19338)
@@ -153,15 +153,18 @@
 PyObject * BinaryPredicate0D___call__( BPy_BinaryPredicate0D *self, PyObject *args)
 {
 	BPy_Interface0D *obj1, *obj2;
-	bool b;
 
-	if( !PyArg_ParseTuple(args,(char *)"OO", &obj1, &obj2) ) {
-		cout << "ERROR: BinaryPredicate0D___call__ " << endl;		
+	if( !PyArg_ParseTuple(args, "O!O!", &Interface0D_Type, &obj1, &Interface0D_Type, &obj2) )
 		return NULL;
+	
+	if (self->bp0D->operator()( *(obj1->if0D) , *(obj2->if0D) ) < 0) {
+		if (!PyErr_Occurred()) {
+			string msg(self->bp0D->getName() + " __call__ method failed");
+			PyErr_SetString(PyExc_RuntimeError, msg.c_str());
+		}
+		return NULL;
 	}
-	
-	b = self->bp0D->operator()( *(obj1->if0D) , *(obj2->if0D) );
-	return PyBool_from_bool( b );
+	return PyBool_from_bool( self->bp0D->result );
 
 }
 

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate1D.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate1D.cpp	2009-03-20 18:00:51 UTC (rev 19337)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_BinaryPredicate1D.cpp	2009-03-20 22:41:27 UTC (rev 19338)
@@ -182,15 +182,18 @@
 PyObject *BinaryPredicate1D___call__( BPy_BinaryPredicate1D *self, PyObject *args)
 {
 	BPy_Interface1D *obj1, *obj2;
-	bool b;
 	
-	if( !PyArg_ParseTuple(args,(char *)"OO", &obj1, &obj2) ) {
-		cout << "ERROR: BinaryPredicate1D___call__ " << endl;		
+	if( !PyArg_ParseTuple(args, "O!O!", &Interface1D_Type, &obj1, &Interface1D_Type, &obj2) )
 		return NULL;
+	
+	if (self->bp1D->operator()( *(obj1->if1D) , *(obj2->if1D) ) < 0) {
+		if (!PyErr_Occurred()) {
+			string msg(self->bp1D->getName() + " __call__ method failed");
+			PyErr_SetString(PyExc_RuntimeError, msg.c_str());
+		}
+		return NULL;
 	}
-	
-	b = self->bp1D->operator()( *(obj1->if1D) , *(obj2->if1D) );
-	return PyBool_from_bool( b );
+	return PyBool_from_bool( self->bp1D->result );
 }
 
 

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate0D.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate0D.cpp	2009-03-20 18:00:51 UTC (rev 19337)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate0D.cpp	2009-03-20 22:41:27 UTC (rev 19338)
@@ -165,19 +165,24 @@
 {
 	PyObject *py_if0D_it;
 
-	if(!( PyArg_ParseTuple(args, "O", &py_if0D_it) && BPy_Interface0DIterator_Check(py_if0D_it) )) {
-		cout << "ERROR: UnaryPredicate0D___call__ " << endl;		
+	if( !PyArg_ParseTuple(args, "O!", &Interface0DIterator_Type, &py_if0D_it) )
 		return NULL;
-	}
 
 	Interface0DIterator *if0D_it = ((BPy_Interface0DIterator *) py_if0D_it)->if0D_it;
 
-	if( if0D_it )
-		return PyBool_from_bool( self->up0D->operator()(*if0D_it) );
-	else
-		cerr << "ERROR: UnaryPredicate0D___call__ (no Interface0DIterator)" << endl;
-
-	Py_RETURN_NONE;
+	if( !if0D_it ) {
+		string msg(self->up0D->getName() + " has no Interface0DIterator");
+		PyErr_SetString(PyExc_RuntimeError, msg.c_str());
+		return NULL;
+	}
+	if (self->up0D->operator()(*if0D_it) < 0) {
+		if (!PyErr_Occurred()) {
+			string msg(self->up0D->getName() + " __call__ method failed");
+			PyErr_SetString(PyExc_RuntimeError, msg.c_str());
+		}
+		return NULL;
+	}
+	return PyBool_from_bool( self->up0D->result );
 }
 
 

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate1D.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate1D.cpp	2009-03-20 18:00:51 UTC (rev 19337)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/BPy_UnaryPredicate1D.cpp	2009-03-20 22:41:27 UTC (rev 19338)
@@ -207,19 +207,24 @@
 {
 	PyObject *py_if1D;
 
-	if(!( PyArg_ParseTuple(args, "O", &py_if1D) && BPy_Interface1D_Check(py_if1D) )) {
-		cout << "ERROR: UnaryPredicate1D___call__ " << endl;		
+	if( !PyArg_ParseTuple(args, "O!", &Interface1D_Type, &py_if1D) )
 		return NULL;
-	}
 	
 	Interface1D *if1D = ((BPy_Interface1D *) py_if1D)->if1D;
 	
-	if( if1D )
-		return PyBool_from_bool( self->up1D->operator()(*if1D) );
-	else
-		cerr << "ERROR: UnaryPredicate1D___call__ (no Interface1D)" << endl;
-		
-	Py_RETURN_NONE;
+	if( !if1D ) {
+		string msg(self->up1D->getName() + " has no Interface0DIterator");
+		PyErr_SetString(PyExc_RuntimeError, msg.c_str());
+		return NULL;
+	}
+	if( self->up1D->operator()(*if1D) < 0 ) {
+		if (!PyErr_Occurred()) {
+			string msg(self->up1D->getName() + " __call__ method failed");
+			PyErr_SetString(PyExc_RuntimeError, msg.c_str());
+		}
+		return NULL;
+	}
+	return PyBool_from_bool( self->up1D->result );
 }
 
 

Modified: branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp
===================================================================
--- branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp	2009-03-20 18:00:51 UTC (rev 19337)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp	2009-03-20 22:41:27 UTC (rev 19338)
@@ -43,123 +43,108 @@
 
 
 //   BinaryPredicate0D: __call__
-bool Director_BPy_BinaryPredicate0D___call__( PyObject *obj, Interface0D& i1, Interface0D& i2) {
+int Director_BPy_BinaryPredicate0D___call__( PyObject *obj, Interface0D& i1, Interface0D& i2) {
 	PyObject *arg1 = BPy_Interface0D_from_Interface0D(i1);
 	PyObject *arg2 = BPy_Interface0D_from_Interface0D(i2);
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "OO", arg1, arg2 );
 	Py_DECREF(arg1);
 	Py_DECREF(arg2);
-	if (!result) {
-		cerr << "Warning: BinaryPredicate0D::__call__() failed." << endl;
-		PyErr_Clear();
-		return false;
-	}
-	bool ret = bool_from_PyBool(result);
+	if (!result)
+		return -1;
+	int ret = PyObject_IsTrue(result);
 	Py_DECREF(result);
 	return ret;
 }
 
 
 //   BinaryPredicate1D: __call__
-bool Director_BPy_BinaryPredicate1D___call__( PyObject *obj, Interface1D& i1, Interface1D& i2) {
-	PyObject *arg1 = BPy_Interface1D_from_Interface1D(i1);
-	PyObject *arg2 = BPy_Interface1D_from_Interface1D(i2);
+int Director_BPy_BinaryPredicate1D___call__( PyObject *obj, Interface1D& i1, Interface1D& i2) {
+	PyObject *arg1, *arg2;
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "OO", arg1, arg2 );
 	Py_DECREF(arg1);
 	Py_DECREF(arg2);
-	if (!result) {
-		cerr << "Warning: BinaryPredicate1D::__call__() failed." << endl;
-		PyErr_Clear();
-		return false;
-	}
-	bool ret = bool_from_PyBool(result);
+	if (!result)
+		return -1;
+	int ret = PyObject_IsTrue(result);
 	Py_DECREF(result);
 	return ret;
 }
 
 
 //   UnaryPredicate0D: __call__
-bool Director_BPy_UnaryPredicate0D___call__( PyObject *obj, Interface0DIterator& if0D_it) {
+int Director_BPy_UnaryPredicate0D___call__( PyObject *obj, Interface0DIterator& if0D_it) {
 	PyObject *arg = BPy_Interface0DIterator_from_Interface0DIterator(if0D_it);
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );
 	Py_DECREF(arg);
-	if (!result) {
-		cerr << "Warning: UnaryPredicate0D::__call__() failed." << endl;
-		PyErr_Clear();
-		return false;
-	}
-	bool ret = bool_from_PyBool(result);
+	if (!result)
+		return -1;
+	int ret = PyObject_IsTrue(result);
 	Py_DECREF(result);
 	return ret;
 }
 
 
 //   UnaryPredicate1D: __call__
-bool Director_BPy_UnaryPredicate1D___call__( PyObject *obj, Interface1D& if1D) {
-	PyObject *arg = BPy_Interface1D_from_Interface1D(if1D);
+int Director_BPy_UnaryPredicate1D___call__( PyObject *obj, Interface1D& if1D) {
+	PyObject *arg;
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );
 	Py_DECREF(arg);
-	if (!result) {
-		cerr << "Warning: UnaryPredicate1D::__call__() failed." << endl;
-		PyErr_Clear();
-		return false;
-	}
-	bool ret = bool_from_PyBool(result);
+	if (!result)
+		return -1;
+	int ret = PyObject_IsTrue(result);
 	Py_DECREF(result);
 	return ret;
 }
 
 
 //   StrokeShader: shade
-void Director_BPy_StrokeShader_shade( PyObject *obj, Stroke& s) {
+int Director_BPy_StrokeShader_shade( PyObject *obj, Stroke& s) {
 	PyObject *arg = BPy_Stroke_from_Stroke_ptr(&s);
 	PyObject *result = PyObject_CallMethod( obj, "shade", "O", arg );
 	Py_DECREF(arg);
-	if (!result) {
-		cerr << "Warning: StrokeShader::shade() failed" << endl;
-		PyErr_Clear();
-		return;
-	}
+	if (!result)
+		return -1;
 	Py_DECREF(result);
+	return 0;
 }
 
 //   ChainingIterator: init, traverse
-void Director_BPy_ChainingIterator_init( PyObject *obj ) {
+int Director_BPy_ChainingIterator_init( PyObject *obj ) {
 	PyObject *result = PyObject_CallMethod( obj, "init", "", 0 );
-	if (!result) {
-		cerr << "Warning: ChainingIterator::init() failed." << endl;
-		PyErr_Clear();
-		return;
-	}
+	if (!result)
+		return -1;
 	Py_DECREF(result);
+	return 0;
 }
 
-ViewEdge * Director_BPy_ChainingIterator_traverse( PyObject *obj, AdjacencyIterator& a_it ) {
+int Director_BPy_ChainingIterator_traverse( PyObject *obj, AdjacencyIterator& a_it, ViewEdge **ve ) {
 	PyObject *arg = BPy_AdjacencyIterator_from_AdjacencyIterator(a_it);
 	PyObject *result = PyObject_CallMethod( obj, "traverse", "O", arg );
 	Py_DECREF(arg);
-	if (!result) {
-		cerr << "Warning: ChainingIterator::traverse() failed." << endl;
-		PyErr_Clear();
-		return NULL;
+	if (!result)
+		return -1;
+	if (BPy_ViewEdge_Check(result)) {
+		*ve = ((BPy_ViewEdge *) result)->ve;
+	} else if (result == Py_None) {
+		*ve = NULL;
+	} else {
+		PyErr_SetString(PyExc_RuntimeError, "traverse method returned a wrong value");
+		Py_DECREF(result);
+		return -1;
 	}
-	ViewEdge *ret = ((BPy_ViewEdge *) result)->ve;
 	Py_DECREF(result);
-	return ret;
+	return 0;
 }
 
 
 // BPy_UnaryFunction{0D,1D}: __call__
-void Director_BPy_UnaryFunction0D___call__( void *uf0D, PyObject *obj, Interface0DIterator& if0D_it) {
+int Director_BPy_UnaryFunction0D___call__( void *uf0D, PyObject *obj, Interface0DIterator& if0D_it) {
 

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list