Skip to content

Commit e616daf

Browse files
committed
Use PyCapsule in _imagingtk
1 parent c69ad03 commit e616daf

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

src/PIL/ImageTk.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,18 +48,19 @@ def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None:
4848

4949

5050
def _pyimagingtkcall(
51-
command: str, photo: PhotoImage | tkinter.PhotoImage, id: int
51+
command: str, photo: PhotoImage | tkinter.PhotoImage, ptr: object
5252
) -> None:
5353
tk = photo.tk
54+
ptr_str = repr(ptr).strip("<>")
5455
try:
55-
tk.call(command, photo, id)
56+
tk.call(command, photo, ptr_str)
5657
except tkinter.TclError:
5758
# activate Tkinter hook
5859
# may raise an error if it cannot attach to Tkinter
5960
from . import _imagingtk
6061

6162
_imagingtk.tkinit(tk.interpaddr())
62-
tk.call(command, photo, id)
63+
tk.call(command, photo, ptr_str)
6364

6465

6566
# --------------------------------------------------------------------
@@ -181,7 +182,7 @@ def paste(self, im: Image.Image) -> None:
181182
block = image.new_block(self.__mode, im.size)
182183
image.convert2(block, image) # convert directly between buffers
183184

184-
_pyimagingtkcall("PyImagingPhoto", self.__photo, block.id)
185+
_pyimagingtkcall("PyImagingPhoto", self.__photo, block.ptr)
185186

186187

187188
# --------------------------------------------------------------------
@@ -255,9 +256,8 @@ def __str__(self) -> str:
255256
def getimage(photo: PhotoImage) -> Image.Image:
256257
"""Copies the contents of a PhotoImage to a PIL image memory."""
257258
im = Image.new("RGBA", (photo.width(), photo.height()))
258-
block = im.im
259259

260-
_pyimagingtkcall("PyImagingPhotoGet", photo, block.id)
260+
_pyimagingtkcall("PyImagingPhotoGet", photo, im.im.ptr)
261261

262262
return im
263263

src/Tk/tkImaging.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,19 +56,23 @@ static Tk_PhotoPutBlock_t TK_PHOTO_PUT_BLOCK;
5656

5757
static Imaging
5858
ImagingFind(const char *name) {
59-
Py_ssize_t id;
59+
PyObject *capsule;
60+
const char *expected = "capsule object \"" IMAGING_MAGIC "\" at 0x";
6061

61-
/* FIXME: use CObject instead? */
62-
#if defined(_WIN64)
63-
id = _atoi64(name);
64-
#else
65-
id = atol(name);
66-
#endif
67-
if (!id) {
62+
if (strncmp(name, expected, strlen(expected))) {
63+
return NULL;
64+
}
65+
66+
capsule = (PyObject *)strtoull(name + strlen(expected), NULL, 16);
67+
68+
if (!PyCapsule_IsValid(capsule, IMAGING_MAGIC)) {
69+
PyErr_Format(
70+
PyExc_TypeError, "Expected PyCapsule with '%s' name.", IMAGING_MAGIC
71+
);
6872
return NULL;
6973
}
7074

71-
return (Imaging)id;
75+
return (Imaging)PyCapsule_GetPointer(capsule, IMAGING_MAGIC);
7276
}
7377

7478
static int

0 commit comments

Comments
 (0)