fix imaging leaks in putdata and getlist

This commit is contained in:
Benoit Pierre 2015-04-16 16:27:57 +02:00
parent 5be6f810a5
commit 4f6c366e75
2 changed files with 48 additions and 1 deletions

View File

@ -0,0 +1,43 @@
#!/usr/bin/env python
from __future__ import division
from helper import unittest, PillowTestCase
import sys
from PIL import Image, ImageFilter
min_iterations = 100
max_iterations = 10000
@unittest.skipIf(sys.platform.startswith('win32'), "requires Unix or MacOS")
class TestImagingLeaks(PillowTestCase):
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(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
self.assertLessEqual(mem, mem_limit,
msg='memory usage limit exceeded after %d iterations'
% (i + 1))
def test_leak_putdata(self):
im = Image.new('RGB', (25, 25))
self._test_leak(min_iterations, max_iterations, im.putdata, im.getdata())
def test_leak_getlist(self):
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)))
if __name__ == '__main__':
unittest.main()

View File

@ -421,6 +421,7 @@ getlist(PyObject* arg, int* length, const char* wrong_length, int type)
*length = n; *length = n;
PyErr_Clear(); PyErr_Clear();
Py_DECREF(seq);
return list; return list;
} }
@ -1221,7 +1222,7 @@ _putdata(ImagingObject* self, PyObject* args)
Py_ssize_t n, i, x, y; Py_ssize_t n, i, x, y;
PyObject* data; PyObject* data;
PyObject* seq; PyObject* seq = NULL;
PyObject* op; PyObject* op;
double scale = 1.0; double scale = 1.0;
double offset = 0.0; double offset = 0.0;
@ -1329,6 +1330,7 @@ _putdata(ImagingObject* self, PyObject* args)
op = PySequence_Fast_GET_ITEM(seq, i); op = PySequence_Fast_GET_ITEM(seq, i);
if (!op || !getink(op, image, u.ink)) { if (!op || !getink(op, image, u.ink)) {
Py_DECREF(seq);
return NULL; return NULL;
} }
/* FIXME: what about scale and offset? */ /* FIXME: what about scale and offset? */
@ -1342,6 +1344,8 @@ _putdata(ImagingObject* self, PyObject* args)
} }
} }
Py_XDECREF(seq);
Py_INCREF(Py_None); Py_INCREF(Py_None);
return Py_None; return Py_None;
} }