[Bf-blender-cvs] [bf0c8e1] master: PyAPI: add PyList_APPEND

Campbell Barton noreply at git.blender.org
Tue Jan 6 09:13:45 CET 2015


Commit: bf0c8e116db49379900020b5a20ba15d91b3fb9a
Author: Campbell Barton
Date:   Tue Jan 6 17:39:47 2015 +1100
Branches: master
https://developer.blender.org/rBbf0c8e116db49379900020b5a20ba15d91b3fb9a

PyAPI: add PyList_APPEND

This appends while giving ownership to the list, avoiding temp assignment.
This matches PyList_SET_ITEM which bypasses refcount's

Note, this also reduce code-size, Py_DECREF is a rather heavy macro.

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

M	source/blender/python/bmesh/bmesh_py_types.c
M	source/blender/python/bmesh/bmesh_py_types_select.c
M	source/blender/python/generic/python_utildefines.h
M	source/blender/python/intern/bpy.c
M	source/blender/python/intern/bpy_rna.c
M	source/blender/python/intern/bpy_rna_anim.c
M	source/blender/python/mathutils/mathutils_geometry.c

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

diff --git a/source/blender/python/bmesh/bmesh_py_types.c b/source/blender/python/bmesh/bmesh_py_types.c
index d3f4bfc..1da5599 100644
--- a/source/blender/python/bmesh/bmesh_py_types.c
+++ b/source/blender/python/bmesh/bmesh_py_types.c
@@ -44,6 +44,7 @@
 #include "../mathutils/mathutils.h"
 
 #include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
 
 #include "bmesh_py_types.h" /* own include */
 #include "bmesh_py_types_select.h"
