From 2be955b18fe5d7e3f930f30cbd836a85e441ee94 Mon Sep 17 00:00:00 2001 From: Pavel Karateev Date: Wed, 25 Jun 2025 16:56:29 +0200 Subject: [PATCH] PY-77993 drop legacy `pkg_resources`-style namespace package declarations (cherry picked from commit 953ecce0b4f80ecd176f3f867aeed90324e8420e) IJ-MR-166981 GitOrigin-RevId: a7522b79c0fec5d363453d0875b9529b7f232e3b --- .../my_extensions/pydevd_plugins/__init__.py | 7 ++-- .../pydevd_plugins/extensions/__init__.py | 7 ++-- .../test_pydevd_extension_utils.py | 24 +++++++++++++ .../helpers/pydev/pydevd_plugins/__init__.py | 7 ++-- .../pydev/pydevd_plugins/extensions/README.md | 34 +++++++++---------- .../pydevd_plugins/extensions/__init__.py | 7 ++-- .../pydevd_plugins/extensions/types/README.md | 30 ---------------- .../extensions/types/__init__.py | 7 ++-- 8 files changed, 51 insertions(+), 72 deletions(-) create mode 100644 python/helpers/pydev/pydev_tests_python/test_pydevd_extension_utils.py delete mode 100644 python/helpers/pydev/pydevd_plugins/extensions/types/README.md diff --git a/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/__init__.py b/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/__init__.py index afff0c07ddeb..bb61062c911c 100644 --- a/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/__init__.py +++ b/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/__init__.py @@ -1,5 +1,2 @@ -try: - __import__('pkg_resources').declare_namespace(__name__) -except ImportError: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/extensions/__init__.py b/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/extensions/__init__.py index afff0c07ddeb..bb61062c911c 100644 --- a/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/extensions/__init__.py +++ b/python/helpers/pydev/pydev_tests_python/my_extensions/pydevd_plugins/extensions/__init__.py @@ -1,5 +1,2 @@ -try: - __import__('pkg_resources').declare_namespace(__name__) -except ImportError: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/python/helpers/pydev/pydev_tests_python/test_pydevd_extension_utils.py b/python/helpers/pydev/pydev_tests_python/test_pydevd_extension_utils.py new file mode 100644 index 000000000000..6013b4ed6c0d --- /dev/null +++ b/python/helpers/pydev/pydev_tests_python/test_pydevd_extension_utils.py @@ -0,0 +1,24 @@ +import os + +import pytest + + +@pytest.fixture +def with_my_extensions(monkeypatch): + my_extensions_path = os.path.join(os.path.dirname(__file__), "my_extensions") + monkeypatch.syspath_prepend(my_extensions_path) + + +def test_could_load_extensions(with_my_extensions): + from _pydevd_bundle.pydevd_extension_utils import ExtensionManager + + em = ExtensionManager() + em._load_modules() + + expected_modules = [ + 'pydevd_plugins.extensions.pydevd_plugin_test_events', + 'pydevd_plugins.extensions.pydevd_plugin_test_exttype', + 'pydevd_plugins.extensions.types.pydevd_plugin_numpy_types', + 'pydevd_plugins.extensions.types.pydevd_plugins_django_form_str', + ] + assert [m.__name__ for m in em.loaded_extensions] == expected_modules diff --git a/python/helpers/pydev/pydevd_plugins/__init__.py b/python/helpers/pydev/pydevd_plugins/__init__.py index 867169c5cb95..bb61062c911c 100644 --- a/python/helpers/pydev/pydevd_plugins/__init__.py +++ b/python/helpers/pydev/pydevd_plugins/__init__.py @@ -1,5 +1,2 @@ -try: - __import__('pkg_resources').declare_namespace(__name__) -except: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/python/helpers/pydev/pydevd_plugins/extensions/README.md b/python/helpers/pydev/pydevd_plugins/extensions/README.md index 030e303ee89a..02174adee554 100644 --- a/python/helpers/pydev/pydevd_plugins/extensions/README.md +++ b/python/helpers/pydev/pydevd_plugins/extensions/README.md @@ -1,25 +1,25 @@ -Extensions allow extending the debugger without modifying the debugger code. This is implemented with explicit namespace -packages. +Extensions allow extending the debugger without modifying the debugger code. +This is implemented with explicit namespace packages. -To implement your own extension: +To implement your own extension ... -1. Ensure that the root folder of your extension is in sys.path (add it to PYTHONPATH) +1. Ensure that the root folder of your extension is in `sys.path` (add it to `PYTHONPATH`) 2. Ensure that your module follows the directory structure below -3. The ``__init__.py`` files inside the pydevd_plugin and extension folder must contain the preamble below, -and nothing else. -Preamble: +3. The `__init__.py` files inside the pydevd_plugin and extension folder must contain the preamble below, + and nothing else +4. Your plugin name inside the extensions folder must start with `pydevd_plugin` +5. Implement one or more of the abstract base classes defined in `_pydevd_bundle.pydevd_extension_api`, + this can be done by either inheriting from them or registering with the abstract base class + +# Preamble + ```python -try: - __import__('pkg_resources').declare_namespace(__name__) -except ImportError: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) ``` -4. Your plugin name inside the extensions folder must start with `"pydevd_plugin"` -5. Implement one or more of the abstract base classes defined in `_pydevd_bundle.pydevd_extension_api`. This can be done -by either inheriting from them or registering with the abstract base class. + +# Directory Structure -* Directory structure: ``` |-- root_directory-> must be on python path | |-- pydevd_plugins @@ -27,4 +27,4 @@ by either inheriting from them or registering with the abstract base class. | | |-- extensions | | | |-- __init__.py -> must contain preamble | | | |-- pydevd_plugin_plugin_name.py -``` \ No newline at end of file +``` diff --git a/python/helpers/pydev/pydevd_plugins/extensions/__init__.py b/python/helpers/pydev/pydevd_plugins/extensions/__init__.py index 867169c5cb95..bb61062c911c 100644 --- a/python/helpers/pydev/pydevd_plugins/extensions/__init__.py +++ b/python/helpers/pydev/pydevd_plugins/extensions/__init__.py @@ -1,5 +1,2 @@ -try: - __import__('pkg_resources').declare_namespace(__name__) -except: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__) diff --git a/python/helpers/pydev/pydevd_plugins/extensions/types/README.md b/python/helpers/pydev/pydevd_plugins/extensions/types/README.md deleted file mode 100644 index 030e303ee89a..000000000000 --- a/python/helpers/pydev/pydevd_plugins/extensions/types/README.md +++ /dev/null @@ -1,30 +0,0 @@ -Extensions allow extending the debugger without modifying the debugger code. This is implemented with explicit namespace -packages. - -To implement your own extension: - -1. Ensure that the root folder of your extension is in sys.path (add it to PYTHONPATH) -2. Ensure that your module follows the directory structure below -3. The ``__init__.py`` files inside the pydevd_plugin and extension folder must contain the preamble below, -and nothing else. -Preamble: -```python -try: - __import__('pkg_resources').declare_namespace(__name__) -except ImportError: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) -``` -4. Your plugin name inside the extensions folder must start with `"pydevd_plugin"` -5. Implement one or more of the abstract base classes defined in `_pydevd_bundle.pydevd_extension_api`. This can be done -by either inheriting from them or registering with the abstract base class. - -* Directory structure: -``` -|-- root_directory-> must be on python path -| |-- pydevd_plugins -| | |-- __init__.py -> must contain preamble -| | |-- extensions -| | | |-- __init__.py -> must contain preamble -| | | |-- pydevd_plugin_plugin_name.py -``` \ No newline at end of file diff --git a/python/helpers/pydev/pydevd_plugins/extensions/types/__init__.py b/python/helpers/pydev/pydevd_plugins/extensions/types/__init__.py index 867169c5cb95..bb61062c911c 100644 --- a/python/helpers/pydev/pydevd_plugins/extensions/types/__init__.py +++ b/python/helpers/pydev/pydevd_plugins/extensions/types/__init__.py @@ -1,5 +1,2 @@ -try: - __import__('pkg_resources').declare_namespace(__name__) -except: - import pkgutil - __path__ = pkgutil.extend_path(__path__, __name__) +import pkgutil +__path__ = pkgutil.extend_path(__path__, __name__)