From b2d6aa8c8b8e6434def8aa15ce6c26e8768ddd19 Mon Sep 17 00:00:00 2001 From: Matt Wozniski Date: Tue, 30 Jan 2024 19:12:47 -0500 Subject: [PATCH] Correct histograms for zero byte allocations If the program performs zero byte allocations, pretend that they were one byte allocations instead. This way they'll be counted in the lowest histogram bucket, as they should be, but we won't attempt to call `math.log(0)` and get a math domain error. Signed-off-by: Matt Wozniski --- src/pytest_memray/plugin.py | 6 ++---- tests/test_pytest_memray.py | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/src/pytest_memray/plugin.py b/src/pytest_memray/plugin.py index c9114cb..c1f7d3d 100644 --- a/src/pytest_memray/plugin.py +++ b/src/pytest_memray/plugin.py @@ -85,12 +85,10 @@ def histogram( def cli_hist(data: Iterable[float], bins: int, *, log_scale: bool = True) -> str: bars = " ▁▂▃▄▅▆▇█" + if log_scale: + data = [math.log(number if number else 1) for number in data] low = min(data) high = max(data) - if log_scale: - data = map(math.log, filter(lambda number: number != 0, data)) - low = math.log(low) - high = math.log(high) data_bins = histogram(data, low=low, high=high, bins=bins) bar_indexes = (int(elem * (len(bars) - 1) / max(data_bins)) for elem in data_bins) result = " ".join(bars[bar_index] for bar_index in bar_indexes) diff --git a/tests/test_pytest_memray.py b/tests/test_pytest_memray.py index 4b7cd6f..0ac4eec 100644 --- a/tests/test_pytest_memray.py +++ b/tests/test_pytest_memray.py @@ -660,6 +660,7 @@ def test_memory_alloc_fails(): @pytest.mark.parametrize( "size, outcome", [ + (0, ExitCode.OK), (1, ExitCode.OK), (1024 * 1 / 10, ExitCode.OK), (1024 * 1, ExitCode.TESTS_FAILED),