From a673cbea24dc390b23b69677e185fe4e3b8accac Mon Sep 17 00:00:00 2001 From: Charlie Hayden <46412508+Starbuck5@users.noreply.github.com> Date: Wed, 17 Jan 2024 00:12:13 -0800 Subject: [PATCH] Merge pull request #2633 from Matiiss/matiiss-improve-window-flip-err-msg Improved `Window.flip` error message when no surface is associated with the `Window` --- src_c/window.c | 9 ++++++++- test/window_test.py | 9 +++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src_c/window.c b/src_c/window.c index fddbc6f374..0c4ce65cb1 100644 --- a/src_c/window.c +++ b/src_c/window.c @@ -183,11 +183,18 @@ window_flip(pgWindowObject *self) { int result; + if (!self->surf) { + return RAISE(pgExc_SDLError, + "the Window has no surface associated with it, did " + "you forget to call Window.get_surface()"); + } + Py_BEGIN_ALLOW_THREADS; result = SDL_UpdateWindowSurface(self->_win); Py_END_ALLOW_THREADS; - if (result) + if (result) { return RAISE(pgExc_SDLError, SDL_GetError()); + } Py_RETURN_NONE; } diff --git a/test/window_test.py b/test/window_test.py index 4b6b9fdd3a..302ccc78a4 100644 --- a/test/window_test.py +++ b/test/window_test.py @@ -360,6 +360,15 @@ def test_window_flip(self): self.assertIs(win.flip(), None) win.destroy() + # creates a new window with no surface associated + win = Window(size=(640, 480)) + self.assertRaisesRegex( + pygame.error, + "the Window has no surface associated with it, did you forget to call Window.get_surface()", + win.flip, + ) + win.destroy() + def tearDown(self): self.win.destroy()