From 9eb8c47ddb6b48e14dbdb87bb1b02fcb580cb20d Mon Sep 17 00:00:00 2001 From: Pierre Sassoulas Date: Tue, 10 Jan 2023 23:18:16 +0100 Subject: Fix a regression in 2.13.2 where a ``RunTimeError`` could be raised unexpectedly (#1959) * Add a unit test for broken __getattr__ in extension modules Co-authored-by: Florian Bruhin Closes #1958 --- astroid/raw_building.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'astroid') diff --git a/astroid/raw_building.py b/astroid/raw_building.py index fe19bb75..cc3aa015 100644 --- a/astroid/raw_building.py +++ b/astroid/raw_building.py @@ -324,6 +324,17 @@ def _build_from_function( object_build_function(node, member, name) +def _safe_has_attribute(obj, member: str) -> bool: + """Required because unexpected RunTimeError can be raised. + + See https://github.com/PyCQA/astroid/issues/1958 + """ + try: + return hasattr(obj, member) + except Exception: # pylint: disable=broad-except + return False + + class InspectBuilder: """class for building nodes from living object @@ -419,7 +430,7 @@ class InspectBuilder: # This should be called for Jython, where some builtin # methods aren't caught by isbuiltin branch. _build_from_function(node, name, member, self._module) - elif hasattr(member, "__all__"): + elif _safe_has_attribute(member, "__all__"): module = build_module(name) _attach_local_node(node, module, name) # recursion -- cgit v1.2.1