From 22dacffd5c6be242125311adaac610f95dd094e3 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Tue, 10 Jan 2023 22:39:19 +0000 Subject: Fix a regression in 2.13.2 where a ``RunTimeError`` could be raised unexpectedly (#1959) (#1962) * Add a unit test for broken __getattr__ in extension modules Co-authored-by: Florian Bruhin Closes #1958 (cherry picked from commit 9eb8c47ddb6b48e14dbdb87bb1b02fcb580cb20d) Co-authored-by: Pierre Sassoulas --- ChangeLog | 2 ++ astroid/raw_building.py | 13 ++++++++++++- .../python3/data/fake_module_with_broken_getattr.py | 7 +++++++ tests/unittest_raw_building.py | 16 +++++++++++++++- 4 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 tests/testdata/python3/data/fake_module_with_broken_getattr.py diff --git a/ChangeLog b/ChangeLog index c61ee159..94208b67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,7 +12,9 @@ What's New in astroid 2.13.3? ============================= Release date: TBA +* Fix a regression in 2.13.2 where a RunTimeError could be raised unexpectedly. + Closes #1958 What's New in astroid 2.13.2? ============================= 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 diff --git a/tests/testdata/python3/data/fake_module_with_broken_getattr.py b/tests/testdata/python3/data/fake_module_with_broken_getattr.py new file mode 100644 index 00000000..e860928e --- /dev/null +++ b/tests/testdata/python3/data/fake_module_with_broken_getattr.py @@ -0,0 +1,7 @@ +class Broken: + + def __getattr__(self, name): + raise Exception("boom") + + +broken = Broken() diff --git a/tests/unittest_raw_building.py b/tests/unittest_raw_building.py index 59bd6ce1..64c22727 100644 --- a/tests/unittest_raw_building.py +++ b/tests/unittest_raw_building.py @@ -1,3 +1,9 @@ +""" +'tests.testdata.python3.data.fake_module_with_warnings' and +'tests.testdata.python3.data.fake_module_with_warnings' are fake modules +to simulate issues in unittest below +""" + # Licensed under the LGPL: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.en.html # For details: https://github.com/PyCQA/astroid/blob/main/LICENSE # Copyright (c) https://github.com/PyCQA/astroid/blob/main/CONTRIBUTORS.txt @@ -8,7 +14,7 @@ import unittest import _io import pytest -# A fake module to simulate pandas in unittest below +import tests.testdata.python3.data.fake_module_with_broken_getattr as fm_getattr import tests.testdata.python3.data.fake_module_with_warnings as fm from astroid.builder import AstroidBuilder from astroid.const import IS_PYPY @@ -102,6 +108,14 @@ class RawBuildingTC(unittest.TestCase): # This should not raise an exception AstroidBuilder().module_build(m, "test") + def test_module_object_with_broken_getattr(self) -> None: + # Tests https://github.com/PyCQA/astroid/issues/1958 + # When astroid deep inspection of modules raises + # errors when using hasattr(). + + # This should not raise an exception + AstroidBuilder().inspect_build(fm_getattr, "test") + if __name__ == "__main__": unittest.main() -- cgit v1.2.1