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

Last error code errorneously set by some modules #2301

Open
CristiFati opened this issue Jun 23, 2024 · 0 comments
Open

Last error code errorneously set by some modules #2301

CristiFati opened this issue Jun 23, 2024 · 0 comments

Comments

@CristiFati
Copy link
Contributor

CristiFati commented Jun 23, 2024

Was 1st reported in #2163 (comment).

One might be tricked to think that it has something to do with my EnumWindows functions family related changes, but it turns out it's a subtle issue, and it's more generic.

After half hour of trying I was able to come up with a (related) MCVE.

err.py:

import ctypes as cts
import ctypes.wintypes as wts

GetLastError = cts.windll.kernel32.GetLastError
GetLastError.argtypes = ()
GetLastError.restype = wts.DWORD

print(f"GetLastError (cts): {GetLastError()}")

print("import pywintypes")
import pywintypes as pwts
print(f"GetLastError (cts): {GetLastError()}")

print("import win32api")
import win32api as wapi
print(f"GetLastError (cts): {GetLastError()}")
print(f"GetLastError (wapi): {wapi.GetLastError()}")

print(f"GetLastError (cts): {GetLastError()}")
last_err = 0
print(f"Setting (wapi) error to {last_err}")
wapi.SetLastError(last_err)

print(f"GetLastError (wapi): {wapi.GetLastError()}")
print(f"GetLastError (cts): {GetLastError()}")

Output:

(py_pc064_03.10_test1_pw32) [cfati@CFATI-5510-0:e:\Work\Dev\Repos\GitHub\CristiFati\pywin32]> python err.py
GetLastError (cts): 0
import pywintypes
GetLastError (cts): 0
import win32api
GetLastError (cts): 126
GetLastError (wapi): 126
GetLastError (cts): 126
Setting (wapi) error to 0
GetLastError (wapi): 0
GetLastError (cts): 0

So, for some reason, win32api sets the last error code.
Same thing happens for win32gui, yielding my (PyWin_SetAPIErrorOrReturnNone) changes a nop.

Also, this has probably been present since forever (also reproduced with v304).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant