[Bf-blender-cvs] [8d839f9] temp-pyapi-units: Make units a real submodule.

Bastien Montagne noreply at git.blender.org
Tue Jun 17 10:46:11 CEST 2014


Commit: 8d839f93ac83b9a6fafdc64a0ad20c0f8271d7c2
Author: Bastien Montagne
Date:   Tue Jun 17 10:41:18 2014 +0200
https://developer.blender.org/rB8d839f93ac83b9a6fafdc64a0ad20c0f8271d7c2

Make units a real submodule.

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

M	source/blender/python/intern/bpy.c
M	source/blender/python/intern/bpy_utils_units.c
M	source/blender/python/intern/bpy_utils_units.h

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

diff --git a/source/blender/python/intern/bpy.c b/source/blender/python/intern/bpy.c
index a40a047..5fd19d3 100644
--- a/source/blender/python/intern/bpy.c
+++ b/source/blender/python/intern/bpy.c
@@ -335,7 +335,7 @@ void BPy_init_modules(void)
 	/* ops is now a python module that does the conversion from SOME_OT_foo -> some.foo */
 	PyModule_AddObject(mod, "ops", BPY_operator_module());
 	PyModule_AddObject(mod, "app", BPY_app_struct());
-	PyModule_AddObject(mod, "_utils_units", BPY_utils_units_struct());
+	PyModule_AddObject(mod, "_utils_units", BPY_utils_units());
 
 	/* bpy context */
 	RNA_pointer_create(NULL, &RNA_Context, (void *)BPy_GetContext(), &ctx_ptr);
diff --git a/source/blender/python/intern/bpy_utils_units.c b/source/blender/python/intern/bpy_utils_units.c
index 7d50bc7..283ba79 100644
--- a/source/blender/python/intern/bpy_utils_units.c
+++ b/source/blender/python/intern/bpy_utils_units.c
@@ -50,19 +50,6 @@
 #include "RNA_types.h"
 #include "RNA_access.h"
 
-typedef struct
-{
-	PyObject_HEAD
-	/* A "named tuple" (StructSequence actually...) containing all C-defined unit systems. */
-	PyObject *systems;
-	/* A "named tuple" (StructSequence actually...) containing all C-defined unit categories (types). */
-	PyObject *categories;
-} BlenderUtilsUnits;
-
-/* Our singleton instance pointer */
-static BlenderUtilsUnits *bpyunits_units = NULL;
-
-
 /***** C-defined systems and types *****/
 
 static PyTypeObject BlenderUtilsUnitsSystemsType;
@@ -148,17 +135,6 @@ static PyObject *bpyunits_categories_make(void)
 
 /***** Main BlenderAppTranslations Py object definition *****/
 
