Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ImagingCore.ptr instead of ImagingCore.id #8341

Open
wants to merge 37 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
2303c06
Use PyCapsule in _imagingmath
homm Sep 1, 2024
a9798e7
Use PyCapsule in _imagingcms
homm Sep 1, 2024
2fcab26
Use PyCapsule in _imagingmorph
homm Sep 1, 2024
f246be7
Use s# in PyArg_ParseTuple
homm Sep 1, 2024
7435a06
Deprecate ImageCore.id and ImageCore.unsafe_ptrs
homm Sep 1, 2024
c69ad03
Remove legacy 1-bit api, fix AttributeError
homm Sep 1, 2024
920c4ac
Use PyCapsule in _imagingtk
homm Sep 2, 2024
147f75e
Use PyCapsule in _imagingft
homm Sep 2, 2024
bf11639
rename PyCapsule -> Capsule
homm Sep 3, 2024
8833548
Move new_block to module
homm Sep 4, 2024
8dcf229
Merge branch 'main' into use-ptr
radarhere Sep 7, 2024
6f9128b
Updated type hint
radarhere Sep 7, 2024
fe002a7
Use PyCapsule in _imagingmath
homm Sep 1, 2024
56bc6a1
Use PyCapsule in _imagingcms
homm Sep 1, 2024
f916b5d
Use PyCapsule in _imagingmorph
homm Sep 1, 2024
7f48567
Use s# in PyArg_ParseTuple
homm Sep 1, 2024
5428e35
Deprecate ImageCore.id and ImageCore.unsafe_ptrs
homm Sep 1, 2024
cb3a4e6
Remove legacy 1-bit api, fix AttributeError
homm Sep 1, 2024
ee65b30
Use PyCapsule in _imagingtk
homm Sep 2, 2024
882ac78
Use PyCapsule in _imagingft
homm Sep 2, 2024
934ae12
rename PyCapsule -> Capsule
homm Sep 3, 2024
d29fa73
Move new_block to module
homm Sep 4, 2024
4318834
Merge branch 'use-ptr' into use-ptr
homm Sep 8, 2024
bd14915
Merge pull request #144 from radarhere/use-ptr
homm Sep 8, 2024
a2988da
ImageCore → ImagingCore
homm Sep 11, 2024
6921f83
Update docs/deprecations.rst
homm Sep 13, 2024
1f3fe6f
Use getim()
radarhere Sep 16, 2024
3b09f43
Merge pull request #145 from radarhere/use-ptr
homm Sep 16, 2024
d8ef314
Remove extra load() calls
homm Sep 16, 2024
bc97369
Increase reference to the image while capsule is alive
homm Sep 16, 2024
af521a1
Merge branch 'main' into use-ptr
homm Sep 18, 2024
aa22b24
Load before trying to catch exceptions
radarhere Sep 21, 2024
5d430ea
Added release notes
radarhere Sep 21, 2024
9f409e8
Use getim()
radarhere Sep 21, 2024
87414b3
Merge pull request #147 from radarhere/use-ptr
homm Sep 22, 2024
11bcd5a
Fix hasattr for ImageTk.PhotoImage.__del__
homm Sep 22, 2024
b9d1768
Catch AttributeError for BitmapImage.__photo
homm Sep 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions Tests/test_image_getim.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,19 @@
from __future__ import annotations

import pytest

from .helper import hopper


def test_sanity() -> None:
im = hopper()
type_repr = repr(type(im.getim()))

type_repr = repr(type(im.getim()))
assert "PyCapsule" in type_repr
assert isinstance(im.im.id, int)

with pytest.warns(DeprecationWarning):
assert isinstance(im.im.id, int)

with pytest.warns(DeprecationWarning):
ptrs = dict(im.im.unsafe_ptrs)
assert all(k in ptrs for k in ["image8", "image32", "image"])
8 changes: 4 additions & 4 deletions Tests/test_imagemorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,13 +328,13 @@ def test_wrong_mode() -> None:
iml = Image.new("L", (10, 10))

