Skip to content

Commit 715af60

Browse files
fix: narrow window title exception handling (#141)
* fix: narrow window title exception handling * test: cover window title errors without a GUI --------- Co-authored-by: Peter Corke <peter.i.corke@gmail.com>
1 parent bcf63e4 commit 715af60

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

‎src/machinevisiontoolbox/base/imageio.py‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1030,7 +1030,8 @@ def _idisp_opencv(
10301030
def set_window_title(title: str) -> None:
10311031
try:
10321032
_plt().gcf().canvas.manager.set_window_title(title) # for 3.4 onward
1033-
except:
1033+
except (AttributeError, RuntimeError):
1034+
# Some noninteractive backends do not have a usable window manager.
10341035
pass
10351036

10361037

‎tests/test_window_title_errors.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
from types import SimpleNamespace
2+
from unittest.mock import Mock, patch
3+
4+
import pytest
5+
6+
from machinevisiontoolbox.base import imageio
7+
8+
9+
def test_window_title_is_forwarded():
10+
"""Forward the title to a supported window manager."""
11+
manager = Mock()
12+
figure = SimpleNamespace(canvas=SimpleNamespace(manager=manager))
13+
with patch.object(imageio, "_plt") as pyplot:
14+
pyplot.return_value.gcf.return_value = figure
15+
imageio.set_window_title("camera")
16+
manager.set_window_title.assert_called_once_with("camera")
17+
18+
19+
@pytest.mark.parametrize("error", [AttributeError, RuntimeError])
20+
def test_unsupported_window_title_is_ignored(error):
21+
"""Tolerate window managers that cannot set a title."""
22+
with patch.object(imageio, "_plt", side_effect=error("unsupported")):
23+
imageio.set_window_title("camera")
24+
25+
26+
@pytest.mark.parametrize("error", [ValueError, KeyboardInterrupt])
27+
def test_unexpected_window_title_errors_propagate(error):
28+
"""Do not hide unrelated failures or user interrupts."""
29+
with patch.object(imageio, "_plt", side_effect=error()):
30+
with pytest.raises(error):
31+
imageio.set_window_title("camera")

0 commit comments

Comments
 (0)