Skip to content

Commit

Permalink
Cast handle to int
Browse files Browse the repository at this point in the history
  • Loading branch information
radarhere committed Sep 15, 2024
1 parent a607363 commit 8a086ed
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
12 changes: 12 additions & 0 deletions Tests/test_imagewin.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,18 @@ def test_dib_mode_string(self) -> None:
with pytest.raises(ValueError):
ImageWin.Dib(mode)

def test_dib_hwnd(self) -> None:
mode = "RGBA"
size = (128, 128)
wnd = 0

dib = ImageWin.Dib(mode, size)
hwnd = ImageWin.HWND(wnd)

dib.expose(hwnd)
dib.draw(hwnd, (0, 0) + size)
assert isinstance(dib.query_palette(hwnd), int)

def test_dib_paste(self) -> None:
# Arrange
im = hopper()
Expand Down
19 changes: 11 additions & 8 deletions src/PIL/ImageWin.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,14 +98,15 @@ def expose(self, handle: int | HDC | HWND) -> None:
HDC or HWND instance. In PythonWin, you can use
``CDC.GetHandleAttrib()`` to get a suitable handle.
"""
handle_int = int(handle)
if isinstance(handle, HWND):
dc = self.image.getdc(handle)
dc = self.image.getdc(handle_int)
try:
self.image.expose(dc)
finally:
self.image.releasedc(handle, dc)
self.image.releasedc(handle_int, dc)
else:
self.image.expose(handle)
self.image.expose(handle_int)

def draw(
self,
Expand All @@ -124,14 +125,15 @@ def draw(
"""
if src is None:
src = (0, 0) + self.size
handle_int = int(handle)
if isinstance(handle, HWND):
dc = self.image.getdc(handle)
dc = self.image.getdc(handle_int)
try:
self.image.draw(dc, dst, src)
finally:
self.image.releasedc(handle, dc)
self.image.releasedc(handle_int, dc)
else:
self.image.draw(handle, dst, src)
self.image.draw(handle_int, dst, src)

def query_palette(self, handle: int | HDC | HWND) -> int:
"""
Expand All @@ -148,14 +150,15 @@ def query_palette(self, handle: int | HDC | HWND) -> int:
:return: The number of entries that were changed (if one or more entries,
this indicates that the image should be redrawn).
"""
handle_int = int(handle)
if isinstance(handle, HWND):
handle = self.image.getdc(handle)
handle = self.image.getdc(handle_int)
try:
result = self.image.query_palette(handle)
finally:
self.image.releasedc(handle, handle)
else:
result = self.image.query_palette(handle)
result = self.image.query_palette(handle_int)
return result

def paste(
Expand Down

0 comments on commit 8a086ed

Please sign in to comment.