[Bf-blender-cvs] [f3c5b0394f0] master: IDProp API: expose repr utility function

Campbell Barton noreply at git.blender.org
Fri May 4 07:29:17 CEST 2018


Commit: f3c5b0394f095bb017c19c5a945c8e7714205bf2
Author: Campbell Barton
Date:   Fri May 4 07:26:42 2018 +0200
Branches: master
https://developer.blender.org/rBf3c5b0394f095bb017c19c5a945c8e7714205bf2

IDProp API: expose repr utility function

Useful for logging properties passed to operators.

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

M	source/blender/blenkernel/BKE_idprop.h
M	source/blender/blenkernel/intern/idprop.c
M	source/blender/python/generic/idprop_py_api.c
M	source/blender/windowmanager/intern/wm_keymap.c

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

diff --git a/source/blender/blenkernel/BKE_idprop.h b/source/blender/blenkernel/BKE_idprop.h
index 5d8cd02756d..48a5db93504 100644
--- a/source/blender/blenkernel/BKE_idprop.h
+++ b/source/blender/blenkernel/BKE_idprop.h
@@ -145,9 +145,8 @@ void IDP_RelinkProperty(struct IDProperty *prop);
 #  define IDP_Id(prop)               ((ID *) (prop)->data.pointer)
 #endif
 
-#ifndef NDEBUG
-/* for printout only */
-void IDP_spit(IDProperty *prop);
-#endif
+/* for printout/logging only */
+char *IDP_reprN(const struct IDProperty *prop);
+void  IDP_print(const struct IDProperty *prop);
 
 #endif /* __BKE_IDPROP_H__ */
diff --git a/source/blender/blenkernel/intern/idprop.c b/source/blender/blenkernel/intern/idprop.c
index 5c13ba7907d..a224ef1e212 100644
--- a/source/blender/blenkernel/intern/idprop.c
+++ b/source/blender/blenkernel/intern/idprop.c
@@ -832,9 +832,9 @@ bool IDP_EqualsProperties_ex(IDProperty *prop1, IDProperty *prop2, const bool is
 				if ((p1 != p2) && ((fabsf(p1 - p2) / max_ff(p1, p2)) < 0.001f)) {
 					printf("WARNING: Comparing two float properties that have nearly the same value (%f vs. %f)\n", p1, p2);
 					printf("    p1: ");
-					IDP_spit(prop1);
+					IDP_print(prop1);
 					printf("    p2: ");
-					IDP_spit(prop2);
+					IDP_print(prop2);
 				}
 			}
 #endif
@@ -1069,3 +1069,18 @@ void IDP_ClearProperty(IDProperty *prop)
 }
 
 /** \} */
+
+/* We could write a C version, see: idprop_py_api.c */
+#ifndef WITH_PYTHON
+char *IDP_reprN(IDProperty *UNUSED(prop))
+{
+	return BLI_strdup("<unsupported>");
+}
+
+void IDP_print(IDProperty *prop)
+{
+	char *repr = IDP_reprN(prop);
+	printf("IDProperty(%p): %s\n", prop, repr);
+	MEM_freeN(repr);
+}
+#endif /* WITH_PYTHON */
diff --git a/source/blender/python/generic/idprop_py_api.c b/source/blender/python/generic/idprop_py_api.c
index 1153e0176df..164fe656129 100644
--- a/source/blender/python/generic/idprop_py_api.c
+++ b/source/blender/python/generic/idprop_py_api.c
@@ -30,6 +30,7 @@
 #include "MEM_guardedalloc.h"
 
 #include "BLI_utildefines.h"
+#include "BLI_string.h"
 
 #include "idprop_py_api.h"
 
@@ -1804,39 +1805,48 @@ PyObject *BPyInit_idprop(void)
 	return mod;
 }
 
-
-#ifndef NDEBUG
 /* -------------------------------------------------------------------- */
 /* debug only function */
 
