[Bf-blender-cvs] [98800aa4e0f] master: C/Python API: Add PyC_RunString_AsIntPtr

Campbell Barton noreply at git.blender.org
Fri Aug 31 06:15:21 CEST 2018


Commit: 98800aa4e0f7b4aa169e799e804b1ba658be6e69
Author: Campbell Barton
Date:   Fri Aug 31 14:21:32 2018 +1000
Branches: master
https://developer.blender.org/rB98800aa4e0f7b4aa169e799e804b1ba658be6e69

C/Python API: Add PyC_RunString_AsIntPtr

Utility to get an int or pointer from a Python expression.

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

M	source/blender/python/BPY_extern.h
M	source/blender/python/generic/py_capi_utils.c
M	source/blender/python/generic/py_capi_utils.h
M	source/blender/python/intern/bpy_interface.c

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

diff --git a/source/blender/python/BPY_extern.h b/source/blender/python/BPY_extern.h
index b4c36a7c516..69a64793ba4 100644
--- a/source/blender/python/BPY_extern.h
+++ b/source/blender/python/BPY_extern.h
@@ -76,6 +76,7 @@ void BPY_thread_restore(BPy_ThreadStatePtr tstate);
 bool	BPY_execute_filepath(struct bContext *C, const char *filepath, struct ReportList *reports);
 bool	BPY_execute_text(struct bContext *C, struct Text *text, struct ReportList *reports, const bool do_jump);
 bool	BPY_execute_string_as_number(struct bContext *C, const char *expr, const bool verbose, double *r_value);
+bool	BPY_execute_string_as_intptr(struct bContext *C, const char *expr, const bool verbose, intptr_t *r_value);
 bool	BPY_execute_string_as_string(struct bContext *C, const char *expr, const bool verbose, char **r_value);
 bool	BPY_execute_string_ex(struct bContext *C, const char *expr, bool use_eval);
 bool	BPY_execute_string(struct bContext *C, const char *expr);
diff --git a/source/blender/python/generic/py_capi_utils.c b/source/blender/python/generic/py_capi_utils.c
index d2d78167fae..d8d6b5c7ce7 100644
--- a/source/blender/python/generic/py_capi_utils.c
+++ b/source/blender/python/generic/py_capi_utils.c
@@ -784,7 +784,7 @@ bool PyC_IsInterpreterActive(void)
 }
 
 /* Would be nice if python had this built in
- * See: http://wiki.blender.org/index.php/Dev:Doc/Tools/Debugging/PyFromC
+ * See: https://wiki.blender.org/wiki/Tools/Debugging/PyFromC
  */
 void PyC_RunQuicky(const char *filepath, int n, ...)
 {
@@ -1140,6 +1140,40 @@ bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_va
 	return ok;
 }
 
+bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_value)
+{
+	PyObject *py_dict, *retval;
+	bool ok = true;
+	PyObject *main_mod = NULL;
+
+	PyC_MainModule_Backup(&main_mod);
+
+	py_dict = PyC_DefaultNameSpace(filename);
+
+	retval = PyRun_String(expr, Py_eval_input, py_dict, py_dict);
+
+	if (retval == NULL) {
+		ok = false;
+	}
+	else {
+		intptr_t val;
+
+		val = (intptr_t)PyLong_AsVoidPtr(retval);
+		if (val == 0 && PyErr_Occurred()) {
+			ok = false;
+		}
+		else {
+			*r_value = val;
+		}
+
+		Py_DECREF(retval);
+	}
+
+	PyC_MainModule_Restore(main_mod);
+
+	return ok;
+}
+
 bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value)
 {
 	PyObject *py_dict, *retval;
diff --git a/source/blender/python/generic/py_capi_utils.h b/source/blender/python/generic/py_capi_utils.h
index 10419ce71d4..4c1f14f08da 100644
--- a/source/blender/python/generic/py_capi_utils.h
+++ b/source/blender/python/generic/py_capi_utils.h
@@ -102,6 +102,7 @@ int       PyC_FlagSet_ToBitfield(PyC_FlagSet *items, PyObject *value, int *r_val
 PyObject *PyC_FlagSet_FromBitfield(PyC_FlagSet *items, int flag);
 
 bool PyC_RunString_AsNumber(const char *expr, const char *filename, double *r_value);
+bool PyC_RunString_AsIntPtr(const char *expr, const char *filename, intptr_t *r_value);
 bool PyC_RunString_AsString(const char *expr, const char *filename, char **r_value);
 
 int PyC_ParseBool(PyObject *o, void *p);
diff --git a/source/blender/python/intern/bpy_interface.c b/source/blender/python/intern/bpy_interface.c
index c84765e9a47..66abc017fe1 100644
--- a/source/blender/python/intern/bpy_interface.c
+++ b/source/blender/python/intern/bpy_interface.c
@@ -655,6 +655,42 @@ bool BPY_execute_string_as_string(bContext *C, const char *expr, const bool verb
 	return ok;
 }
 
+/**
+ * Support both int and pointers.
+ *
+ * \return success
+ */
+bool BPY_execute_string_as_intptr(bContext *C, const char *expr, const bool verbose, intptr_t *r_value)
+{
+	PyGILState_STATE gilstate;
+	bool ok = true;
+
+	if (!r_value || !expr) {
+		return -1;
+	}
+
+	if (expr[0] == '\0') {
+		*r_value = 0;
+		return ok;
+	}
+
+	bpy_context_set(C, &gilstate);
+
+	ok = PyC_RunString_AsIntPtr(expr, "<blender button>", r_value);
+
+	if (ok == false) {
+		if (verbose) {
+			BPy_errors_to_report_ex(CTX_wm_reports(C), false, false);
+		}
+		else {
+			PyErr_Clear();
+		}
+	}
+
+	bpy_context_clear(C, &gilstate);
+
+	return ok;
+}
 
 bool BPY_execute_string_ex(bContext *C, const char *expr, bool use_eval)
 {



More information about the Bf-blender-cvs mailing list