Skip to content

Fix message handling for errors raised from HRESULT or WinHTTP. #144

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/_native/helpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
2 changes: 2 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions tests/test_shortcut.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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()

Expand Down
2 changes: 1 addition & 1 deletion tests/test_urlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down