Skip to content

Commit 5b45025

Browse files
junkmdpicnixz
andauthored
gh-126615: ctypes: Make COMError public (GH-126686)
Co-authored-by: Bénédikt Tran <[email protected]>
1 parent 9d2a879 commit 5b45025

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

Doc/library/ctypes.rst

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1707,12 +1707,6 @@ in :mod:`!ctypes`) which inherits from the private :class:`_CFuncPtr` class:
17071707
and raise an exception if the foreign function call failed.
17081708

17091709

1710-
.. exception:: ArgumentError
1711-
1712-
This exception is raised when a foreign function call cannot convert one of the
1713-
passed arguments.
1714-
1715-
17161710
.. audit-event:: ctypes.set_exception code foreign-functions
17171711

17181712
On Windows, when a foreign function call raises a system exception (for
@@ -1799,10 +1793,15 @@ different ways, depending on the type and number of the parameters in the call:
17991793
integer. *name* is name of the COM method. *iid* is an optional pointer to
18001794
the interface identifier which is used in extended error reporting.
18011795

1796+
If *iid* is not specified, an :exc:`OSError` is raised if the COM method
1797+
call fails. If *iid* is specified, a :exc:`~ctypes.COMError` is raised
1798+
instead.
1799+
18021800
COM methods use a special calling convention: They require a pointer to
18031801
the COM interface as first argument, in addition to those parameters that
18041802
are specified in the :attr:`!argtypes` tuple.
18051803

1804+
18061805
The optional *paramflags* parameter creates foreign function wrappers with much
18071806
more functionality than the features described above.
18081807

@@ -2741,3 +2740,39 @@ Arrays and pointers
27412740

27422741
Returns the object to which to pointer points. Assigning to this
27432742
attribute changes the pointer to point to the assigned object.
2743+
2744+
2745+
.. _ctypes-exceptions:
2746+
2747+
Exceptions
2748+
^^^^^^^^^^
2749+
2750+
.. exception:: ArgumentError
2751+
2752+
This exception is raised when a foreign function call cannot convert one of the
2753+
passed arguments.
2754+
2755+
2756+
.. exception:: COMError(hresult, text, details)
2757+
2758+
Windows only: This exception is raised when a COM method call failed.
2759+
2760+
.. attribute:: hresult
2761+
2762+
The integer value representing the error code.
2763+
2764+
.. attribute:: text
2765+
2766+
The error message.
2767+
2768+
.. attribute:: details
2769+
2770+
The 5-tuple ``(descr, source, helpfile, helpcontext, progid)``.
2771+
2772+
*descr* is the textual description. *source* is the language-dependent
2773+
``ProgID`` for the class or application that raised the error. *helpfile*
2774+
is the path of the help file. *helpcontext* is the help context
2775+
identifier. *progid* is the ``ProgID`` of the interface that defined the
2776+
error.
2777+
2778+
.. versionadded:: next

Doc/whatsnew/3.14.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,9 @@ ctypes
273273
to help match a non-default ABI.
274274
(Contributed by Petr Viktorin in :gh:`97702`.)
275275

276+
* The :exc:`~ctypes.COMError` exception is now public.
277+
(Contributed by Jun Komoda in :gh:`126686`.)
278+
276279
datetime
277280
--------
278281

Lib/ctypes/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
raise Exception("Version number mismatch", __version__, _ctypes_version)
2020

2121
if _os.name == "nt":
22-
from _ctypes import FormatError
22+
from _ctypes import COMError, FormatError
2323

2424
DEFAULT_MODE = RTLD_LOCAL
2525
if _os.name == "posix" and _sys.platform == "darwin":

Lib/test/test_ctypes/test_win32.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,16 @@ def test_PARAM(self):
6565
sizeof(c_void_p))
6666

6767
def test_COMError(self):
68-
from _ctypes import COMError
68+
from ctypes import COMError
6969
if support.HAVE_DOCSTRINGS:
7070
self.assertEqual(COMError.__doc__,
7171
"Raised when a COM method call failed.")
7272

73-
ex = COMError(-1, "text", ("details",))
73+
ex = COMError(-1, "text", ("descr", "source", "helpfile", 0, "progid"))
7474
self.assertEqual(ex.hresult, -1)
7575
self.assertEqual(ex.text, "text")
76-
self.assertEqual(ex.details, ("details",))
76+
self.assertEqual(ex.details,
77+
("descr", "source", "helpfile", 0, "progid"))
7778

7879
self.assertEqual(COMError.mro(),
7980
[COMError, Exception, BaseException, object])
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :exc:`~ctypes.COMError` exception is now public.
2+
Previously, this was private and only available in ``_ctypes``.

0 commit comments

Comments
 (0)