[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [20855] branches/blender2.5/blender/source /blender/python/intern/bpy_operator_wrap.c: convert non-string pyoperator exceptions into strings if they are not already .

Campbell Barton ideasman42 at gmail.com
Sat Jun 13 15:57:56 CEST 2009


Revision: 20855
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=20855
Author:   campbellbarton
Date:     2009-06-13 15:57:56 +0200 (Sat, 13 Jun 2009)

Log Message:
-----------
convert non-string pyoperator exceptions into strings if they are not already.

Modified Paths:
--------------
    branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c

Modified: branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c
===================================================================
--- branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c	2009-06-13 13:02:01 UTC (rev 20854)
+++ branches/blender2.5/blender/source/blender/python/intern/bpy_operator_wrap.c	2009-06-13 13:57:56 UTC (rev 20855)
@@ -140,12 +140,47 @@
 /* TODO - a whole traceback would be ideal */
 static void pyop_error_report(ReportList *reports)
 {
+	const char *string;
 	PyObject *exception, *v, *tb;
 	PyErr_Fetch(&exception, &v, &tb);
 	if (exception == NULL)
 		return;
+	
+	/* get the string from the exception */
+	if(v==NULL) {
+		string= "py exception not set";
+	}
+	else if(string = _PyUnicode_AsString(v)) {
+		/* do nothing */
+	}
+	else { /* a valid PyObject but not a string, try get its string value */
+		PyObject *repr;
+		
+		Py_INCREF(v); /* incase clearing the error below somehow frees this */
+		PyErr_Clear();
+		
+		repr= PyObject_Repr(v);
+		
+		if(repr==NULL) {
+			PyErr_Clear();
+			string= "py exception found but can't be converted";
+		}
+		else {
+			string = _PyUnicode_AsString(repr);
+			Py_DECREF(repr);
+			
+			if(string==NULL) { /* unlikely to happen */
+				PyErr_Clear();
+				string= "py exception found but can't be converted";
+			}
+		}
+		
+		Py_DECREF(v); /* finished dealing with v, PyErr_Clear isnt called anymore so can decref it */
+	}
+	/* done getting the string */
+	
 	/* Now we know v != NULL too */
-	BKE_report(reports, RPT_ERROR, _PyUnicode_AsString(v));
+	BKE_report(reports, RPT_ERROR, string);
 	
 	PyErr_Print();
 }





More information about the Bf-blender-cvs mailing list