[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [35109] trunk/blender/source/blender/ python/intern: py/rna api:

Campbell Barton ideasman42 at gmail.com
Wed Feb 23 14:43:45 CET 2011


Revision: 35109
          http://projects.blender.org/scm/viewvc.php?view=rev&root=bf-blender&revision=35109
Author:   campbellbarton
Date:     2011-02-23 13:43:45 +0000 (Wed, 23 Feb 2011)
Log Message:
-----------
py/rna api:
 ifdef'd out weakref for blender/py api, worth testing to see if it adds much overhead.

Modified Paths:
--------------
    trunk/blender/source/blender/python/intern/bpy_rna.c
    trunk/blender/source/blender/python/intern/bpy_rna.h

Modified: trunk/blender/source/blender/python/intern/bpy_rna.c
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.c	2011-02-23 12:02:43 UTC (rev 35108)
+++ trunk/blender/source/blender/python/intern/bpy_rna.c	2011-02-23 13:43:45 UTC (rev 35109)
@@ -24,6 +24,7 @@
 
 #include <Python.h>
 
+#include <stddef.h>
 #include <float.h> /* FLT_MIN/MAX */
 
 #include "bpy_rna.h"
@@ -655,7 +656,7 @@
 }
 
 /* use our own dealloc so we can free a property if we use one */
