Welcome to mirror list, hosted at ThFree Co, Russian Federation.

git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCampbell Barton <ideasman42@gmail.com>2009-03-21 09:55:30 +0300
committerCampbell Barton <ideasman42@gmail.com>2009-03-21 09:55:30 +0300
commit6ab2d7ad659606cbf2a315ef9a576c364e6ec9bb (patch)
tree56250d1706115c5e4fd5aa8157a8dda91b8d950a /source/blender/python/intern
parentb4209c56565660c20718fc2e1ad74d4257683a3e (diff)
- lazy subtype initialization rna, was initializing every type in bpy.types at startup, which is slow and doesn't allow access to dynamically added types.
- bpy.types isnt a module anymore, defined as its own PyType, getattr looks up the rna collection each time. - refcounting fixes - fixe epydoc generation with undefined values
Diffstat (limited to 'source/blender/python/intern')
-rw-r--r--source/blender/python/intern/bpy_operator.c27
-rw-r--r--source/blender/python/intern/bpy_opwrapper.c4
-rw-r--r--source/blender/python/intern/bpy_rna.c160
-rw-r--r--source/blender/python/intern/bpy_rna.h3
-rw-r--r--source/blender/python/intern/bpy_util.c2
5 files changed, 133 insertions, 63 deletions
diff --git a/source/blender/python/intern/bpy_operator.c b/source/blender/python/intern/bpy_operator.c
index a35c5ed2cb4..b40d4640bcb 100644
--- a/source/blender/python/intern/bpy_operator.c
+++ b/source/blender/python/intern/bpy_operator.c
@@ -108,8 +108,10 @@ int PYOP_props_from_dict(PointerRNA *ptr, PyObject *kw)
}
static PyObject *pyop_base_dir(PyObject *self);
+static PyObject *pyop_base_rna(PyObject *self, PyObject *pyname);
static struct PyMethodDef pyop_base_methods[] = {
{"__dir__", (PyCFunction)pyop_base_dir, METH_NOARGS, ""},
+ {"__rna__", (PyCFunction)pyop_base_rna, METH_O, ""},
{"add", (PyCFunction)PYOP_wrap_add, METH_O, ""},
{"remove", (PyCFunction)PYOP_wrap_remove, METH_O, ""},
{NULL, NULL, 0, NULL}
@@ -199,8 +201,9 @@ static PyObject *pyop_base_getattro( BPy_OperatorBase * self, PyObject *pyname )
{
char *name = _PyUnicode_AsString(pyname);
PyObject *ret;
+ wmOperatorType *ot;
- if ((WM_operatortype_find(name))) {
+ if ((ot= WM_operatortype_find(name))) {
ret = PyCFunction_New( pyop_base_call_meth, pyname); /* set the name string as self, PyCFunction_New incref's self */
}
else if ((ret = PyObject_GenericGetAttr((PyObject *)self, pyname))) {
@@ -235,6 +238,28 @@ static PyObject *pyop_base_dir(PyObject *self)
return list;
}
+static PyObject *pyop_base_rna(PyObject *self, PyObject *pyname)
+{
+ char *name = _PyUnicode_AsString(pyname);
+ wmOperatorType *ot;
+
+ if ((ot= WM_operatortype_find(name))) {
+ BPy_StructRNA *pyrna;
+ PointerRNA ptr;
+
+ /* XXX POINTER - if this 'ot' is python generated, it could be free'd */
+ RNA_pointer_create(NULL, ot->srna, NULL, &ptr);
+
+ pyrna= (BPy_StructRNA *)pyrna_struct_CreatePyObject(&ptr); /* were not really using &ptr, overwite next */
+ //pyrna->freeptr= 1;
+ return pyrna;
+ }
+ else {
+ PyErr_SetString(PyExc_AttributeError, "Operator not found");
+ return NULL;
+ }
+}
+
PyTypeObject pyop_base_Type = {NULL};
PyObject *BPY_operator_module( bContext *C )
diff --git a/source/blender/python/intern/bpy_opwrapper.c b/source/blender/python/intern/bpy_opwrapper.c
index 379fda35bb6..b7d4c82588e 100644
--- a/source/blender/python/intern/bpy_opwrapper.c
+++ b/source/blender/python/intern/bpy_opwrapper.c
@@ -384,7 +384,8 @@ PyObject *PYOP_wrap_add(PyObject *self, PyObject *value)
//PyObject *optype = PyObject_GetAttrString(PyObject_GetAttrString(PyDict_GetItemString(PyEval_GetGlobals(), "bpy"), "types"), "Operator");
optype = PyObject_GetAttrStringArgs(PyDict_GetItemString(PyEval_GetGlobals(), "bpy"), 2, "types", "Operator");
Py_DECREF(optype);
-
+
+
if (!PyObject_IsSubclass(value, optype)) {
PyErr_SetString( PyExc_AttributeError, "expected Operator subclass of bpy.types.Operator");
return NULL;
@@ -450,7 +451,6 @@ PyObject *PYOP_wrap_add(PyObject *self, PyObject *value)
} else {
PyErr_Clear();
}
- Py_XDECREF(item);
}
/* If we have properties set, check its a list of dicts */
diff --git a/source/blender/python/intern/bpy_rna.c b/source/blender/python/intern/bpy_rna.c
index ca65eee0c6f..8f08ce6308d 100644
--- a/source/blender/python/intern/bpy_rna.c
+++ b/source/blender/python/intern/bpy_rna.c
@@ -574,21 +574,27 @@ static PyMappingMethods pyrna_prop_as_mapping = {
static PyObject *pyrna_struct_dir(BPy_StructRNA * self)
{
PyObject *ret, *dict;
- PyObject *pystring = PyUnicode_FromString("__dict__");
+ PyObject *pystring;
/* Include this incase this instance is a subtype of a python class
* In these instances we may want to return a function or variable provided by the subtype
* */
- dict = PyObject_GenericGetAttr((PyObject *)self, pystring);
- Py_DECREF(pystring);
-
- if (dict==NULL) {
- PyErr_Clear();
+
+ if (BPy_StructRNA_CheckExact(self)) {
ret = PyList_New(0);
- }
- else {
- ret = PyDict_Keys(dict);
- Py_DECREF(dict);
+ } else {
+ pystring = PyUnicode_FromString("__dict__");
+ dict = PyObject_GenericGetAttr((PyObject *)self, pystring);
+ Py_DECREF(pystring);
+
+ if (dict==NULL) {
+ PyErr_Clear();
+ ret = PyList_New(0);
+ }
+ else {
+ ret = PyDict_Keys(dict);
+ Py_DECREF(dict);
+ }
}
/* Collect RNA items*/
@@ -629,12 +635,12 @@ static PyObject *pyrna_struct_getattro( BPy_StructRNA * self, PyObject *pyname )
/* Include this incase this instance is a subtype of a python class
* In these instances we may want to return a function or variable provided by the subtype
+ *
+ * Also needed to return methods when its not a subtype
* */
- if (BPy_StructRNA_CheckExact(self) == 0) {
- ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
- if (ret) return ret;
- else PyErr_Clear();
- }
+ ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
+ if (ret) return ret;
+ else PyErr_Clear();
/* done with subtypes */
prop = RNA_struct_find_property(&self->ptr, name);
@@ -1061,6 +1067,8 @@ PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
if (ptr->type==NULL) {
newclass= NULL; /* Nothing to do */
+ } else if ((newclass= BPy_RNA_PYTYPE(ptr->data))) {
+ Py_INCREF(newclass);
} else if ((nameprop = RNA_struct_name_property(ptr))) {
/* for now, return the base RNA type rather then a real module */
@@ -1076,7 +1084,6 @@ PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
PyObject *args = PyTuple_New(3);
PyObject *bases = PyTuple_New(1);
PyObject *dict = PyDict_New();
- PyObject *rna;
nameptr= RNA_property_string_get_alloc(ptr, nameprop, name, sizeof(name));
@@ -1087,6 +1094,7 @@ PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
// arg 2
PyTuple_SET_ITEM(bases, 0, (PyObject *)&pyrna_struct_Type);
Py_INCREF(&pyrna_struct_Type);
+
PyTuple_SET_ITEM(args, 1, bases);
// arg 3 - add an instance of the rna
@@ -1100,9 +1108,11 @@ PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
newclass = PyObject_CallObject((PyObject *)&PyType_Type, args);
// Set this later
+
if (newclass) {
+ PyObject *rna;
BPy_RNA_PYTYPE(ptr->data) = (void *)newclass; /* Store for later use */
-
+
/* Not 100% needed but useful,
* having an instance within a type looks wrong however this instance IS an rna type */
rna = pyrna_struct_CreatePyObject(ptr);
@@ -1110,6 +1120,7 @@ PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
Py_DECREF(rna);
/* done with rna instance */
}
+
Py_DECREF(args);
if ((char *)&name != nameptr)
@@ -1123,15 +1134,23 @@ PyObject* pyrna_struct_Subtype(PointerRNA *ptr)
/*-----------------------CreatePyObject---------------------------------*/
PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
{
- BPy_StructRNA *pyrna;
+ BPy_StructRNA *pyrna= NULL;
+ int tp_init= 0;
if (ptr->data==NULL && ptr->type==NULL) { /* Operator RNA has NULL data */
Py_RETURN_NONE;
}
- if (ptr->type && BPy_RNA_PYTYPE(ptr->type)) {
- PyTypeObject *tp = BPy_RNA_PYTYPE(ptr->type);
- pyrna = (BPy_StructRNA *) tp->tp_alloc(tp, 0);
+ if (ptr->type == &RNA_Struct) { /* always return a python subtype from rna struct types */
+ PyTypeObject *tp = pyrna_struct_Subtype(ptr);
+
+ if (tp) {
+ pyrna = (BPy_StructRNA *) tp->tp_alloc(tp, 0);
+ }
+ else {
+ fprintf(stderr, "Could not make type\n");
+ pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
+ }
}
else {
pyrna = ( BPy_StructRNA * ) PyObject_NEW( BPy_StructRNA, &pyrna_struct_Type );
@@ -1144,7 +1163,6 @@ PyObject *pyrna_struct_CreatePyObject( PointerRNA *ptr )
pyrna->ptr= *ptr;
pyrna->freeptr= 0;
-
return ( PyObject * ) pyrna;
}
@@ -1201,51 +1219,75 @@ PyObject *BPY_rna_doc( void )
}
#endif
- PyObject *BPY_rna_types(void)
- {
- /* Now initialize new subtypes based on pyrna_struct_Type */
- PointerRNA ptr;
- CollectionPropertyIterator iter;
- PropertyRNA *prop;
+/* pyrna_basetype_* - BPy_BaseTypeRNA is just a BPy_PropertyRNA struct with a differnt type
+ * the self->ptr and self->prop are always set to the "structs" collection */
+//---------------getattr--------------------------------------------
+static PyObject *pyrna_basetype_getattro( BPy_BaseTypeRNA * self, PyObject *pyname )
+{
+ PointerRNA newptr;
+ PyObject *ret;
+
+ ret = PyObject_GenericGetAttr((PyObject *)self, pyname);
+ if (ret) return ret;
+ else PyErr_Clear();
+
+ if (RNA_property_collection_lookup_string(&self->ptr, self->prop, _PyUnicode_AsString(pyname), &newptr)) {
+ return pyrna_struct_Subtype(&newptr);
+ }
+ else { /* Override the error */
+ PyErr_Format(PyExc_AttributeError, "bpy.types.%s not a valid RNA_Struct", _PyUnicode_AsString(pyname));
+ return NULL;
+ }
+}
+
+static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self);
+static struct PyMethodDef pyrna_basetype_methods[] = {
+ {"__dir__", (PyCFunction)pyrna_basetype_dir, METH_NOARGS, ""},
+ {NULL, NULL, 0, NULL}
+};
- PyObject *mod, *dict, *type, *name;
-
- mod = PyModule_New("types");
- dict = PyModule_GetDict(mod);
+static PyObject *pyrna_basetype_dir(BPy_BaseTypeRNA *self)
+{
+ PyObject *list, *name;
+ PyMethodDef *meth;
- /* for now, return the base RNA type rather then a real module */
- RNA_blender_rna_pointer_create(&ptr);
- prop = RNA_struct_find_property(&ptr, "structs");
-
- RNA_property_collection_begin(&ptr, prop, &iter);
- for(; iter.valid; RNA_property_collection_next(&iter)) {
- if(iter.ptr.data) {
- type = (PyObject *)BPy_RNA_PYTYPE(iter.ptr.data);
- if (type==NULL) {
- type = pyrna_struct_Subtype(&iter.ptr);
- }
- if (type) {
- name = PyObject_GetAttrString(type, "__name__"); /* myClass.__name__ */
- if (name) {
- Py_DECREF(name);
- PyDict_SetItem(dict, name, type);
- }
- else {
- printf("could not get type __name__\n");
- }
- }
- else {
- printf("could not generate type\n");
- }
- }
+ list= pyrna_prop_keys(self); /* like calling structs.keys(), avoids looping here */
+
+ for(meth=pyrna_basetype_methods; meth->ml_name; meth++) {
+ name = PyUnicode_FromString(meth->ml_name);
+ PyList_Append(list, name);
+ Py_DECREF(name);
}
- RNA_property_collection_end(&iter);
- return mod;
+ return list;
+}
+
+PyTypeObject pyrna_basetype_Type = {NULL};
+
+PyObject *BPY_rna_types(void)
+{
+ BPy_BaseTypeRNA *self;
+ pyrna_basetype_Type.tp_name = "RNA_Types";
+ 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;
+
+ if( PyType_Ready( &pyrna_basetype_Type ) < 0 )
+ return NULL;
+
+ self= (BPy_BaseTypeRNA *)PyObject_NEW( BPy_BaseTypeRNA, &pyrna_basetype_Type );
+
+ /* avoid doing this lookup for every getattr */
+ RNA_blender_rna_pointer_create(&self->ptr);
+ self->prop = RNA_struct_find_property(&self->ptr, "structs");
+
+ return (PyObject *)self;
}
+
/* Orphan functions, not sure where they should go */
/* Function that sets RNA, NOTE - self is NULL when called from python, but being abused from C so we can pass the srna allong
diff --git a/source/blender/python/intern/bpy_rna.h b/source/blender/python/intern/bpy_rna.h
index 904529b58de..878b2a7d17a 100644
--- a/source/blender/python/intern/bpy_rna.h
+++ b/source/blender/python/intern/bpy_rna.h
@@ -61,6 +61,9 @@ typedef struct {
PropertyRNA *prop;
} BPy_PropertyRNA;
+/* cheap trick */
+#define BPy_BaseTypeRNA BPy_PropertyRNA
+
PyObject *BPY_rna_module( void );
/*PyObject *BPY_rna_doc( void );*/
PyObject *BPY_rna_types( void );
diff --git a/source/blender/python/intern/bpy_util.c b/source/blender/python/intern/bpy_util.c
index 47ac739eac0..ce307b5d8ac 100644
--- a/source/blender/python/intern/bpy_util.c
+++ b/source/blender/python/intern/bpy_util.c
@@ -236,6 +236,6 @@ PyObject *PyObject_GetAttrStringArgs(PyObject *o, Py_ssize_t n, ...)
}
va_end(vargs);
- Py_INCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */
+ Py_XINCREF(item); /* final value has is increfed, to match PyObject_GetAttrString */
return item;
}