diff --git a/CHANGELOG.md b/CHANGELOG.md index 0d9eaf7..cf63873 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Changelog +- 2025-11-05: + Make the `pkg_resources.declare_namespace` patch work even if there's no longer a real `pkg_resources` module. - 2025-11-05: Fix handling of namespaces that have an `__init__.py` installed. Now we read the namespaces from `*.dist-info/namespace_packages.txt` instead of looking for modules that were loaded with NamespaceLoader. diff --git a/src/horse_with_no_namespace/__about__.py b/src/horse_with_no_namespace/__about__.py index c86b0cc..d426b1d 100644 --- a/src/horse_with_no_namespace/__about__.py +++ b/src/horse_with_no_namespace/__about__.py @@ -1,4 +1,4 @@ # SPDX-FileCopyrightText: 2025 David Glick # # SPDX-License-Identifier: MIT -__version__ = "20251105.0" +__version__ = "20251105.1" diff --git a/src/horse_with_no_namespace/pkg_resources.py b/src/horse_with_no_namespace/pkg_resources.py index 9c3ac3e..65083db 100644 --- a/src/horse_with_no_namespace/pkg_resources.py +++ b/src/horse_with_no_namespace/pkg_resources.py @@ -47,10 +47,21 @@ def _lazy_load_pkg_resources(): # This is called when something from pkg_resources is accessed. # We import it here to avoid importing it at the top of the file, # which would cause a circular import. - del sys.modules["pkg_resources"] - import pkg_resources - - pkg_resources.declare_namespace = declare_namespace + # But let's be careful in case the real pkg_resources isn't available... + existing_pkg_resources = sys.modules["pkg_resources"] + try: + del sys.modules["pkg_resources"] + import pkg_resources + except ModuleNotFoundError: + # There is no actual pkg_resources module + # (maybe it's a new version of setuptools that removed it). + # So keep this stub and stop trying to replace it. + pkg_resources = sys.modules["pkg_resources"] = existing_pkg_resources + del globals()["__getattr__"] + del globals()["__dir__"] + else: + # Patch the real pkg_resources to use our declare_namespace function. + pkg_resources.declare_namespace = declare_namespace _pkg_resources = pkg_resources