[Bf-blender-cvs] [a63aa05] temp-pyapi-units: Modifications from Campbell's review, thanks! :)

Bastien Montagne noreply at git.blender.org
Mon Jun 16 20:25:50 CEST 2014


Commit: a63aa0524a2e7d0706983b5124d21fefc5b63c59
Author: Bastien Montagne
Date:   Mon Jun 16 16:57:27 2014 +0200
https://developer.blender.org/rBa63aa0524a2e7d0706983b5124d21fefc5b63c59

Modifications from Campbell's review, thanks! :)

===================================================================

M	source/blender/blenlib/BLI_string.h
M	source/blender/blenlib/intern/string.c
M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/intern/CMakeLists.txt
M	source/blender/python/intern/bpy_interface.c
D	source/blender/python/intern/bpy_interface.h
M	source/blender/python/intern/bpy_utils_units.c

===================================================================

diff --git a/source/blender/blenlib/BLI_string.h b/source/blender/blenlib/BLI_string.h
index 2cf9047..d3172fd 100644
--- a/source/blender/blenlib/BLI_string.h
+++ b/source/blender/blenlib/BLI_string.h
@@ -76,6 +76,8 @@ void BLI_ascii_strtolower(char *str, const size_t len) ATTR_NONNULL();
 void BLI_ascii_strtoupper(char *str, const size_t len) ATTR_NONNULL();
 int BLI_str_rstrip_float_zero(char *str, const char pad) ATTR_NONNULL();
 
+int BLI_str_index_in_array(const char *str, const char **str_array, const int len);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/blenlib/intern/string.c b/source/blender/blenlib/intern/string.c
index ab81c8f..8a4de00 100644
--- a/source/blender/blenlib/intern/string.c
+++ b/source/blender/blenlib/intern/string.c
@@ -639,3 +639,36 @@ int BLI_str_rstrip_float_zero(char *str, const char pad)
 
 	return totstrip;
 }
+
+/**
+ * Return index of a string in a string array.
+ * Note it accepts a 'null' len value (-1), in which case it expects str_array to be NULL terminated.
+ *
+ * \param str The string to find.
+ * \param str_array The array of strings, must be NULL-terminated if len is -1.
+ * \param len the length of the array, or -1 for a NULL-terminated array.
+ * \return The index of str in str_array if found, else -1.
+ */
+int BLI_str_index_in_array(const char *str, const char **str_array, const int len)
+{
+	int idx;
+	char **arr = str_array;
+
+	if (len == -1) {
+		for (idx = 0; *arr; ++arr, ++idx) {
+			if (STREQ(str, *arr)) {
+				break;
+			}
+		}
+		return *arr ? idx : -1;
+	}
+	else {
+		for (idx = 0; idx < len; ++arr, ++idx) {
+			if (STREQ(str, *arr)) {
+				break;
+			}
+		}
+		return (idx == len) ? -1 : idx;
+	}
+}
+
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index c000478..680bd73 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -905,3 +905,71 @@ PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag)
 
 	return ret;
 }
