Skip to content

Commit a447e92

Browse files
committed
Ensure file is closed if it is opened by ImageQt.ImageQt
1 parent 1722b8d commit a447e92

File tree

2 files changed

+24
-8
lines changed

2 files changed

+24
-8
lines changed

Tests/test_imageqt.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44

55
from .helper import hopper
66

7+
pytestmark = pytest.mark.skipif(
8+
not ImageQt.qt_is_installed, reason="Qt bindings are not installed"
9+
)
10+
711
if ImageQt.qt_is_installed:
812
from PIL.ImageQt import qRgba
913

1014

11-
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
1215
def test_rgb():
1316
# from https://doc.qt.io/archives/qt-4.8/qcolor.html
1417
# typedef QRgb
@@ -38,7 +41,13 @@ def checkrgb(r, g, b):
3841
checkrgb(0, 0, 255)
3942

4043

41-
@pytest.mark.skipif(not ImageQt.qt_is_installed, reason="Qt bindings are not installed")
4244
def test_image():
4345
for mode in ("1", "RGB", "RGBA", "L", "P"):
4446
ImageQt.ImageQt(hopper(mode))
47+
48+
49+
def test_closed_file():
50+
with pytest.warns(None) as record:
51+
ImageQt.ImageQt("Tests/images/hopper.gif")
52+
53+
assert not record

src/PIL/ImageQt.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,15 @@ def align8to32(bytes, width, mode):
129129
def _toqclass_helper(im):
130130
data = None
131131
colortable = None
132+
exclusive_fp = False
132133

133134
# handle filename, if given instead of image name
134135
if hasattr(im, "toUtf8"):
135136
# FIXME - is this really the best way to do this?
136137
im = str(im.toUtf8(), "utf-8")
137138
if isPath(im):
138139
im = Image.open(im)
140+
exclusive_fp = True
139141

140142
qt_format = QImage.Format if qt_version == "6" else QImage
141143
if im.mode == "1":
@@ -158,10 +160,15 @@ def _toqclass_helper(im):
158160
data = im.tobytes("raw", "BGRA")
159161
format = qt_format.Format_ARGB32
160162
else:
163+
if exclusive_fp:
164+
im.close()
161165
raise ValueError(f"unsupported image mode {repr(im.mode)}")
162166

163-
__data = data or align8to32(im.tobytes(), im.size[0], im.mode)
164-
return {"data": __data, "im": im, "format": format, "colortable": colortable}
167+
size = im.size
168+
__data = data or align8to32(im.tobytes(), size[0], im.mode)
169+
if exclusive_fp:
170+
im.close()
171+
return {"data": __data, "size": size, "format": format, "colortable": colortable}
165172

166173

167174
if qt_is_installed:
@@ -183,8 +190,8 @@ def __init__(self, im):
183190
self.__data = im_data["data"]
184191
super().__init__(
185192
self.__data,
186-
im_data["im"].size[0],
187-
im_data["im"].size[1],
193+
im_data["size"][0],
194+
im_data["size"][1],
188195
im_data["format"],
189196
)
190197
if im_data["colortable"]:
@@ -198,8 +205,8 @@ def toqimage(im):
198205
def toqpixmap(im):
199206
# # This doesn't work. For now using a dumb approach.
200207
# im_data = _toqclass_helper(im)
201-
# result = QPixmap(im_data['im'].size[0], im_data['im'].size[1])
202-
# result.loadFromData(im_data['data'])
208+
# result = QPixmap(im_data["size"][0], im_data["size"][1])
209+
# result.loadFromData(im_data["data"])
203210
# Fix some strange bug that causes
204211
if im.mode == "RGB":
205212
im = im.convert("RGBA")

0 commit comments

Comments
 (0)