Skip to content

Commit

Permalink
video_test: Mitigate unsuitability of SDL_GetError() for detecting fa…
Browse files Browse the repository at this point in the history
…ilure

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: #257
Signed-off-by: Simon McVittie <smcv@debian.org>
  • Loading branch information
smcv committed Aug 19, 2023
1 parent fd67df5 commit 915f7fa
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions sdl2/test/video_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down Expand Up @@ -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""

0 comments on commit 915f7fa

Please sign in to comment.