Commit: 5c8c900ad64fbfa531957184d2a53a70ccb4c11e
Author: Campbell Barton
Date:   Tue Jun 17 19:53:06 2014 +1000
https://developer.blender.org/rB5c8c900ad64fbfa531957184d2a53a70ccb4c11e

Deduplicate some code for struct-seq initialization

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

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

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

diff --git a/source/blender/python/intern/bpy_utils_units.c 
b/source/blender/python/intern/bpy_utils_units.c
index 283ba79..68d6b0f 100644
--- a/source/blender/python/intern/bpy_utils_units.c
+++ b/source/blender/python/intern/bpy_utils_units.c
@@ -52,18 +52,18 @@
 
 /***** C-defined systems and types *****/
 
-static PyTypeObject BlenderUtilsUnitsSystemsType;
-static PyTypeObject BlenderUtilsUnitsCategoriesType;
+static PyTypeObject BPyUnitsSystemsType;
+static PyTypeObject BPyUnitsCategoriesType;
 
 /* XXX Maybe better as externs of BKE_unit.h ? */
-static const char *bpyunits_usystems[] = {
+static const char *bpyunits_usystem_items[] = {
        "NONE",
        "METRIC",
        "IMPERIAL",
        NULL,
 };
 
-static const char *bpyunits_ucategories[] = {
+static const char *bpyunits_ucategorie_items[] = {
        "NONE",
        "LENGTH",
        "AREA",
@@ -77,12 +77,13 @@ static const char *bpyunits_ucategories[] = {
        NULL,
 };
 
-/* These fields are just empty placeholders, actual values get set in 
app_translations_struct().
+/**
+ * These fields are just empty placeholders, actual values get set in 
initializations functions.
  * This allows us to avoid many handwriting, and above all, to keep all 
systems/categories definition stuff in
- * BKE_unit.h!
+ * ``BKE_unit.h``.
  */
-static PyStructSequence_Field 
bpyunits_systems_fields[ARRAY_SIZE(bpyunits_usystems)] = {{NULL}};
-static PyStructSequence_Field 
bpyunits_categories_fields[ARRAY_SIZE(bpyunits_ucategories)] = {{NULL}};
+static PyStructSequence_Field 
bpyunits_systems_fields[ARRAY_SIZE(bpyunits_usystem_items)];
+static PyStructSequence_Field 
bpyunits_categories_fields[ARRAY_SIZE(bpyunits_ucategorie_items)];
 
 static PyStructSequence_Desc bpyunits_systems_desc = {
        (char *)"bpy.utils.units.systems",     /* name */
@@ -97,47 +98,47 @@ static PyStructSequence_Desc bpyunits_categories_desc = {
        ARRAY_SIZE(bpyunits_categories_fields) - 1
 };
 
-static PyObject *bpyunits_systems_make(void)
+/**
+ * Simple utility function to initialize #PyStructSequence_Desc
+ */
+static PyObject *py_structseq_from_strings(
+        PyTypeObject *py_type,
+        PyStructSequence_Desc *py_sseq_desc,
+        const char **str_items)
 {
-       PyObject *units_systems;
-       const char **sys;
+       PyObject *py_struct_seq;
        int pos = 0;
 
-       units_systems = PyStructSequence_New(&BlenderUtilsUnitsSystemsType);
-       if (units_systems == NULL) {
-               return NULL;
-       }
+       const char **str_iter;
+       PyStructSequence_Field *desc;
 
-       for (sys = bpyunits_usystems; *sys; sys++) {
-               PyStructSequence_SET_ITEM(units_systems, pos++, 
PyUnicode_FromString((*sys)));
+       /* initialize array */
+       /* We really populate the contexts' fields here! */
+       for (str_iter = str_items, desc = py_sseq_desc->fields; *str_iter; 
str_iter++, desc++) {
+               desc->name = (char *)*str_iter;
+               desc->doc = NULL;
        }
+       /* end sentinel */
+       desc->name = desc->doc = NULL;
 
-       return units_systems;
-}
 
-static PyObject *bpyunits_categories_make(void)
-{
-       PyObject *units_categories;
-       const char **categories;
-       int pos = 0;
+       PyStructSequence_InitType(py_type, py_sseq_desc);
 
-       units_categories = 
PyStructSequence_New(&BlenderUtilsUnitsCategoriesType);
-       if (units_categories == NULL) {
-               return NULL;
-       }
+       /* initialize pytype */
+       py_struct_seq = PyStructSequence_New(py_type);
+       BLI_assert(py_struct_seq != NULL);
 
-       for (categories = bpyunits_ucategories; *categories; categories++) {
-               PyStructSequence_SET_ITEM(units_categories, pos++, 
PyUnicode_FromString((*categories)));
+       for (str_iter = str_items; *str_iter; str_iter++) {
+               PyStructSequence_SET_ITEM(py_struct_seq, pos++, 
PyUnicode_FromString((*str_iter)));
        }
 
-       return units_categories;
-}
+       return py_struct_seq;
 
-/***** Main BlenderAppTranslations Py object definition *****/
+}
 
 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);
+       *r_usys = BLI_str_index_in_array(usys_str, bpyunits_usystem_items);
        if (*r_usys < 0) {
                PyErr_Format(PyExc_ValueError,
                             "Unknown unit system specified: %.200s.",
@@ -145,7 +146,7 @@ static bool bpyunits_validate(const char *usys_str, const 
char *ucat_str, int *r
                return false;
        }
 
-       *r_ucat = BLI_str_index_in_array(ucat_str, bpyunits_ucategories);
+       *r_ucat = BLI_str_index_in_array(ucat_str, bpyunits_ucategorie_items);
        if (*r_ucat < 0) {
                PyErr_Format(PyExc_ValueError,
                             "Unknown unit category specified: %.200s.",
@@ -168,18 +169,17 @@ PyDoc_STRVAR(bpyunits_to_value_doc,
 "\n"
 "   Convert a given input string into a float value.\n"
 "\n"
-"   :arg str unit_system: The unit system, from 
:attribute:`bpy.utils.units.systems`.\n"
+"   :arg str unit_system: The unit system, from 
:attr:`bpy.utils.units.systems`.\n"
 "   :arg str unit_category: The category of data we are converting (length, 
area, rotation, etc.), "
-"      from :attribute:`bpy.utils.units.categories`.\n"
+"      from :attr:`bpy.utils.units.categories`.\n"
 "   :arg str str_input: The string to convert to a float value.\n"
 "   :arg str_ref_unit: A reference string from which to extract a default 
unit, if none is found in :arg:`str_input`.\n"
 "   :type: string or None\n"
 "   :return: The converted/interpreted value.\n"
 "   :rtype: float\n"
 "   :raises ValueError: if conversion fails to generate a valid python float 
value.\n"
-"\n"
 );
-static PyObject *bpyunits_to_value(PyObject *self, PyObject *args, PyObject 
*kw)
+static PyObject *bpyunits_to_value(PyObject *UNUSED(self), PyObject *args, 
PyObject *kw)
 {
        static const char *kwlist[] = {"unit_system", "unit_category", 
"str_input", "str_ref_unit", NULL};
 
@@ -230,9 +230,9 @@ PyDoc_STRVAR(bpyunits_to_string_doc,
 "\n"
 "   Convert a given input float value into a string with units.\n"
 "\n"
-"   :arg str unit_system: The unit system, from 
:attribute:`bpy.utils.units.systems`.\n"
+"   :arg str unit_system: The unit system, from 
:attr:`bpy.utils.units.systems`.\n"
 "   :arg str unit_category: The category of data we are converting (length, 
area, rotation, etc.), "
-"      from :attribute:`bpy.utils.units.categories`.\n"
+"      from :attr:`bpy.utils.units.categories`.\n"
 "   :arg float value: The value to convert to a string.\n"
 "   :arg int precision: Number of digits after the comma.\n"
 "   :arg bool split_unit: Whether to use several units if needed (1m1cm), or 
always only one (1.01m).\n"
@@ -240,9 +240,8 @@ PyDoc_STRVAR(bpyunits_to_string_doc,
 "   :return: The converted string.\n"
 "   :rtype: str\n"
 "   :raises ValueError: if conversion fails to generate a valid python 
string.\n"
-"\n"
 );
-static PyObject *bpyunits_to_string(PyObject *self, PyObject *args, PyObject 
*kw)
+static PyObject *bpyunits_to_string(PyObject *UNUSED(self), PyObject *args, 
PyObject *kw)
 {
        static const char *kwlist[] = {"unit_system", "unit_category", "value",
                                       "precision", "split_unit", 
"compatible_unit", NULL};
@@ -303,11 +302,10 @@ static PyMethodDef bpyunits_methods[] = {
 };
 
 PyDoc_STRVAR(bpyunits_doc,
-"This module contains some data/methods regarding units handling in Blender.\n"
-"\n"
+"This module contains some data/methods regarding units handling."
 );
 
-static struct PyModuleDef props_module = {
+static struct PyModuleDef bpyunits_module = {
        PyModuleDef_HEAD_INIT,
        "bpy.utils.units",
        bpyunits_doc,
@@ -318,50 +316,21 @@ static struct PyModuleDef props_module = {
 
 PyObject *BPY_utils_units(void)
 {
-       PyObject *submodule;
-
-       submodule = PyModule_Create(&props_module);
-       PyDict_SetItemString(PyImport_GetModuleDict(), props_module.m_name, 
submodule);
+       PyObject *submodule, *item;
 
-       /* 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. */
+       submodule = PyModule_Create(&bpyunits_module);
+       PyDict_SetItemString(PyImport_GetModuleDict(), bpyunits_module.m_name, 
submodule);
        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++) {
-                       desc->name = (char *)*sys;
-                       desc->doc = NULL;
-               }
-               desc->name = desc->doc = NULL;  /* End sentinel! */
+       /* Finalize our unit systems and types structseq definitions! */
 
-               PyStructSequence_InitType(&BlenderUtilsUnitsSystemsType, 
&bpyunits_systems_desc);
+       /* bpy.utils.units.system */
+       item = py_structseq_from_strings(&BPyUnitsSystemsType, 
&bpyunits_systems_desc, bpyunits_usystem_items);
+       PyModule_AddObject(submodule, "systems", item);  /* steals ref */
 
-               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++) {
-                       desc->name = (char *)*cat;
-                       desc->doc = NULL;
-               }
-               desc->name = desc->doc = NULL;  /* End sentinel! */
-
-               PyStructSequence_InitType(&BlenderUtilsUnitsCategoriesType, 
&bpyunits_categories_desc);
-
-               categories = bpyunits_categories_make();
-               PyModule_AddObject(submodule, "categories", categories);  /* 
Steals ref. */
-       }
+       /* bpy.utils.units.categories */
+       item = py_structseq_from_strings(&BPyUnitsCategoriesType, 
&bpyunits_categories_desc, bpyunits_ucategorie_items);
+       PyModule_AddObject(submodule, "categories", item);  /* steals ref */
 
        return submodule;
 }

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
http://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to