[Bf-blender-cvs] [a696a0f] master: Python API: create args for meta-classes directly (a little faster).

Campbell Barton noreply at git.blender.org
Mon Jan 20 08:16:39 CET 2014


Commit: a696a0ff1e9c55c21696c18a6cf6e5b1b2a40e37
Author: Campbell Barton
Date:   Mon Jan 20 18:03:23 2014 +1100
https://developer.blender.org/rBa696a0ff1e9c55c21696c18a6cf6e5b1b2a40e37

Python API: create args for meta-classes directly (a little faster).

Also add some new interned strings.

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

M	source/blender/python/intern/bpy_intern_string.c
M	source/blender/python/intern/bpy_intern_string.h
M	source/blender/python/intern/bpy_rna.c

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

diff --git a/source/blender/python/intern/bpy_intern_string.c b/source/blender/python/intern/bpy_intern_string.c
index 294f230..9c93af1 100644
--- a/source/blender/python/intern/bpy_intern_string.c
+++ b/source/blender/python/intern/bpy_intern_string.c
@@ -32,38 +32,50 @@
 
 #include "bpy_intern_string.h"
 
+#include "BLI_utildefines.h"
+
+static PyObject *bpy_intern_str_arr[11];
+
 PyObject *bpy_intern_str_register;
 PyObject *bpy_intern_str_unregister;
 PyObject *bpy_intern_str_bl_rna;
 PyObject *bpy_intern_str_bl_property;
+PyObject *bpy_intern_str_bpy_types;
 PyObject *bpy_intern_str_order;
 PyObject *bpy_intern_str_attr;
 PyObject *bpy_intern_str___slots__;
 PyObject *bpy_intern_str___name__;
 PyObject *bpy_intern_str___doc__;
+PyObject *bpy_intern_str___module__;
 
 void bpy_intern_string_init(void)
 {
-	bpy_intern_str_register = PyUnicode_FromString("register");
-	bpy_intern_str_unregister = PyUnicode_FromString("unregister");
-	bpy_intern_str_bl_rna = PyUnicode_FromString("bl_rna");
-	bpy_intern_str_bl_property = PyUnicode_FromString("bl_property");
-	bpy_intern_str_order = PyUnicode_FromString("order");
-	bpy_intern_str_attr = PyUnicode_FromString("attr");
-	bpy_intern_str___slots__ = PyUnicode_FromString("__slots__");
-	bpy_intern_str___name__ = PyUnicode_FromString("__name__");
-	bpy_intern_str___doc__ = PyUnicode_FromString("__doc__");
+	unsigned int i = 0;
+
+#define BPY_INTERN_STR(var, str) \
+	{ var = bpy_intern_str_arr[i++] = PyUnicode_FromString(str); } (void)0
+
+	BPY_INTERN_STR(bpy_intern_str_register, "register");
+	BPY_INTERN_STR(bpy_intern_str_unregister, "unregister");
+	BPY_INTERN_STR(bpy_intern_str_bl_rna, "bl_rna");
+	BPY_INTERN_STR(bpy_intern_str_bl_property, "bl_property");
+	BPY_INTERN_STR(bpy_intern_str_bpy_types, "bpy.types");
+	BPY_INTERN_STR(bpy_intern_str_order, "order");
+	BPY_INTERN_STR(bpy_intern_str_attr, "attr");
+	BPY_INTERN_STR(bpy_intern_str___slots__, "__slots__");
+	BPY_INTERN_STR(bpy_intern_str___name__, "__name__");
+	BPY_INTERN_STR(bpy_intern_str___doc__, "__doc__");
+	BPY_INTERN_STR(bpy_intern_str___module__, "__module__");
+
+#undef BPY_INTERN_STR
+
+	BLI_assert(i == (sizeof(bpy_intern_str_arr) / sizeof(*bpy_intern_str_arr)));
 }
 
 void bpy_intern_string_exit(void)
 {
-	Py_DECREF(bpy_intern_str_register);
-	Py_DECREF(bpy_intern_str_unregister);
-	Py_DECREF(bpy_intern_str_bl_rna);
-	Py_DECREF(bpy_intern_str_bl_property);
-	Py_DECREF(bpy_intern_str_order);
-	Py_DECREF(bpy_intern_str_attr);
-	Py_DECREF(bpy_intern_str___slots__);
-	Py_DECREF(bpy_intern_str___name__);
-	Py_DECREF(bpy_intern_str___doc__);
+	unsigned int i = sizeof(bpy_intern_str_arr) / sizeof(*bpy_intern_str_arr);
+	while (i--) {
+		Py_DECREF(bpy_intern_str_arr[i]);
+	}
 }
