Skip to content
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

ctypes: Make COMError, mentioned in the "Function prototypes" section of documentation, public. #126615

Closed
junkmd opened this issue Nov 9, 2024 · 0 comments

Comments

@junkmd
Copy link
Contributor

junkmd commented Nov 9, 2024

Documentation

I have been part of a project that handles COM with Python for several years now.

When I first joined the project, _ctypes.COMError was a "mysterious error" to me.

Over time, by implementing error handling within that project, contributing to comtypes, and adding _ctypes.pyi to typeshed, I deepened my understanding of this error.
Yet, I always wondered why ctypes documentation never mentioned COMError.

While reading through the function prototype section and working on gh-126384 and gh-126610, I realized that the section on "extended error reporting" may have implied COMError.

cpython/Doc/library/ctypes.rst

Lines 1793 to 1800 in 6293d00

.. function:: prototype(vtbl_index, name[, paramflags[, iid]])
:noindex:
:module:
Returns a foreign function that will call a COM method. *vtbl_index* is
the index into the virtual function table, a small non-negative
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 a foreign function generated from a function prototype without passing an iid fails to call a COM method, a WindowsError with the winerror attribute is raised.
  • On the other hand, if the foreign function is generated with an iid and fails to call, a COMError is raised, providing more "extended error reporting" with the following attributes:
    • hresult: an HRESULT value indicating an error, with any integer other than S_OK = 0
    • text: an error message string
    • details: a tuple of length 5 representing Description, Source, HelpFile, HelpContext, and scode

Clearly mentioning this behavior would benefit not only those working with comtypes but also anyone involved in projects where COMError may arise.
At the very least, it would guide developers encountering this error to search hresult as a Windows error code.

Referring to the COMError exception type as a private element such as _CData and specific to Windows such as HRESULT could help prevent developers from feeling it’s a "mysterious error" as I once did.

Any opinions would be appreciated.

Linked PRs

@junkmd junkmd added the docs Documentation in the Doc dir label Nov 9, 2024
@encukou encukou changed the title Mention COMError in the "Function prototypes" section of ctypes documentation. ctypes: Make COMError, mentioned in the "Function prototypes" section of documentation, public. Nov 20, 2024
@encukou encukou removed the docs Documentation in the Doc dir label Nov 20, 2024
@encukou encukou removed this from docs issues Nov 20, 2024
encukou pushed a commit that referenced this issue Nov 20, 2024
@junkmd junkmd closed this as completed Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants