|
6 | 6 |
|
7 | 7 | import pytest |
8 | 8 |
|
| 9 | +from ddtrace.internal.wrapping import is_wrapped |
| 10 | +from ddtrace.internal.wrapping import is_wrapped_with |
9 | 11 | from ddtrace.internal.wrapping import unwrap |
10 | 12 | from ddtrace.internal.wrapping import wrap |
11 | 13 | from ddtrace.internal.wrapping.context import WrappingContext |
@@ -95,6 +97,71 @@ def f(a, b, c=None): |
95 | 97 | assert not channel1 and not channel2 |
96 | 98 |
|
97 | 99 |
|
| 100 | +def test_is_wrapped(): |
| 101 | + """Test that `is_wrapped` and `is_wrapped_with` work as expected.""" |
| 102 | + |
| 103 | + def first_wrapper(f, args, kwargs): |
| 104 | + return f(*args, **kwargs) |
| 105 | + |
| 106 | + def second_wrapper(f, args, kwargs): |
| 107 | + return f(*args, **kwargs) |
| 108 | + |
| 109 | + def f(a, b, c=None): |
| 110 | + return (a, b, c) |
| 111 | + |
| 112 | + # Function works |
| 113 | + assert f(1, 2) == (1, 2, None) |
| 114 | + |
| 115 | + # Not wrapped yet |
| 116 | + assert not is_wrapped(f) |
| 117 | + assert not is_wrapped_with(f, first_wrapper) |
| 118 | + assert not is_wrapped_with(f, second_wrapper) |
| 119 | + |
| 120 | + # Wrap with first wrapper |
| 121 | + wrap(f, first_wrapper) |
| 122 | + |
| 123 | + # Function still works |
| 124 | + assert f(1, 2) == (1, 2, None) |
| 125 | + |
| 126 | + # Only wrapped with first_wrapper |
| 127 | + assert is_wrapped(f) |
| 128 | + assert is_wrapped_with(f, first_wrapper) |
| 129 | + assert not is_wrapped_with(f, second_wrapper) |
| 130 | + |
| 131 | + # Wrap with second wrapper |
| 132 | + wrap(f, second_wrapper) |
| 133 | + |
| 134 | + # Function still works |
| 135 | + assert f(1, 2) == (1, 2, None) |
| 136 | + |
| 137 | + # Wrapped with everything |
| 138 | + assert is_wrapped(f) |
| 139 | + assert is_wrapped_with(f, first_wrapper) |
| 140 | + assert is_wrapped_with(f, second_wrapper) |
| 141 | + |
| 142 | + # Unwrap first wrapper |
| 143 | + unwrap(f, first_wrapper) |
| 144 | + |
| 145 | + # Function still works |
| 146 | + assert f(1, 2) == (1, 2, None) |
| 147 | + |
| 148 | + # Still wrapped with second_wrapper |
| 149 | + assert is_wrapped(f) |
| 150 | + assert not is_wrapped_with(f, first_wrapper) |
| 151 | + assert is_wrapped_with(f, second_wrapper) |
| 152 | + |
| 153 | + # Unwrap second wrapper |
| 154 | + unwrap(f, second_wrapper) |
| 155 | + |
| 156 | + # Function still works |
| 157 | + assert f(1, 2) == (1, 2, None) |
| 158 | + |
| 159 | + # Not wrapped anymore |
| 160 | + assert not is_wrapped(f) |
| 161 | + assert not is_wrapped_with(f, first_wrapper) |
| 162 | + assert not is_wrapped_with(f, second_wrapper) |
| 163 | + |
| 164 | + |
98 | 165 | @pytest.mark.skipif(sys.version_info > (3, 12), reason="segfault on 3.13") |
99 | 166 | def test_wrap_generator(): |
100 | 167 | channel = [] |
|
0 commit comments