+
+
+/* return -1 on error, else 0
+ * Note it is caller's responsibility to acquire & release GIL!
+ */
+int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename)
+{
+	PyObject *py_dict, *mod, *retval;
+	int error_ret = 0;
+	PyObject *main_mod = NULL;
+
+	PyC_MainModule_Backup(&main_mod);
+
+	py_dict = PyC_DefaultNameSpace(filename);
+
+	mod = PyImport_ImportModule("math");
+	if (mod) {
+		PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
+		Py_DECREF(mod);
+	}
+	else { /* highly unlikely but possibly */
+		PyErr_Print();
+		PyErr_Clear();
+	}
+
+	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
+
+	if (retval == NULL) {
+		error_ret = -1;
+	}
+	else {
+		double val;
+
+		if (PyTuple_Check(retval)) {
+			/* Users my have typed in 10km, 2m
+			 * add up all values */
+			int i;
+			val = 0.0;
+
+			for (i = 0; i < PyTuple_GET_SIZE(retval); i++) {
+				const double val_item = PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
+				if (val_item == -1 && PyErr_Occurred()) {
+					val = -1;
+					break;
+				}
+				val += val_item;
+			}
+		}
+		else {
+			val = PyFloat_AsDouble(retval);
+		}
+		Py_DECREF(retval);
+
+		if (val == -1 && PyErr_Occurred()) {
+			error_ret = -1;
+		}
+		else if (!finite(val)) {
+			*value = 0.0;
+		}
+		else {
+			*value = val;
+		}
+	}
+
+	PyC_MainModule_Restore(main_mod);
+
+	return error_ret;
+}
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 0afc4dd..559a8e1 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -73,4 +73,6 @@ int       PyC_FlagSet_ValueFromID(PyC_FlagSet *item, const char *identifier, int
 int       PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_value, const char *error_prefix);
 PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag);
 
+int PyC_RunString_AsNumber(const char *expr, double *value, const char *filename);
+
 #endif  /* __PY_CAPI_UTILS_H__ */
diff --git a/source/blender/python/intern/CMakeLists.txt b/source/blender/python/intern/CMakeLists.txt
index d85ec18..70b4df7 100644
--- a/source/blender/python/intern/CMakeLists.txt
+++ b/source/blender/python/intern/CMakeLists.txt
@@ -81,7 +81,6 @@ set(SRC
 	bpy_app_oiio.h
 	bpy_app_translations.h
 	bpy_driver.h
-	bpy_interface.h
 	bpy_intern_string.h
 	bpy_library.h
 	bpy_operator.h
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index 69f376d..a29e7f6 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -59,7 +59,6 @@
 #include "bpy_path.h"
 #include "bpy_util.h"
 #include "bpy_traceback.h"
-#include "bpy_interface.h"
 #include "bpy_intern_string.h"
 
 #include "DNA_text_types.h"
@@ -567,73 +566,6 @@ void BPY_DECREF_RNA_INVALIDATE(void *pyob_ptr)
 	PyGILState_Release(gilstate);
 }
 
