2015-04-16 17:27:57 +03:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
from __future__ import division
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2015-04-16 17:27:57 +03:00
|
|
|
import sys
|
2019-07-06 23:40:53 +03:00
|
|
|
|
2015-07-03 08:03:25 +03:00
|
|
|
from PIL import Image
|
2015-04-16 17:27:57 +03:00
|
|
|
|
2019-07-06 23:40:53 +03:00
|
|
|
from .helper import PillowTestCase, unittest
|
|
|
|
|
2015-04-16 17:27:57 +03:00
|
|
|
min_iterations = 100
|
|
|
|
max_iterations = 10000
|
|
|
|
|
2015-04-24 02:26:52 +03:00
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
@unittest.skipIf(sys.platform.startswith("win32"), "requires Unix or macOS")
|
2015-04-16 17:27:57 +03:00
|
|
|
class TestImagingLeaks(PillowTestCase):
|
|
|
|
def _get_mem_usage(self):
|
|
|
|
from resource import getpagesize, getrusage, RUSAGE_SELF
|
2019-06-13 18:53:42 +03:00
|
|
|
|
2015-04-16 17:27:57 +03:00
|
|
|
mem = getrusage(RUSAGE_SELF).ru_maxrss
|
|
|
|
return mem * getpagesize() / 1024 / 1024
|
|
|
|
|
|
|
|
def _test_leak(self, min_iterations, max_iterations, fn, *args, **kwargs):
|
|
|
|
mem_limit = None
|
|
|
|
for i in range(max_iterations):
|
|
|
|
fn(*args, **kwargs)
|
|
|
|
mem = self._get_mem_usage()
|
|
|
|
if i < min_iterations:
|
|
|
|
mem_limit = mem + 1
|
|
|
|
continue
|
2019-06-13 18:53:42 +03:00
|
|
|
msg = "memory usage limit exceeded after %d iterations" % (i + 1)
|
2018-09-27 13:35:00 +03:00
|
|
|
self.assertLessEqual(mem, mem_limit, msg)
|
2015-04-16 17:27:57 +03:00
|
|
|
|
|
|
|
def test_leak_putdata(self):
|
2019-06-13 18:53:42 +03:00
|
|
|
im = Image.new("RGB", (25, 25))
|
|
|
|
self._test_leak(min_iterations, max_iterations, im.putdata, im.getdata())
|
2015-04-16 17:27:57 +03:00
|
|
|
|
|
|
|
def test_leak_getlist(self):
|
2019-06-13 18:53:42 +03:00
|
|
|
im = Image.new("P", (25, 25))
|
|
|
|
self._test_leak(
|
|
|
|
min_iterations,
|
|
|
|
max_iterations,
|
|
|
|
# Pass a new list at each iteration.
|
|
|
|
lambda: im.point(range(256)),
|
|
|
|
)
|
2015-04-16 17:27:57 +03:00
|
|
|
|
2018-03-03 12:54:00 +03:00
|
|
|
|
2019-06-13 18:53:42 +03:00
|
|
|
if __name__ == "__main__":
|
2015-04-16 17:27:57 +03:00
|
|
|
unittest.main()
|