Skip to content

Commit f4f76ae

Browse files
authored
test(di): avoid pytest loader when importing modules (#12812)
We avoid the use of the pytest loader when importing a module in one of the DI tests for decorated function discovery. Because the current implementation of the pytest loader does not implement the get_code method, we cannot wrap around it to get the code object for the module before it is executed. As a workaround in tests, we temporarily remove the pytest loader so that we can import the module under "normal" conditions. Once the test is done we restore the state of the sys.meta_path list. ## Checklist - [x] PR author has checked that all the criteria below are met - The PR description includes an overview of the change - The PR description articulates the motivation for the change - The change includes tests OR the PR description describes a testing strategy - The PR description notes risks associated with the change, if any - Newly-added code is easy to change - The change follows the [library release note guidelines](https://ddtrace.readthedocs.io/en/stable/releasenotes.html) - The change includes or references documentation updates if necessary - Backport labels are set (if [applicable](https://ddtrace.readthedocs.io/en/latest/contributing.html#backporting)) ## Reviewer Checklist - [ ] Reviewer has checked that all the criteria below are met - Title is accurate - All changes are related to the pull request's stated goal - Avoids breaking [API](https://ddtrace.readthedocs.io/en/stable/versioning.html#interfaces) changes - Testing strategy adequately addresses listed risks - Newly-added code is easy to change - Release note makes sense to a user of the library - If necessary, author has acknowledged and discussed the performance implications of this PR as reported in the benchmarks PR comment - 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)
1 parent 1aa3946 commit f4f76ae

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

tests/debugging/function/test_discovery.py

+23-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,25 @@
1+
import sys
2+
13
import pytest
24

35
from ddtrace.debugging._function.discovery import FunctionDiscovery
46
from ddtrace.internal.module import ModuleWatchdog
57
import tests.submod.stuff as stuff
68

79

10+
@pytest.fixture
11+
def no_pytest_loader():
12+
from _pytest.assertion.rewrite import AssertionRewritingHook
13+
14+
i = next(i for i, hook in enumerate(sys.meta_path) if isinstance(hook, AssertionRewritingHook))
15+
pytest_loader = sys.meta_path.pop(i)
16+
17+
try:
18+
yield
19+
finally:
20+
sys.meta_path.insert(i, pytest_loader)
21+
22+
823
@pytest.fixture
924
def stuff_discovery():
1025
return FunctionDiscovery.from_module(stuff)
@@ -124,18 +139,20 @@ def test_property_non_function_getter(stuff_discovery):
124139
stuff_discovery.by_name("PropertyStuff.foo")
125140

126141

127-
def test_custom_decorated_stuff():
142+
def test_custom_decorated_stuff(no_pytest_loader):
128143
class DiscoveryModuleWatchdog(ModuleWatchdog):
129144
def transform(self, code, module):
130145
return FunctionDiscovery.transformer(code, module)
131146

132147
DiscoveryModuleWatchdog.install()
133148

134-
import tests.submod.custom_decorated_stuff as custom_decorated_stuff
149+
try:
150+
import tests.submod.custom_decorated_stuff as custom_decorated_stuff
135151

136-
fd = FunctionDiscovery.from_module(custom_decorated_stuff)
152+
fd = FunctionDiscovery.from_module(custom_decorated_stuff)
137153

138-
(home,) = fd.at_line(17)
139-
assert home.__qualname__ == "home"
154+
(home,) = fd.at_line(17)
155+
assert home.__qualname__ == "home"
140156

141-
DiscoveryModuleWatchdog.uninstall()
157+
finally:
158+
DiscoveryModuleWatchdog.uninstall()

0 commit comments

Comments
 (0)