Skip to content

Commit

Permalink
Prevent a crash when a module's __path__ is missing
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed Oct 19, 2022
1 parent fccbc97 commit a97d958
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
2 changes: 2 additions & 0 deletions ChangeLog
Expand Up @@ -24,7 +24,9 @@ Release date: TBA

Refs PyCQA/pylint#4039

* Prevent a crash when a module's ``__path__`` attribute is unexpectedly missing.

Refs PyCQA/pylint#7592

What's New in astroid 2.12.11?
==============================
Expand Down
2 changes: 2 additions & 0 deletions astroid/interpreter/_import/util.py
Expand Up @@ -42,6 +42,8 @@ def is_namespace(modname: str) -> bool:
found_spec = _find_spec_from_path(
working_modname, path=last_submodule_search_locations
)
except AttributeError:
return False
except ValueError:
if modname == "__main__":
return False
Expand Down
9 changes: 9 additions & 0 deletions tests/unittest_manager.py
Expand Up @@ -9,6 +9,7 @@
import unittest
from collections.abc import Iterator
from contextlib import contextmanager
from unittest import mock

import pytest

Expand Down Expand Up @@ -144,6 +145,14 @@ def test_module_unexpectedly_missing_spec(self) -> None:
finally:
astroid_module.__spec__ = original_spec

@mock.patch(
"astroid.interpreter._import.util._find_spec_from_path",
side_effect=AttributeError,
)
def test_module_unexpectedly_missing_path(self, mocked) -> None:
"""https://github.com/PyCQA/pylint/issues/7592"""
self.assertFalse(util.is_namespace("astroid"))

def test_module_unexpectedly_spec_is_none(self) -> None:
astroid_module = sys.modules["astroid"]
original_spec = astroid_module.__spec__
Expand Down

0 comments on commit a97d958

Please sign in to comment.