From: Brian Schubert Date: Sun, 6 Apr 2025 21:05:19 +0000 (-0400) Subject: gh-132168: Add `__class_getitem__` to `ctypes.py_object` (#132169) X-Git-Tag: v3.14.0a7~34 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=e2476398ee9911b6b0b80e3ca182647805fde81f;p=thirdparty%2FPython%2Fcpython.git gh-132168: Add `__class_getitem__` to `ctypes.py_object` (#132169) --- diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 1a7b456a8fc6..a550da9c9766 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -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. diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index de76e253eb92..3223dde270f6 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -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 -------- diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index d9e558162117..bba08b99b95b 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -162,6 +162,7 @@ class py_object(_SimpleCData): return super().__repr__() except ValueError: return "%s()" % type(self).__name__ + __class_getitem__ = classmethod(_types.GenericAlias) _check_size(py_object, "P") class c_short(_SimpleCData): diff --git a/Lib/test/test_genericalias.py b/Lib/test/test_genericalias.py index 61547d98ba66..5c13897b8d9d 100644 --- a/Lib/test/test_genericalias.py +++ b/Lib/test/test_genericalias.py @@ -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 index 000000000000..5e8f95b18921 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-04-06-13-23-41.gh-issue-132168.6UMEpo.rst @@ -0,0 +1,2 @@ +The :class:`ctypes.py_object` type now supports subscription, making it a +:term:`generic type`.