From 0db52d54afd46d94ed9c6d13cdce2e38ee29afbb Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 25 Jun 2025 21:24:25 +0100 Subject: [PATCH 1/2] Fix message handling for errors raised from HRESULT or WinHTTP. Fixes #142 --- src/_native/helpers.cpp | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/_native/helpers.cpp b/src/_native/helpers.cpp index b81bfc9..4d59aa4 100644 --- a/src/_native/helpers.cpp +++ b/src/_native/helpers.cpp @@ -51,6 +51,15 @@ void err_SetFromWindowsErrWithMessage(int error, const char *message, const wcha cause = PyErr_GetRaisedException(); } + if ((error & 0xFFFF0000) == 0x80070000) { + // Error code is an HRESULT containing a regular Windows error + error &= 0xFFFF; + } + if (!hModule && error >= 12000 && error <= 12184) { + // Error codes are from WinHTTP, which means we need a module + hModule = GetModuleHandleW(L"winhttp"); + } + if (!os_message) { DWORD len = FormatMessageW( /* Error API error */ From 7d1c184231d34b31ba1480554cd9d8e027613c53 Mon Sep 17 00:00:00 2001 From: Steve Dower Date: Wed, 25 Jun 2025 23:00:03 +0100 Subject: [PATCH 2/2] Fix expected error code and ensure COM is initialised for tests --- tests/conftest.py | 2 ++ tests/test_shortcut.py | 2 -- tests/test_urlutils.py | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index d475ad5..6d99e0b 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -17,6 +17,8 @@ if k[:1] not in ("", "_"): setattr(_native, k, getattr(_native_test, k)) +_native.coinitialize() + # Importing in order carefully to ensure the variables we override are handled # correctly by submodules. diff --git a/tests/test_shortcut.py b/tests/test_shortcut.py index a965160..1243ff7 100644 --- a/tests/test_shortcut.py +++ b/tests/test_shortcut.py @@ -7,7 +7,6 @@ def test_simple_shortcut(tmp_path): open(tmp_path / "target.txt", "wb").close() - _native.coinitialize() _native.shortcut_create( tmp_path / "test.lnk", tmp_path / "target.txt", @@ -17,7 +16,6 @@ def test_simple_shortcut(tmp_path): def test_start_path(): - _native.coinitialize() p = Path(_native.shortcut_get_start_programs()) assert p.is_dir() diff --git a/tests/test_urlutils.py b/tests/test_urlutils.py index 14e5986..e78769e 100644 --- a/tests/test_urlutils.py +++ b/tests/test_urlutils.py @@ -227,7 +227,7 @@ def inject_error(): def test_bits_errors(localserver, tmp_path, inject_error): import uuid - ERROR_MR_MID_NOT_FOUND = 0x8007013D + ERROR_MR_MID_NOT_FOUND = 0x13D dest = tmp_path / "read.txt" url = localserver + "/128kb"