with pytest.raises(RuntimeError):
_imagingmorph.apply(bytes(lut), imrgb.im.id, iml.im.id)
_imagingmorph.apply(bytes(lut), imrgb.im.ptr, iml.im.ptr)

with pytest.raises(RuntimeError):
_imagingmorph.apply(bytes(lut), iml.im.id, imrgb.im.id)
_imagingmorph.apply(bytes(lut), iml.im.ptr, imrgb.im.ptr)

with pytest.raises(RuntimeError):
_imagingmorph.match(bytes(lut), imrgb.im.id)
_imagingmorph.match(bytes(lut), imrgb.im.ptr)

# Should not raise
_imagingmorph.match(bytes(lut), iml.im.id)
_imagingmorph.match(bytes(lut), iml.im.ptr)
10 changes: 10 additions & 0 deletions docs/deprecations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,16 @@ Specific WebP Feature Checks
``True`` if the WebP module is installed, until they are removed in Pillow
12.0.0 (2025-10-15).

Get Internal Pointers to Objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

.. deprecated:: 11.0.0

``Image.core.ImageCore.id`` and ``Image.core.ImageCore.unsafe_ptrs``
have been deprecated and will be removed in Pillow 12 (2025-10-15).
They were used for obtaining raw pointers to ``ImageCore`` internals. To interact with
C code, you can use ``Image.core.ImageCore.ptr``, which returns a ``Capsule`` object.
homm marked this conversation as resolved.
Show resolved Hide resolved

Removed features
----------------

Expand Down
7 changes: 1 addition & 6 deletions src/PIL/Image.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,12 +224,7 @@ class Quantize(IntEnum):
from IPython.lib.pretty import PrettyPrinter

from . import ImageFile, ImageFilter, ImagePalette, ImageQt, TiffImagePlugin
from ._typing import NumpyArray, StrOrBytesPath, TypeGuard

