[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [19276] branches/soc-2008-mxcurioni/source /blender/freestyle/intern/python/Director.cpp: Better error handling in the Python API.

Tamito Kajiyama rd6t-kjym at asahi-net.or.jp
Fri Mar 13 20:24:05 CET 2009


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

Log Message:
-----------
Better error handling in the Python API.
Added error handling to prevent crashes even when errors have occurred in user-defined predicates, stroke shaders, and chaining iterators.

Modified Paths:
--------------
    branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp

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-13 18:04:38 UTC (rev 19275)
+++ branches/soc-2008-mxcurioni/source/blender/freestyle/intern/python/Director.cpp	2009-03-13 19:24:05 UTC (rev 19276)
@@ -47,9 +47,14 @@
 	PyObject *arg1 = BPy_Interface0D_from_Interface0D(i1);
 	PyObject *arg2 = BPy_Interface0D_from_Interface0D(i2);
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "OO", arg1, arg2 );
-	bool ret = bool_from_PyBool(result);
 	Py_DECREF(arg1);
 	Py_DECREF(arg2);
+	if (!result) {
+		cerr << "Warning: BinaryPredicate0D::__call__() failed." << endl;
+		PyErr_Clear();
+		return false;
+	}
+	bool ret = bool_from_PyBool(result);
 	Py_DECREF(result);
 	return ret;
 }
@@ -60,9 +65,14 @@
 	PyObject *arg1 = BPy_Interface1D_from_Interface1D(i1);
 	PyObject *arg2 = BPy_Interface1D_from_Interface1D(i2);
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "OO", arg1, arg2 );
-	bool ret = bool_from_PyBool(result);
 	Py_DECREF(arg1);
 	Py_DECREF(arg2);
+	if (!result) {
+		cerr << "Warning: BinaryPredicate1D::__call__() failed." << endl;
+		PyErr_Clear();
+		return false;
+	}
+	bool ret = bool_from_PyBool(result);
 	Py_DECREF(result);
 	return ret;
 }
@@ -72,8 +82,13 @@
 bool 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);
-	Py_DECREF(arg);
 	Py_DECREF(result);
 	return ret;
 }
@@ -83,8 +98,13 @@
 bool Director_BPy_UnaryPredicate1D___call__( PyObject *obj, Interface1D& if1D) {
 	PyObject *arg = BPy_Interface1D_from_Interface1D(if1D);
 	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);
-	Py_DECREF(arg);
 	Py_DECREF(result);
 	return ret;
 }
@@ -95,20 +115,35 @@
 	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;
+	}
 	Py_DECREF(result);
 }
 
 //   ChainingIterator: init, traverse
 void Director_BPy_ChainingIterator_init( PyObject *obj ) {
 	PyObject *result = PyObject_CallMethod( obj, "init", "", 0 );
+	if (!result) {
+		cerr << "Warning: ChainingIterator::init() failed." << endl;
+		PyErr_Clear();
+		return;
+	}
 	Py_DECREF(result);
 }
 
 ViewEdge * Director_BPy_ChainingIterator_traverse( PyObject *obj, AdjacencyIterator& a_it ) {
 	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;
+	}
 	ViewEdge *ret = ((BPy_ViewEdge *) result)->ve;
-	Py_DECREF(arg);
 	Py_DECREF(result);
 	return ret;
 }
@@ -119,6 +154,12 @@
 
 	PyObject *arg = BPy_Interface0DIterator_from_Interface0DIterator(if0D_it);
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );
+	Py_DECREF(arg);
+	if (!result) {
+		cerr << "Warning: UnaryFunction0D::__call__() failed." << endl;
+		PyErr_Clear();
+		return;
+	}
 	
 	if( BPy_UnaryFunction0DDouble_Check(obj) ) {	
 		((UnaryFunction0D<double> *) uf0D)->result = PyFloat_AsDouble(result);
@@ -162,7 +203,6 @@
 	
 	}	
 
-	Py_DECREF(arg);
 	Py_DECREF(result);
 }
 
@@ -170,6 +210,12 @@
 
 	PyObject *arg = BPy_Interface1D_from_Interface1D(if1D);
 	PyObject *result = PyObject_CallMethod( obj, "__call__", "O", arg );
+	Py_DECREF(arg);
+	if (!result) {
+		cerr << "Warning: UnaryFunction1D::__call__() failed." << endl;
+		PyErr_Clear();
+		return;
+	}
 	
 	if( BPy_UnaryFunction1DDouble_Check(obj) ) {	
 		((UnaryFunction1D<double> *) uf1D)->result = PyFloat_AsDouble(result);
@@ -204,7 +250,6 @@
 	
 	} 
 
-	Py_DECREF(arg);
 	Py_DECREF(result);
 }
 





More information about the Bf-blender-cvs mailing list