-PyDoc_STRVAR(bpyunits_systems_doc, "A named tuple containing all pre-defined unit systems.\n");
-PyDoc_STRVAR(bpyunits_categories_doc, "A named tuple containing all pre-defined unit categories.\n");
-
-static PyMemberDef bpyunits_members[] = {
-	{(char *)"systems", T_OBJECT_EX, offsetof(BlenderUtilsUnits, systems), READONLY, bpyunits_systems_doc},
-	{(char *)"categories", T_OBJECT_EX, offsetof(BlenderUtilsUnits, categories), READONLY, bpyunits_categories_doc},
-	{NULL},
-};
-
-static PyGetSetDef bpyunits_getseters[] = {{NULL}};
-
 static bool bpyunits_validate(const char *usys_str, const char *ucat_str, int *r_usys, int *r_ucat)
 {
 	*r_usys = BLI_str_index_in_array(usys_str, bpyunits_usystems);
@@ -203,7 +179,7 @@ PyDoc_STRVAR(bpyunits_to_value_doc,
 "   :raises ValueError: if conversion fails to generate a valid python float value.\n"
 "\n"
 );
-static PyObject *bpyunits_to_value(BlenderUtilsUnits *UNUSED(self), PyObject *args, PyObject *kw)
+static PyObject *bpyunits_to_value(PyObject *self, PyObject *args, PyObject *kw)
 {
 	static const char *kwlist[] = {"unit_system", "unit_category", "str_input", "str_ref_unit", NULL};
 
@@ -266,7 +242,7 @@ PyDoc_STRVAR(bpyunits_to_string_doc,
 "   :raises ValueError: if conversion fails to generate a valid python string.\n"
 "\n"
 );
-static PyObject *bpyunits_to_string(BlenderUtilsUnits *UNUSED(self), PyObject *args, PyObject *kw)
+static PyObject *bpyunits_to_string(PyObject *self, PyObject *args, PyObject *kw)
 {
 	static const char *kwlist[] = {"unit_system", "unit_category", "value",
 	                               "precision", "split_unit", "compatible_unit", NULL};
@@ -326,114 +302,36 @@ static PyMethodDef bpyunits_methods[] = {
 	{NULL, NULL, 0, NULL}
 };
 
-static PyObject *bpyunits_new(PyTypeObject *type, PyObject *UNUSED(args), PyObject *UNUSED(kw))
-{
-	if (!bpyunits_units) {
-		bpyunits_units = (BlenderUtilsUnits *)type->tp_alloc(type, 0);
-		if (bpyunits_units) {
-			bpyunits_units->systems = bpyunits_systems_make();
-			bpyunits_units->categories = bpyunits_categories_make();
-		}
-	}
-
-	return (PyObject *)bpyunits_units;
-}
-
-static void bpyunits_free(void *obj)
-{
-	PyObject_Del(obj);
-}
-
 PyDoc_STRVAR(bpyunits_doc,
-"This object contains some data/methods regarding units handling in Blender.\n"
+"This module contains some data/methods regarding units handling in Blender.\n"
 "\n"
 );
-static PyTypeObject BlenderUtilsUnitsType = {
-	PyVarObject_HEAD_INIT(NULL, 0)
-	"_utils_units",             /* tp_name */
-	sizeof(BlenderUtilsUnits),  /* tp_basicsize */
-	0,                          /* tp_itemsize */
-	/* methods */
-	/* No destructor, this is a singleton! */
-	NULL,                       /* tp_dealloc */
-	NULL,                       /* printfunc tp_print; */
-	NULL,                       /* getattrfunc tp_getattr; */
-	NULL,                       /* setattrfunc tp_setattr; */
-	NULL,                       /* tp_compare */ /* DEPRECATED in python 3.0! */
-	NULL,                       /* tp_repr */
-
-	/* Method suites for standard classes */
-	NULL,                       /* PyNumberMethods *tp_as_number; */
-	NULL,                       /* PySequenceMethods *tp_as_sequence; */
-	NULL,                       /* PyMappingMethods *tp_as_mapping; */
-
-	/* More standard operations (here for binary compatibility) */
-	NULL,                       /* hashfunc tp_hash; */
-	NULL,                       /* ternaryfunc tp_call; */
-	NULL,                       /* reprfunc tp_str; */
-	NULL,                       /* getattrofunc tp_getattro; */
-	NULL,                       /* setattrofunc tp_setattro; */
-
-	/* Functions to access object as input/output buffer */
-	NULL,                       /* PyBufferProcs *tp_as_buffer; */
-
-	/*** Flags to define presence of optional/expanded features ***/
-	Py_TPFLAGS_DEFAULT,         /* long tp_flags; */
-
-	bpyunits_doc,         /* char *tp_doc;  Documentation string */
-
-	/*** Assigned meaning in release 2.0 ***/
-	/* call function for all accessible objects */
-	NULL,                       /* traverseproc tp_traverse; */
-
-	/* delete references to contained objects */
-	NULL,                       /* inquiry tp_clear; */
-
-	/***  Assigned meaning in release 2.1 ***/
-	/*** rich comparisons ***/
-	NULL,                       /* richcmpfunc tp_richcompare; */
-
-	/***  weak reference enabler ***/
-	0,                          /* long tp_weaklistoffset */
-
-	/*** Added in release 2.2 ***/
-	/*   Iterators */
-	NULL,                       /* getiterfunc tp_iter; */
-	NULL,                       /* iternextfunc tp_iternext; */
-
-	/*** Attribute descriptor and subclassing stuff ***/
-	bpyunits_methods,           /* struct PyMethodDef *tp_methods; */
-	bpyunits_members,           /* struct PyMemberDef *tp_members; */
-	bpyunits_getseters,         /* struct PyGetSetDef *tp_getset; */
-	NULL,                       /* struct _typeobject *tp_base; */
-	NULL,                       /* PyObject *tp_dict; */
-	NULL,                       /* descrgetfunc tp_descr_get; */
-	NULL,                       /* descrsetfunc tp_descr_set; */
-	0,                          /* long tp_dictoffset; */
-	NULL,                       /* initproc tp_init; */
-	NULL,                       /* allocfunc tp_alloc; */
-	(newfunc)bpyunits_new, /* newfunc tp_new; */
-	/*  Low-level free-memory routine */
-	bpyunits_free,        /* freefunc tp_free;  */
-	/* For PyObject_IS_GC */
-	NULL,                       /* inquiry tp_is_gc;  */
-	NULL,                       /* PyObject *tp_bases; */
-	/* method resolution order */
-	NULL,                       /* PyObject *tp_mro;  */
-	NULL,                       /* PyObject *tp_cache; */
-	NULL,                       /* PyObject *tp_subclasses; */
-	NULL,                       /* PyObject *tp_weaklist; */
-	NULL
+
+static struct PyModuleDef props_module = {
+	PyModuleDef_HEAD_INIT,
+	"bpy.utils.units",
+	bpyunits_doc,
+	-1, /* multiple "initialization" just copies the module dict. */
+	bpyunits_methods,
+	NULL, NULL, NULL, NULL
 };
 
-PyObject *BPY_utils_units_struct(void)
+PyObject *BPY_utils_units(void)
 {
-	PyObject *ret;
+	PyObject *submodule;
+
+	submodule = PyModule_Create(&props_module);
+	PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, submodule);
+
+	/* INCREF since it's assumed that all these functions return the module with a new ref like PyDict_New,
+	 * since they are passed to PyModule_AddObject which steals a ref. */
+	Py_INCREF(submodule);
 
 	/* Let's finalize our unit systems and types structseq definitions! */
 	{
 		const char **sys;
 		PyStructSequence_Field *desc;
+		PyObject *systems;
 
 		/* We really populate the contexts' fields here! */
 		for (sys = bpyunits_usystems, desc = bpyunits_systems_desc.fields; *sys; sys++, desc++) {
@@ -443,10 +341,14 @@ PyObject *BPY_utils_units_struct(void)
 		desc->name = desc->doc = NULL;  /* End sentinel! */
 
 		PyStructSequence_InitType(&BlenderUtilsUnitsSystemsType, &bpyunits_systems_desc);
+
+		systems = bpyunits_systems_make();
+		PyModule_AddObject(submodule, "systems", systems);  /* Steals ref. */
 	}
 	{
 		const char **cat;
 		PyStructSequence_Field *desc;
+		PyObject *categories;
 
 		/* We really populate the contexts' fields here! */
 		for (cat = bpyunits_ucategories, desc = bpyunits_categories_desc.fields; *cat; cat++, desc++) {
@@ -456,17 +358,10 @@ PyObject *BPY_utils_units_struct(void)
 		desc->name = desc->doc = NULL;  /* End sentinel! */
 
 		PyStructSequence_InitType(&BlenderUtilsUnitsCategoriesType, &bpyunits_categories_desc);
-	}
-
-	if (PyType_Ready(&BlenderUtilsUnitsType) < 0)
-		return NULL;
-
-	ret = PyObject_CallObject((PyObject *)&BlenderUtilsUnitsType, NULL);
 
-	/* prevent user from creating new instances */
-	BlenderUtilsUnitsType.tp_new = NULL;
-	/* without this we can't do set(sys.modules) [#29635] */
-	BlenderUtilsUnitsType.tp_hash = (hashfunc)_Py_HashPointer;
+		categories = bpyunits_categories_make();
+		PyModule_AddObject(submodule, "categories", categories);  /* Steals ref. */
+	}
 
-	return ret;
+	return submodule;
 }
diff --git a/source/blender/python/intern/bpy_utils_units.h b/source/blender/python/intern/bpy_utils_units.h
index 4522022..5f840a2 100644
--- a/source/blender/python/intern/bpy_utils_units.h
+++ b/source/blender/python/intern/bpy_utils_units.h
@@ -27,6 +27,6 @@
 #ifndef __BPY_UTILS_UNITS_H__
 #define __BPY_UTILS_UNITS_H__
 
-PyObject *BPY_utils_units_struct(void);
+PyObject *BPY_utils_units(void);
 
 #endif  /* __BPY_UTILS_UNITS_H__ */




More information about the Bf-blender-cvs mailing list