diff --git a/ChangeLog b/ChangeLog index 7c8a7d443..977d8ead5 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,12 @@ What's New in astroid 2.12.10? ============================== Release date: TBA + +* Fixed a crash when introspecting modules compiled by `cffi`. + + Closes #1776 + Closes PyCQA/pylint#7399 + * ``decorators.cached`` now gets its cache cleared by calling ``AstroidManager.clear_cache``. Refs #1780 diff --git a/astroid/interpreter/_import/util.py b/astroid/interpreter/_import/util.py index 8cdb8ea19..c9466999a 100644 --- a/astroid/interpreter/_import/util.py +++ b/astroid/interpreter/_import/util.py @@ -49,7 +49,13 @@ def is_namespace(modname: str) -> bool: # .pth files will be on sys.modules # __spec__ is set inconsistently on PyPy so we can't really on the heuristic here # See: https://foss.heptapod.net/pypy/pypy/-/issues/3736 - return sys.modules[modname].__spec__ is None and not IS_PYPY + # Check first fragment of modname, e.g. "astroid", not "astroid.interpreter" + # because of cffi's behavior + # See: https://github.com/PyCQA/astroid/issues/1776 + return ( + sys.modules[processed_components[0]].__spec__ is None + and not IS_PYPY + ) except KeyError: return False except AttributeError: