From e70aa54456782d9cde3f612d11adf0a771faf18a Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 09:18:34 +0000 Subject: [PATCH 01/19] add `COMError` exception to `Doc/library/ctypes.rst`. --- Doc/library/ctypes.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 99909de20ef439..f207de59ef9508 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1803,6 +1803,24 @@ different ways, depending on the type and number of the parameters in the call: the COM interface as first argument, in addition to those parameters that are specified in the :attr:`!argtypes` tuple. + +.. exception:: COMError(hresult, text, details) + + Windows only: This non-public exception is raised when a COM method call + fails. + + .. attribute:: hresult + + The integer value representing the error code. + + .. attribute:: text + + A string containing the error message or ``None``. + + .. attribute:: details + + A tuple of length 5 representing additional details about the error. + The optional *paramflags* parameter creates foreign function wrappers with much more functionality than the features described above. From 181f512798590f8ba08bddbb82f8eeaacd68697e Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 09:18:34 +0000 Subject: [PATCH 02/19] Add a mention of the difference in exceptions that can be raised. --- Doc/library/ctypes.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index f207de59ef9508..68a63b41f80a22 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1799,6 +1799,9 @@ different ways, depending on the type and number of the parameters in the call: integer. *name* is name of the COM method. *iid* is an optional pointer to the interface identifier which is used in extended error reporting. + If *iid* is not specified, a :exc:`WindowsError` is raised if the COM method + call fails. If *iid* is specified, a :exc:`COMError` is raised instead. + COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that are specified in the :attr:`!argtypes` tuple. From 857fef5b074f90cca5bc99baef296fe374c6a3aa Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 09:18:34 +0000 Subject: [PATCH 03/19] Add a dot to refer to the object within the same module. --- Doc/library/ctypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 68a63b41f80a22..8bcb60acacbbb7 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1800,7 +1800,7 @@ different ways, depending on the type and number of the parameters in the call: the interface identifier which is used in extended error reporting. If *iid* is not specified, a :exc:`WindowsError` is raised if the COM method - call fails. If *iid* is specified, a :exc:`COMError` is raised instead. + call fails. If *iid* is specified, a :exc:`.COMError` is raised instead. COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that From 90fb47fd75400eecca9bb17807657e04777df450 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 09:22:11 +0000 Subject: [PATCH 04/19] Simplify descriptions of attributes. --- Doc/library/ctypes.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 8bcb60acacbbb7..2d7d4b25249b45 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1818,11 +1818,11 @@ different ways, depending on the type and number of the parameters in the call: .. attribute:: text - A string containing the error message or ``None``. + The error message. .. attribute:: details - A tuple of length 5 representing additional details about the error. + The 5-tuple representing additional details about the error. The optional *paramflags* parameter creates foreign function wrappers with much more functionality than the features described above. From 1f9d4eea65de2dd20e789b64c6c7c2216d28922a Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 11:13:31 +0000 Subject: [PATCH 05/19] Align with the docstring defined in the C implementation: "Raised when a COM method call failed." https://github.com/python/cpython/blob/2313f8421080ceb3343c6f5d291279adea85e073/Modules/_ctypes/_ctypes.c#L5548 --- Doc/library/ctypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 2d7d4b25249b45..8547dc82d2e3dc 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1810,7 +1810,7 @@ different ways, depending on the type and number of the parameters in the call: .. exception:: COMError(hresult, text, details) Windows only: This non-public exception is raised when a COM method call - fails. + failed. .. attribute:: hresult From b71e310fab5ca60dd5c51bde2476d7a162f25f86 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 20:59:47 +0900 Subject: [PATCH 06/19] Apply suggestions from code review MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- Doc/library/ctypes.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 8547dc82d2e3dc..f96de638eec9be 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1800,7 +1800,7 @@ different ways, depending on the type and number of the parameters in the call: the interface identifier which is used in extended error reporting. If *iid* is not specified, a :exc:`WindowsError` is raised if the COM method - call fails. If *iid* is specified, a :exc:`.COMError` is raised instead. + call fails. If *iid* is specified, a :exc:`COMError` is raised instead. COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that @@ -1824,6 +1824,7 @@ different ways, depending on the type and number of the parameters in the call: The 5-tuple representing additional details about the error. + The optional *paramflags* parameter creates foreign function wrappers with much more functionality than the features described above. From dafb5b225a451adfa8763575fde7b77dcfa8f334 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 12:21:42 +0000 Subject: [PATCH 07/19] Add the dot to make the reflink. --- Doc/library/ctypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index f96de638eec9be..14affb8e16ccdc 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1800,7 +1800,7 @@ different ways, depending on the type and number of the parameters in the call: the interface identifier which is used in extended error reporting. If *iid* is not specified, a :exc:`WindowsError` is raised if the COM method - call fails. If *iid* is specified, a :exc:`COMError` is raised instead. + call fails. If *iid* is specified, a :exc:`.COMError` is raised instead. COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that From 49d5e5f35b68e6a0e3cb2c542c8f95e7381bfb11 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 12:24:46 +0000 Subject: [PATCH 08/19] Add the `availability`. --- Doc/library/ctypes.rst | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 14affb8e16ccdc..681305a4e43ba6 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1809,8 +1809,7 @@ different ways, depending on the type and number of the parameters in the call: .. exception:: COMError(hresult, text, details) - Windows only: This non-public exception is raised when a COM method call - failed. + This non-public exception is raised when a COM method call failed. .. attribute:: hresult @@ -1824,6 +1823,8 @@ different ways, depending on the type and number of the parameters in the call: The 5-tuple representing additional details about the error. + .. availability:: Windows + The optional *paramflags* parameter creates foreign function wrappers with much more functionality than the features described above. From 1fa159ab9a5c24125beb1302fd29d76755fb6355 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 13:38:32 +0000 Subject: [PATCH 09/19] Create the "Exceptions" section. --- Doc/library/ctypes.rst | 55 +++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 681305a4e43ba6..307c60c8622e60 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1707,12 +1707,6 @@ in :mod:`!ctypes`) which inherits from the private :class:`_CFuncPtr` class: and raise an exception if the foreign function call failed. -.. exception:: ArgumentError - - This exception is raised when a foreign function call cannot convert one of the - passed arguments. - - .. audit-event:: ctypes.set_exception code foreign-functions On Windows, when a foreign function call raises a system exception (for @@ -1807,25 +1801,6 @@ different ways, depending on the type and number of the parameters in the call: are specified in the :attr:`!argtypes` tuple. -.. exception:: COMError(hresult, text, details) - - This non-public exception is raised when a COM method call failed. - - .. attribute:: hresult - - The integer value representing the error code. - - .. attribute:: text - - The error message. - - .. attribute:: details - - The 5-tuple representing additional details about the error. - - .. availability:: Windows - - The optional *paramflags* parameter creates foreign function wrappers with much more functionality than the features described above. @@ -2764,3 +2739,33 @@ Arrays and pointers Returns the object to which to pointer points. Assigning to this attribute changes the pointer to point to the assigned object. + + +.. _ctypes-exceptions: + +Exceptions +^^^^^^^^^^ + +.. exception:: ArgumentError + + This exception is raised when a foreign function call cannot convert one of the + passed arguments. + + +.. exception:: COMError(hresult, text, details) + + This non-public exception is raised when a COM method call failed. + + .. attribute:: hresult + + The integer value representing the error code. + + .. attribute:: text + + The error message. + + .. attribute:: details + + The 5-tuple representing additional details about the error. + + .. availability:: Windows From 934c36ae4a19069c15a66e3c3c5b45c845fecd77 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Sat, 16 Nov 2024 13:59:53 +0000 Subject: [PATCH 10/19] Remove the `availability` directive in order to maintain consistency with other sections. --- Doc/library/ctypes.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 307c60c8622e60..6b55856a942182 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -2754,7 +2754,8 @@ Exceptions .. exception:: COMError(hresult, text, details) - This non-public exception is raised when a COM method call failed. + Windows only: This non-public exception is raised when a COM method call + failed. .. attribute:: hresult @@ -2767,5 +2768,3 @@ Exceptions .. attribute:: details The 5-tuple representing additional details about the error. - - .. availability:: Windows From 9461642637560ed31dfbc49e971f2e642920ccfb Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:04:22 +0000 Subject: [PATCH 11/19] `WindowsError` -> `OSError` --- Doc/library/ctypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 6b55856a942182..ac500d45ff6222 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1793,7 +1793,7 @@ different ways, depending on the type and number of the parameters in the call: integer. *name* is name of the COM method. *iid* is an optional pointer to the interface identifier which is used in extended error reporting. - If *iid* is not specified, a :exc:`WindowsError` is raised if the COM method + If *iid* is not specified, an :exc:`OSError` is raised if the COM method call fails. If *iid* is specified, a :exc:`.COMError` is raised instead. COM methods use a special calling convention: They require a pointer to From 705ffcc7f27957f9ae2a58761ee8e64ae9654901 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Mon, 18 Nov 2024 15:04:22 +0000 Subject: [PATCH 12/19] details of `details`. --- Doc/library/ctypes.rst | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index ac500d45ff6222..1cc3be81dc1a1d 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -2767,4 +2767,10 @@ Exceptions .. attribute:: details - The 5-tuple representing additional details about the error. + The 5-tuple ``(descr, source, helpfile, helpcontext, progid)``. + + *descr* is the textual description. *source* is the language-dependent + ``ProgID`` for the class or application that raised the error. *helpfile* + is the path of the help file. *helpcontext* is the help context + identifier. *progid* is ``ProgID`` of the interface that defined the + error. From b5a2170bc371bd0f8dbf31f84a25ae349775e694 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Mon, 18 Nov 2024 23:27:46 +0000 Subject: [PATCH 13/19] Small fix. --- Doc/library/ctypes.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 1cc3be81dc1a1d..15a15a77cb57de 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -2772,5 +2772,5 @@ Exceptions *descr* is the textual description. *source* is the language-dependent ``ProgID`` for the class or application that raised the error. *helpfile* is the path of the help file. *helpcontext* is the help context - identifier. *progid* is ``ProgID`` of the interface that defined the + identifier. *progid* is the ``ProgID`` of the interface that defined the error. From a16f1e20458498673fc95dc6bdcfbe0ac394fae1 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:19:21 +0000 Subject: [PATCH 14/19] Make `COMError` public. --- Lib/ctypes/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/ctypes/__init__.py b/Lib/ctypes/__init__.py index 4a368f02d851c5..ac6493892068e9 100644 --- a/Lib/ctypes/__init__.py +++ b/Lib/ctypes/__init__.py @@ -19,7 +19,7 @@ raise Exception("Version number mismatch", __version__, _ctypes_version) if _os.name == "nt": - from _ctypes import FormatError + from _ctypes import COMError, FormatError DEFAULT_MODE = RTLD_LOCAL if _os.name == "posix" and _sys.platform == "darwin": From e2617f3aa2eaf05c6cb052d1e12ca14d516c77bf Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:19:21 +0000 Subject: [PATCH 15/19] Update docs. --- Doc/library/ctypes.rst | 8 ++++++-- Doc/whatsnew/3.14.rst | 3 +++ .../2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst | 1 + 3 files changed, 10 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 15a15a77cb57de..4eb89020dc61c2 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -2754,8 +2754,7 @@ Exceptions .. exception:: COMError(hresult, text, details) - Windows only: This non-public exception is raised when a COM method call - failed. + Windows only: This exception is raised when a COM method call failed. .. attribute:: hresult @@ -2774,3 +2773,8 @@ Exceptions is the path of the help file. *helpcontext* is the help context identifier. *progid* is the ``ProgID`` of the interface that defined the error. + + .. versionchanged:: 3.14 + + This exception is now public. + Previously, this was private and only available in ``_ctypes``. diff --git a/Doc/whatsnew/3.14.rst b/Doc/whatsnew/3.14.rst index 958efbe73c1c27..5ac43a52d208fd 100644 --- a/Doc/whatsnew/3.14.rst +++ b/Doc/whatsnew/3.14.rst @@ -270,6 +270,9 @@ ctypes to help match a non-default ABI. (Contributed by Petr Viktorin in :gh:`97702`.) +* The :exc:`~ctypes.COMError` exception is now public. + (Contributed by Jun Komoda in :gh:`126686`.) + decimal ------- diff --git a/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst b/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst new file mode 100644 index 00000000000000..97bff9c9dd0878 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst @@ -0,0 +1 @@ +The :exc:`~ctypes.COMError` exception is now public. From 57d76c5c23cf4c7222e5233bf8ec1abc6ac2b3fc Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Tue, 19 Nov 2024 15:19:22 +0000 Subject: [PATCH 16/19] Update `test_COMError`. --- Lib/test/test_ctypes/test_win32.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Lib/test/test_ctypes/test_win32.py b/Lib/test/test_ctypes/test_win32.py index 31919118670613..54b47dc28fbc73 100644 --- a/Lib/test/test_ctypes/test_win32.py +++ b/Lib/test/test_ctypes/test_win32.py @@ -65,15 +65,16 @@ def test_PARAM(self): sizeof(c_void_p)) def test_COMError(self): - from _ctypes import COMError + from ctypes import COMError if support.HAVE_DOCSTRINGS: self.assertEqual(COMError.__doc__, "Raised when a COM method call failed.") - ex = COMError(-1, "text", ("details",)) + ex = COMError(-1, "text", ("descr", "source", "helpfile", 0, "progid")) self.assertEqual(ex.hresult, -1) self.assertEqual(ex.text, "text") - self.assertEqual(ex.details, ("details",)) + self.assertEqual(ex.details, + ("descr", "source", "helpfile", 0, "progid")) self.assertEqual(COMError.mro(), [COMError, Exception, BaseException, object]) From 36f16071f3d15e1e75a44ce91fc05de873374588 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:18:20 +0000 Subject: [PATCH 17/19] Update the NEWS. --- .../next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst b/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst index 97bff9c9dd0878..8c7a2ade03c19e 100644 --- a/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst +++ b/Misc/NEWS.d/next/Library/2024-11-19-14-34-05.gh-issue-126615.LOskwi.rst @@ -1 +1,2 @@ The :exc:`~ctypes.COMError` exception is now public. +Previously, this was private and only available in ``_ctypes``. From 068ea2a61c8e3c07239b739351f5d6c65ba13168 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:18:50 +0000 Subject: [PATCH 18/19] `.. versionchanged::` -> `.. versionadded::` --- Doc/library/ctypes.rst | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 4eb89020dc61c2..7b556f15d5fa11 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -2774,7 +2774,4 @@ Exceptions identifier. *progid* is the ``ProgID`` of the interface that defined the error. - .. versionchanged:: 3.14 - - This exception is now public. - Previously, this was private and only available in ``_ctypes``. + .. versionadded:: next From 3da9e15d0fcd1ddd9ba3537ddd23541a530e1053 Mon Sep 17 00:00:00 2001 From: Jun Komoda <45822440+junkmd@users.noreply.github.com> Date: Wed, 20 Nov 2024 12:20:00 +0000 Subject: [PATCH 19/19] Update the `prototype` section. --- Doc/library/ctypes.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Doc/library/ctypes.rst b/Doc/library/ctypes.rst index 7b556f15d5fa11..8ed75d9b7560b9 100644 --- a/Doc/library/ctypes.rst +++ b/Doc/library/ctypes.rst @@ -1794,7 +1794,8 @@ different ways, depending on the type and number of the parameters in the call: the interface identifier which is used in extended error reporting. If *iid* is not specified, an :exc:`OSError` is raised if the COM method - call fails. If *iid* is specified, a :exc:`.COMError` is raised instead. + call fails. If *iid* is specified, a :exc:`~ctypes.COMError` is raised + instead. COM methods use a special calling convention: They require a pointer to the COM interface as first argument, in addition to those parameters that