Skip to content

Commit 6183cdf

Browse files
committed
Add NoArcadeWindowError and have_window function
* Add NoArcadeWindowError subclassing RuntimeError * Make arcade.window_commands.get_window() raise NoArcadeWindowError when no window exists * Add arcade.window_commands.have_window() function * Improve documentation of get_window() function
1 parent 33e5960 commit 6183cdf

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

arcade/exceptions.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from typing import TypeVar
88

99
__all__ = [
10+
"NoArcadeWindowError",
1011
"OutsideRangeError",
1112
"IntOutsideRangeError",
1213
"FloatOutsideRangeError",
@@ -23,6 +24,15 @@
2324
_CT = TypeVar("_CT") # Comparable type, ie supports the <= operator
2425

2526

27+
28+
class NoArcadeWindowError(RuntimeError):
29+
"""No valid Arcade window exists.
30+
31+
It may be handled as a :py:class:`RuntimeError`.
32+
"""
33+
...
34+
35+
2636
class OutsideRangeError(ValueError):
2737
"""
2838
Raised when a value is outside and expected range

arcade/window_commands.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pyglet
1616

1717
from arcade.types import RGBA255, Color
18+
from arcade.exceptions import NoArcadeWindowError
1819

1920
if TYPE_CHECKING:
2021
from arcade import Window
@@ -25,6 +26,7 @@
2526
__all__ = [
2627
"get_display_size",
2728
"get_window",
29+
"have_window",
2830
"set_window",
2931
"close_window",
3032
"run",
@@ -54,14 +56,31 @@ def get_display_size(screen_id: int = 0) -> tuple[int, int]:
5456
return screen.width, screen.height
5557

5658

57-
def get_window() -> Window:
59+
def have_window() -> bool:
60+
"""Returns ``True`` if an Arcade window exists.
61+
62+
.. tip:: Use this to avoid an :py:class:`~arcade.exceptions.NoArcadeWindowError`.
63+
64+
65+
Returns:
66+
Whether a :py:class:`~arcade.Window` exists.
5867
"""
59-
Return a handle to the current window.
68+
return _window is None
6069

61-
:return: Handle to the current window.
70+
71+
def get_window() -> Window:
72+
"""Return a handle to the current window.
73+
74+
If no window exists, it will raise an exception you can
75+
handle as a :py:class:`RuntimeError`. Use :py:func:`have_window`
76+
to prevent raising an exception.
77+
78+
Raises:
79+
:py:class:`~arcade.exceptions.NoArcadeWindowError` when no window exists.
6280
"""
81+
# This avoids calling the function above because it may be a hot code path.
6382
if _window is None:
64-
raise RuntimeError("No window is active. It has not been created yet, or it was closed.")
83+
raise NoArcadeWindowError("No window is active. It has not been created yet, or it was closed.")
6584

6685
return _window
6786

0 commit comments

Comments
 (0)