Skip to content

Commit beedc25

Browse files
authored
Fix message handling for errors raised from HRESULT or WinHTTP. (#144)
Fixes #142
1 parent 2f864b7 commit beedc25

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed

src/_native/helpers.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ void err_SetFromWindowsErrWithMessage(int error, const char *message, const wcha
5151
cause = PyErr_GetRaisedException();
5252
}
5353

54+
if ((error & 0xFFFF0000) == 0x80070000) {
55+
// Error code is an HRESULT containing a regular Windows error
56+
error &= 0xFFFF;
57+
}
58+
if (!hModule && error >= 12000 && error <= 12184) {
59+
// Error codes are from WinHTTP, which means we need a module
60+
hModule = GetModuleHandleW(L"winhttp");
61+
}
62+
5463
if (!os_message) {
5564
DWORD len = FormatMessageW(
5665
/* Error API error */

tests/conftest.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
if k[:1] not in ("", "_"):
1818
setattr(_native, k, getattr(_native_test, k))
1919

20+
_native.coinitialize()
21+
2022

2123
# Importing in order carefully to ensure the variables we override are handled
2224
# correctly by submodules.

tests/test_shortcut.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77

88
def test_simple_shortcut(tmp_path):
99
open(tmp_path / "target.txt", "wb").close()
10-
_native.coinitialize()
1110
_native.shortcut_create(
1211
tmp_path / "test.lnk",
1312
tmp_path / "target.txt",
@@ -17,7 +16,6 @@ def test_simple_shortcut(tmp_path):
1716

1817

1918
def test_start_path():
20-
_native.coinitialize()
2119
p = Path(_native.shortcut_get_start_programs())
2220
assert p.is_dir()
2321

tests/test_urlutils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,7 @@ def inject_error():
227227
def test_bits_errors(localserver, tmp_path, inject_error):
228228
import uuid
229229

230-
ERROR_MR_MID_NOT_FOUND = 0x8007013D
230+
ERROR_MR_MID_NOT_FOUND = 0x13D
231231

232232
dest = tmp_path / "read.txt"
233233
url = localserver + "/128kb"

0 commit comments

Comments
 (0)