Depreate support for EOL PyQt4 and PySide

This commit is contained in:
Hugo 2019-02-14 22:59:14 +02:00
parent a5919d9384
commit 3c088db7ba
2 changed files with 22 additions and 1 deletions

View File

@ -2,6 +2,12 @@ from .helper import PillowTestCase, hopper
from PIL import ImageQt from PIL import ImageQt
try:
# Python 3
from importlib import reload
except ImportError:
# Python 2.7
pass
if ImageQt.qt_is_installed: if ImageQt.qt_is_installed:
from PIL.ImageQt import qRgba from PIL.ImageQt import qRgba
@ -78,3 +84,7 @@ class TestImageQt(PillowQtTestCase, PillowTestCase):
def test_image(self): def test_image(self):
for mode in ('1', 'RGB', 'RGBA', 'L', 'P'): for mode in ('1', 'RGB', 'RGBA', 'L', 'P'):
ImageQt.ImageQt(hopper(mode)) ImageQt.ImageQt(hopper(mode))
def test_deprecated(self):
expected = DeprecationWarning if ImageQt.qt_version in ["4", "side"] else None
self.assert_warning(expected, reload, ImageQt)

View File

@ -20,6 +20,7 @@ from . import Image
from ._util import isPath, py3 from ._util import isPath, py3
from io import BytesIO from io import BytesIO
import sys import sys
import warnings
qt_versions = [ qt_versions = [
['5', 'PyQt5'], ['5', 'PyQt5'],
@ -27,6 +28,12 @@ qt_versions = [
['4', 'PyQt4'], ['4', 'PyQt4'],
['side', 'PySide'] ['side', 'PySide']
] ]
WARNING_TEXT = (
"Support for EOL {} is deprecated and will be removed in a future version. "
"Please upgrade to PyQt5 or PySide2."
)
# If a version has already been imported, attempt it first # If a version has already been imported, attempt it first
qt_versions.sort(key=lambda qt_version: qt_version[1] in sys.modules, qt_versions.sort(key=lambda qt_version: qt_version[1] in sys.modules,
reverse=True) reverse=True)
@ -41,9 +48,13 @@ for qt_version, qt_module in qt_versions:
elif qt_module == 'PyQt4': elif qt_module == 'PyQt4':
from PyQt4.QtGui import QImage, qRgba, QPixmap from PyQt4.QtGui import QImage, qRgba, QPixmap
from PyQt4.QtCore import QBuffer, QIODevice from PyQt4.QtCore import QBuffer, QIODevice
warnings.warn(WARNING_TEXT.format(qt_module), DeprecationWarning)
elif qt_module == 'PySide': elif qt_module == 'PySide':
from PySide.QtGui import QImage, qRgba, QPixmap from PySide.QtGui import QImage, qRgba, QPixmap
from PySide.QtCore import QBuffer, QIODevice from PySide.QtCore import QBuffer, QIODevice
warnings.warn(WARNING_TEXT.format(qt_module), DeprecationWarning)
except (ImportError, RuntimeError): except (ImportError, RuntimeError):
continue continue
qt_is_installed = True qt_is_installed = True
@ -67,7 +78,7 @@ def fromqimage(im):
""" """
buffer = QBuffer() buffer = QBuffer()
buffer.open(QIODevice.ReadWrite) buffer.open(QIODevice.ReadWrite)
# preserve alha channel with png # preserve alpha channel with png
# otherwise ppm is more friendly with Image.open # otherwise ppm is more friendly with Image.open
if im.hasAlphaChannel(): if im.hasAlphaChannel():
im.save(buffer, 'png') im.save(buffer, 'png')