-void IDP_spit(IDProperty *prop)
+char *IDP_reprN(const IDProperty *prop)
 {
-	if (prop) {
-		PyGILState_STATE gilstate;
-		bool use_gil = true; /* !PyC_IsInterpreterActive(); */
-		PyObject *ret_dict;
-		PyObject *ret_str;
+	if (prop == NULL) {
+		return BLI_strdup("None");
+	}
 
-		if (use_gil) {
-			gilstate = PyGILState_Ensure();
-		}
+	PyGILState_STATE gilstate;
+	bool use_gil = true; /* !PyC_IsInterpreterActive(); */
+	PyObject *ret_dict;
+	PyObject *ret_str;
 
-		/* to_dict() */
-		ret_dict = BPy_IDGroup_MapDataToPy(prop);
-		ret_str = PyObject_Repr(ret_dict);
-		Py_DECREF(ret_dict);
+	if (use_gil) {
+		gilstate = PyGILState_Ensure();
+	}
 
-		printf("IDProperty(%p): %s\n", prop, _PyUnicode_AsString(ret_str));
+	/* Note: non-const cast is safe here since we only repr the result. */
+	/* to_dict() */
+	ret_dict = BPy_IDGroup_MapDataToPy((IDProperty *)prop);
+	ret_str = PyObject_Repr(ret_dict);
+	Py_DECREF(ret_dict);
 
-		Py_DECREF(ret_str);
+	Py_ssize_t res_str_len = 0;
+	char *res_str_bytes = _PyUnicode_AsStringAndSize(ret_str, &res_str_len);
 
-		if (use_gil) {
-			PyGILState_Release(gilstate);
-		}
-	}
-	else {
-		printf("IDProperty: <NIL>\n");
+	res_str_bytes = BLI_strdupn(res_str_bytes, res_str_len);
+
+	Py_DECREF(ret_str);
+
+	if (use_gil) {
+		PyGILState_Release(gilstate);
 	}
+	return res_str_bytes;
+}
+
+
+void IDP_print(const IDProperty *prop)
+{
+	char *repr = IDP_reprN(prop);
+	printf("IDProperty(%p): %s\n", prop, repr);
+	MEM_freeN(repr);
 }
 
-#endif
diff --git a/source/blender/windowmanager/intern/wm_keymap.c b/source/blender/windowmanager/intern/wm_keymap.c
index bcfc97a1e23..39dd26339eb 100644
--- a/source/blender/windowmanager/intern/wm_keymap.c
+++ b/source/blender/windowmanager/intern/wm_keymap.c
@@ -1118,9 +1118,9 @@ static wmKeyMapItem *wm_keymap_item_find_handlers(
 						if (kmi->ptr) {
 							if (STREQ("MESH_OT_rip_move", opname)) {
 								printf("OPERATOR\n");
-								IDP_spit(properties);
+								IDP_print(properties);
 								printf("KEYMAP\n");
-								IDP_spit(kmi->ptr->data);
+								IDP_print(kmi->ptr->data);
 							}
 						}
 #endif
@@ -1151,9 +1151,9 @@ static wmKeyMapItem *wm_keymap_item_find_handlers(
 #ifndef NDEBUG
 #ifdef WITH_PYTHON
 										printf("OPERATOR\n");
-										IDP_spit(properties);
+										IDP_print(properties);
 										printf("KEYMAP\n");
-										IDP_spit(kmi->ptr->data);
+										IDP_print(kmi->ptr->data);
 #endif
 #endif
 										printf("\n");
@@ -1300,9 +1300,9 @@ static wmKeyMapItem *wm_keymap_item_find(
 #ifndef NDEBUG
 #ifdef WITH_PYTHON
 					printf("OPERATOR\n");
-					IDP_spit(properties);
+					IDP_print(properties);
 					printf("KEYMAP\n");
-					IDP_spit(kmi->ptr->data);
+					IDP_print(kmi->ptr->data);
 #endif
 #endif
 					printf("\n");



More information about the Bf-blender-cvs mailing list