2015-04-16 18:38:15 +03:00
|
|
|
from __future__ import division
|
2015-02-23 12:24:33 +03:00
|
|
|
from helper import unittest, PillowTestCase
|
|
|
|
import sys
|
|
|
|
from PIL import Image
|
|
|
|
from io import BytesIO
|
|
|
|
|
|
|
|
# Limits for testing the leak
|
2015-04-24 02:26:52 +03:00
|
|
|
mem_limit = 16 # max increase in MB
|
2015-02-23 12:24:33 +03:00
|
|
|
iterations = 5000
|
|
|
|
test_file = "Tests/images/hopper.webp"
|
|
|
|
|
|
|
|
|
|
|
|
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
|
|
|
|
class TestWebPLeaks(PillowTestCase):
|
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
try:
|
|
|
|
from PIL import _webp
|
|
|
|
except ImportError:
|
|
|
|
self.skipTest('WebP support not installed')
|
|
|
|
|
|
|
|
def _get_mem_usage(self):
|
|
|
|
from resource import getpagesize, getrusage, RUSAGE_SELF
|
|
|
|
mem = getrusage(RUSAGE_SELF).ru_maxrss
|
|
|
|
return mem * getpagesize() / 1024 / 1024
|
|
|
|
|
|
|
|
def test_leak_load(self):
|
|
|
|
with open(test_file, 'rb') as f:
|
|
|
|
im_data = f.read()
|
|
|
|
start_mem = self._get_mem_usage()
|
2015-04-08 14:12:37 +03:00
|
|
|
for _ in range(iterations):
|
2015-02-23 12:24:33 +03:00
|
|
|
with Image.open(BytesIO(im_data)) as im:
|
|
|
|
im.load()
|
|
|
|
mem = (self._get_mem_usage() - start_mem)
|
|
|
|
self.assertLess(mem, mem_limit, msg='memory usage limit exceeded')
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|