if sys.version_info >= (3, 13):
from types import CapsuleType
else:
CapsuleType = object
from ._typing import CapsuleType, NumpyArray, StrOrBytesPath, TypeGuard
ID: list[str] = []
OPEN: dict[
str,
Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageCms.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ def apply(self, im: Image.Image, imOut: Image.Image | None = None) -> Image.Imag
im.load()
if imOut is None:
imOut = Image.new(self.output_mode, im.size, None)
homm marked this conversation as resolved.
Show resolved Hide resolved
self.transform.apply(im.im.id, imOut.im.id)
self.transform.apply(im.im.ptr, imOut.im.ptr)
imOut.info["icc_profile"] = self.output_profile.tobytes()
return imOut

Expand All @@ -361,7 +361,7 @@ def apply_in_place(self, im: Image.Image) -> Image.Image:
if im.mode != self.output_mode:
msg = "mode mismatch"
raise ValueError(msg) # wrong output mode
self.transform.apply(im.im.id, im.im.id)
self.transform.apply(im.im.ptr, im.im.ptr)
im.info["icc_profile"] = self.output_profile.tobytes()
return im

Expand Down
4 changes: 2 additions & 2 deletions src/PIL/ImageMath.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def apply(
except AttributeError as e:
msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e
_imagingmath.unop(op, out.im.id, im_1.im.id)
_imagingmath.unop(op, out.im.ptr, im_1.im.ptr)
else:
# binary operation
im_2 = self.__fixup(im2)
Expand Down Expand Up @@ -93,7 +93,7 @@ def apply(
except AttributeError as e:
msg = f"bad operand type for '{op}'"
raise TypeError(msg) from e
_imagingmath.binop(op, out.im.id, im_1.im.id, im_2.im.id)
_imagingmath.binop(op, out.im.ptr, im_1.im.ptr, im_2.im.ptr)
return _Operand(out)

# unary operators
Expand Down
6 changes: 3 additions & 3 deletions src/PIL/ImageMorph.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def apply(self, image: Image.Image) -> tuple[int, Image.Image]:
msg = "Image mode must be L"
raise ValueError(msg)
outimage = Image.new(image.mode, image.size, None)
count = _imagingmorph.apply(bytes(self.lut), image.im.id, outimage.im.id)
count = _imagingmorph.apply(bytes(self.lut), image.im.ptr, outimage.im.ptr)
return count, outimage

def match(self, image: Image.Image) -> list[tuple[int, int]]:
Expand All @@ -229,7 +229,7 @@ def match(self, image: Image.Image) -> list[tuple[int, int]]:
if image.mode != "L":
msg = "Image mode must be L"
raise ValueError(msg)
return _imagingmorph.match(bytes(self.lut), image.im.id)
return _imagingmorph.match(bytes(self.lut), image.im.ptr)

def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]:
"""Get a list of all turned on pixels in a binary image
Expand All @@ -240,7 +240,7 @@ def get_on_pixels(self, image: Image.Image) -> list[tuple[int, int]]:
if image.mode != "L":
msg = "Image mode must be L"
raise ValueError(msg)
return _imagingmorph.get_on_pixels(image.im.id)
return _imagingmorph.get_on_pixels(image.im.ptr)

def load_lut(self, filename: str) -> None:
"""Load an operator from an mrl file"""
Expand Down
44 changes: 14 additions & 30 deletions src/PIL/ImageTk.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,12 @@

from . import Image, ImageFile

if TYPE_CHECKING:
from ._typing import CapsuleType

# --------------------------------------------------------------------
# Check for Tkinter interface hooks

_pilbitmap_ok = None


def _pilbitmap_check() -> int:
global _pilbitmap_ok
if _pilbitmap_ok is None:
try:
im = Image.new("1", (1, 1))
tkinter.BitmapImage(data=f"PIL:{im.im.id}")
_pilbitmap_ok = 1
except tkinter.TclError:
_pilbitmap_ok = 0
return _pilbitmap_ok


def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None:
source = None
Expand All @@ -62,18 +51,18 @@ def _get_image_from_kw(kw: dict[str, Any]) -> ImageFile.ImageFile | None:


def _pyimagingtkcall(
command: str, photo: PhotoImage | tkinter.PhotoImage, id: int
command: str, photo: PhotoImage | tkinter.PhotoImage, ptr: CapsuleType
) -> None:
tk = photo.tk
try:
tk.call(command, photo, id)
tk.call(command, photo, repr(ptr))
except tkinter.TclError:
# activate Tkinter hook
# may raise an error if it cannot attach to Tkinter
from . import _imagingtk

_imagingtk.tkinit(tk.interpaddr())
tk.call(command, photo, id)
tk.call(command, photo, repr(ptr))


# --------------------------------------------------------------------
Expand Down Expand Up @@ -142,6 +131,8 @@ def __init__(
self.paste(image)

def __del__(self) -> None:
if not hasattr(self, "__photo"):
return
homm marked this conversation as resolved.
Show resolved Hide resolved
name = self.__photo.name
self.__photo.name = None
try:
Expand Down Expand Up @@ -190,10 +181,10 @@ def paste(self, im: Image.Image) -> None:
if image.isblock() and im.mode == self.__mode:
block = image
else:
block = image.new_block(self.__mode, im.size)
block = Image.core.new_block(self.__mode, im.size)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine this change was just because you didn't like image.new_block(self.__mode, im.size)?

Rather than moving new_block in C, wouldn't it have been better for the C method to access the size on the image object instead of being passed it? Then this call could become image.new_block(self.__mode).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’d like to clarify that the reason for this change is not a matter of personal preference. The new_block method doesn't pertain to an existing image but instead creates a new block based on the passed parameters. As such, it makes more sense for it to reside in the Image.core module rather than being a method of the image object itself.

There is no intent to modify or enhance the behavior; I was simply correcting the method's improper placement.

image.convert2(block, image) # convert directly between buffers

_pyimagingtkcall("PyImagingPhoto", self.__photo, block.id)
_pyimagingtkcall("PyImagingPhoto", self.__photo, block.ptr)


# --------------------------------------------------------------------
Expand Down Expand Up @@ -225,17 +216,11 @@ def __init__(self, image: Image.Image | None = None, **kw: Any) -> None:
self.__mode = image.mode
self.__size = image.size

if _pilbitmap_check():
# fast way (requires the pilbitmap booster patch)
Copy link
Member

@radarhere radarhere Sep 19, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the record, the last mention of this patch was removed in Pillow 4.1.0 by #2360

image.load()
kw["data"] = f"PIL:{image.im.id}"
self.__im = image # must keep a reference
else:
# slow but safe way
kw["data"] = image.tobitmap()
self.__photo = tkinter.BitmapImage(**kw)
self.__photo = tkinter.BitmapImage(data=image.tobitmap(), **kw)

def __del__(self) -> None:
if not hasattr(self, "__photo"):
return
name = self.__photo.name
homm marked this conversation as resolved.
Show resolved Hide resolved
self.__photo.name = None
try:
Expand Down Expand Up @@ -273,9 +258,8 @@ def __str__(self) -> str:
def getimage(photo: PhotoImage) -> Image.Image:
"""Copies the contents of a PhotoImage to a PIL image memory."""
im = Image.new("RGBA", (photo.width(), photo.height()))
block = im.im

_pyimagingtkcall("PyImagingPhotoGet", photo, block.id)
_pyimagingtkcall("PyImagingPhotoGet", photo, im.im.ptr)

return im

Expand Down
5 changes: 5 additions & 0 deletions src/PIL/_typing.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
except (ImportError, AttributeError):
pass

if sys.version_info >= (3, 13):
from types import CapsuleType

Check warning on line 19 in src/PIL/_typing.py

View check run for this annotation

Codecov / codecov/patch

src/PIL/_typing.py#L19

Added line #L19 was not covered by tests
else:
CapsuleType = object

if sys.version_info >= (3, 12):
from collections.abc import Buffer
else:
Expand Down
33 changes: 24 additions & 9 deletions src/Tk/tkImaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,34 @@

static Imaging
ImagingFind(const char *name) {
Py_ssize_t id;
PyObject *capsule;
int direct_pointer = 0;
const char *expected = "capsule object \"" IMAGING_MAGIC "\" at 0x";

/* FIXME: use CObject instead? */
#if defined(_WIN64)
id = _atoi64(name);
#else
id = atol(name);
#endif
if (!id) {
if (name[0] == '<') {
name++;
} else {
// Special case for PyPy, where the string representation of a Capsule
// refers directly to the pointer itself, not to the PyCapsule object.
direct_pointer = 1;

Check warning on line 68 in src/Tk/tkImaging.c

View check run for this annotation

Codecov / codecov/patch

src/Tk/tkImaging.c#L68

Added line #L68 was not covered by tests
}

if (strncmp(name, expected, strlen(expected))) {
return NULL;

Check warning on line 72 in src/Tk/tkImaging.c

View check run for this annotation

Codecov / codecov/patch

src/Tk/tkImaging.c#L72

Added line #L72 was not covered by tests
}

capsule = (PyObject *)strtoull(name + strlen(expected), NULL, 16);

if (direct_pointer) {
return (Imaging)capsule;

Check warning on line 78 in src/Tk/tkImaging.c

View check run for this annotation

Codecov / codecov/patch

src/Tk/tkImaging.c#L78

Added line #L78 was not covered by tests
}

if (!PyCapsule_IsValid(capsule, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);

Check warning on line 82 in src/Tk/tkImaging.c

View check run for this annotation

Codecov / codecov/patch

src/Tk/tkImaging.c#L82

Added line #L82 was not covered by tests
return NULL;
}

return (Imaging)id;
return (Imaging)PyCapsule_GetPointer(capsule, IMAGING_MAGIC);
}

static int
Expand Down
18 changes: 15 additions & 3 deletions src/_imaging.c
Original file line number Diff line number Diff line change
Expand Up @@ -3674,15 +3674,12 @@
/* Unsharpmask extension */
{"gaussian_blur", (PyCFunction)_gaussian_blur, METH_VARARGS},
{"unsharp_mask", (PyCFunction)_unsharp_mask, METH_VARARGS},

{"box_blur", (PyCFunction)_box_blur, METH_VARARGS},

/* Special effects */
{"effect_spread", (PyCFunction)_effect_spread, METH_VARARGS},

/* Misc. */
{"new_block", (PyCFunction)_new_block, METH_VARARGS},

{"save_ppm", (PyCFunction)_save_ppm, METH_VARARGS},

{NULL, NULL} /* sentinel */
Expand All @@ -3707,6 +3704,13 @@

static PyObject *
_getattr_id(ImagingObject *self, void *closure) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"id property is deprecated and will be removed in Pillow 12.0",
1
) < 0) {
return NULL;

Check warning on line 3712 in src/_imaging.c

View check run for this annotation

Codecov / codecov/patch

src/_imaging.c#L3712

Added line #L3712 was not covered by tests
}
return PyLong_FromSsize_t((Py_ssize_t)self->image);
}

Expand All @@ -3717,6 +3721,13 @@

static PyObject *
_getattr_unsafe_ptrs(ImagingObject *self, void *closure) {
if (PyErr_WarnEx(
PyExc_DeprecationWarning,
"unsafe_ptrs property is deprecated and will be removed in Pillow 12.0",
1
) < 0) {
return NULL;

Check warning on line 3729 in src/_imaging.c

View check run for this annotation

Codecov / codecov/patch

src/_imaging.c#L3729

Added line #L3729 was not covered by tests
}
return Py_BuildValue(
"(sn)(sn)(sn)",
"image8",
Expand Down Expand Up @@ -4198,6 +4209,7 @@
{"blend", (PyCFunction)_blend, METH_VARARGS},
{"fill", (PyCFunction)_fill, METH_VARARGS},
{"new", (PyCFunction)_new, METH_VARARGS},
{"new_block", (PyCFunction)_new_block, METH_VARARGS},
{"merge", (PyCFunction)_merge, METH_VARARGS},

/* Functions */
Expand Down
19 changes: 10 additions & 9 deletions src/_imagingcms.c
Original file line number Diff line number Diff line change
Expand Up @@ -531,23 +531,24 @@

static PyObject *
cms_transform_apply(CmsTransformObject *self, PyObject *args) {
Py_ssize_t idIn;
Py_ssize_t idOut;
PyObject *i0, *i1;
Imaging im;
Imaging imOut;

int result;

if (!PyArg_ParseTuple(args, "nn:apply", &idIn, &idOut)) {
if (!PyArg_ParseTuple(args, "OO:apply", &i0, &i1)) {
return NULL;
}

im = (Imaging)idIn;
imOut = (Imaging)idOut;
if (!PyCapsule_IsValid(i0, IMAGING_MAGIC) ||
!PyCapsule_IsValid(i1, IMAGING_MAGIC)) {
PyErr_Format(PyExc_TypeError, "Expected '%s' Capsule", IMAGING_MAGIC);
return NULL;

Check warning on line 545 in src/_imagingcms.c

View check run for this annotation

Codecov / codecov/patch

src/_imagingcms.c#L544-L545

Added lines #L544 - L545 were not covered by tests
}

result = pyCMSdoTransform(im, imOut, self->transform);
im = (Imaging)PyCapsule_GetPointer(i0, IMAGING_MAGIC);
imOut = (Imaging)PyCapsule_GetPointer(i1, IMAGING_MAGIC);

return Py_BuildValue("i", result);
return Py_BuildValue("i", pyCMSdoTransform(im, imOut, self->transform));
}

/* -------------------------------------------------------------------- */
Expand Down
Loading
Loading