Skip to content

Commit 9ef90c6

Browse files
PR comments; rebased
1 parent 546c482 commit 9ef90c6

File tree

2 files changed

+7
-22
lines changed

2 files changed

+7
-22
lines changed

tests/profiling/collector/test_asyncio.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
import glob
44
import os
55
import sys
6+
from typing import Optional
67
import uuid
78

89
import pytest
910

1011
from ddtrace import ext
1112
from ddtrace.internal.datadog.profiling import ddup
1213
from ddtrace.profiling.collector import asyncio as collector_asyncio
14+
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper as LockAllocatorWrapper
1315
from tests.profiling.collector import pprof_utils
1416
from tests.profiling.collector import test_collector
1517
from tests.profiling.collector.lock_utils import get_lock_linenos
@@ -53,28 +55,18 @@ def teardown_method(self):
5355

5456
async def test_subclassing_wrapped_lock(self) -> None:
5557
"""Test that subclassing of a wrapped lock type when profiling is active."""
56-
from typing import Optional
57-
58-
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
59-
6058
with collector_asyncio.AsyncioLockCollector(capture_pct=100):
61-
assert isinstance(asyncio.Lock, _LockAllocatorWrapper)
59+
assert isinstance(asyncio.Lock, LockAllocatorWrapper)
6260

6361
# This should NOT raise TypeError
6462
class CustomLock(asyncio.Lock): # type: ignore[misc]
6563
def __init__(self) -> None:
6664
super().__init__()
67-
self._owner: Optional[int] = None
68-
self._count: int = 0
6965

7066
# Verify subclassing and functionality
7167
custom_lock: CustomLock = CustomLock()
72-
assert hasattr(custom_lock, "_owner")
73-
assert hasattr(custom_lock, "_count")
74-
assert custom_lock._owner is None
75-
assert custom_lock._count == 0
7668

77-
# Test async acquire/release
69+
# Verify subclassing and functionality
7870
await custom_lock.acquire()
7971
assert custom_lock.locked()
8072
custom_lock.release()

tests/profiling/collector/test_threading.py

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from ddtrace._trace.span import Span
2121
from ddtrace._trace.tracer import Tracer
2222
from ddtrace.internal.datadog.profiling import ddup
23+
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper as LockAllocatorWrapper
2324
from ddtrace.profiling.collector.threading import ThreadingBoundedSemaphoreCollector
2425
from ddtrace.profiling.collector.threading import ThreadingLockCollector
2526
from ddtrace.profiling.collector.threading import ThreadingRLockCollector
@@ -171,16 +172,14 @@ def test_lock_repr(
171172

172173

173174
def test_patch():
174-
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
175-
176175
lock = threading.Lock
177176
collector = ThreadingLockCollector()
178177
collector.start()
179178
assert lock == collector._original_lock
180179
# After patching, threading.Lock is replaced with our wrapper
181180
# The old reference (lock) points to the original builtin Lock class
182181
assert lock != threading.Lock # They're different after patching
183-
assert isinstance(threading.Lock, _LockAllocatorWrapper) # threading.Lock is now wrapped
182+
assert isinstance(threading.Lock, LockAllocatorWrapper) # threading.Lock is now wrapped
184183
assert callable(threading.Lock) # and it's callable
185184
collector.stop()
186185
# After stopping, everything is restored
@@ -1385,22 +1384,16 @@ def test_subclassing_wrapped_lock(self) -> None:
13851384
threading.Lock and threading.RLock are C types that don't support subclassing
13861385
through __mro_entries__.
13871386
"""
1388-
from ddtrace.profiling.collector._lock import _LockAllocatorWrapper
1389-
13901387
with self.collector_class():
1391-
assert isinstance(self.lock_class, _LockAllocatorWrapper)
1388+
assert isinstance(self.lock_class, LockAllocatorWrapper)
13921389

13931390
# This should NOT raise TypeError
13941391
class CustomLock(self.lock_class): # type: ignore[misc]
13951392
def __init__(self) -> None:
13961393
super().__init__()
1397-
self.custom_attr: str = "test"
13981394

13991395
# Verify subclassing and functionality
14001396
custom_lock: CustomLock = CustomLock()
1401-
assert hasattr(custom_lock, "custom_attr")
1402-
assert custom_lock.custom_attr == "test"
1403-
14041397
assert custom_lock.acquire()
14051398
custom_lock.release()
14061399

0 commit comments

Comments
 (0)