@@ -110,7 +110,7 @@ def allocating_function(): # pragma: no cover
110
110
func , filename , line = bottom_frame
111
111
assert func == "allocating_function"
112
112
assert filename .endswith (__file__ )
113
- assert line == 83
113
+ assert line == 85
114
114
115
115
frees = [
116
116
event
@@ -173,7 +173,7 @@ def foo2():
173
173
func , filename , line = bottom_frame
174
174
assert func == "test_extension_that_uses_pygilstate_ensure"
175
175
assert filename .endswith (__file__ )
176
- assert line == 154
176
+ assert line == 156
177
177
178
178
# We should have 2 frames here: this function calling `allocator.valloc`,
179
179
# and `allocator.valloc` calling the C `valloc`.
@@ -188,7 +188,7 @@ def foo2():
188
188
func , filename , line = caller
189
189
assert func == "test_extension_that_uses_pygilstate_ensure"
190
190
assert filename .endswith (__file__ )
191
- assert line == 155
191
+ assert line == 157
192
192
193
193
frees = [
194
194
event
@@ -244,7 +244,7 @@ def allocating_function():
244
244
func , filename , line = bottom_frame
245
245
assert func == "test_native_dlopen"
246
246
assert filename .endswith (__file__ )
247
- assert line == 226
247
+ assert line == 228
248
248
249
249
frees = [
250
250
event
@@ -389,22 +389,28 @@ def test_dlopen_with_rpath(tmpdir, monkeypatch):
389
389
with Tracker (output ):
390
390
hello_world ()
391
391
392
+
392
393
@pytest .mark .skipif (
393
394
not hasattr (ctypes .CDLL (None ), "free_sized" ),
394
- reason = "free_sized not available on this system"
395
+ reason = "free_sized not available on this system" ,
395
396
)
396
397
def test_free_sized_extension (tmpdir , monkeypatch ):
397
- """Test tracking allocations in a native extension which uses free_sized and free_aligned_sized."""
398
+ """Test allocations in a native extension using free_sized and free_aligned_sized."""
398
399
# GIVEN
399
400
output = Path (tmpdir ) / "test.bin"
400
401
extension_name = "free_sized_extension"
401
402
extension_path = tmpdir / extension_name
402
403
shutil .copytree (TEST_FREE_SIZED_EXTENSION , extension_path )
403
-
404
+
404
405
# Try to build the extension, skip if compilation fails
405
406
try :
406
407
subprocess .run (
407
- [sys .executable , str (extension_path / "setup.py" ), "build_ext" , "--inplace" ],
408
+ [
409
+ sys .executable ,
410
+ str (extension_path / "setup.py" ),
411
+ "build_ext" ,
412
+ "--inplace" ,
413
+ ],
408
414
check = True ,
409
415
cwd = extension_path ,
410
416
capture_output = True ,
@@ -417,7 +423,7 @@ def test_free_sized_extension(tmpdir, monkeypatch):
417
423
# WHEN
418
424
with monkeypatch .context () as ctx :
419
425
ctx .setattr (sys , "path" , [* sys .path , str (extension_path )])
420
-
426
+
421
427
try :
422
428
from free_sized_test import run_both_tests # type: ignore
423
429
except ImportError as e :
@@ -426,7 +432,7 @@ def test_free_sized_extension(tmpdir, monkeypatch):
426
432
with Tracker (output ):
427
433
# Get allocation info from the extension
428
434
result = run_both_tests ()
429
-
435
+
430
436
# Skip test if functions not available (e.g., on macOS)
431
437
if result is None :
432
438
pytest .skip ("C23 functions not available on this system" )
@@ -436,7 +442,9 @@ def test_free_sized_extension(tmpdir, monkeypatch):
436
442
assert records
437
443
438
444
# Check that at least 2 allocations from malloc and aligned_alloc
439
- mallocs = [record for record in records if record .allocator == AllocatorType .ALIGNED_ALLOC ]
445
+ mallocs = [
446
+ record for record in records if record .allocator == AllocatorType .ALIGNED_ALLOC
447
+ ]
440
448
assert len (mallocs ) >= 2
441
449
442
450
# Check that corresponding FREE records - this verifies hooks are working!
@@ -447,27 +455,32 @@ def test_free_sized_extension(tmpdir, monkeypatch):
447
455
if record .address in mallocs_addr and record .allocator == AllocatorType .FREE
448
456
]
449
457
assert len (frees ) == len (mallocs )
450
-
458
+
451
459
assert all (len (malloc .stack_trace ()) == 0 for malloc in mallocs )
452
460
assert all (len (free .stack_trace ()) == 0 for free in frees )
453
-
461
+
454
462
# Verify that the specific addresses returned by the extension were tracked
455
463
if result is not None :
456
- # result should be a tuple of (address, size, alignment)
457
464
expected_address = result [0 ]
458
- expected_size = result [1 ]
459
- expected_alignment = result [2 ]
460
-
465
+
461
466
# Find the allocation record for this address
462
467
matching_allocs = [
463
- record for record in records
464
- if record .address == expected_address and record .allocator == AllocatorType .ALIGNED_ALLOC
468
+ record
469
+ for record in records
470
+ if record .address == expected_address
471
+ and record .allocator == AllocatorType .ALIGNED_ALLOC
465
472
]
466
- assert len (matching_allocs ) >= 1 , f"Expected allocation at address { expected_address } not found"
467
-
473
+ assert (
474
+ len (matching_allocs ) >= 1
475
+ ), f"Expected allocation at address { expected_address } not found"
476
+
468
477
# Find the corresponding free record
469
478
matching_frees = [
470
- record for record in records
471
- if record .address == expected_address and record .allocator == AllocatorType .FREE
479
+ record
480
+ for record in records
481
+ if record .address == expected_address
482
+ and record .allocator == AllocatorType .FREE
472
483
]
473
- assert len (matching_frees ) >= 1 , f"Expected free at address { expected_address } not found"
484
+ assert (
485
+ len (matching_frees ) >= 1
486
+ ), f"Expected free at address { expected_address } not found"
0 commit comments