Skip to content

Commit b0aa8d7

Browse files
cursoragentogrisel
andcommitted
Fix black formatting and address review feedback
Remove trivial availability helpers from the private API and inline the Windows/admin checks in integration test skipif predicates. Co-authored-by: Olivier Grisel <olivier.grisel@ensta.org>
1 parent 38a1202 commit b0aa8d7

4 files changed

Lines changed: 18 additions & 55 deletions

File tree

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,10 @@
11
"""Private experimental thread spawn tracer (not part of the public API)."""
22

3-
import sys
4-
53
from threadpoolctl._thread_tracer._types import ThreadSpawnStats, ThreadTracerError
64
from threadpoolctl._thread_tracer._windows_etw import WindowsThreadSpawnTracer
75

8-
9-
def windows_tracer_available():
10-
"""Return whether the Windows ETW tracer can run on this platform."""
11-
return sys.platform == "win32"
12-
13-
14-
def windows_etw_admin_available():
15-
"""Return whether the current process can start kernel ETW sessions."""
16-
if not windows_tracer_available():
17-
return False
18-
try:
19-
import ctypes
20-
21-
return bool(ctypes.windll.shell32.IsUserAnAdmin())
22-
except (AttributeError, OSError):
23-
return False
24-
25-
266
__all__ = [
277
"ThreadSpawnStats",
288
"ThreadTracerError",
299
"WindowsThreadSpawnTracer",
30-
"windows_etw_admin_available",
31-
"windows_tracer_available",
3210
]

threadpoolctl/_thread_tracer/_windows_etw.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ def from_string(cls, guid_string):
5959
guid.Data3 = int(parts[2], 16)
6060
hex_data4 = parts[3] + parts[4]
6161
for index in range(8):
62-
guid.Data4[index] = int(hex_data4[index * 2:index * 2 + 2], 16)
62+
guid.Data4[index] = int(hex_data4[index * 2 : index * 2 + 2], 16)
6363
return guid
6464

6565

@@ -188,9 +188,7 @@ def _ensure_advapi32():
188188
return _advapi32
189189

190190
if sys.platform != "win32":
191-
raise ThreadTracerError(
192-
"Windows ETW tracer is only supported on Windows"
193-
)
191+
raise ThreadTracerError("Windows ETW tracer is only supported on Windows")
194192

195193
_advapi32 = ct.windll.advapi32
196194

@@ -232,7 +230,9 @@ def _ensure_advapi32():
232230

233231
def _make_trace_properties(enable_flags):
234232
max_str_len = 1024
235-
buf_size = ct.sizeof(EVENT_TRACE_PROPERTIES) + 2 * ct.sizeof(ct.c_wchar) * max_str_len
233+
buf_size = (
234+
ct.sizeof(EVENT_TRACE_PROPERTIES) + 2 * ct.sizeof(ct.c_wchar) * max_str_len
235+
)
236236
buf = (ct.c_char * buf_size)()
237237
props = ct.cast(ct.pointer(buf), ct.POINTER(EVENT_TRACE_PROPERTIES))
238238

threadpoolctl/tests/test_windows_thread_tracer.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import struct
2-
import sys
32

43
import pytest
54

@@ -10,7 +9,6 @@
109
classify_kernel_thread_event,
1110
parse_kernel_thread_payload,
1211
)
13-
from threadpoolctl._thread_tracer import windows_tracer_available
1412

1513

1614
def _pack_thread_payload(process_id, thread_id):
@@ -42,7 +40,3 @@ def test_classify_kernel_thread_event_ignores_other_process():
4240

4341
def test_parse_kernel_thread_payload_rejects_short_buffers():
4442
assert parse_kernel_thread_payload(b"\x01\x02\x03", 3) is None
45-
46-
47-
def test_windows_tracer_available_matches_platform():
48-
assert windows_tracer_available() == (sys.platform == "win32")

threadpoolctl/tests/test_windows_thread_tracer_integration.py

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import ctypes
12
import os
23
import subprocess
34
import sys
@@ -6,20 +7,15 @@
67

78
import pytest
89

9-
from threadpoolctl._thread_tracer import (
10-
ThreadTracerError,
11-
WindowsThreadSpawnTracer,
12-
windows_etw_admin_available,
13-
windows_tracer_available,
14-
)
10+
from threadpoolctl._thread_tracer import ThreadTracerError, WindowsThreadSpawnTracer
1511

1612
pytestmark = [
1713
pytest.mark.skipif(
18-
not windows_tracer_available(),
14+
sys.platform != "win32",
1915
reason="Windows ETW integration tests require Windows",
2016
),
2117
pytest.mark.skipif(
22-
not windows_etw_admin_available(),
18+
sys.platform == "win32" and not ctypes.windll.shell32.IsUserAnAdmin(),
2319
reason="Windows ETW kernel tracing requires an elevated process",
2420
),
2521
]
@@ -69,15 +65,13 @@ def _configure_blas_thread_env():
6965

7066

7167
def _child_script(body):
72-
return textwrap.dedent(
73-
"""
68+
return textwrap.dedent("""
7469
import time
7570
7671
time.sleep({attach_delay})
7772
{body}
7873
time.sleep({flush_delay})
79-
"""
80-
).format(
74+
""").format(
8175
attach_delay=TRACER_ATTACH_DELAY_SECONDS,
8276
body=textwrap.indent(textwrap.dedent(body).strip(), " "),
8377
flush_delay=TRACER_FLUSH_DELAY_SECONDS,
@@ -88,9 +82,7 @@ def _run_traced_child(body, minimum_spawn_count):
8882
env = os.environ.copy()
8983
pythonpath = env.get("PYTHONPATH", "")
9084
env["PYTHONPATH"] = (
91-
str(REPO_ROOT)
92-
if not pythonpath
93-
else str(REPO_ROOT) + os.pathsep + pythonpath
85+
str(REPO_ROOT) if not pythonpath else str(REPO_ROOT) + os.pathsep + pythonpath
9486
)
9587

9688
proc = subprocess.Popen(
@@ -116,13 +108,12 @@ def _run_traced_child(body, minimum_spawn_count):
116108

117109
stats = tracer.stop()
118110
assert return_code == 0, "traced child exited with code {0}".format(return_code)
119-
assert stats.spawn_count >= minimum_spawn_count, (
120-
"expected at least {expected} thread spawn events, got {actual} ({stats})"
121-
.format(
122-
expected=minimum_spawn_count,
123-
actual=stats.spawn_count,
124-
stats=stats,
125-
)
111+
assert (
112+
stats.spawn_count >= minimum_spawn_count
113+
), "expected at least {expected} thread spawn events, got {actual} ({stats})".format(
114+
expected=minimum_spawn_count,
115+
actual=stats.spawn_count,
116+
stats=stats,
126117
)
127118
return stats
128119

0 commit comments

Comments
 (0)