Skip to content

Commit

Permalink
Extend PyWin_SetAPIError (#2188)
Browse files Browse the repository at this point in the history
  • Loading branch information
CristiFati authored Mar 18, 2024
1 parent e0fda45 commit 7cac4eb
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
5 changes: 4 additions & 1 deletion win32/src/PyWinTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,10 @@ 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(char *fnName, long err = ERROR_SUCCESS);

// A PyWin_SetAPIError variant that returns None (Py_None) on success.
PYWINTYPES_EXPORT PyObject *PyWin_SetAPIErrorOrReturnNone(char *fnName, long err = ERROR_SUCCESS);

/* Basic COM Exception handling. The main COM exception object
is actually defined here. However, the most useful functions
Expand Down
13 changes: 11 additions & 2 deletions win32/src/PyWinTypesmodule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,9 @@ 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(char *fnName, long err /*= ERROR_SUCCESS*/)
{
DWORD errorCode = err == 0 ? GetLastError() : err;
DWORD errorCode = err == ERROR_SUCCESS ? GetLastError() : err;
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 Expand Up @@ -308,6 +308,15 @@ PyObject *PyWin_SetAPIError(char *fnName, long err /*= 0*/)
return NULL;
}

/* error helper - like PyWin_SetAPIError, but returns None on success */
PyObject *PyWin_SetAPIErrorOrReturnNone(char *fnName, long err /*= ERROR_SUCCESS*/)
{
DWORD errorCode = err == ERROR_SUCCESS ? GetLastError() : err;
if (errorCode == ERROR_SUCCESS)
Py_RETURN_NONE;
return PyWin_SetAPIError(fnName, errorCode);
}

// This function sets a basic COM error - it is a valid COM
// error, but may not contain rich error text about the error.
// Designed to be used before pythoncom has been loaded.
Expand Down

0 comments on commit 7cac4eb

Please sign in to comment.