2021-04-09 12:10:36 +03:00
|
|
|
#!/usr/bin/env python3
|
2023-12-21 14:13:31 +03:00
|
|
|
from __future__ import annotations
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2024-01-19 13:50:27 +03:00
|
|
|
from typing import Any, Callable
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
import pytest
|
2020-09-01 20:16:46 +03:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
from PIL import Image
|
2015-04-16 17:27:57 +03:00
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
from .helper import is_win32
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2015-04-16 17:27:57 +03:00
|
|
|
min_iterations = 100
|
|
|
|
max_iterations = 10000
|
|
|
|
|
2020-03-28 04:51:28 +03:00
|
|
|
pytestmark = pytest.mark.skipif(is_win32(), reason="requires Unix or macOS")
|
|
|
|
|
|
|
|
|
2024-01-19 13:50:27 +03:00
|
|
|
def _get_mem_usage() -> float:
|
2020-09-01 20:16:46 +03:00
|
|
|
from resource import RUSAGE_SELF, getpagesize, getrusage
|
2020-03-28 04:51:28 +03:00
|
|
|
|
|
|
|
mem = getrusage(RUSAGE_SELF).ru_maxrss
|
|
|
|
return mem * getpagesize() / 1024 / 1024
|
|
|
|
|
|
|
|
|
2024-01-19 13:50:27 +03:00
|
|
|
def _test_leak(
|
|
|
|
min_iterations: int, max_iterations: int, fn: Callable[..., None], *args: Any
|
|
|
|
) -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
mem_limit = None
|
|
|
|
for i in range(max_iterations):
|
2024-01-19 13:50:27 +03:00
|
|
|
fn(*args)
|
2020-03-28 04:51:28 +03:00
|
|
|
mem = _get_mem_usage()
|
|
|
|
if i < min_iterations:
|
|
|
|
mem_limit = mem + 1
|
|
|
|
continue
|
2020-07-16 12:43:29 +03:00
|
|
|
msg = f"memory usage limit exceeded after {i + 1} iterations"
|
2024-01-19 13:50:27 +03:00
|
|
|
assert mem_limit is not None
|
2020-03-28 04:51:28 +03:00
|
|
|
assert mem <= mem_limit, msg
|
|
|
|
|
|
|
|
|
2024-01-19 13:50:27 +03:00
|
|
|
def test_leak_putdata() -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
im = Image.new("RGB", (25, 25))
|
|
|
|
_test_leak(min_iterations, max_iterations, im.putdata, im.getdata())
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2024-01-19 13:50:27 +03:00
|
|
|
def test_leak_getlist() -> None:
|
2020-03-28 04:51:28 +03:00
|
|
|
im = Image.new("P", (25, 25))
|
|
|
|
_test_leak(
|
|
|
|
min_iterations,
|
|
|
|
max_iterations,
|
|
|
|
# Pass a new list at each iteration.
|
|
|
|
lambda: im.point(range(256)),
|
|
|
|
)
|