Skip to content

Commit ce1cbf4

Browse files
fix(internal): support for namespace packages [backport 2.5] (#8087)
Backport bed61ae from #8048 to 2.5. We fix an issue with the import detection of namespace packages that causes a warning to be emitted, notifying that the import machinery is falling back to the legacy behaviour. Fixes #8039. ## 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] Risk is outlined (performance impact, potential for breakage, maintainability, etc). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] [Library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) are followed. If no release note is required, add label `changelog/no-changelog`. - [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)) ## Reviewer Checklist - [x] Title is accurate. - [x] No unnecessary changes are introduced. - [x] Description motivates each change. - [x] Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes unless absolutely necessary. - [x] Testing strategy adequately addresses listed risk(s). - [x] Change is maintainable (easy to change, telemetry, documentation). - [x] Release note makes sense to a user of the library. - [x] Reviewer has explicitly 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) - [x] If this PR 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`. - [x] This PR doesn't touch any of that. Co-authored-by: Gabriele N. Tornetta <[email protected]>
1 parent 2c0384f commit ce1cbf4

File tree

4 files changed

+22
-3
lines changed

4 files changed

+22
-3
lines changed

ddtrace/appsec/_iast/_loader.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ def _exec_iast_patched_module(module_watchdog, module):
2525
# Patched source is executed instead of original module
2626
compiled_code = compile(patched_source, module_path, "exec")
2727
exec(compiled_code, module.__dict__) # nosec B102
28-
else:
28+
elif module_watchdog.loader is not None:
2929
module_watchdog.loader.exec_module(module)

ddtrace/internal/module.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,9 +144,10 @@ def __init__(self, loader, spec=None):
144144

145145
self.callbacks = {} # type: Dict[Any, Callable[[ModuleType], None]]
146146

147-
if hasattr(loader, "create_module"):
147+
# A missing loader is generally an indication of a namespace package.
148+
if loader is None or hasattr(loader, "create_module"):
148149
self.create_module = self._create_module
149-
if hasattr(loader, "exec_module"):
150+
if loader is None or hasattr(loader, "exec_module"):
150151
self.exec_module = self._exec_module
151152

152153
def __getattr__(self, name):
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
fixes:
3+
- |
4+
Fix for an import issue that caused the pytest plugin to fail to properly
5+
initialize a test session and exit with an import exception.

tests/internal/test_module.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -438,3 +438,16 @@ def ns_hook(module):
438438
finally:
439439
sys.path.pop(0)
440440
ModuleWatchdog.uninstall()
441+
442+
443+
@pytest.mark.subprocess(
444+
ddtrace_run=True,
445+
env=dict(
446+
PYTHONPATH=os.pathsep.join((str(Path(__file__).parent), os.environ.get("PYTHONPATH", ""))),
447+
PYTHONDEVMODE="1",
448+
),
449+
)
450+
def test_module_watchdog_namespace_import_no_warnings():
451+
# Test that the namespace import does not emit warnings (e.g. fallback to
452+
# legacy import machinery).
453+
import namespace_test.ns_module # noqa:F401

0 commit comments

Comments
 (0)