PyAPI: fast keyword parsing for bpy modules
authorCampbell Barton <ideasman42@gmail.com>
Wed, 4 Oct 2017 23:52:18 +0000 (10:52 +1100)
committerCampbell Barton <ideasman42@gmail.com>
Wed, 4 Oct 2017 23:52:18 +0000 (10:52 +1100)
No functional changes.

source/blender/python/intern/bpy.c
source/blender/python/intern/bpy_library_load.c
source/blender/python/intern/bpy_library_write.c
source/blender/python/intern/bpy_rna_id_collection.c
source/blender/python/intern/bpy_utils_units.c
source/blender/python/intern/gpu.c

index 5bbfb4912e6aadc1015bc08130af622066f031e6..6e4a61484056df60ab279245d80e864159f93126 100644 (file)
@@ -118,10 +118,11 @@ static PyObject *bpy_blend_paths(PyObject *UNUSED(self), PyObject *args, PyObjec
        bool absolute = false;
        bool packed   = false;
        bool local    = false;
-       static const char *kwlist[] = {"absolute", "packed", "local", NULL};
 
-       if (!PyArg_ParseTupleAndKeywords(
-               args, kw, "|O&O&O&:blend_paths", (char **)kwlist,
+       static const char *_keywords[] = {"absolute", "packed", "local", NULL};
+       static _PyArg_Parser _parser = {"|O&O&O&:blend_paths", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
                PyC_ParseBool, &absolute,
                PyC_ParseBool, &packed,
                PyC_ParseBool, &local))
@@ -147,13 +148,18 @@ static PyObject *bpy_user_resource(PyObject *UNUSED(self), PyObject *args, PyObj
        const char *type;
        const char *subdir = NULL;
        int folder_id;
-       static const char *kwlist[] = {"type", "subdir", NULL};
 
        const char *path;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "s|s:user_resource", (char **)kwlist, &type, &subdir))
+       static const char *_keywords[] = {"type", "subdir", NULL};
+       static _PyArg_Parser _parser = {"s|s:user_resource", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
+               &type, &subdir))
+       {
                return NULL;
-       
+       }
+
        /* stupid string compare */
        if      (STREQ(type, "DATAFILES")) folder_id = BLENDER_USER_DATAFILES;
        else if (STREQ(type, "CONFIG"))    folder_id = BLENDER_USER_CONFIG;
@@ -191,12 +197,17 @@ static PyObject *bpy_resource_path(PyObject *UNUSED(self), PyObject *args, PyObj
 {
        const char *type;
        int major = BLENDER_VERSION / 100, minor = BLENDER_VERSION % 100;
-       static const char *kwlist[] = {"type", "major", "minor", NULL};
        int folder_id;
        const char *path;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "s|ii:resource_path", (char **)kwlist, &type, &major, &minor))
+       static const char *_keywords[] = {"type", "major", "minor", NULL};
+       static _PyArg_Parser _parser = {"s|ii:resource_path", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
+               &type, &major, &minor))
+       {
                return NULL;
+       }
 
        /* stupid string compare */
        if      (STREQ(type, "USER"))    folder_id = BLENDER_RESOURCE_PATH_USER;
index cb6a7147368e681654c4e6e4af47d1e1a1b8f64e..39fce293bd0f8fc6b5f0a4b4a25bf2dc48230e18 100644 (file)
@@ -183,17 +183,17 @@ PyDoc_STRVAR(bpy_lib_load_doc,
 "   :arg relative: When True the path is stored relative to the open blend file.\n"
 "   :type relative: bool\n"
 );
