From 5428e3568e09d69241b03827eecb08d7b5dc9ea9 Mon Sep 17 00:00:00 2001
From: Aleksandr Karpinskii <homm86@gmail.com>
Date: Mon, 2 Sep 2024 02:19:44 +0400
Subject: [PATCH] Deprecate ImageCore.id and ImageCore.unsafe_ptrs

---
 Tests/test_image_getim.py | 12 ++++++++++--
 docs/deprecations.rst     | 10 ++++++++++
 src/_imaging.c            | 14 ++++++++++++++
 src/libImaging/Imaging.h  |  2 +-
 4 files changed, 35 insertions(+), 3 deletions(-)

diff --git a/Tests/test_image_getim.py b/Tests/test_image_getim.py
index 9afa02b0a..75d97cd44 100644
--- a/Tests/test_image_getim.py
+++ b/Tests/test_image_getim.py
@@ -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"])
diff --git a/docs/deprecations.rst b/docs/deprecations.rst
index cafda3803..abb902c88 100644
--- a/docs/deprecations.rst
+++ b/docs/deprecations.rst
@@ -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 ``PyCapsule`` object.
+
 Removed features
 ----------------
 
diff --git a/src/_imaging.c b/src/_imaging.c
index 07d9a64cc..a455c873b 100644
--- a/src/_imaging.c
+++ b/src/_imaging.c
@@ -3707,6 +3707,13 @@ _getattr_bands(ImagingObject *self, void *closure) {
 
 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;
+    }
     return PyLong_FromSsize_t((Py_ssize_t)self->image);
 }
 
@@ -3717,6 +3724,13 @@ _getattr_ptr(ImagingObject *self, void *closure) {
 
 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;
+    }
     return Py_BuildValue(
         "(sn)(sn)(sn)",
         "image8",
diff --git a/src/libImaging/Imaging.h b/src/libImaging/Imaging.h
index 321dce988..870f5ed97 100644
--- a/src/libImaging/Imaging.h
+++ b/src/libImaging/Imaging.h
@@ -61,7 +61,7 @@ typedef struct ImagingOutlineInstance *ImagingOutline;
 typedef struct ImagingPaletteInstance *ImagingPalette;
 
 /* handle magics (used with PyCObject). */
-#define IMAGING_MAGIC "PIL Imaging"
+#define IMAGING_MAGIC "Pillow Imaging"
 
 /* pixel types */
 #define IMAGING_TYPE_UINT8 0