diff options
| author | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2023-01-10 23:18:16 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-01-10 22:18:16 +0000 |
| commit | 9eb8c47ddb6b48e14dbdb87bb1b02fcb580cb20d (patch) | |
| tree | d653afb5983cf7a4338b491edc5c06650371ab05 | |
| parent | 49691cc04f2d38b174787280f7ed38f818c828bd (diff) | |
| download | astroid-git-9eb8c47ddb6b48e14dbdb87bb1b02fcb580cb20d.tar.gz | |
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 <me@the-compiler.org>
Closes #1958
| -rw-r--r-- | ChangeLog | 2 | ||||
| -rw-r--r-- | astroid/raw_building.py | 13 | ||||
| -rw-r--r-- | tests/testdata/python3/data/fake_module_with_broken_getattr.py | 7 | ||||
| -rw-r--r-- | tests/unittest_raw_building.py | 16 |
4 files changed, 36 insertions, 2 deletions
@@ -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() |
