https://github.com/python/cpython/commit/a3e389d46d9ce9e60d2eb6fdf2f903b5f90b0f4e commit: a3e389d46d9ce9e60d2eb6fdf2f903b5f90b0f4e branch: main author: Peter Bierma <[email protected]> committer: ZeroIntensity <[email protected]> date: 2026-09-21T14:03:09-04:00 summary:
gh-92347: Document pitfalls of `ctypes.pointer` iteration (GH-157801) files: M Doc/library/ctypes.rst diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 8946ee7a02da48..1d33f593fffe94 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -997,6 +997,15 @@ Generally you only use this feature if you receive a pointer from a C function, and you *know* that the pointer actually points to an array instead of a single item. +.. warning:: + + Because pointer objects support subscription, they implicitly support + :term:`iteration <iterator>`. Unless doing this in a controlled manner, + such as by manually calling :func:`next` on a :func:`pointer` iterator, this + will typically lead to infinite loops or crashes, because ctypes has no way + of knowing when to stop iteration. In other words, a ``pointer`` iterator + will infinitely yield arbitrary memory. + Behind the scenes, the :func:`pointer` function does more than simply create pointer instances, it has to create pointer *types* first. This is done with the :func:`POINTER` function, which accepts any :mod:`!ctypes` type, and returns a _______________________________________________ Python-checkins mailing list -- [email protected] To unsubscribe send an email to [email protected] https://mail.python.org/mailman3//lists/python-checkins.python.org Member address: [email protected]
