2011-02-23 10:52:22 +00:00
|
|
|
/*
|
2008-12-28 08:41:49 +00:00
|
|
|
* ***** BEGIN GPL LICENSE BLOCK *****
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software Foundation,
|
2010-02-12 13:34:04 +00:00
|
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
2008-12-28 08:41:49 +00:00
|
|
|
*
|
|
|
|
* Contributor(s): Campbell Barton
|
|
|
|
*
|
|
|
|
* ***** END GPL LICENSE BLOCK *****
|
|
|
|
*/
|
|
|
|
|
2011-02-27 20:10:08 +00:00
|
|
|
/** \file blender/python/intern/bpy_util.c
|
|
|
|
* \ingroup pythonintern
|
2011-11-05 08:21:12 +00:00
|
|
|
*
|
|
|
|
* This file contains blender/python utility functions for the api's internal
|
|
|
|
* use (unrelated to 'bpy.utils')
|
2011-02-27 20:10:08 +00:00
|
|
|
*/
|
|
|
|
|
2011-02-14 04:15:25 +00:00
|
|
|
#include <Python.h>
|
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
#include "BLI_utildefines.h"
|
2008-12-28 08:41:49 +00:00
|
|
|
#include "BLI_dynstr.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
|
|
|
|
#include "bpy_util.h"
|
|
|
|
|
2008-12-28 08:41:49 +00:00
|
|
|
#include "MEM_guardedalloc.h"
|
2013-01-07 05:26:12 +00:00
|
|
|
|
2009-04-19 13:37:59 +00:00
|
|
|
#include "BKE_report.h"
|
2010-09-01 14:13:48 +00:00
|
|
|
#include "BKE_context.h"
|
2008-12-28 08:41:49 +00:00
|
|
|
|
2015-08-16 17:32:01 +10:00
|
|
|
#include "BLT_translation.h"
|
2012-10-14 15:29:09 +00:00
|
|
|
|
2010-09-01 14:13:48 +00:00
|
|
|
#include "../generic/py_capi_utils.h"
|
2009-05-25 13:48:44 +00:00
|
|
|
|
2012-09-30 06:12:47 +00:00
|
|
|
static bContext *__py_context = NULL;
|
2012-03-26 20:41:54 +00:00
|
|
|
bContext *BPy_GetContext(void) { return __py_context; }
|
|
|
|
void BPy_SetContext(bContext *C) { __py_context = C; }
|
2009-05-25 13:48:44 +00:00
|
|
|
|
2017-10-18 15:07:26 +11:00
|
|
|
char *BPy_enum_as_string(const EnumPropertyItem *item)
|
2009-04-01 12:43:07 +00:00
|
|
|
{
|
2011-12-26 12:26:11 +00:00
|
|
|
DynStr *dynstr = BLI_dynstr_new();
|
2017-10-18 15:07:26 +11:00
|
|
|
const EnumPropertyItem *e;
|
2009-04-01 12:43:07 +00:00
|
|
|
char *cstring;
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
for (e = item; item->identifier; item++) {
|
2011-10-13 01:29:08 +00:00
|
|
|
if (item->identifier[0])
|
2012-03-26 20:41:54 +00:00
|
|
|
BLI_dynstr_appendf(dynstr, (e == item) ? "'%s'" : ", '%s'", item->identifier);
|
2009-04-01 12:43:07 +00:00
|
|
|
}
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
cstring = BLI_dynstr_get_cstring(dynstr);
|
2009-04-01 12:43:07 +00:00
|
|
|
BLI_dynstr_free(dynstr);
|
|
|
|
return cstring;
|
|
|
|
}
|
2009-04-19 13:37:59 +00:00
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear)
|
2009-04-19 13:37:59 +00:00
|
|
|
{
|
|
|
|
char *report_str;
|
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
report_str = BKE_reports_string(reports, RPT_ERROR);
|
2009-04-19 13:37:59 +00:00
|
|
|
|
2013-01-07 05:26:12 +00:00
|
|
|
if (clear == true) {
|
2010-12-31 05:40:30 +00:00
|
|
|
BKE_reports_clear(reports);
|
|
|
|
}
|
|
|
|
|
2011-10-13 01:29:08 +00:00
|
|
|
if (report_str) {
|
2011-03-12 15:18:08 +00:00
|
|
|
PyErr_SetString(exception, report_str);
|
2009-04-19 13:37:59 +00:00
|
|
|
MEM_freeN(report_str);
|
|
|
|
}
|
|
|
|
|
2011-03-12 15:18:08 +00:00
|
|
|
return (report_str == NULL) ? 0 : -1;
|
2009-04-19 13:37:59 +00:00
|
|
|
}
|
|
|
|
|
2009-06-14 12:53:47 +00:00
|
|
|
|
2015-05-18 09:12:26 +10:00
|
|
|
bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const bool use_location)
|
2009-06-14 12:53:47 +00:00
|
|
|
{
|
|
|
|
PyObject *pystring;
|
2009-11-04 22:36:46 +00:00
|
|
|
|
2009-06-14 12:53:47 +00:00
|
|
|
if (!PyErr_Occurred())
|
|
|
|
return 1;
|
|
|
|
|
|
|
|
/* less hassle if we allow NULL */
|
2011-12-26 12:26:11 +00:00
|
|
|
if (reports == NULL) {
|
2009-06-14 12:53:47 +00:00
|
|
|
PyErr_Print();
|
|
|
|
PyErr_Clear();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-05-18 09:12:26 +10:00
|
|
|
if (use_full) {
|
|
|
|
pystring = PyC_ExceptionBuffer();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
pystring = PyC_ExceptionBuffer_Simple();
|
|
|
|
}
|
2009-06-14 12:53:47 +00:00
|
|
|
|
2011-12-26 12:26:11 +00:00
|
|
|
if (pystring == NULL) {
|
2012-10-26 17:32:50 +00:00
|
|
|
BKE_report(reports, RPT_ERROR, "Unknown py-exception, could not convert");
|
2009-06-14 12:53:47 +00:00
|
|
|
return 0;
|
|
|
|
}
|
2015-05-18 09:12:26 +10:00
|
|
|
|
|
|
|
if (use_location) {
|
2015-05-26 21:36:11 +10:00
|
|
|
const char *filename;
|
|
|
|
int lineno;
|
|
|
|
|
2015-05-18 09:12:26 +10:00
|
|
|
PyObject *pystring_format; /* workaround, see below */
|
2015-06-03 12:36:32 +10:00
|
|
|
const char *cstring;
|
2015-05-18 09:12:26 +10:00
|
|
|
|
|
|
|
PyC_FileAndNum(&filename, &lineno);
|
|
|
|
if (filename == NULL) {
|
|
|
|
filename = "<unknown location>";
|
|
|
|
}
|
|
|
|
|
2012-10-15 09:11:17 +00:00
|
|
|
#if 0 /* ARG!. workaround for a bug in blenders use of vsnprintf */
|
2015-06-03 17:08:28 +10:00
|
|
|
BKE_reportf(reports, RPT_ERROR, "%s\nlocation: %s:%d\n", _PyUnicode_AsString(pystring), filename, lineno);
|
2009-11-23 17:36:44 +00:00
|
|
|
#else
|
2015-06-03 17:08:28 +10:00
|
|
|
pystring_format = PyUnicode_FromFormat(
|
|
|
|
TIP_("%s\nlocation: %s:%d\n"),
|
|
|
|
_PyUnicode_AsString(pystring), filename, lineno);
|
|
|
|
|
2015-05-18 09:12:26 +10:00
|
|
|
cstring = _PyUnicode_AsString(pystring_format);
|
|
|
|
BKE_report(reports, RPT_ERROR, cstring);
|
2015-05-26 21:36:11 +10:00
|
|
|
|
|
|
|
/* not exactly needed. just for testing */
|
|
|
|
fprintf(stderr, TIP_("%s\nlocation: %s:%d\n"), cstring, filename, lineno);
|
2015-06-03 12:36:32 +10:00
|
|
|
|
|
|
|
Py_DECREF(pystring_format); /* workaround */
|
|
|
|
#endif
|
2015-05-18 09:12:26 +10:00
|
|
|
}
|
|
|
|
else {
|
2015-06-03 12:36:32 +10:00
|
|
|
BKE_report(reports, RPT_ERROR, _PyUnicode_AsString(pystring));
|
2015-05-18 09:12:26 +10:00
|
|
|
}
|
2012-10-15 09:11:17 +00:00
|
|
|
|
|
|
|
|
2009-06-14 12:53:47 +00:00
|
|
|
Py_DECREF(pystring);
|
|
|
|
return 1;
|
|
|
|
}
|
2015-05-18 09:12:26 +10:00
|
|
|
|
|
|
|
bool BPy_errors_to_report(ReportList *reports)
|
|
|
|
{
|
|
|
|
return BPy_errors_to_report_ex(reports, true, true);
|
2016-05-16 00:48:02 +02:00
|
|
|
}
|