Skip to content

Commit

Permalink
Backport PR matplotlib#28269: Handle GetForegroundWindow() returning …
Browse files Browse the repository at this point in the history
…NULL.
  • Loading branch information
QuLogic authored and meeseeksmachine committed May 21, 2024
1 parent cb4f0d6 commit 8cb17f8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/matplotlib/backends/_backend_tk.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def _restore_foreground_window_at_end():
try:
yield
finally:
if mpl.rcParams['tk.window_focus']:
if foreground and mpl.rcParams['tk.window_focus']:
_c_internal_utils.Win32_SetForegroundWindow(foreground)


Expand Down
6 changes: 5 additions & 1 deletion src/_c_internal_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,11 @@ static py::object
mpl_GetForegroundWindow(void)
{
#ifdef _WIN32
return py::capsule(GetForegroundWindow(), "HWND");
if (HWND hwnd = GetForegroundWindow()) {
return py::capsule(hwnd, "HWND");
} else {
return py::none();
}
#else
return py::none();
#endif
Expand Down

0 comments on commit 8cb17f8

Please sign in to comment.