From e32cfb0743d9b4da08403f9fd33abd1cadbd0299 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Wed, 29 Nov 2017 21:11:29 +1100 Subject: Cleanup: rename bpy_util -> bpy_capi_utils This is for internal CAPI use only, avoid confusion w/ bpy.utils module. --- .../freestyle/intern/system/PythonInterpreter.h | 2 +- source/blender/python/intern/CMakeLists.txt | 10 +- source/blender/python/intern/bpy.c | 2 +- source/blender/python/intern/bpy_capi_utils.c | 151 +++++++++++++++++++++ source/blender/python/intern/bpy_capi_utils.h | 53 ++++++++ source/blender/python/intern/bpy_interface.c | 2 +- .../blender/python/intern/bpy_interface_atexit.c | 2 +- source/blender/python/intern/bpy_library_load.c | 2 +- source/blender/python/intern/bpy_library_write.c | 2 +- source/blender/python/intern/bpy_operator.c | 2 +- source/blender/python/intern/bpy_props.c | 2 +- source/blender/python/intern/bpy_rna.c | 2 +- source/blender/python/intern/bpy_rna_anim.c | 2 +- source/blender/python/intern/bpy_rna_callback.c | 2 +- .../blender/python/intern/bpy_rna_id_collection.c | 2 +- source/blender/python/intern/bpy_util.c | 151 --------------------- source/blender/python/intern/bpy_util.h | 53 -------- 17 files changed, 221 insertions(+), 221 deletions(-) create mode 100644 source/blender/python/intern/bpy_capi_utils.c create mode 100644 source/blender/python/intern/bpy_capi_utils.h delete mode 100644 source/blender/python/intern/bpy_util.c delete mode 100644 source/blender/python/intern/bpy_util.h (limited to 'source/blender') diff --git a/source/blender/freestyle/intern/system/PythonInterpreter.h b/source/blender/freestyle/intern/system/PythonInterpreter.h index 0e08af1bac8..79ba9421cda 100644 --- a/source/blender/freestyle/intern/system/PythonInterpreter.h +++ b/source/blender/freestyle/intern/system/PythonInterpreter.h @@ -52,7 +52,7 @@ extern "C" { #include "BPY_extern.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" } namespace Freestyle { diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt index dd84ce0ebe1..cdbf65546d3 100644 --- a/source/blender/python/intern/CMakeLists.txt +++ b/source/blender/python/intern/CMakeLists.txt @@ -45,8 +45,6 @@ set(INC_SYS ) set(SRC - gpu.c - gpu_offscreen.c bpy.c bpy_app.c bpy_app_alembic.c @@ -59,6 +57,7 @@ set(SRC bpy_app_openvdb.c bpy_app_sdl.c bpy_app_translations.c + bpy_capi_utils.c bpy_driver.c bpy_interface.c bpy_interface_atexit.c @@ -76,12 +75,12 @@ set(SRC bpy_rna_driver.c bpy_rna_id_collection.c bpy_traceback.c - bpy_util.c bpy_utils_previews.c bpy_utils_units.c + gpu.c + gpu_offscreen.c stubs.c - gpu.h bpy.h bpy_app.h bpy_app_alembic.h @@ -94,6 +93,7 @@ set(SRC bpy_app_openvdb.h bpy_app_sdl.h bpy_app_translations.h + bpy_capi_utils.h bpy_driver.h bpy_intern_string.h bpy_library.h @@ -107,9 +107,9 @@ set(SRC bpy_rna_driver.h bpy_rna_id_collection.h bpy_traceback.h - bpy_util.h bpy_utils_previews.h bpy_utils_units.h + gpu.h ../BPY_extern.h ) diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c index 6e4a6148405..7150cc65942 100644 --- a/source/blender/python/intern/bpy.c +++ b/source/blender/python/intern/bpy.c @@ -42,7 +42,7 @@ #include "RNA_access.h" #include "bpy.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_rna.h" #include "bpy_app.h" #include "bpy_rna_id_collection.h" diff --git a/source/blender/python/intern/bpy_capi_utils.c b/source/blender/python/intern/bpy_capi_utils.c new file mode 100644 index 00000000000..7232e4ea821 --- /dev/null +++ b/source/blender/python/intern/bpy_capi_utils.c @@ -0,0 +1,151 @@ +/* + * ***** 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, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/python/intern/bpy_capi_utils.c + * \ingroup pythonintern + * + * This file contains Blender/Python utility functions to help implementing API's. + * This is not related to a particular module. + */ + +#include + +#include "BLI_utildefines.h" +#include "BLI_dynstr.h" + +#include "bpy_capi_utils.h" + +#include "MEM_guardedalloc.h" + +#include "BKE_report.h" +#include "BKE_context.h" + +#include "BLT_translation.h" + +#include "../generic/py_capi_utils.h" + +static bContext *__py_context = NULL; +bContext *BPy_GetContext(void) { return __py_context; } +void BPy_SetContext(bContext *C) { __py_context = C; } + +char *BPy_enum_as_string(const EnumPropertyItem *item) +{ + DynStr *dynstr = BLI_dynstr_new(); + const EnumPropertyItem *e; + char *cstring; + + for (e = item; item->identifier; item++) { + if (item->identifier[0]) + BLI_dynstr_appendf(dynstr, (e == item) ? "'%s'" : ", '%s'", item->identifier); + } + + cstring = BLI_dynstr_get_cstring(dynstr); + BLI_dynstr_free(dynstr); + return cstring; +} + +short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear) +{ + char *report_str; + + report_str = BKE_reports_string(reports, RPT_ERROR); + + if (clear == true) { + BKE_reports_clear(reports); + } + + if (report_str) { + PyErr_SetString(exception, report_str); + MEM_freeN(report_str); + } + + return (report_str == NULL) ? 0 : -1; +} + + +bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const bool use_location) +{ + PyObject *pystring; + + if (!PyErr_Occurred()) + return 1; + + /* less hassle if we allow NULL */ + if (reports == NULL) { + PyErr_Print(); + PyErr_Clear(); + return 1; + } + + if (use_full) { + pystring = PyC_ExceptionBuffer(); + } + else { + pystring = PyC_ExceptionBuffer_Simple(); + } + + if (pystring == NULL) { + BKE_report(reports, RPT_ERROR, "Unknown py-exception, could not convert"); + return 0; + } + + if (use_location) { + const char *filename; + int lineno; + + PyObject *pystring_format; /* workaround, see below */ + const char *cstring; + + PyC_FileAndNum(&filename, &lineno); + if (filename == NULL) { + filename = ""; + } + +#if 0 /* ARG!. workaround for a bug in blenders use of vsnprintf */ + BKE_reportf(reports, RPT_ERROR, "%s\nlocation: %s:%d\n", _PyUnicode_AsString(pystring), filename, lineno); +#else + pystring_format = PyUnicode_FromFormat( + TIP_("%s\nlocation: %s:%d\n"), + _PyUnicode_AsString(pystring), filename, lineno); + + cstring = _PyUnicode_AsString(pystring_format); + BKE_report(reports, RPT_ERROR, cstring); + + /* not exactly needed. just for testing */ + fprintf(stderr, TIP_("%s\nlocation: %s:%d\n"), cstring, filename, lineno); + + Py_DECREF(pystring_format); /* workaround */ +#endif + } + else { + BKE_report(reports, RPT_ERROR, _PyUnicode_AsString(pystring)); + } + + + Py_DECREF(pystring); + return 1; +} + +bool BPy_errors_to_report(ReportList *reports) +{ + return BPy_errors_to_report_ex(reports, true, true); +} diff --git a/source/blender/python/intern/bpy_capi_utils.h b/source/blender/python/intern/bpy_capi_utils.h new file mode 100644 index 00000000000..171c46d8e86 --- /dev/null +++ b/source/blender/python/intern/bpy_capi_utils.h @@ -0,0 +1,53 @@ +/* + * ***** 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, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + * Contributor(s): Campbell Barton + * + * ***** END GPL LICENSE BLOCK ***** + */ + +/** \file blender/python/intern/bpy_capi_utils.h + * \ingroup pythonintern + */ + +#ifndef __BPY_UTIL_H__ +#define __BPY_UTIL_H__ + +#if PY_VERSION_HEX < 0x03060000 +# error "Python 3.6 or greater is required, you'll need to update your python." +#endif + +struct EnumPropertyItem; +struct ReportList; + +char *BPy_enum_as_string(const struct EnumPropertyItem *item); + +#define BLANK_PYTHON_TYPE {PyVarObject_HEAD_INIT(NULL, 0) NULL} + +/* error reporting */ +short BPy_reports_to_error(struct ReportList *reports, PyObject *exception, const bool clear); +bool BPy_errors_to_report_ex(struct ReportList *reports, const bool use_full, const bool use_location); +bool BPy_errors_to_report(struct ReportList *reports); + +/* TODO - find a better solution! */ +struct bContext *BPy_GetContext(void); +void BPy_SetContext(struct bContext *C); + +extern void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate); +extern void bpy_context_clear(struct bContext *C, PyGILState_STATE *gilstate); + +#endif /* __BPY_UTIL_H__ */ diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c index 20cfd364a0c..ae8e35ae3fc 100644 --- a/source/blender/python/intern/bpy_interface.c +++ b/source/blender/python/intern/bpy_interface.c @@ -47,7 +47,7 @@ #include "gpu.h" #include "bpy_rna.h" #include "bpy_path.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_traceback.h" #include "bpy_intern_string.h" diff --git a/source/blender/python/intern/bpy_interface_atexit.c b/source/blender/python/intern/bpy_interface_atexit.c index 5c4f6ba327c..3608a26d113 100644 --- a/source/blender/python/intern/bpy_interface_atexit.c +++ b/source/blender/python/intern/bpy_interface_atexit.c @@ -33,7 +33,7 @@ #include "BLI_utildefines.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy.h" /* own include */ #include "WM_api.h" diff --git a/source/blender/python/intern/bpy_library_load.c b/source/blender/python/intern/bpy_library_load.c index 39fce293bd0..20226ffe6a3 100644 --- a/source/blender/python/intern/bpy_library_load.c +++ b/source/blender/python/intern/bpy_library_load.c @@ -48,7 +48,7 @@ #include "DNA_space_types.h" /* FILE_LINK, FILE_RELPATH */ -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_library.h" #include "../generic/py_capi_utils.h" diff --git a/source/blender/python/intern/bpy_library_write.c b/source/blender/python/intern/bpy_library_write.c index c054183034a..9ca6092eab3 100644 --- a/source/blender/python/intern/bpy_library_write.c +++ b/source/blender/python/intern/bpy_library_write.c @@ -43,7 +43,7 @@ #include "RNA_types.h" #include "bpy_rna.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_library.h" #include "../generic/py_capi_utils.h" diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c index c1fcb0792af..5b84a7cf73c 100644 --- a/source/blender/python/intern/bpy_operator.c +++ b/source/blender/python/intern/bpy_operator.c @@ -43,7 +43,7 @@ #include "bpy_operator.h" #include "bpy_operator_wrap.h" #include "bpy_rna.h" /* for setting arg props only - pyrna_py_to_prop() */ -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "../generic/bpy_internal_import.h" #include "../generic/py_capi_utils.h" #include "../generic/python_utildefines.h" diff --git a/source/blender/python/intern/bpy_props.c b/source/blender/python/intern/bpy_props.c index abeaeb27d88..d3eddff4b9a 100644 --- a/source/blender/python/intern/bpy_props.c +++ b/source/blender/python/intern/bpy_props.c @@ -37,7 +37,7 @@ #include "bpy_props.h" #include "bpy_rna.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "BKE_idprop.h" diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c index d17117a4e37..1a9fd920af5 100644 --- a/source/blender/python/intern/bpy_rna.c +++ b/source/blender/python/intern/bpy_rna.c @@ -49,7 +49,7 @@ #include "bpy_rna.h" #include "bpy_rna_anim.h" #include "bpy_props.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_rna_callback.h" #include "bpy_intern_string.h" diff --git a/source/blender/python/intern/bpy_rna_anim.c b/source/blender/python/intern/bpy_rna_anim.c index 8a0130babd5..9f9bb46c8bb 100644 --- a/source/blender/python/intern/bpy_rna_anim.c +++ b/source/blender/python/intern/bpy_rna_anim.c @@ -53,7 +53,7 @@ #include "WM_types.h" #include "bpy_rna.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_rna_anim.h" #include "../generic/python_utildefines.h" diff --git a/source/blender/python/intern/bpy_rna_callback.c b/source/blender/python/intern/bpy_rna_callback.c index fe3565fc44e..68783feacd9 100644 --- a/source/blender/python/intern/bpy_rna_callback.c +++ b/source/blender/python/intern/bpy_rna_callback.c @@ -36,7 +36,7 @@ #include "bpy_rna.h" #include "bpy_rna_callback.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "DNA_space_types.h" #include "DNA_screen_types.h" diff --git a/source/blender/python/intern/bpy_rna_id_collection.c b/source/blender/python/intern/bpy_rna_id_collection.c index 8def52dc8fb..ee40d30d73b 100644 --- a/source/blender/python/intern/bpy_rna_id_collection.c +++ b/source/blender/python/intern/bpy_rna_id_collection.c @@ -44,7 +44,7 @@ #include "DNA_object_types.h" #include "DNA_key_types.h" -#include "bpy_util.h" +#include "bpy_capi_utils.h" #include "bpy_rna_id_collection.h" #include "../generic/py_capi_utils.h" diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c deleted file mode 100644 index c15ff50df04..00000000000 --- a/source/blender/python/intern/bpy_util.c +++ /dev/null @@ -1,151 +0,0 @@ -/* - * ***** 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, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * Contributor(s): Campbell Barton - * - * ***** END GPL LICENSE BLOCK ***** - */ - -/** \file blender/python/intern/bpy_util.c - * \ingroup pythonintern - * - * This file contains blender/python utility functions for the api's internal - * use (unrelated to 'bpy.utils') - */ - -#include - -#include "BLI_utildefines.h" -#include "BLI_dynstr.h" - -#include "bpy_util.h" - -#include "MEM_guardedalloc.h" - -#include "BKE_report.h" -#include "BKE_context.h" - -#include "BLT_translation.h" - -#include "../generic/py_capi_utils.h" - -static bContext *__py_context = NULL; -bContext *BPy_GetContext(void) { return __py_context; } -void BPy_SetContext(bContext *C) { __py_context = C; } - -char *BPy_enum_as_string(const EnumPropertyItem *item) -{ - DynStr *dynstr = BLI_dynstr_new(); - const EnumPropertyItem *e; - char *cstring; - - for (e = item; item->identifier; item++) { - if (item->identifier[0]) - BLI_dynstr_appendf(dynstr, (e == item) ? "'%s'" : ", '%s'", item->identifier); - } - - cstring = BLI_dynstr_get_cstring(dynstr); - BLI_dynstr_free(dynstr); - return cstring; -} - -short BPy_reports_to_error(ReportList *reports, PyObject *exception, const bool clear) -{ - char *report_str; - - report_str = BKE_reports_string(reports, RPT_ERROR); - - if (clear == true) { - BKE_reports_clear(reports); - } - - if (report_str) { - PyErr_SetString(exception, report_str); - MEM_freeN(report_str); - } - - return (report_str == NULL) ? 0 : -1; -} - - -bool BPy_errors_to_report_ex(ReportList *reports, const bool use_full, const bool use_location) -{ - PyObject *pystring; - - if (!PyErr_Occurred()) - return 1; - - /* less hassle if we allow NULL */ - if (reports == NULL) { - PyErr_Print(); - PyErr_Clear(); - return 1; - } - - if (use_full) { - pystring = PyC_ExceptionBuffer(); - } - else { - pystring = PyC_ExceptionBuffer_Simple(); - } - - if (pystring == NULL) { - BKE_report(reports, RPT_ERROR, "Unknown py-exception, could not convert"); - return 0; - } - - if (use_location) { - const char *filename; - int lineno; - - PyObject *pystring_format; /* workaround, see below */ - const char *cstring; - - PyC_FileAndNum(&filename, &lineno); - if (filename == NULL) { - filename = ""; - } - -#if 0 /* ARG!. workaround for a bug in blenders use of vsnprintf */ - BKE_reportf(reports, RPT_ERROR, "%s\nlocation: %s:%d\n", _PyUnicode_AsString(pystring), filename, lineno); -#else - pystring_format = PyUnicode_FromFormat( - TIP_("%s\nlocation: %s:%d\n"), - _PyUnicode_AsString(pystring), filename, lineno); - - cstring = _PyUnicode_AsString(pystring_format); - BKE_report(reports, RPT_ERROR, cstring); - - /* not exactly needed. just for testing */ - fprintf(stderr, TIP_("%s\nlocation: %s:%d\n"), cstring, filename, lineno); - - Py_DECREF(pystring_format); /* workaround */ -#endif - } - else { - BKE_report(reports, RPT_ERROR, _PyUnicode_AsString(pystring)); - } - - - Py_DECREF(pystring); - return 1; -} - -bool BPy_errors_to_report(ReportList *reports) -{ - return BPy_errors_to_report_ex(reports, true, true); -} diff --git a/source/blender/python/intern/bpy_util.h b/source/blender/python/intern/bpy_util.h deleted file mode 100644 index 466941359a8..00000000000 --- a/source/blender/python/intern/bpy_util.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * ***** 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, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - * - * Contributor(s): Campbell Barton - * - * ***** END GPL LICENSE BLOCK ***** - */ - -/** \file blender/python/intern/bpy_util.h - * \ingroup pythonintern - */ - -#ifndef __BPY_UTIL_H__ -#define __BPY_UTIL_H__ - -#if PY_VERSION_HEX < 0x03060000 -# error "Python 3.6 or greater is required, you'll need to update your python." -#endif - -struct EnumPropertyItem; -struct ReportList; - -char *BPy_enum_as_string(const struct EnumPropertyItem *item); - -#define BLANK_PYTHON_TYPE {PyVarObject_HEAD_INIT(NULL, 0) NULL} - -/* error reporting */ -short BPy_reports_to_error(struct ReportList *reports, PyObject *exception, const bool clear); -bool BPy_errors_to_report_ex(struct ReportList *reports, const bool use_full, const bool use_location); -bool BPy_errors_to_report(struct ReportList *reports); - -/* TODO - find a better solution! */ -struct bContext *BPy_GetContext(void); -void BPy_SetContext(struct bContext *C); - -extern void bpy_context_set(struct bContext *C, PyGILState_STATE *gilstate); -extern void bpy_context_clear(struct bContext *C, PyGILState_STATE *gilstate); - -#endif /* __BPY_UTIL_H__ */ -- cgit v1.2.3