From 915f7fa2c99450a74e117c42b59ba956eae7b102 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Sat, 19 Aug 2023 15:44:52 +0100 Subject: [PATCH] video_test: Mitigate unsuitability of SDL_GetError() for detecting failure SDL_GetError() is like errno: it's documented not to be suitable for detecting failure, only for getting more details if failure was already detected (its result is unspecified on success, because a successful API call might have been implemented by doing something that failed, detecting that, and falling back to doing something different). However, some functions in SDL2 return void, so we have no other way to tell whether they have failed (they do return a result in SDL3). To make it less likely that upgrading SDL2 will make these tests regress, clear the error indicator immediately before calling the function under test. It is still not guaranteed to be empty on success, but at least this way we make sure it doesn't already contain an error message from a previous function call. Helps: https://github.com/py-sdl/py-sdl2/issues/257 Signed-off-by: Simon McVittie --- sdl2/test/video_test.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/sdl2/test/video_test.py b/sdl2/test/video_test.py index 57fa9c6..ca5a8e4 100644 --- a/sdl2/test/video_test.py +++ b/sdl2/test/video_test.py @@ -444,7 +444,10 @@ def test_SDL_SetWindowIcon(window): 0, 16, 16, 16, 0xF000, 0x0F00, 0x00F0, 0x000F ) assert isinstance(sf.contents, surface.SDL_Surface) + sdl2.SDL_ClearError() sdl2.SDL_SetWindowIcon(window, sf) + # TODO: This is not 100% safe, but in SDL2, SetWindowIcon returns void, + # so we can't reliably detect error assert SDL_GetError() == b"" @pytest.mark.xfail(is_pypy, reason="PyPy can't create proper py_object values") @@ -526,7 +529,10 @@ def test_SDL_GetSetWindowMinimumSize(window): sdl2.SDL_GetWindowSize(window, byref(sx), byref(sy)) assert (sx.value, sy.value) == (12, 13) # Set and verify the minimum window size + sdl2.SDL_ClearError() sdl2.SDL_SetWindowMinimumSize(window, 10, 10) + # TODO: This is not 100% safe, but in SDL2, SetWindowMinimumSize returns + # void, so we can't reliably detect error assert SDL_GetError() == b"" sdl2.SDL_GetWindowMinimumSize(window, byref(minx), byref(miny)) assert (minx.value, miny.value) == (10, 10) @@ -540,8 +546,11 @@ def test_SDL_GetSetWindowMaximumSize(window): maxx, maxy = c_int(0), c_int(0) sdl2.SDL_GetWindowSize(window, byref(sx), byref(sy)) assert (sx.value, sy.value) == (12, 13) + sdl2.SDL_ClearError() # Set and verify the maximum window size sdl2.SDL_SetWindowMaximumSize(window, 32, 32) + # TODO: This is not 100% safe, but in SDL2, SetWindowMaximumSize returns + # void, so we can't reliably detect error assert SDL_GetError() == b"" sdl2.SDL_GetWindowMaximumSize(window, byref(maxx), byref(maxy)) assert (maxx.value, maxy.value) == (32, 32) @@ -936,7 +945,10 @@ def test_SDL_GL_SwapWindow(gl_window): window, ctx = gl_window ret = sdl2.SDL_GL_MakeCurrent(window, ctx) assert ret == 0, _check_error_msg() + sdl2.SDL_ClearError() sdl2.SDL_GL_SwapWindow(window) sdl2.SDL_GL_SwapWindow(window) sdl2.SDL_GL_SwapWindow(window) + # TODO: This is not 100% safe, but in SDL2, GL_SwapWindow returns + # void, so we can't reliably detect error assert SDL_GetError() == b""