-static void pyrna_struct_dealloc( BPy_StructRNA *self )
+static void pyrna_struct_dealloc(BPy_StructRNA *self)
 {
 	if (self->freeptr && self->ptr.data) {
 		IDP_FreeProperty(self->ptr.data);
@@ -663,11 +664,39 @@
 		self->ptr.data= NULL;
 	}
 
+#ifdef USE_WEAKREFS
+    if (self->in_weakreflist != NULL) {
+        PyObject_ClearWeakRefs((PyObject *)self);
+	}
+#endif
+
 	/* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
 	Py_TYPE(self)->tp_free(self);
-	return;
 }
 
+/* use our own dealloc so we can free a property if we use one */
+static void pyrna_prop_dealloc(BPy_PropertyRNA *self)
+{
+#ifdef USE_WEAKREFS
+    if (self->in_weakreflist != NULL) {
+        PyObject_ClearWeakRefs((PyObject *)self);
+	}
+#endif
+	/* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
+	Py_TYPE(self)->tp_free(self);
+}
+
+static void pyrna_prop_array_dealloc(BPy_PropertyRNA *self)
+{
+#ifdef USE_WEAKREFS
+    if (self->in_weakreflist != NULL) {
+        PyObject_ClearWeakRefs((PyObject *)self);
+	}
+#endif
+	/* Note, for subclassed PyObjects we cant just call PyObject_DEL() directly or it will crash */
+	Py_TYPE(self)->tp_free(self);
+}
+
 static const char *pyrna_enum_as_string(PointerRNA *ptr, PropertyRNA *prop)
 {
 	EnumPropertyItem *item;
@@ -4238,10 +4267,10 @@
 PyTypeObject pyrna_struct_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	"bpy_struct",			/* tp_name */
-	sizeof( BPy_StructRNA ),	/* tp_basicsize */
+	sizeof(BPy_StructRNA),	/* tp_basicsize */
 	0,			/* tp_itemsize */
 	/* methods */
-	( destructor ) pyrna_struct_dealloc,/* tp_dealloc */
+	(destructor) pyrna_struct_dealloc,/* tp_dealloc */
 	NULL,                       /* printfunc tp_print; */
 	NULL,						/* getattrfunc tp_getattr; */
 	NULL,						/* setattrfunc tp_setattr; */
@@ -4281,8 +4310,11 @@
 	(richcmpfunc)pyrna_struct_richcmp,	/* richcmpfunc tp_richcompare; */
 
   /***  weak reference enabler ***/
-	0,                          /* long tp_weaklistoffset; */
-
+#ifdef USE_WEAKREFS
+	offsetof(BPy_StructRNA, in_weakreflist),	/* long tp_weaklistoffset; */
+#else
+	0,
+#endif
   /*** Added in release 2.2 ***/
 	/*   Iterators */
 	NULL,                       /* getiterfunc tp_iter; */
@@ -4317,11 +4349,11 @@
 PyTypeObject pyrna_prop_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	"bpy_prop",		/* tp_name */
-	sizeof( BPy_PropertyRNA ),			/* tp_basicsize */
+	sizeof(BPy_PropertyRNA),			/* tp_basicsize */
 	0,			/* tp_itemsize */
 	/* methods */
-	NULL,						/* tp_dealloc */
-	NULL,                   /* printfunc tp_print; */
+	(destructor) pyrna_prop_dealloc, /* tp_dealloc */
+	NULL,						/* printfunc tp_print; */
 	NULL,						/* getattrfunc tp_getattr; */
 	NULL,                       /* setattrfunc tp_setattr; */
 	NULL,						/* tp_compare */ /* DEPRECATED in python 3.0! */
@@ -4362,7 +4394,11 @@
 	(richcmpfunc)pyrna_prop_richcmp,	/* richcmpfunc tp_richcompare; */
 
   /***  weak reference enabler ***/
-	0,                          /* long tp_weaklistoffset; */
+#ifdef USE_WEAKREFS
+	offsetof(BPy_PropertyRNA, in_weakreflist),	/* long tp_weaklistoffset; */
+#else
+	0,
+#endif
 
   /*** Added in release 2.2 ***/
 	/*   Iterators */
@@ -4397,10 +4433,10 @@
 PyTypeObject pyrna_prop_array_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	"bpy_prop_array",		/* tp_name */
-	sizeof( BPy_PropertyArrayRNA ),			/* tp_basicsize */
-	0,			/* tp_itemsize */
+	sizeof(BPy_PropertyArrayRNA),			/* tp_basicsize */
+	0,							/* tp_itemsize */
 	/* methods */
-	NULL,						/* tp_dealloc */
+	(destructor)pyrna_prop_array_dealloc, /* tp_dealloc */
 	NULL,                       /* printfunc tp_print; */
 	NULL,						/* getattrfunc tp_getattr; */
 	NULL,                       /* setattrfunc tp_setattr; */
@@ -4442,8 +4478,11 @@
 	NULL, /* subclassed */		/* richcmpfunc tp_richcompare; */
 
   /***  weak reference enabler ***/
-	0,                          /* long tp_weaklistoffset; */
-
+#ifdef USE_WEAKREFS
+	offsetof(BPy_PropertyArrayRNA, in_weakreflist),	/* long tp_weaklistoffset; */
+#else
+	0,
+#endif
   /*** Added in release 2.2 ***/
 	/*   Iterators */
 	(getiterfunc)pyrna_prop_array_iter,	/* getiterfunc tp_iter; */
@@ -4477,10 +4516,10 @@
 PyTypeObject pyrna_prop_collection_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	"bpy_prop_collection",		/* tp_name */
-	sizeof( BPy_PropertyRNA ),			/* tp_basicsize */
+	sizeof(BPy_PropertyRNA),			/* tp_basicsize */
 	0,			/* tp_itemsize */
 	/* methods */
-	NULL,						/* tp_dealloc */
+	(destructor)pyrna_prop_dealloc, /* tp_dealloc */
 	NULL,                       /* printfunc tp_print; */
 	NULL,						/* getattrfunc tp_getattr; */
 	NULL,                       /* setattrfunc tp_setattr; */
@@ -4522,7 +4561,11 @@
 	NULL, /* subclassed */		/* richcmpfunc tp_richcompare; */
 
   /***  weak reference enabler ***/
-	0,                          /* long tp_weaklistoffset; */
+#ifdef USE_WEAKREFS
+	offsetof(BPy_PropertyRNA, in_weakreflist),	/* long tp_weaklistoffset; */
+#else
+	0,
+#endif
 
   /*** Added in release 2.2 ***/
 	/*   Iterators */
@@ -4558,10 +4601,10 @@
 static PyTypeObject pyrna_prop_collection_idprop_Type = {
 	PyVarObject_HEAD_INIT(NULL, 0)
 	"bpy_prop_collection_idprop",		/* tp_name */
-	sizeof( BPy_PropertyRNA ),			/* tp_basicsize */
-	0,			/* tp_itemsize */
+	sizeof(BPy_PropertyRNA),			/* tp_basicsize */
+	0,							/* tp_itemsize */
 	/* methods */
-	NULL,						/* tp_dealloc */
+	(destructor)pyrna_prop_dealloc, /* tp_dealloc */
 	NULL,                       /* printfunc tp_print; */
 	NULL,						/* getattrfunc tp_getattr; */
 	NULL,                       /* setattrfunc tp_setattr; */
@@ -4603,7 +4646,11 @@
 	NULL, /* subclassed */		/* richcmpfunc tp_richcompare; */
 
   /***  weak reference enabler ***/
-	0,                          /* long tp_weaklistoffset; */
+#ifdef USE_WEAKREFS
+	offsetof(BPy_PropertyRNA, in_weakreflist),	/* long tp_weaklistoffset; */
+#else
+	0,
+#endif
 
   /*** Added in release 2.2 ***/
 	/*   Iterators */
@@ -4844,6 +4891,9 @@
 		else {
 			fprintf(stderr, "Could not make type\n");
 			pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
+#ifdef USE_WEAKREFS
+			pyrna->in_weakreflist= NULL;
+#endif
 		}
 	}
 
@@ -4880,11 +4930,17 @@
 		}
 
 		pyrna = (BPy_PropertyRNA *) PyObject_NEW(BPy_PropertyRNA, type);
+#ifdef USE_WEAKREFS
+		pyrna->in_weakreflist= NULL;
+#endif
 	}
 	else {
 		pyrna = (BPy_PropertyRNA *) PyObject_NEW(BPy_PropertyArrayRNA, &pyrna_prop_array_Type);
 		((BPy_PropertyArrayRNA *)pyrna)->arraydim= 0;
 		((BPy_PropertyArrayRNA *)pyrna)->arrayoffset= 0;
+#ifdef USE_WEAKREFS
+		((BPy_PropertyArrayRNA *)pyrna)->in_weakreflist= NULL;
+#endif
 	}
 
 	if( !pyrna ) {
@@ -5029,7 +5085,7 @@
 
 	if ((pyrna_basetype_Type.tp_flags & Py_TPFLAGS_READY)==0)  {
 		pyrna_basetype_Type.tp_name = "RNA_Types";
-		pyrna_basetype_Type.tp_basicsize = sizeof( BPy_BaseTypeRNA );
+		pyrna_basetype_Type.tp_basicsize = sizeof(BPy_BaseTypeRNA);
 		pyrna_basetype_Type.tp_getattro = ( getattrofunc )pyrna_basetype_getattro;
 		pyrna_basetype_Type.tp_flags = Py_TPFLAGS_DEFAULT;
 		pyrna_basetype_Type.tp_methods = pyrna_basetype_methods;
@@ -5043,7 +5099,9 @@
 	/* avoid doing this lookup for every getattr */
 	RNA_blender_rna_pointer_create(&self->ptr);
 	self->prop = RNA_struct_find_property(&self->ptr, "structs");
-
+#ifdef USE_WEAKREFS
+	self->in_weakreflist= NULL;
+#endif
 	return (PyObject *)self;
 }
 

Modified: trunk/blender/source/blender/python/intern/bpy_rna.h
===================================================================
--- trunk/blender/source/blender/python/intern/bpy_rna.h	2011-02-23 12:02:43 UTC (rev 35108)
+++ trunk/blender/source/blender/python/intern/bpy_rna.h	2011-02-23 13:43:45 UTC (rev 35109)
@@ -39,21 +39,33 @@
 #define BPy_PropertyRNA_Check(v)		(PyObject_TypeCheck(v, &pyrna_prop_Type))
 #define BPy_PropertyRNA_CheckExact(v)	(Py_TYPE(v) == &pyrna_prop_Type)
 
+/* play it safe and keep optional for now, need to test further now this affects looping on 10000's of verts for eg. */
+// #define USE_WEAKREFS
+
 typedef struct {
 	PyObject_HEAD /* required python macro   */
-	PointerRNA ptr;
+	PointerRNA	ptr;
+#ifdef USE_WEAKREFS
+	PyObject *in_weakreflist;
+#endif
 } BPy_DummyPointerRNA;
 
 typedef struct {
 	PyObject_HEAD /* required python macro   */
 	PointerRNA ptr;
 	int freeptr; /* needed in some cases if ptr.data is created on the fly, free when deallocing */
+#ifdef USE_WEAKREFS
+	PyObject *in_weakreflist;
+#endif
 } BPy_StructRNA;
 
 typedef struct {
 	PyObject_HEAD /* required python macro   */
 	PointerRNA ptr;
 	PropertyRNA *prop;
+#ifdef USE_WEAKREFS
+	PyObject *in_weakreflist;
+#endif
 } BPy_PropertyRNA;
 
 typedef struct {
@@ -64,6 +76,9 @@
 	/* Arystan: this is a hack to allow sub-item r/w access like: face.uv[n][m] */
 	int arraydim; /* array dimension, e.g: 0 for face.uv, 2 for face.uv[n][m], etc. */
 	int arrayoffset; /* array first item offset, e.g. if face.uv is [4][2], arrayoffset for face.uv[n] is 2n */
+#ifdef USE_WEAKREFS
+	PyObject *in_weakreflist;
+#endif
 } BPy_PropertyArrayRNA;
 
 /* cheap trick */




More information about the Bf-blender-cvs mailing list