@@ -2796,7 +2797,6 @@ static PyObject *bpy_bmelemseq_subscript_slice(BPy_BMElemSeq *self, Py_ssize_t s
 	bool ok;
 
 	PyObject *list;
-	PyObject *item;
 	BMHeader *ele;
 
 	BPY_BM_CHECK_OBJ(self);
@@ -2821,9 +2821,7 @@ static PyObject *bpy_bmelemseq_subscript_slice(BPy_BMElemSeq *self, Py_ssize_t s
 
 	/* add items until stop */
 	while ((ele = BM_iter_step(&iter))) {
-		item = BPy_BMElem_CreatePyObject(self->bm, ele);
-		PyList_Append(list, item);
-		Py_DECREF(item);
+		PyList_APPEND(list, BPy_BMElem_CreatePyObject(self->bm, ele));
 
 		count++;
 		if (count == stop) {
diff --git a/source/blender/python/bmesh/bmesh_py_types_select.c b/source/blender/python/bmesh/bmesh_py_types_select.c
index 6dd0ec5..7b792e9 100644
--- a/source/blender/python/bmesh/bmesh_py_types_select.c
+++ b/source/blender/python/bmesh/bmesh_py_types_select.c
@@ -44,6 +44,7 @@
 #include "bmesh_py_types_select.h"
 
 #include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
 
 PyDoc_STRVAR(bpy_bmeditselseq_active_doc,
 "The last selected element or None (read-only).\n\n:type: :class:`BMVert`, :class:`BMEdge` or :class:`BMFace`"
@@ -193,7 +194,6 @@ static PyObject *bpy_bmeditselseq_subscript_slice(BPy_BMEditSelSeq *self, Py_ssi
 	bool ok;
 
 	PyObject *list;
-	PyObject *item;
 	BMEditSelection *ese;
 
 	BPY_BM_CHECK_OBJ(self);
@@ -218,9 +218,7 @@ static PyObject *bpy_bmeditselseq_subscript_slice(BPy_BMEditSelSeq *self, Py_ssi
 
 	/* add items until stop */
 	while ((ese = ese->next)) {
-		item = BPy_BMElem_CreatePyObject(self->bm, &ese->ele->head);
-		PyList_Append(list, item);
-		Py_DECREF(item);
+		PyList_APPEND(list, BPy_BMElem_CreatePyObject(self->bm, &ese->ele->head));
 
 		count++;
 		if (count == stop) {
diff --git a/source/blender/python/generic/python_utildefines.h b/source/blender/python/generic/python_utildefines.h
index 555ad48..f7d3e7a 100644
--- a/source/blender/python/generic/python_utildefines.h
+++ b/source/blender/python/generic/python_utildefines.h
@@ -44,6 +44,14 @@ extern "C" {
  * use sparingly to avoid comma operator or temp var assignment */
 BLI_INLINE PyObject *Py_INCREF_RET(PyObject *op) { Py_INCREF(op); return op; }
 
+/* append & transfer ownership to the list, avoids inline Py_DECREF all over (which is quite a large macro) */
+BLI_INLINE int PyList_APPEND(PyObject *op, PyObject *v)
+{
+	int ret = PyList_Append(op, v);
+	Py_DecRef(v);
+	return ret;
+}
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index dbeccfd..ec3c017 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -51,6 +51,7 @@
 #include "bpy_utils_units.h"
 
 #include "../generic/py_capi_utils.h"
+#include "../generic/python_utildefines.h"
 
 /* external util modules */
 #include "../generic/idprop_py_api.h"
@@ -89,10 +90,7 @@ static PyObject *bpy_script_paths(PyObject *UNUSED(self))
 
 static bool bpy_blend_paths_visit_cb(void *userdata, char *UNUSED(path_dst), const char *path_src)
 {
-	PyObject *list = (PyObject *)userdata;
-	PyObject *item = PyC_UnicodeFromByte(path_src);
-	PyList_Append(list, item);
-	Py_DECREF(item);
+	PyList_APPEND((PyObject *)userdata, PyC_UnicodeFromByte(path_src));
 	return false; /* never edits the path */
 }
 
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index 74006c4..5921502 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -2302,8 +2302,7 @@ static PyObject *pyrna_prop_collection_subscript_slice(BPy_PropertyRNA *self, Py
 	     RNA_property_collection_next(&rna_macro_iter))
 	{
 		item = pyrna_struct_CreatePyObject(&rna_macro_iter.ptr);
-		PyList_Append(list, item);
-		Py_DECREF(item);
+		PyList_APPEND(list, item);
 
 		count++;
 		if (count == stop) {
@@ -3427,7 +3426,6 @@ static void pyrna_dir_members_py(PyObject *list, PyObject *self)
 
 static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
 {
-	PyObject *pystring;
 	const char *idname;
 
 	/* for looping over attrs and funcs */
@@ -3443,10 +3441,7 @@ static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
 			FunctionRNA *func = itemptr.data;
 			if (RNA_function_defined(func)) {
 				idname = RNA_function_identifier(itemptr.data);
-
-				pystring = PyUnicode_FromString(idname);
-				PyList_Append(list, pystring);
-				Py_DECREF(pystring);
+				PyList_APPEND(list, PyUnicode_FromString(idname));
 			}
 		}
 		RNA_PROP_END;
@@ -3466,9 +3461,7 @@ static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
 			nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
 
 			if (nameptr) {
-				pystring = PyUnicode_FromStringAndSize(nameptr, namelen);
-				PyList_Append(list, pystring);
-				Py_DECREF(pystring);
+				PyList_APPEND(list, PyUnicode_FromStringAndSize(nameptr, namelen));
 
 				if (name != nameptr) {
 					MEM_freeN(nameptr);
@@ -3483,7 +3476,6 @@ static void pyrna_dir_members_rna(PyObject *list, PointerRNA *ptr)
 static PyObject *pyrna_struct_dir(BPy_StructRNA *self)
 {
 	PyObject *ret;
-	PyObject *pystring;
 
 	PYRNA_STRUCT_CHECK_OBJ(self);
 
@@ -3502,9 +3494,7 @@ static PyObject *pyrna_struct_dir(BPy_StructRNA *self)
 		LinkData *link;
 
 		for (link = lb.first; link; link = link->next) {
-			pystring = PyUnicode_FromString(link->data);
-			PyList_Append(ret, pystring);
-			Py_DECREF(pystring);
+			PyList_APPEND(ret, PyUnicode_FromString(link->data));
 		}
 
 		BLI_freelistN(&lb);
@@ -3584,14 +3574,11 @@ static PyObject *pyrna_struct_getattro(BPy_StructRNA *self, PyObject *pyname)
 					case CTX_DATA_TYPE_COLLECTION:
 					{
 						CollectionPointerLink *link;
-						PyObject *linkptr;
 
 						ret = PyList_New(0);
 
 						for (link = newlb.first; link; link = link->next) {
-							linkptr = pyrna_struct_CreatePyObject(&link->ptr);
-							PyList_Append(ret, linkptr);
-							Py_DECREF(linkptr);
+							PyList_APPEND(ret, pyrna_struct_CreatePyObject(&link->ptr));
 						}
 						break;
 					}
@@ -4098,7 +4085,6 @@ PyDoc_STRVAR(pyrna_prop_collection_keys_doc,
 static PyObject *pyrna_prop_collection_keys(BPy_PropertyRNA *self)
 {
 	PyObject *ret = PyList_New(0);
-	PyObject *item;
 	char name[256], *nameptr;
 	int namelen;
 
@@ -4107,11 +4093,7 @@ static PyObject *pyrna_prop_collection_keys(BPy_PropertyRNA *self)
 		nameptr = RNA_struct_name_get_alloc(&itemptr, name, sizeof(name), &namelen);
 
 		if (nameptr) {
-			/* add to python list */
-			item = PyUnicode_FromStringAndSize(nameptr, namelen);
-			PyList_Append(ret, item);
-			Py_DECREF(item);
-			/* done */
+			PyList_APPEND(ret, PyUnicode_FromStringAndSize(nameptr, namelen));
 
 			if (name != nameptr) {
 				MEM_freeN(nameptr);
@@ -4157,8 +4139,7 @@ static PyObject *pyrna_prop_collection_items(BPy_PropertyRNA *self)
 			}
 			PyTuple_SET_ITEM(item, 1, pyrna_struct_CreatePyObject(&itemptr));
 
-			PyList_Append(ret, item);
-			Py_DECREF(item);
+			PyList_APPEND(ret, item);
 
 			i++;
 		}
@@ -4967,14 +4948,11 @@ static PyObject *pyrna_param_to_py(PointerRNA *ptr, PropertyRNA *prop, void *dat
 			{
 				ListBase *lb = (ListBase *)data;
 				CollectionPointerLink *link;
-				PyObject *linkptr;
 
 				ret = PyList_New(0);
 
 				for (link = lb->first; link; link = link->next) {
-					linkptr = pyrna_struct_CreatePyObject(&link->ptr);
-					PyList_Append(ret, linkptr);
-					Py_DECREF(linkptr);
+					PyList_APPEND(ret, pyrna_struct_CreatePyObject(&link->ptr));
 				}
 
 				break;
@@ -5138,9 +5116,7 @@ static PyObject *pyrna_func_call(BPy_FunctionRNA *self, PyObject *args, PyObject
 #ifdef DEBUG_STRING_FREE
 		if (item) {
 			if (PyUnicode_Check(item)) {
-				item = PyUnicode_FromString(_PyUnicode_AsString(item));
-				PyList_Append(string_free_ls, item);
-				Py_DECREF(item);
+				PyList_APPEND(string_free_ls, PyUnicode_FromString(_PyUnicode_AsString(item)));
 			}
 		}
 #endif
@@ -6597,7 +6573,6 @@ static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
 {
 	PyObject *list;
 #if 0
-	PyObject *name;
 	PyMethodDef *meth;
 #endif
 
@@ -6605,9 +6580,7 @@ static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
 
 #if 0 /* for now only contains __dir__ */
 	for (meth = pyrna_basetype_methods; meth->ml_name; meth++) {
-		name = PyUnicode_FromString(meth->ml_name);
-		PyList_Append(list, name);
-		Py_DECREF(name);
+		PyList_APPEND(list, PyUnicode_FromString(meth->ml_name));
 	}
 #endif
 	return list;
@@ -6618,7 +6591,6 @@ static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
 static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
 {
 	PyObject *ret = PyList_New(0);
-	PyObject *item;
 
 	RNA_PROP_BEGIN (&self->ptr, itemptr, self->prop)
 	{
@@ -6630,9 +6602,7 @@ static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
 		}
 		else {
 			/* add to python list */
-			item = PyUnicode_FromString(RNA_struct_identifier(srna));
-			PyList_Append(ret, item);
-			Py_DECREF(item);
+			PyList_APPEND(ret, PyUnicode_FromString(RNA_struct_identifier(srna)));
 		}
 	}
 	RNA_PROP_END;
diff --git a/source/blender/python/intern/bpy_rna_anim.c b/source/blender/python/intern/bpy_rna_anim.c
index ed51ba5..ced7d6a 100644
--- a/source/blender/python/intern/bpy_rna_anim.c
+++ b/source/blender/python/intern/bpy_rna_anim.c
@@ -53,6 +53,8 @@
 #include "bpy_util.h"
 #include "bpy_rna_anim.h"
 
+#include "../generic/python_utildefines.h"
+
 /* for keyframes and drivers */
 static int pyrna_struct_anim_args_parse(
         PointerRNA *ptr, const char *error_prefix, const char *path,
@@ -329,16 +331,13 @@ PyObject *pyrna_struct_driver_add(BPy_StructRNA *self, PyObject *args)
 			FCurve *fcu;
 
 			PointerRNA tptr;
-			PyObject *item;
 
 			if (index == -1) { /* all, use a list */
 				int i = 0;
 				ret = PyList_New(0);
 				while ((

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list