]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132168: Add `__class_getitem__` to `ctypes.py_object` (#132169)
authorBrian Schubert <brianm.schubert@gmail.com>
Sun, 6 Apr 2025 21:05:19 +0000 (17:05 -0400)
committerGitHub <noreply@github.com>
Sun, 6 Apr 2025 21:05:19 +0000 (22:05 +0100)
Doc/library/ctypes.rst
Doc/whatsnew/3.14.rst
Lib/ctypes/__init__.py
Lib/test/test_genericalias.py
Misc/NEWS.d/next/Library/2025-04-06-13-23-41.gh-issue-132168.6UMEpo.rst [new file with mode: 0644]

index 1a7b456a8fc6ab81ec2a60885fe05094938c95ae..a550da9c976696783ae4c21fad46bad7c4330da6 100644 (file)
@@ -2632,6 +2632,9 @@ These are the fundamental ctypes data types:
    Represents the C :c:expr:`PyObject *` datatype.  Calling this without an
    argument creates a ``NULL`` :c:expr:`PyObject *` pointer.
 
+   .. versionchanged:: next
+      :class:`!py_object` is now a :term:`generic type`.
+
 The :mod:`!ctypes.wintypes` module provides quite some other Windows specific
 data types, for example :c:type:`!HWND`, :c:type:`!WPARAM`, or :c:type:`!DWORD`.
 Some useful structures like :c:type:`!MSG` or :c:type:`!RECT` are also defined.
index de76e253eb9264d5b2f27ef6120bd5434c611fa1..3223dde270f60b1ce0286481aaac5dcd9d92ddf9 100644 (file)
@@ -622,6 +622,11 @@ ctypes
   loaded by the current process.
   (Contributed by Brian Ward in :gh:`119349`.)
 
+* The :class:`ctypes.py_object` type now supports subscription,
+  making it a :term:`generic type`.
+  (Contributed by Brian Schubert in :gh:`132168`.)
+
+
 datetime
 --------
 
index d9e5581621173712302afd4774a7405200350f5c..bba08b99b95b8b3d466b9eb4454a8796b838dc8a 100644 (file)
@@ -162,6 +162,7 @@ class py_object(_SimpleCData):
             return super().__repr__()
         except ValueError:
             return "%s(<NULL>)" % type(self).__name__
+    __class_getitem__ = classmethod(_types.GenericAlias)
 _check_size(py_object, "P")
 
 class c_short(_SimpleCData):
index 61547d98ba6666ae81b5ad407c5171a1718d397e..5c13897b8d9d4f33d013ce0c057adeb4291ebb8f 100644 (file)
@@ -139,7 +139,7 @@ class BaseTest(unittest.TestCase):
                      DictReader, DictWriter,
                      array]
     if ctypes is not None:
-        generic_types.extend((ctypes.Array, ctypes.LibraryLoader))
+        generic_types.extend((ctypes.Array, ctypes.LibraryLoader, ctypes.py_object))
     if ValueProxy is not None:
         generic_types.extend((ValueProxy, DictProxy, ListProxy, ApplyResult,
                               MPSimpleQueue, MPQueue, MPJoinableQueue))
diff --git a/Misc/NEWS.d/next/Library/2025-04-06-13-23-41.gh-issue-132168.6UMEpo.rst b/Misc/NEWS.d/next/Library/2025-04-06-13-23-41.gh-issue-132168.6UMEpo.rst
new file mode 100644 (file)
index 0000000..5e8f95b
--- /dev/null
@@ -0,0 +1,2 @@
+The :class:`ctypes.py_object` type now supports subscription, making it a
+:term:`generic type`.