Skip to content

Commit 8a086ed

Browse files
committed
Cast handle to int
1 parent a607363 commit 8a086ed

File tree

2 files changed

+23
-8
lines changed

2 files changed

+23
-8
lines changed

Tests/test_imagewin.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,18 @@ def test_dib_mode_string(self) -> None:
6060
with pytest.raises(ValueError):
6161
ImageWin.Dib(mode)
6262

63+
def test_dib_hwnd(self) -> None:
64+
mode = "RGBA"
65+
size = (128, 128)
66+
wnd = 0
67+
68+
dib = ImageWin.Dib(mode, size)
69+
hwnd = ImageWin.HWND(wnd)
70+
71+
dib.expose(hwnd)
72+
dib.draw(hwnd, (0, 0) + size)
73+
assert isinstance(dib.query_palette(hwnd), int)
74+
6375
def test_dib_paste(self) -> None:
6476
# Arrange
6577
im = hopper()

src/PIL/ImageWin.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,14 +98,15 @@ def expose(self, handle: int | HDC | HWND) -> None:
9898
HDC or HWND instance. In PythonWin, you can use
9999
``CDC.GetHandleAttrib()`` to get a suitable handle.
100100
"""
101+
handle_int = int(handle)
101102
if isinstance(handle, HWND):
102-
dc = self.image.getdc(handle)
103+
dc = self.image.getdc(handle_int)
103104
try:
104105
self.image.expose(dc)
105106
finally:
106-
self.image.releasedc(handle, dc)
107+
self.image.releasedc(handle_int, dc)
107108
else:
108-
self.image.expose(handle)
109+
self.image.expose(handle_int)
109110

110111
def draw(
111112
self,
@@ -124,14 +125,15 @@ def draw(
124125
"""
125126
if src is None:
126127
src = (0, 0) + self.size
128+
handle_int = int(handle)
127129
if isinstance(handle, HWND):
128-
dc = self.image.getdc(handle)
130+
dc = self.image.getdc(handle_int)
129131
try:
130132
self.image.draw(dc, dst, src)
131133
finally:
132-
self.image.releasedc(handle, dc)
134+
self.image.releasedc(handle_int, dc)
133135
else:
134-
self.image.draw(handle, dst, src)
136+
self.image.draw(handle_int, dst, src)
135137

136138
def query_palette(self, handle: int | HDC | HWND) -> int:
137139
"""
@@ -148,14 +150,15 @@ def query_palette(self, handle: int | HDC | HWND) -> int:
148150
:return: The number of entries that were changed (if one or more entries,
149151
this indicates that the image should be redrawn).
150152
"""
153+
handle_int = int(handle)
151154
if isinstance(handle, HWND):
152-
handle = self.image.getdc(handle)
155+
handle = self.image.getdc(handle_int)
153156
try:
154157
result = self.image.query_palette(handle)
155158
finally:
156159
self.image.releasedc(handle, handle)
157160
else:
158-
result = self.image.query_palette(handle)
161+
result = self.image.query_palette(handle_int)
159162
return result
160163

161164
def paste(

0 commit comments

Comments
 (0)