mhammond / pywin32

Python for Windows (pywin32) Extensions
4.9k stars 783 forks source link

Cleanly break out of windows iteration #2183

Closed CristiFati closed 4 months ago

CristiFati commented 4 months ago

This is a (proposed) fix for #2163. If the user wants to stop iteration (by returning FALSE (or equivalent) from the callback) EnumWindows (&& friends) should not raise exception. It should only raise exception if callback:

Notes:

mhammond commented 4 months ago

I quite like these semantics, but I'm not clear on the b/w compat story. Although you added this as an "option", each call site is what determines this option.

In other words, I'd like to understand how your tests change if you do not ever specify that new bool.

CristiFati commented 4 months ago

I think that design-wise the correct behavior would be to always return None when there is no error (like calling the function with the (extra) bool set to true). But I am afraid that there is code relying on the fact that an exception is raised even if GetLastError returns 0, so I took the safe side, and I just added support for this behavior, because I think that changing it altogether might break some (external) things. As code is right now, nothing changes in all places that call PyWin_SetAPIError. If you think that changing default behavior wouldn't be a problem I can change that bool's default value (or remove it altogether (keeping the new behavior)).

CristiFati commented 4 months ago

Closing, as it was replaced by the 2 linked PRs.