-static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
+static PyObject *bpy_lib_load(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
-       static const char *kwlist[] = {"filepath", "link", "relative", NULL};
        Main *bmain = CTX_data_main(BPy_GetContext());
        BPy_Library *ret;
        const char *filename = NULL;
        bool is_rel = false, is_link = false;
 
-       if (!PyArg_ParseTupleAndKeywords(
-               args, kwds,
-               "s|O&O&:load", (char **)kwlist,
+       static const char *_keywords[] = {"filepath", "link", "relative", NULL};
+       static _PyArg_Parser _parser = {"s|O&O&:load", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
                &filename,
                PyC_ParseBool, &is_link,
                PyC_ParseBool, &is_rel))
index bf91253141a8a9584355d9c32d393faf2a891b45..c054183034abea215ce7b80eff085e6d554c2f72 100644 (file)
@@ -69,24 +69,23 @@ PyDoc_STRVAR(bpy_lib_write_doc,
 "   :arg compress: When True, write a compressed blend file.\n"
 "   :type compress: bool\n"
 );
-static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
+static PyObject *bpy_lib_write(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
-       static const char *kwlist[] = {
-               "filepath", "datablocks",
-               /* optional */
-               "relative_remap", "fake_user", "compress",
-               NULL,
-       };
-
        /* args */
        const char *filepath;
        char filepath_abs[FILE_MAX];
        PyObject *datablocks = NULL;
        bool use_relative_remap = false, use_fake_user = false, use_compress = false;
 
-       if (!PyArg_ParseTupleAndKeywords(
-               args, kwds,
-               "sO!|$O&O&O&:write", (char **)kwlist,
+       static const char *_keywords[] = {
+               "filepath", "datablocks",
+               /* optional */
+               "relative_remap", "fake_user", "compress",
+               NULL,
+       };
+       static _PyArg_Parser _parser = {"sO!|$O&O&O&:write", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
                &filepath,
                &PySet_Type, &datablocks,
                PyC_ParseBool, &use_relative_remap,
index 72705ffb3fb44e73e2627954d282445331a96524..8def52dc8fb6335eb512aae711057d2199492702 100644 (file)
@@ -163,7 +163,6 @@ static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args, PyObject *
        Main *bmain = G.main;  /* XXX Ugly, but should work! */
 #endif
 
-       static const char *kwlist[] = {"subset", "key_types", "value_types", NULL};
        PyObject *subset = NULL;
 
        PyObject *key_types = NULL;
@@ -173,9 +172,10 @@ static PyObject *bpy_user_map(PyObject *UNUSED(self), PyObject *args, PyObject *
 
        PyObject *ret = NULL;
 
-
-       if (!PyArg_ParseTupleAndKeywords(
-               args, kwds, "|O$O!O!:user_map", (char **)kwlist,
+       static const char *_keywords[] = {"subset", "key_types", "value_types", NULL};
+       static _PyArg_Parser _parser = {"|O$O!O!:user_map", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kwds, &_parser,
                &subset,
                &PySet_Type, &key_types,
                &PySet_Type, &val_types))
index e0c70483e3cea500e95acf5d1e5c2666f91dec85..0ef689d1a5a3b52babc31f209e92246961ab6cc3 100644 (file)
@@ -174,8 +174,6 @@ PyDoc_STRVAR(bpyunits_to_value_doc,
 );
 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};
-
        char *usys_str = NULL, *ucat_str = NULL, *inpt = NULL, *uref = NULL;
        const float scale = 1.0f;
 
@@ -185,8 +183,13 @@ static PyObject *bpyunits_to_value(PyObject *UNUSED(self), PyObject *args, PyObj
        int usys, ucat;
        PyObject *ret;
 
-       if (!PyArg_ParseTupleAndKeywords(args, kw, "sss#|z:bpy.utils.units.to_value", (char **)kwlist,
-                                        &usys_str, &ucat_str, &inpt, &str_len, &uref))
+       static const char *_keywords[] = {
+               "unit_system", "unit_category", "str_input", "str_ref_unit", NULL,
+       };
+       static _PyArg_Parser _parser = {"sss#|z:to_value", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
+               &usys_str, &ucat_str, &inpt, &str_len, &uref))
        {
                return NULL;
        }
@@ -244,9 +247,6 @@ PyDoc_STRVAR(bpyunits_to_string_doc,
 );
 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};
-
        char *usys_str = NULL, *ucat_str = NULL;
        double value = 0.0;
        int precision = 3;
@@ -254,9 +254,13 @@ static PyObject *bpyunits_to_string(PyObject *UNUSED(self), PyObject *args, PyOb
 
        int usys, ucat;
 
-       if (!PyArg_ParseTupleAndKeywords(
-               args, kw,
-               "ssd|iO&O&:bpy.utils.units.to_string", (char **)kwlist,
+       static const char *_keywords[] = {
+               "unit_system", "unit_category", "value",
+               "precision", "split_unit", "compatible_unit", NULL,
+       };
+       static _PyArg_Parser _parser = {"ssd|iO&O&:to_string", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
                &usys_str, &ucat_str, &value, &precision,
                PyC_ParseBool, &split_unit,
                PyC_ParseBool, &compatible_unit))
index 48230a723d2212c330b9a09ebd6bf97dc32d36c2..60ce00c583e5272843a8c7eef12e83d0a6b67aa3 100644 (file)
@@ -207,7 +207,7 @@ PyDoc_STRVAR(GPU_export_shader_doc,
 "   :return: Dictionary defining the shader, uniforms and attributes.\n"
 "   :rtype: Dict"
 );
-static PyObject *GPU_export_shader(PyObject *UNUSED(self), PyObject *args, PyObject *kwds)
+static PyObject *GPU_export_shader(PyObject *UNUSED(self), PyObject *args, PyObject *kw)
 {
        PyObject *pyscene;
        PyObject *pymat;
@@ -224,11 +224,14 @@ static PyObject *GPU_export_shader(PyObject *UNUSED(self), PyObject *args, PyObj
        GPUInputUniform *uniform;
        GPUInputAttribute *attribute;
 
-       static const char *kwlist[] = {"scene", "material", NULL};
-
-       if (!PyArg_ParseTupleAndKeywords(args, kwds, "OO:export_shader", (char **)(kwlist), &pyscene, &pymat))
+       static const char *_keywords[] = {"scene", "material", NULL};
+       static _PyArg_Parser _parser = {"OO:export_shader", _keywords, 0};
+       if (!_PyArg_ParseTupleAndKeywordsFast(
+               args, kw, &_parser,
+               &pyscene, &pymat))
+       {
                return NULL;
-
+       }
        scene = (Scene *)PyC_RNA_AsPointer(pyscene, "Scene");
        if (scene == NULL) {
                return NULL;