Pillow/Tests/check_imaging_leaks.py

46 lines
1.1 KiB
Python
Raw Normal View History

#!/usr/bin/env python
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
2020-03-28 04:51:28 +03:00
from .helper import is_win32
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")
def _get_mem_usage():
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
def _test_leak(min_iterations, max_iterations, fn, *args, **kwargs):
mem_limit = None
for i in range(max_iterations):
fn(*args, **kwargs)
mem = _get_mem_usage()
if i < min_iterations:
mem_limit = mem + 1
continue
msg = f"memory usage limit exceeded after {i + 1} iterations"
2020-03-28 04:51:28 +03:00
assert mem <= mem_limit, msg
def test_leak_putdata():
im = Image.new("RGB", (25, 25))
_test_leak(min_iterations, max_iterations, im.putdata, im.getdata())
2015-04-24 02:26:52 +03:00
2020-03-28 04:51:28 +03:00
def test_leak_getlist():
im = Image.new("P", (25, 25))
_test_leak(
min_iterations,
max_iterations,
# Pass a new list at each iteration.
lambda: im.point(range(256)),
)