From 90d513f7d3c9bd7a893ecf4d6dfd99b5a0cc744f Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Sat, 1 Jun 2024 16:08:56 +1000 Subject: [PATCH] Cleanup: improve checks for function types for RNA introspection Function checks were too ambiguous making debugging difficult. --- scripts/modules/rna_info.py | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/scripts/modules/rna_info.py b/scripts/modules/rna_info.py index cb76e3d0c22..60a9d475548 100644 --- a/scripts/modules/rna_info.py +++ b/scripts/modules/rna_info.py @@ -187,9 +187,15 @@ class InfoStructRNA: import types functions = [] for identifier, attr in self._get_py_visible_attrs(): - # methods may be python wrappers to C functions - attr_func = getattr(attr, "__func__", attr) - if type(attr_func) in {types.FunctionType, types.MethodType}: + # Methods may be python wrappers to C functions. + ok = False + if (attr_func := getattr(attr, "__func__", None)) is not None: + if type(attr_func) == types.FunctionType: + ok = True + else: + if type(attr) in {types.FunctionType, types.MethodType}: + ok = True + if ok: functions.append((identifier, attr)) return functions @@ -197,13 +203,19 @@ class InfoStructRNA: import types functions = [] for identifier, attr in self._get_py_visible_attrs(): - # methods may be python wrappers to C functions - attr_func = getattr(attr, "__func__", attr) - if ( - (type(attr_func) in {types.BuiltinMethodType, types.BuiltinFunctionType}) or + # Methods may be python wrappers to C functions. + ok = False + if (attr_func := getattr(attr, "__func__", None)) is not None: + if type(attr_func) == types.BuiltinFunctionType: + ok = True + else: + if type(attr) == types.BuiltinMethodType: + ok = True + elif type(attr) == types.MethodDescriptorType: # Without the `objclass` check, many inherited methods are included. - (type(attr_func) == types.MethodDescriptorType and attr_func.__objclass__ == self.py_class) - ): + if attr.__objclass__ == self.py_class: + ok = True + if ok: functions.append((identifier, attr)) return functions