https://github.com/python/cpython/commit/9cf4661d069756dd53b7fd1b5dd0270baffb0eb0 commit: 9cf4661d069756dd53b7fd1b5dd0270baffb0eb0 branch: 3.15 author: Miss Islington (bot) <[email protected]> committer: hugovk <[email protected]> date: 2026-09-28T18:32:06+03:00 summary:
[3.15] gh-92347: Document pitfalls of `ctypes.pointer` iteration (GH-157801) (#157925) Co-authored-by: Peter Bierma <[email protected]> files: M Doc/library/ctypes.rst diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index da1aaa89fef7c7..53b05cfd66f259 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -955,6 +955,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]
