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 --- .../python3/data/fake_module_with_broken_getattr.py | 7 +++++++ tests/unittest_raw_building.py | 16 +++++++++++++++- 2 files changed, 22 insertions(+), 1 deletion(-) create mode 100644 tests/testdata/python3/data/fake_module_with_broken_getattr.py (limited to 'tests') 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