Skip to content

Commit

Permalink
fix: incomplete pkg_resources support [backport 2.5] (#8180)
Browse files Browse the repository at this point in the history
Backport cc0a798 from #8176 to 2.5.

We fix an incomplete support for pkg_resources. This change covers for
the case where pkg_resources was somehow already imported before
ddtrace.

Follow-up on #8113.

## Checklist

- [x] Change(s) are motivated and described in the PR description
- [x] Testing strategy is described if automated tests are not included
in the PR
- [x] Risks are described (performance impact, potential for breakage,
maintainability)
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] [Library release note
guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html)
are followed or label `changelog/no-changelog` is set
- [x] Documentation is included (in-code, generated user docs, [public
corp docs](https://github.com/DataDog/documentation/))
- [x] Backport labels are set (if
[applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting))
- [x] If this PR changes the public interface, I've notified
`@DataDog/apm-tees`.
- [x] If change touches code that signs or publishes builds or packages,
or handles credentials of any kind, I've requested a review from
`@DataDog/security-design-and-guidance`.

## Reviewer Checklist

- [x] Title is accurate
- [x] All changes are related to the pull request's stated goal
- [x] Description motivates each change
- [x] Avoids breaking
[API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces)
changes
- [x] Testing strategy adequately addresses listed risks
- [x] Change is maintainable (easy to change, telemetry, documentation)
- [x] Release note makes sense to a user of the library
- [x] Author has acknowledged and discussed the performance implications
of this PR as reported in the benchmarks PR comment
- [x] Backport labels are set in a manner that is consistent with the
[release branch maintenance
policy](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)

Co-authored-by: Gabriele N. Tornetta <P403n1x87@users.noreply.github.com>
  • Loading branch information
github-actions[bot] and P403n1x87 committed Jan 24, 2024
1 parent 5705913 commit 64ba71c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 6 deletions.
9 changes: 8 additions & 1 deletion ddtrace/internal/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,12 @@ def __init__(self):
# type: () -> None
self._finding = set() # type: Set[str]

# DEV: pkg_resources support to prevent errors such as
# NotImplementedError: Can't perform this operation for unregistered
pkg_resources = sys.modules.get("pkg_resources")
if pkg_resources is not None:
pkg_resources.register_loader_type(_ImportHookChainedLoader, pkg_resources.DefaultProvider)

def _add_to_meta_path(self):
# type: () -> None
sys.meta_path.insert(0, self) # type: ignore[arg-type]
Expand Down Expand Up @@ -369,9 +375,10 @@ class ModuleWatchdog(BaseModuleWatchdog):

def __init__(self):
# type: () -> None
super().__init__()

self._hook_map = defaultdict(list) # type: DefaultDict[str, List[ModuleHookType]]
self._om = None # type: Optional[Dict[str, ModuleType]]
self._finding = set() # type: Set[str]
self._pre_exec_module_hooks = [] # type: List[Tuple[PreExecHookCond, PreExecHookType]]

@property
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fix an incomplete support for pkg_resouces that could have caused an
exception on start-up.
31 changes: 26 additions & 5 deletions tests/internal/test_module.py
Original file line number Diff line number Diff line change
Expand Up @@ -453,15 +453,36 @@ def test_module_watchdog_namespace_import_no_warnings():
import namespace_test.ns_module # noqa:F401


@pytest.mark.subprocess(ddtrace_run=True, env=dict(NSPATH=str(Path(__file__).parent)))
def test_module_watchdog_pkg_resources_support():
# Test that we can access resource files with pkg_resources without raising
# an exception.
import os
import sys

sys.path.insert(0, os.getenv("NSPATH"))

import pkg_resources as p

p.resource_listdir("namespace_test.ns_module", ".")


@pytest.mark.subprocess(
ddtrace_run=True,
env=dict(
PYTHONPATH=os.pathsep.join((str(Path(__file__).parent), os.environ.get("PYTHONPATH", ""))),
),
env=dict(NSPATH=str(Path(__file__).parent)),
err=lambda _: "Can't perform this operation for unregistered loader type" not in _,
)
def test_module_watchdog_pkg_resources_support():
def test_module_watchdog_pkg_resources_support_already_imported():
# Test that we can access resource files with pkg_resources without raising
# an exception.
import os
import sys

assert "ddtrace" not in sys.modules

sys.path.insert(0, os.getenv("NSPATH"))

import pkg_resources as p

import ddtrace # noqa

p.resource_listdir("namespace_test.ns_module", ".")

0 comments on commit 64ba71c

Please sign in to comment.