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

Extend PyWin_SetAPIError #2188

Merged
merged 2 commits into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
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
Loading