Skip to content

Commit

Permalink
Extend PyWin_SetAPIError
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiFati committed Mar 17, 2024
1 parent e0fda45 commit 7145515
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
2 changes: 1 addition & 1 deletion win32/src/PyWinTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ extern PYWINTYPES_EXPORT BOOL PyWin_RegisterErrorMessageModule(DWORD first, DWOR
extern PYWINTYPES_EXPORT HINSTANCE PyWin_GetErrorMessageModule(DWORD err);

/* A global function that sets an API style error (ie, (code, fn, errTest)) */
PYWINTYPES_EXPORT PyObject *PyWin_SetAPIError(char *fnName, long err = 0);
PYWINTYPES_EXPORT PyObject *PyWin_SetAPIError(const char *fnName, long err = 0, bool returnNoneOnSuccess = false);

/* Basic COM Exception handling. The main COM exception object
is actually defined here. However, the most useful functions
Expand Down
4 changes: 3 additions & 1 deletion win32/src/PyWinTypesmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,11 @@ HINSTANCE PyWin_GetErrorMessageModule(DWORD err)
}

/* error helper - GetLastError() is provided, but this is for exceptions */
PyObject *PyWin_SetAPIError(char *fnName, long err /*= 0*/)
PyObject *PyWin_SetAPIError(const char *fnName, long err /*= 0*/, bool returnNoneOnSuccess /*= false*/)
{
DWORD errorCode = err == 0 ? GetLastError() : err;
if ((returnNoneOnSuccess) && (!errorCode))
Py_RETURN_NONE;
DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS;
// try and find the hmodule providing this error.
HMODULE hmodule = PyWin_GetErrorMessageModule(errorCode);
Expand Down

0 comments on commit 7145515

Please sign in to comment.