Blender V2.61 - r43446

bpy_util.c

Go to the documentation of this file.
00001 /*
00002  * ***** BEGIN GPL LICENSE BLOCK *****
00003  *
00004  * This program is free software; you can redistribute it and/or
00005  * modify it under the terms of the GNU General Public License
00006  * as published by the Free Software Foundation; either version 2
00007  * of the License, or (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU General Public License
00015  * along with this program; if not, write to the Free Software Foundation,
00016  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017  *
00018  * Contributor(s): Campbell Barton
00019  *
00020  * ***** END GPL LICENSE BLOCK *****
00021  */
00022 
00030 #include <Python.h>
00031 
00032 #include "bpy_util.h"
00033 #include "BLI_dynstr.h"
00034 #include "MEM_guardedalloc.h"
00035 #include "BKE_report.h"
00036 #include "BKE_context.h"
00037 
00038 #include "../generic/py_capi_utils.h"
00039 
00040 static bContext*    __py_context = NULL;
00041 bContext*   BPy_GetContext(void) { return __py_context; }
00042 void        BPy_SetContext(bContext *C) { __py_context = C; }
00043 
00044 char *BPy_enum_as_string(EnumPropertyItem *item)
00045 {
00046     DynStr *dynstr = BLI_dynstr_new();
00047     EnumPropertyItem *e;
00048     char *cstring;
00049 
00050     for (e = item; item->identifier; item++) {
00051         if (item->identifier[0])
00052             BLI_dynstr_appendf(dynstr, (e == item)?"'%s'":", '%s'", item->identifier);
00053     }
00054 
00055     cstring = BLI_dynstr_get_cstring(dynstr);
00056     BLI_dynstr_free(dynstr);
00057     return cstring;
00058 }
00059 
00060 short BPy_reports_to_error(ReportList *reports, PyObject *exception, const short clear)
00061 {
00062     char *report_str;
00063 
00064     report_str = BKE_reports_string(reports, RPT_ERROR);
00065 
00066     if (clear) {
00067         BKE_reports_clear(reports);
00068     }
00069 
00070     if (report_str) {
00071         PyErr_SetString(exception, report_str);
00072         MEM_freeN(report_str);
00073     }
00074 
00075     return (report_str == NULL) ? 0 : -1;
00076 }
00077 
00078 
00079 short BPy_errors_to_report(ReportList *reports)
00080 {
00081     PyObject *pystring;
00082     PyObject *pystring_format = NULL; // workaround, see below
00083     char *cstring;
00084 
00085     const char *filename;
00086     int lineno;
00087 
00088     if (!PyErr_Occurred())
00089         return 1;
00090     
00091     /* less hassle if we allow NULL */
00092     if (reports == NULL) {
00093         PyErr_Print();
00094         PyErr_Clear();
00095         return 1;
00096     }
00097     
00098     pystring = PyC_ExceptionBuffer();
00099     
00100     if (pystring == NULL) {
00101         BKE_report(reports, RPT_ERROR, "unknown py-exception, couldn't convert");
00102         return 0;
00103     }
00104     
00105     PyC_FileAndNum(&filename, &lineno);
00106     if (filename == NULL)
00107         filename = "<unknown location>";
00108     
00109     cstring = _PyUnicode_AsString(pystring);
00110 
00111 #if 0 // ARG!. workaround for a bug in blenders use of vsnprintf
00112     BKE_reportf(reports, RPT_ERROR, "%s\nlocation:%s:%d\n", cstring, filename, lineno);
00113 #else
00114     pystring_format = PyUnicode_FromFormat("%s\nlocation:%s:%d\n", cstring, filename, lineno);
00115     cstring = _PyUnicode_AsString(pystring_format);
00116     BKE_report(reports, RPT_ERROR, cstring);
00117 #endif
00118     
00119     fprintf(stderr, "%s\nlocation:%s:%d\n", cstring, filename, lineno); // not exactly needed. just for testing
00120     
00121     Py_DECREF(pystring);
00122     Py_DECREF(pystring_format); // workaround
00123     return 1;
00124 }