Skip to content

gh-132168: Add __class_getitem__ to ctypes.py_object #132169

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

Merged
merged 2 commits into from
Apr 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
--------

Expand Down
1 change: 1 addition & 0 deletions Lib/ctypes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ def __repr__(self):
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):
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_genericalias.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
The :class:`ctypes.py_object` type now supports subscription, making it a
:term:`generic type`.
Loading