8000 gh-126615: Make `COMError` public and add to `ctypes` doc. by junkmd · Pull Request #126686 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-126615: Make COMError public and add to ctypes doc. #126686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Nov 20, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Create the "Exceptions" section.
  • Loading branch information
junkmd committed Nov 16, 2024
commit 1fa159ab9a5c24125beb1302fd29d76755fb6355
55 changes: 30 additions & 25 deletions Doc/library/ctypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.

Expand Down Expand Up @@ -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.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not very helpful. Could you document what the items are?

Copy link
Contributor Author
@junkmd junkmd Nov 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on the C implementation source code, these items are retrieved via methods of IErrorInfo1: GetDescription, GetSource, GetHelpFile, GetHelpContext, and the ProgID obtained via using ProgIDFromCLSID and GetGUID.

However, this exception can be raised not only from foreign functions but also intentionally by passing variable-length tuples, as seen in test_win32.py and the comtypes package.

def test_COMError(self):
from _ctypes import COMError
if support.HAVE_DOCSTRINGS:
self.assertEqual(COMError.__doc__,
"Raised when a COM method call failed.")
ex = COMError(-1, "text", ("details",))
self.assertEqual(ex.hresult, -1)
self.assertEqual(ex.text, "text")
self.assertEqual(ex.details, ("details",))

I will revise this part, taking these facts into account.

Footnotes

  1. Reference: IErrorInfo implementation in comtypes.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since _ctypes is internal API that is subject to change, I think it would be best to change test_win32 and comtypes so that they raise the same 5-tuple as ctypes itself, and document that 5-tuple.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wrote details of details.

I plan to improve test_win32 after improving this documentation.

For comtypes, I will investigate how much COMError.details is being used and then discuss the matter with the project's community to decide how to proceed.

As mentioned earlier, I would like to focus on improving the documentation in this PR.


.. availability:: Windows
Loading
0