[Bf-blender-cvs] [9900addf11d] master: Cleanup: _bpy.ops utility to lookup an operator

Campbell Barton noreply at git.blender.org
Thu Sep 13 09:38:17 CEST 2018


Commit: 9900addf11d9a5b964aa745cf141998195f7c8db
Author: Campbell Barton
Date:   Thu Sep 13 17:47:56 2018 +1000
Branches: master
https://developer.blender.org/rB9900addf11d9a5b964aa745cf141998195f7c8db

Cleanup: _bpy.ops utility to lookup an operator

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

M	source/blender/python/intern/bpy_operator.c

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

diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c
index 4183970c9a1..78ae9976586 100644
--- a/source/blender/python/intern/bpy_operator.c
+++ b/source/blender/python/intern/bpy_operator.c
@@ -64,6 +64,21 @@
 /* so operators called can spawn threads which acquire the GIL */
 #define BPY_RELEASE_GIL
 
+static wmOperatorType *ot_lookup_from_py_string(PyObject *value, const char *py_fn_id)
+{
+	const char *opname = _PyUnicode_AsString(value);
+	if (opname == NULL) {
+		PyErr_Format(PyExc_TypeError, "%s() expects a string argument", py_fn_id);
+		return NULL;
+	}
+
+	wmOperatorType *ot = WM_operatortype_find(opname, true);
+	if (ot == NULL) {
+		PyErr_Format(PyExc_KeyError, "%s(\"%s\") not found", py_fn_id, opname);
+		return NULL;
+	}
+	return ot;
+}
 
 static PyObject *pyop_poll(PyObject *UNUSED(self), PyObject *args)
 {
@@ -397,17 +412,8 @@ static PyObject *pyop_dir(PyObject *UNUSED(self))
 static PyObject *pyop_getrna(PyObject *UNUSED(self), PyObject *value)
 {
 	wmOperatorType *ot;
-	PointerRNA ptr;
-	const char *opname = _PyUnicode_AsString(value);
-	BPy_StructRNA *pyrna = NULL;
 
-	if (opname == NULL) {
-		PyErr_SetString(PyExc_TypeError, "_bpy.ops.get_rna() expects a string argument");
-		return NULL;
-	}
-	ot = WM_operatortype_find(opname, true);
-	if (ot == NULL) {
-		PyErr_Format(PyExc_KeyError, "_bpy.ops.get_rna(\"%s\") not found", opname);
+	if ((ot = ot_lookup_from_py_string(value, "getrna")) == NULL) {
 		return NULL;
 	}
 
@@ -415,11 +421,11 @@ static PyObject *pyop_getrna(PyObject *UNUSED(self), PyObject *value)
 	//RNA_pointer_create(NULL, &RNA_Struct, ot->srna, &ptr);
 
 	/* XXX - should call WM_operator_properties_free */
+	PointerRNA ptr;
 	WM_operator_properties_create_ptr(&ptr, ot);
 	WM_operator_properties_sanitize(&ptr, 0);
 
-
-	pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
+	BPy_StructRNA *pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
 #ifdef PYRNA_FREE_SUPPORT
 	pyrna->freeptr = true;
 #endif
@@ -429,21 +435,11 @@ static PyObject *pyop_getrna(PyObject *UNUSED(self), PyObject *value)
 static PyObject *pyop_getinstance(PyObject *UNUSED(self), PyObject *value)
 {
 	wmOperatorType *ot;
-	wmOperator *op;
-	PointerRNA ptr;
-	const char *opname = _PyUnicode_AsString(value);
-	BPy_StructRNA *pyrna = NULL;
-
-	if (opname == NULL) {
-		PyErr_SetString(PyExc_TypeError, "_bpy.ops.get_instance() expects a string argument");
-		return NULL;
-	}
-	ot = WM_operatortype_find(opname, true);
-	if (ot == NULL) {
-		PyErr_Format(PyExc_KeyError, "_bpy.ops.get_instance(\"%s\") not found", opname);
+	if ((ot = ot_lookup_from_py_string(value, "get_instance")) == NULL) {
 		return NULL;
 	}
 
+	wmOperator *op;
 #ifdef PYRNA_FREE_SUPPORT
 	op = MEM_callocN(sizeof(wmOperator), __func__);
 #else
@@ -453,9 +449,10 @@ static PyObject *pyop_getinstance(PyObject *UNUSED(self), PyObject *value)
 	BLI_strncpy(op->idname, ot->idname, sizeof(op->idname)); /* in case its needed */
 	op->type = ot;
 
+	PointerRNA ptr;
 	RNA_pointer_create(NULL, &RNA_Operator, op, &ptr);
 
-	pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
+	BPy_StructRNA *pyrna = (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr);
 #ifdef PYRNA_FREE_SUPPORT
 	pyrna->freeptr = true;
 #endif



More information about the Bf-blender-cvs mailing list