diff --git a/source/blender/python/intern/bpy_intern_string.h b/source/blender/python/intern/bpy_intern_string.h
index 41b7303..f4f4560 100644
--- a/source/blender/python/intern/bpy_intern_string.h
+++ b/source/blender/python/intern/bpy_intern_string.h
@@ -34,10 +34,12 @@ extern PyObject *bpy_intern_str_register;
 extern PyObject *bpy_intern_str_unregister;
 extern PyObject *bpy_intern_str_bl_rna;
 extern PyObject *bpy_intern_str_bl_property;
+extern PyObject *bpy_intern_str_bpy_types;
 extern PyObject *bpy_intern_str_order;
 extern PyObject *bpy_intern_str_attr;
 extern PyObject *bpy_intern_str___slots__;
 extern PyObject *bpy_intern_str___name__;
 extern PyObject *bpy_intern_str___doc__;
+extern PyObject *bpy_intern_str___module__;
 
 #endif  /* __BPY_INTERN_STRING_H__ */
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index 8d6c7d9..86ed459 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -6234,7 +6234,7 @@ static PyObject *pyrna_srna_Subtype(StructRNA *srna)
 		/* subclass equivalents
 		 * - class myClass(myBase):
 		 *     some = 'value' # or ...
-		 * - myClass = type(name='myClass', bases=(myBase,), dict={'__module__':'bpy.types'})
+		 * - myClass = type(name='myClass', bases=(myBase,), dict={'__module__': 'bpy.types', '__slots__': ()})
 		 */
 
 		/* Assume RNA_struct_py_type_get(srna) was already checked */
@@ -6257,8 +6257,39 @@ static PyObject *pyrna_srna_Subtype(StructRNA *srna)
 		}
 
 		/* always use O not N when calling, N causes refcount errors */
+#if 0
 		newclass = PyObject_CallFunction(metaclass, (char *)"s(O) {sss()}",
 		                                 idname, py_base, "__module__", "bpy.types", "__slots__");
+#else
+		{
+			/* longhand of the call above */
+			PyObject *args, *item, *value;
+			int ok;
+
+			args = PyTuple_New(3);
+
+			/* arg[0] (name=...) */
+			PyTuple_SET_ITEM(args, 0, PyUnicode_FromString(idname));
+
+
+			/* arg[1] (bases=...) */
+			PyTuple_SET_ITEM(args, 1, item = PyTuple_New(1));
+			PyTuple_SET_ITEM(item, 0, py_base);
+
+
+			/* arg[2] (dict=...) */
+			PyTuple_SET_ITEM(args, 2, item = PyDict_New());
+			ok = PyDict_SetItem(item, bpy_intern_str___module__, bpy_intern_str_bpy_types);
+			BLI_assert(ok != -1);
+			ok = PyDict_SetItem(item, bpy_intern_str___slots__, value = PyTuple_New(0)); Py_DECREF(value);
+			BLI_assert(ok != -1);
+
+			newclass = PyObject_CallObject(metaclass, args);
+			Py_DECREF(args);
+
+			(void)ok;
+		}
+#endif
 
 		/* newclass will now have 2 ref's, ???, probably 1 is internal since decrefing here segfaults */




More information about the Bf-blender-cvs mailing list