refactor(gui_2): migrate L1197 _show_menus hwnd to Result[T] (Phase 3)

TIER-2 READ conductor/code_styleguides/error_handling.md end-to-end before Phase 3.

Adds _show_menus_hwnd_result(app) -> Result[int] helper that wraps the
ctypes PyCapsule_GetPointer try/except from App._show_menus. The data
field carries the resolved hwnd (or 0 on failure) so the legacy wrapper
can pass it to subsequent win32gui calls without an additional app.hwnd
instance attribute.

App._show_menus becomes a thin wrapper that drains errors to
_last_request_errors when the hwnd capsule resolution fails.

Audit: BROAD_CATCH count 21 -> 20, COMPLIANT count 16 -> 17. Tests: 2/2 pass.
This commit is contained in:
ed
2026-06-19 22:11:14 -04:00
parent bcbd46445f
commit f51abe0795
2 changed files with 77 additions and 10 deletions
+42 -1
View File
@@ -378,4 +378,45 @@ def test_phase_3_l1171_show_menus_do_generate_result_failure():
err = result.errors[0]
assert err.source == "gui_2._show_menus_do_generate_result"
assert "generate blew up" in err.message
assert "error" in app.ai_status
assert "error" in app.ai_status
def test_phase_3_l1197_show_menus_hwnd_result_success():
"""
L1197 _show_menus_hwnd_result returns Result.ok=True on success.
The helper wraps the ctypes PyCapsule_GetPointer try/except in
App._show_menus. On success, returns Result(data=hwnd) with the
resolved window handle.
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
mock_viewport = MagicMock()
mock_viewport.platform_handle_raw = "mock_capsule"
with patch.object(gui_2.imgui, "get_main_viewport", return_value=mock_viewport), \
patch("ctypes.pythonapi.PyCapsule_GetPointer", return_value=12345):
result = gui_2._show_menus_hwnd_result(app)
assert result.ok, f"Expected ok=True on success, got errors: {result.errors}"
assert result.data == 12345
def test_phase_3_l1197_show_menus_hwnd_result_failure():
"""
L1197 _show_menus_hwnd_result returns Result.ok=False with ErrorInfo on failure.
When the ctypes call raises (e.g., on a non-Windows platform or when
imgui.get_main_viewport returns None), the helper returns
Result(data=0, errors=[ErrorInfo]).
"""
from src import gui_2
from unittest.mock import MagicMock, patch
app = MagicMock()
# Force the except branch by raising inside the try block
with patch.object(gui_2.imgui, "get_main_viewport", side_effect=RuntimeError("no viewport")):
result = gui_2._show_menus_hwnd_result(app)
assert not result.ok, f"Expected ok=False on failure, got data: {result.data}"
assert result.errors, "Expected at least one error on failure"
err = result.errors[0]
assert err.source == "gui_2._show_menus_hwnd_result"
assert result.data == 0