From ea4f62431c518326cd4aabb126c48ff8951b579c Mon Sep 17 00:00:00 2001 From: CristiFati Date: Sun, 17 Mar 2024 19:14:28 +0200 Subject: [PATCH 1/2] Add PyWin_SetAPIErrorOrReturnNone --- win32/src/PyWinTypes.h | 3 +++ win32/src/PyWinTypesmodule.cpp | 9 +++++++++ 2 files changed, 12 insertions(+) mode change 100644 => 100755 win32/src/PyWinTypes.h mode change 100644 => 100755 win32/src/PyWinTypesmodule.cpp diff --git a/win32/src/PyWinTypes.h b/win32/src/PyWinTypes.h old mode 100644 new mode 100755 index 6a0d3462a6..19a0be1800 --- a/win32/src/PyWinTypes.h +++ b/win32/src/PyWinTypes.h @@ -103,6 +103,9 @@ 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); +// 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 for raising the exception are still in the COM package. Therefore, diff --git a/win32/src/PyWinTypesmodule.cpp b/win32/src/PyWinTypesmodule.cpp old mode 100644 new mode 100755 index 3dfaefc397..8cf10c85ae --- a/win32/src/PyWinTypesmodule.cpp +++ b/win32/src/PyWinTypesmodule.cpp @@ -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. From e325b62716266a39b29cb5281d6691050956ba70 Mon Sep 17 00:00:00 2001 From: CristiFati Date: Sun, 17 Mar 2024 19:16:33 +0200 Subject: [PATCH 2/2] Small macro replace --- win32/src/PyWinTypes.h | 2 +- win32/src/PyWinTypesmodule.cpp | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) mode change 100755 => 100644 win32/src/PyWinTypes.h mode change 100755 => 100644 win32/src/PyWinTypesmodule.cpp diff --git a/win32/src/PyWinTypes.h b/win32/src/PyWinTypes.h old mode 100755 new mode 100644 index 19a0be1800..b45184cf2e --- a/win32/src/PyWinTypes.h +++ b/win32/src/PyWinTypes.h @@ -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(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); diff --git a/win32/src/PyWinTypesmodule.cpp b/win32/src/PyWinTypesmodule.cpp old mode 100755 new mode 100644 index 8cf10c85ae..c669501169 --- a/win32/src/PyWinTypesmodule.cpp +++ b/win32/src/PyWinTypesmodule.cpp @@ -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);