From 51963dacbd2b73b67d5f43780ab8c70f98932fc7 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 25 May 2026 16:37:21 +0100 Subject: [PATCH 1/3] Move the crash warning in `ctypes` doc to the top --- Doc/library/ctypes.rst | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 4330be922013f3..ee8e368b1bae37 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -14,6 +14,13 @@ used to wrap these libraries in pure Python. .. include:: ../includes/optional-module.rst +.. warning:: + + :mod:`!ctypes` can call arbitrary functions in native libraries. + Incorrect use can crash Python or otherwise compromise the running process. + The :mod:`faulthandler` module can help debug crashes, + such as segmentation faults produced by erroneous C library calls. + .. _ctypes-ctypes-tutorial: @@ -198,11 +205,6 @@ argument values:: OSError: exception: access violation reading 0x00000020 >>> -There are, however, enough ways to crash Python with :mod:`!ctypes`, so you -should be careful anyway. The :mod:`faulthandler` module can be helpful in -debugging crashes (e.g. from segmentation faults produced by erroneous C library -calls). - ``None``, integers, bytes objects and (unicode) strings are the only native Python objects that can directly be used as parameters in these function calls. ``None`` is passed as a C ``NULL`` pointer, bytes objects and strings are passed From eac75c656ddb61d0d5d369ca0ed28ed8f27a7222 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Mon, 25 May 2026 18:33:14 +0100 Subject: [PATCH 2/3] Try cover everything --- Doc/library/ctypes.rst | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index ee8e368b1bae37..1cb8bcba05106a 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -16,8 +16,10 @@ used to wrap these libraries in pure Python. .. warning:: - :mod:`!ctypes` can call arbitrary functions in native libraries. - Incorrect use can crash Python or otherwise compromise the running process. + :mod:`!ctypes` provides low-level access to native libraries and the + process's memory. Incorrect use can bypass Python's memory safety, corrupt + memory, crash Python, execute arbitrary native code, + or otherwise compromise the running process. The :mod:`faulthandler` module can help debug crashes, such as segmentation faults produced by erroneous C library calls. From 0b1a090ad9d5ffe8e25b164b0ea8ea5d0e590fce Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Tue, 26 May 2026 13:50:37 +0100 Subject: [PATCH 3/3] Apply Petr's suggestion(s) Co-authored-by: Petr Viktorin --- Doc/library/ctypes.rst | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 1cb8bcba05106a..c5d24660fd8fe2 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -17,11 +17,10 @@ used to wrap these libraries in pure Python. .. warning:: :mod:`!ctypes` provides low-level access to native libraries and the - process's memory. Incorrect use can bypass Python's memory safety, corrupt - memory, crash Python, execute arbitrary native code, - or otherwise compromise the running process. - The :mod:`faulthandler` module can help debug crashes, - such as segmentation faults produced by erroneous C library calls. + process's memory, bypassing Python's safety mechanisms and allowing + execution of arbitrary native code. + Incorrect use can corrupt data and objects, reveal sensitive information, + cause crashes, or otherwise compromise the running process. .. _ctypes-ctypes-tutorial: @@ -207,6 +206,9 @@ argument values:: OSError: exception: access violation reading 0x00000020 >>> +The :mod:`faulthandler` module can help debug crashes, +such as segmentation faults produced by erroneous C library calls. + ``None``, integers, bytes objects and (unicode) strings are the only native Python objects that can directly be used as parameters in these function calls. ``None`` is passed as a C ``NULL`` pointer, bytes objects and strings are passed