Skip to content

Commit d45476a

Browse files
pushfooCleptomania
andauthored
Add NoArcadeWindowError subclassing RuntimeError (#2784)
* 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 * Attempt to make ruff happy * Remove have_window() since the name / location are controversial * Add window_exists function * Fix doc naming --------- Co-authored-by: Darren Eberly <[email protected]>
1 parent 33e5960 commit d45476a

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

arcade/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ def configure_logging(level: int | None = None):
9090
from .window_commands import start_render
9191
from .window_commands import unschedule
9292
from .window_commands import schedule_once
93+
from .window_commands import window_exists
9394

9495
from .sections import Section, SectionManager
9596

@@ -359,6 +360,7 @@ def configure_logging(level: int | None = None):
359360
"create_text_sprite",
360361
"clear_timings",
361362
"get_window",
363+
"window_exists",
362364
"get_fps",
363365
"has_line_of_sight",
364366
"load_animated_gif",

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+
class NoArcadeWindowError(RuntimeError):
28+
"""No valid Arcade window exists.
29+
30+
It may be handled as a :py:class:`RuntimeError`.
31+
"""
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: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import pyglet
1616

17+
from arcade.exceptions import NoArcadeWindowError
1718
from arcade.types import RGBA255, Color
1819

1920
if TYPE_CHECKING:
@@ -26,6 +27,7 @@
2627
"get_display_size",
2728
"get_window",
2829
"set_window",
30+
"window_exists",
2931
"close_window",
3032
"run",
3133
"exit",
@@ -55,13 +57,19 @@ def get_display_size(screen_id: int = 0) -> tuple[int, int]:
5557

5658

5759
def get_window() -> Window:
58-
"""
59-
Return a handle to the current window.
60+
"""Return a handle to the current window.
61+
62+
If no window exists, it will raise an exception you can
63+
handle as a :py:class:`RuntimeError`. Use :py:func:`window_exists`
64+
to prevent raising an exception.
6065
61-
:return: Handle to the current window.
66+
Raises:
67+
:py:class:`~arcade.exceptions.NoArcadeWindowError` when no window exists.
6268
"""
6369
if _window is None:
64-
raise RuntimeError("No window is active. It has not been created yet, or it was closed.")
70+
raise NoArcadeWindowError(
71+
"No window is active. It has not been created yet, or it was closed."
72+
)
6573

6674
return _window
6775

@@ -77,6 +85,21 @@ def set_window(window: Window | None) -> None:
7785
_window = window
7886

7987

88+
def window_exists() -> bool:
89+
"""
90+
Returns True or False based on wether there is currently a Window.
91+
92+
Returns:
93+
Boolean for if a window exists.
94+
"""
95+
try:
96+
get_window()
97+
except NoArcadeWindowError:
98+
return False
99+
100+
return True
101+
102+
80103
def close_window() -> None:
81104
"""
82105
Closes the current window, and then runs garbage collection. The garbage collection

0 commit comments

Comments
 (0)