Merge pull request #2182 from wiredfool/pyside_segfault

Fix for ImageQt Segfault
This commit is contained in:
wiredfool 2016-11-11 17:00:27 +00:00 committed by GitHub
commit 9ee19f2aa9
3 changed files with 58 additions and 3 deletions

View File

@ -48,6 +48,11 @@ install:
# libimagequant
- pushd depends && ./install_imagequant.sh && popd
before_script:
# Qt needs a display for some of the tests, and it's only run on the system site packages install
- "export DISPLAY=:99.0"
- "sh -e /etc/init.d/xvfb start"
script:
- if [ "$TRAVIS_PYTHON_VERSION" != "nightly" ]; then coverage erase; fi
- python setup.py clean

View File

@ -157,7 +157,6 @@ def _toqclass_helper(im):
else:
raise ValueError("unsupported image mode %r" % im.mode)
# must keep a reference, or Qt will crash!
__data = data or align8to32(im.tobytes(), im.size[0], im.mode)
return {
'data': __data, 'im': im, 'format': format, 'colortable': colortable
@ -175,8 +174,13 @@ if qt_is_installed:
string or a PyQt string object).
"""
im_data = _toqclass_helper(im)
# must keep a reference, or Qt will crash!
# All QImage constructors that take data operate on an existing
# buffer, so this buffer has to hang on for the life of the image.
# Fixes https://github.com/python-pillow/Pillow/issues/1370
self.__data = im_data['data']
QImage.__init__(self,
im_data['data'], im_data['im'].size[0],
self.__data, im_data['im'].size[0],
im_data['im'].size[1], im_data['format'])
if im_data['colortable']:
self.setColorTable(im_data['colortable'])

View File

@ -5,7 +5,17 @@ from PIL import ImageQt
if ImageQt.qt_is_installed:
from PIL.ImageQt import QImage
from PIL.ImageQt import QImage, QPixmap
try:
from PyQt5 import QtGui
except (ImportError, RuntimeError):
try:
from PyQt4 import QtGui
except (ImportError, RuntimeError):
from PySide import QtGui
class TestToQImage(PillowQtTestCase, PillowTestCase):
@ -23,5 +33,41 @@ class TestToQImage(PillowQtTestCase, PillowTestCase):
data.save(tempfile)
def test_segfault(self):
PillowQtTestCase.setUp(self)
app = QtGui.QApplication([])
ex = Example()
if ImageQt.qt_is_installed:
class Example(QtGui.QWidget):
def __init__(self):
super(Example, self).__init__()
img = hopper().resize((1000,1000))
qimage = ImageQt.ImageQt(img)
pixmap1 = QtGui.QPixmap.fromImage(qimage)
hbox = QtGui.QHBoxLayout(self)
lbl = QtGui.QLabel(self)
# Segfault in the problem
lbl.setPixmap(pixmap1.copy())
def main():
app = QtGui.QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
unittest.main()