Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tests: Don't assume SDL_Init will leave SDL_GetError unset #258

Merged
merged 1 commit into from
Feb 12, 2023

Conversation

smcv
Copy link
Contributor

@smcv smcv commented Feb 8, 2023

PR Description

SDL_GetError works like Standard C errno: if a call fails it will (generally) set the error indicator, but if a call succeeds there is no guarantee about whether the error indicator has been set, cleared, or left at its previous value.

The SDL documentation says this:

The message is only applicable when an SDL function has signaled an
error. You must check the return values of SDL function calls to
determine when to appropriately call SDL_GetError(). You should not
use the results of SDL_GetError() to decide if an error has occurred!
Sometimes SDL will set an error string even when reporting success.

In particular, with a SDL 2.27.x git snapshot, a successful SDL_Init can leave the error indicator set to a message about inability to open an input device node, and this does not mean it has failed.

In this commit I have not attempted to check or fix other uses of SDL_GetError, only the ones that follow SDL_Init. This is enough to make the test suite pass on Debian 12 alpha with
SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy SDL_RENDER_DRIVER=software for now, but the other SDL_GetError() calls in the test suite should ideally also be checked.

Partial solution for: #257

Merge Checklist

  • the PR has been reviewed and all comments are resolved
  • all CI checks pass
  • (if applicable): the PR description includes the phrase closes #<issue-number> to automatically close an issue
  • (if applicable): bug fixes, new features, or API changes are documented in news.rst

SDL_GetError works like Standard C errno: if a call fails it will
(generally) set the error indicator, but if a call succeeds there is no
guarantee about whether the error indicator has been set, cleared, or
left at its previous value.

The SDL documentation says this:

    The message is only applicable when an SDL function has signaled an
    error. You must check the return values of SDL function calls to
    determine when to appropriately call SDL_GetError(). You should not
    use the results of SDL_GetError() to decide if an error has occurred!
    Sometimes SDL will set an error string even when reporting success.

In particular, with a SDL 2.27.x git snapshot, a successful SDL_Init can
leave the error indicator set to a message about inability to open an
input device node, and this does not mean it has failed.

In this commit I have not attempted to check or fix other uses of
SDL_GetError, only the ones that follow SDL_Init. This is enough to make
the test suite pass on Debian 12 alpha with
`SDL_VIDEODRIVER=dummy SDL_AUDIODRIVER=dummy SDL_RENDER_DRIVER=software`
for now, but the other SDL_GetError() calls in the test suite should
ideally also be checked.

Partial solution for: py-sdl#257

Signed-off-by: Simon McVittie <smcv@collabora.com>
@@ -23,8 +23,7 @@ def with_sdl_audio():
sdl2.SDL_Quit()
sdl2.SDL_ClearError()
ret = sdl2.SDL_Init(sdl2.SDL_INIT_VIDEO | sdl2.SDL_INIT_AUDIO)
assert sdl2.SDL_GetError() == b""
assert ret == 0
assert ret == 0, sdl2.SDL_GetError().decode('utf-8', 'replace')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is intended to result in showing the SDL error indicator as an error message if the call fails.

@a-hurst
Copy link
Member

a-hurst commented Feb 12, 2023

Ah yep, I learned about this convention unexpectedly a release or two ago. I've already fixed a few older tests that were failing due to non-fatal errors, but clearly I wasn't very thorough. Thanks for the heads-up about 2.27 as well as the patch!

When things calm down a bit with work and my studies I'll make time to comprehensively find/fix this assumption throughout the test suite so I can be robust against future releases.

@a-hurst a-hurst merged commit 8c39f40 into py-sdl:master Feb 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants