Skip to content

Commit 88f0b37

Browse files
committed
Fix test line number assertions in test_extensions.py
Update line number assertions in three failing integration tests to match current file structure after recent code additions. The tests were failing due to line number shifts caused by: - Addition of C23 deallocator support tests - Code review improvements Tests affected: - test_misbehaving_extension: 83 → 85 - test_extension_that_uses_pygilstate_ensure: 154 → 156, 155 → 157 - test_native_dlopen: 226 → 228 Signed-off-by: Nana Adjei Manu <[email protected]>
1 parent 2759a03 commit 88f0b37

File tree

4 files changed

+61
-47
lines changed

4 files changed

+61
-47
lines changed

src/memray/_memray/hooks.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
FOR_EACH_HOOKED_FUNCTION(memalign) \
3131
FOR_EACH_HOOKED_FUNCTION(prctl) \
3232
FOR_EACH_HOOKED_FUNCTION(pvalloc) \
33-
FOR_EACH_HOOKED_FUNCTION(mmap64) \
33+
FOR_EACH_HOOKED_FUNCTION(mmap64)
3434
#else
3535
# define MEMRAY_PLATFORM_HOOKED_FUNCTIONS \
3636
FOR_EACH_HOOKED_FUNCTION(memalign) \

tests/integration/free_sized_extension/free_sized_test.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,15 @@ void*
2222
test_free_sized(void)
2323
{
2424
void* address;
25-
25+
2626
check_functions_available();
2727
if (!functions_available) {
2828
return address;
2929
}
30-
30+
3131
void* ptr = malloc(1024);
3232
assert(ptr != NULL);
33-
33+
3434
address = ptr;
3535

3636
free_sized(ptr, 1024);
@@ -41,17 +41,17 @@ void*
4141
test_free_aligned_sized(void)
4242
{
4343
void* address;
44-
44+
4545
check_functions_available();
4646
if (!functions_available) {
4747
return address;
4848
}
49-
49+
5050
void* ptr = aligned_alloc(64, 1024);
5151
assert(ptr != NULL);
52-
52+
5353
address = ptr;
54-
54+
5555
free_aligned_sized(ptr, 64, 1024);
5656
return address;
5757
}
@@ -60,22 +60,22 @@ void*
6060
test_both_free_functions(void)
6161
{
6262
void* address;
63-
63+
6464
check_functions_available();
6565
if (!functions_available) {
6666
return NULL;
6767
}
68-
68+
6969
void* ptr1 = malloc(512);
7070
assert(ptr1 != NULL);
7171
free_sized(ptr1, 512);
72-
72+
7373
void* ptr2 = aligned_alloc(32, 256);
7474
assert(ptr2 != NULL);
7575
free_aligned_sized(ptr2, 32, 256);
76-
76+
7777
address = ptr2;
78-
78+
7979
return address;
8080
}
8181

@@ -84,11 +84,11 @@ run_free_sized_test(PyObject* self, PyObject* args)
8484
{
8585
check_functions_available();
8686
if (!functions_available) {
87-
Py_RETURN_NONE;
87+
Py_RETURN_NONE;
8888
}
89-
89+
9090
void* address = test_free_sized();
91-
91+
9292
// Return address for verification
9393
PyObject* result = Py_BuildValue("(KII)", address);
9494
return result;
@@ -99,11 +99,11 @@ run_free_aligned_sized_test(PyObject* self, PyObject* args)
9999
{
100100
check_functions_available();
101101
if (!functions_available) {
102-
Py_RETURN_NONE;
102+
Py_RETURN_NONE;
103103
}
104-
104+
105105
void* address = test_free_aligned_sized();
106-
106+
107107
PyObject* result = Py_BuildValue("(KII)", address);
108108
return result;
109109
}
@@ -115,9 +115,9 @@ run_both_tests(PyObject* self, PyObject* args)
115115
if (!functions_available) {
116116
Py_RETURN_NONE; // Skip test if functions not available
117117
}
118-
118+
119119
void* address = test_both_free_functions();
120-
120+
121121
PyObject* result = Py_BuildValue("(KII)", address);
122122
return result;
123123
}
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from setuptools import Extension, setup
1+
from setuptools import Extension
2+
from setuptools import setup
23

34
setup(
45
name="free_sized_extension",
@@ -7,7 +8,7 @@
78
"free_sized_test",
89
sources=["free_sized_test.c"],
910
language="c",
10-
extra_compile_args=["-std=c23"]
11+
extra_compile_args=["-std=c23"],
1112
)
1213
],
1314
)

tests/integration/test_extensions.py

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ def allocating_function(): # pragma: no cover
110110
func, filename, line = bottom_frame
111111
assert func == "allocating_function"
112112
assert filename.endswith(__file__)
113-
assert line == 83
113+
assert line == 85
114114

115115
frees = [
116116
event
@@ -173,7 +173,7 @@ def foo2():
173173
func, filename, line = bottom_frame
174174
assert func == "test_extension_that_uses_pygilstate_ensure"
175175
assert filename.endswith(__file__)
176-
assert line == 154
176+
assert line == 156
177177

178178
# We should have 2 frames here: this function calling `allocator.valloc`,
179179
# and `allocator.valloc` calling the C `valloc`.
@@ -188,7 +188,7 @@ def foo2():
188188
func, filename, line = caller
189189
assert func == "test_extension_that_uses_pygilstate_ensure"
190190
assert filename.endswith(__file__)
191-
assert line == 155
191+
assert line == 157
192192

193193
frees = [
194194
event
@@ -244,7 +244,7 @@ def allocating_function():
244244
func, filename, line = bottom_frame
245245
assert func == "test_native_dlopen"
246246
assert filename.endswith(__file__)
247-
assert line == 226
247+
assert line == 228
248248

249249
frees = [
250250
event
@@ -389,22 +389,28 @@ def test_dlopen_with_rpath(tmpdir, monkeypatch):
389389
with Tracker(output):
390390
hello_world()
391391

392+
392393
@pytest.mark.skipif(
393394
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",
395396
)
396397
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."""
398399
# GIVEN
399400
output = Path(tmpdir) / "test.bin"
400401
extension_name = "free_sized_extension"
401402
extension_path = tmpdir / extension_name
402403
shutil.copytree(TEST_FREE_SIZED_EXTENSION, extension_path)
403-
404+
404405
# Try to build the extension, skip if compilation fails
405406
try:
406407
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+
],
408414
check=True,
409415
cwd=extension_path,
410416
capture_output=True,
@@ -417,7 +423,7 @@ def test_free_sized_extension(tmpdir, monkeypatch):
417423
# WHEN
418424
with monkeypatch.context() as ctx:
419425
ctx.setattr(sys, "path", [*sys.path, str(extension_path)])
420-
426+
421427
try:
422428
from free_sized_test import run_both_tests # type: ignore
423429
except ImportError as e:
@@ -426,7 +432,7 @@ def test_free_sized_extension(tmpdir, monkeypatch):
426432
with Tracker(output):
427433
# Get allocation info from the extension
428434
result = run_both_tests()
429-
435+
430436
# Skip test if functions not available (e.g., on macOS)
431437
if result is None:
432438
pytest.skip("C23 functions not available on this system")
@@ -436,7 +442,9 @@ def test_free_sized_extension(tmpdir, monkeypatch):
436442
assert records
437443

438444
# 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+
]
440448
assert len(mallocs) >= 2
441449

442450
# Check that corresponding FREE records - this verifies hooks are working!
@@ -447,27 +455,32 @@ def test_free_sized_extension(tmpdir, monkeypatch):
447455
if record.address in mallocs_addr and record.allocator == AllocatorType.FREE
448456
]
449457
assert len(frees) == len(mallocs)
450-
458+
451459
assert all(len(malloc.stack_trace()) == 0 for malloc in mallocs)
452460
assert all(len(free.stack_trace()) == 0 for free in frees)
453-
461+
454462
# Verify that the specific addresses returned by the extension were tracked
455463
if result is not None:
456-
# result should be a tuple of (address, size, alignment)
457464
expected_address = result[0]
458-
expected_size = result[1]
459-
expected_alignment = result[2]
460-
465+
461466
# Find the allocation record for this address
462467
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
465472
]
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+
468477
# Find the corresponding free record
469478
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
472483
]
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

Comments
 (0)