-/* return -1 on error, else 0
- * Note it is caller's responsibility to acquire & release GIL!
- */
-int bpy_button_exec(const char *expr, double *value)
-{
-	PyObject *py_dict, *mod, *retval;
-	int error_ret = 0;
-	PyObject *main_mod = NULL;
-
-	PyC_MainModule_Backup(&main_mod);
-
-	py_dict = PyC_DefaultNameSpace("<blender button>");
-
-	mod = PyImport_ImportModule("math");
-	if (mod) {
-		PyDict_Merge(py_dict, PyModule_GetDict(mod), 0); /* 0 - don't overwrite existing values */
-		Py_DECREF(mod);
-	}
-	else { /* highly unlikely but possibly */
-		PyErr_Print();
-		PyErr_Clear();
-	}
-
-	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
-
-	if (retval == NULL) {
-		error_ret = -1;
-	}
-	else {
-		double val;
-
-		if (PyTuple_Check(retval)) {
-			/* Users my have typed in 10km, 2m
-			 * add up all values */
-			int i;
-			val = 0.0;
-
-			for (i = 0; i < PyTuple_GET_SIZE(retval); i++) {
-				const double val_item = PyFloat_AsDouble(PyTuple_GET_ITEM(retval, i));
-				if (val_item == -1 && PyErr_Occurred()) {
-					val = -1;
-					break;
-				}
-				val += val_item;
-			}
-		}
-		else {
-			val = PyFloat_AsDouble(retval);
-		}
-		Py_DECREF(retval);
-
-		if (val == -1 && PyErr_Occurred()) {
-			error_ret = -1;
-		}
-		else if (!finite(val)) {
-			*value = 0.0;
-		}
-		else {
-			*value = val;
-		}
-	}
-
-	PyC_MainModule_Restore(main_mod);
-
-	return error_ret;
-}
-
 /* return -1 on error, else 0 */
 int BPY_button_exec(bContext *C, const char *expr, double *value, const bool verbose)
 {
@@ -649,7 +581,7 @@ int BPY_button_exec(bContext *C, const char *expr, double *value, const bool ver
 
 	bpy_context_set(C, &gilstate);
 
-	error_ret = bpy_button_exec(expr, value);
+	error_ret = PyC_RunString_AsNumber(expr, value, "<blender button>");
 
 	if (error_ret) {
 		if (verbose) {
diff --git a/source/blender/python/intern/bpy_interface.h b/source/blender/python/intern/bpy_interface.h
deleted file mode 100644
index 31e0ea2..0000000
--- a/source/blender/python/intern/bpy_interface.h
+++ /dev/null
@@ -1,32 +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): Bastien Montagne
- *
- * ***** END GPL LICENSE BLOCK *****
- */
-
-#ifndef __BPY_INTERFACE_H__
-#define __BPY_INTERFACE_H__
-
-/** \file blender/python/intern/bpy_interface.h
- *  \ingroup pythonintern
- */
-
-int bpy_button_exec(const char *expr, double *value);
-
-#endif  /* __BPY_INTERFACE_H__ */
diff --git a/source/blender/python/intern/bpy_utils_units.c b/source/blender/python/intern/bpy_utils_units.c
index e37c2ff..b78e035 100644
--- a/source/blender/python/intern/bpy_utils_units.c
+++ b/source/blender/python/intern/bpy_utils_units.c
@@ -40,7 +40,8 @@
 
 #include "BPY_extern.h"
 #include "bpy_utils_units.h"
-#include "bpy_interface.h"
+
+#include "../generic/py_capi_utils.h"
 
 #include "MEM_guardedalloc.h"
 
@@ -59,7 +60,7 @@ typedef struct
 } BlenderUtilsUnits;
 
 /* Our singleton instance pointer */
-static BlenderUtilsUnits *_units = NULL;
+static BlenderUtilsUnits *bpyunits_units = NULL;
 
 
 /***** C-defined systems and types *****/
@@ -68,14 +69,14 @@ static PyTypeObject BlenderUtilsUnitsSystemsType;
 static PyTypeObject BlenderUtilsUnitsCategoriesType;
 
 /* XXX Maybe better as externs of BKE_unit.h ? */
-static const char *_usystems[] = {
+static const char *bpyunits_usystems[] = {
 	"NONE",
 	"METRIC",
 	"IMPERIAL",
 	NULL,
 };
 
-static const char *_ucategories[] = {
+static const char *bpyunits_ucategories[] = {
 	"NONE",
 	"LENGTH",
 	"AREA",
@@ -93,8 +94,8 @@ static const char *_ucategories[] = {
  * This allows us to avoid many handwriting, and above all, to keep all systems/categories definition stuff in
  * BKE_unit.h!
  */
-static PyStructSequence_Field utils_units_systems_fields[sizeof(_usystems) / sizeof(*_usystems)] = {{NULL}};
-static PyStructSequence_Field utils_units_categories_fields[sizeof(_ucategories) / sizeof(*_ucategories)] = {{NULL}};
+static PyStructSequence_Field utils_units_systems_fields[ARRAY_SIZE(bpyunits_usystems)] = {{NULL}};
+static PyStructSequence_Field utils_units_categories_fields[ARRAY_SIZE(bpyunits_ucategories)] = {{NULL}};
 
 static PyStructSequence_Desc utils_units_systems_desc = {
 	(char *)"bpy.utils.units.systems",     /* name */
@@ -120,7 +121,7 @@ static PyObject *utils_units_systems_make(void)
 		return NULL;
 	}
 
-	for (sys = _usystems; *sys; sys++) {
+	for (sys = bpyunits_usystems; *sys; sys++) {
 		PyStructSequence_SET_ITEM(units_systems, pos++, PyUnicode_F

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list