Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0097b02

Browse files
committed
Handle GetForegroundWindow() returning NULL.
GetForegroundWindow() is documented as sometimes returning NULL, which is not a valid argument to PyCapsule_New. Use None to cover that case, instead.
1 parent 599a964 commit 0097b02

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

lib/matplotlib/backends/_backend_tk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ def _restore_foreground_window_at_end():
4444
try:
4545
yield
4646
finally:
47-
if mpl.rcParams['tk.window_focus']:
47+
if foreground and mpl.rcParams['tk.window_focus']:
4848
_c_internal_utils.Win32_SetForegroundWindow(foreground)
4949

5050

src/_c_internal_utils.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,11 @@ static py::object
111111
mpl_GetForegroundWindow(void)
112112
{
113113
#ifdef _WIN32
114-
return py::capsule(GetForegroundWindow(), "HWND");
114+
if (HWND hwnd = GetForegroundWindow()) {
115+
return py::capsule(hwnd, "HWND");
116+
} else {
117+
return py::none();
118+
}
115119
#else
116120
return py::none();
117121
#endif

0 commit comments

Comments
 (0)