Skip to content

Commit 7ebf0a3

Browse files
authored
Migrate tests to modern pytest style (#215)
* Misc. cleanup * Add basic conftest module * Migrate simpler modules to new pytest style * Clean up init, error, and power module tests * Clean up pixels module tests * Fix RGB detection bug in ext.Color * Major cleanup of ext.Color tests * Migrate ext.surface to pytest * Fix spritesystem test failures on real macOS * Fix conftest mistake * Clean up spritesystem tests * Move cleanup fixture to conftest * Initial round of video module pytest cleanup * Fix video mode test with dummy driver * Fix ext.color on 2.7 and SDL init test for CI * Whoops * Try fixing color module and init tests again * Fix pixel test bug with SDL 2.0.5 * Initial cleanup/improvements for SDL_image tests * Fix video tests on smaller screens * Clear some non-critical ext errors * Ignore non-critical errors for image tests * Additional video test cleanup * Try fixing initial video driver detection * xfail problem test on PyPy * More video test overhaul * Migrate joystick tests to pytest * Fix new video tests with dummy driver * Improve handling of broken joystick subsystem * Fix tests with real X360 gamepad on Windows * Fix an error with a None-or-string test * Additional test fixes * More fixes and cleanup * Initial cleanup of ext.Renderer tests * Mark unreliable framerate test as xfail * Clean and expland gamecontroller tests * Finish video test rewrites * Mark hidapi init as xfail for now * Migrate more modules to pytest * Migrate timer and syswm tests to pytest * Migrate keyboard tests to pytest * Finish migrating SDL_image to pytest * xfail some timer tests for CI * Clean up and migrate render tests * Migrate sdl2.ext common tests to pytest * Avoid test failure on SDL 2.0.8 * Migrate more ext modules to pytest * Migrate the ext.draw and ext.ebs tests to pytest * Migrate more ext modules to pytest * Make annoying audiovisual tests optional * Migrate ext.window tests to pytest * Migrate ext.events and ext.renderer to pytest * Implement clipboard tests * Fix some tests on real hardware * Minor cleanup of the events tests * Fix more tests for local use * Migrate TTF tests to pytest * Migrate audio tests to pytest * Migrate empty mouse tests to pytest * Try fixing local tests * Relax audio format tests
1 parent d4210dc commit 7ebf0a3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+6424
-7153
lines changed

sdl2/audio.py

Lines changed: 55 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@
5353
"AUDIO_FORMATS"
5454
]
5555

56+
57+
# Constants, enums, and macros
58+
5659
SDL_AudioFormat = Uint16
5760

5861
SDL_AUDIO_MASK_BITSIZE = 0xFF
@@ -82,7 +85,6 @@
8285
AUDIO_F32MSB = 0x9120
8386
AUDIO_F32 = AUDIO_F32LSB
8487

85-
8688
# All of the audio formats should be in this set which is provided as a
8789
# convenience to the end user for purposes of iteration and validation.
8890
# (is the provided audio format in the supported set?)
@@ -98,10 +100,13 @@
98100
AUDIO_F32LSB: "AUDIO_F32LSB",
99101
AUDIO_F32MSB: "AUDIO_F32MSB",
100102
}
101-
AUDIO_FORMATS = set([AUDIO_U8, AUDIO_S8, AUDIO_U16LSB, AUDIO_S16LSB,
102-
AUDIO_U16MSB, AUDIO_S16MSB, AUDIO_U16, AUDIO_S16,
103-
AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32, AUDIO_F32LSB,
104-
AUDIO_F32MSB, AUDIO_F32])
103+
AUDIO_FORMATS = set([
104+
AUDIO_U8, AUDIO_S8,
105+
AUDIO_U16LSB, AUDIO_U16MSB, AUDIO_U16,
106+
AUDIO_S16LSB, AUDIO_S16MSB, AUDIO_S16,
107+
AUDIO_S32LSB, AUDIO_S32MSB, AUDIO_S32,
108+
AUDIO_F32LSB, AUDIO_F32MSB, AUDIO_F32
109+
])
105110

106111
if SDL_BYTEORDER == SDL_LIL_ENDIAN:
107112
AUDIO_U16SYS = AUDIO_U16LSB
@@ -123,21 +128,37 @@
123128
SDL_AUDIO_ALLOW_CHANNELS_CHANGE | SDL_AUDIO_ALLOW_SAMPLES_CHANGE
124129
)
125130

131+
SDL_AudioDeviceID = Uint32
132+
133+
SDL_AudioStatus = c_int
134+
SDL_AUDIO_STOPPED = 0
135+
SDL_AUDIO_PLAYING = 1
136+
SDL_AUDIO_PAUSED = 2
137+
138+
SDL_MIX_MAXVOLUME = 128
139+
SDL_AUDIOCVT_MAX_FILTERS = 9
140+
141+
142+
# Structure, opaque type, and callback definitions
143+
126144
SDL_AudioCallback = CFUNCTYPE(None, c_void_p, POINTER(Uint8), c_int)
127145

128146
class SDL_AudioSpec(Structure):
129-
_fields_ = [("freq", c_int),
130-
("format", SDL_AudioFormat),
131-
("channels", Uint8),
132-
("silence", Uint8),
133-
("samples", Uint16),
134-
("padding", Uint16),
135-
("size", Uint32),
136-
("callback", SDL_AudioCallback),
137-
("userdata", c_void_p)
138-
]
139-
def __init__(self, freq, aformat, channels, samples,
140-
callback=SDL_AudioCallback(), userdata=c_void_p(0)):
147+
_fields_ = [
148+
("freq", c_int),
149+
("format", SDL_AudioFormat),
150+
("channels", Uint8),
151+
("silence", Uint8),
152+
("samples", Uint16),
153+
("padding", Uint16),
154+
("size", Uint32),
155+
("callback", SDL_AudioCallback),
156+
("userdata", c_void_p),
157+
]
158+
def __init__(
159+
self, freq, aformat, channels, samples, callback=SDL_AudioCallback(),
160+
userdata=c_void_p(0)
161+
):
141162
super(SDL_AudioSpec, self).__init__()
142163
self.freq = freq
143164
self.format = aformat
@@ -146,43 +167,41 @@ def __init__(self, freq, aformat, channels, samples,
146167
self.callback = callback
147168
self.userdata = userdata
148169

149-
SDL_AUDIOCVT_MAX_FILTERS = 9
150-
151170
class SDL_AudioCVT(Structure):
152171
pass
153172

154173
SDL_AudioFilter = CFUNCTYPE(POINTER(SDL_AudioCVT), SDL_AudioFormat)
155-
SDL_AudioCVT._fields_ = [("needed", c_int),
156-
("src_format", SDL_AudioFormat),
157-
("dst_format", SDL_AudioFormat),
158-
("rate_incr", c_double),
159-
("buf", POINTER(Uint8)),
160-
("len", c_int),
161-
("len_cvt", c_int),
162-
("len_mult", c_int),
163-
("len_ratio", c_double),
164-
("filters", (SDL_AudioFilter * (SDL_AUDIOCVT_MAX_FILTERS+1))),
165-
("filter_index", c_int)
166-
]
174+
175+
SDL_AudioCVT._fields_ = [
176+
("needed", c_int),
177+
("src_format", SDL_AudioFormat),
178+
("dst_format", SDL_AudioFormat),
179+
("rate_incr", c_double),
180+
("buf", POINTER(Uint8)),
181+
("len", c_int),
182+
("len_cvt", c_int),
183+
("len_mult", c_int),
184+
("len_ratio", c_double),
185+
("filters", (SDL_AudioFilter * (SDL_AUDIOCVT_MAX_FILTERS+1))),
186+
("filter_index", c_int),
187+
]
167188

168189
class SDL_AudioStream(c_void_p):
169190
pass
170191

192+
193+
# Raw ctypes function definitions
194+
171195
SDL_GetNumAudioDrivers = _bind("SDL_GetNumAudioDrivers", None, c_int)
172196
SDL_GetAudioDriver = _bind("SDL_GetAudioDriver", [c_int], c_char_p)
173197
SDL_AudioInit = _bind("SDL_AudioInit", [c_char_p], c_int)
174198
SDL_AudioQuit = _bind("SDL_AudioQuit")
175199
SDL_GetCurrentAudioDriver = _bind("SDL_GetCurrentAudioDriver", None, c_char_p)
176200
SDL_OpenAudio = _bind("SDL_OpenAudio", [POINTER(SDL_AudioSpec), POINTER(SDL_AudioSpec)], c_int)
177-
SDL_AudioDeviceID = Uint32
178201
SDL_GetNumAudioDevices = _bind("SDL_GetNumAudioDevices", [c_int], c_int)
179202
SDL_GetAudioDeviceName = _bind("SDL_GetAudioDeviceName", [c_int, c_int], c_char_p)
180203
SDL_GetAudioDeviceSpec = _bind("SDL_GetAudioDeviceSpec", [c_int, c_int, POINTER(SDL_AudioSpec)], c_int, added='2.0.16')
181204
SDL_OpenAudioDevice = _bind("SDL_OpenAudioDevice", [c_char_p, c_int, POINTER(SDL_AudioSpec), POINTER(SDL_AudioSpec), c_int], SDL_AudioDeviceID)
182-
SDL_AUDIO_STOPPED = 0
183-
SDL_AUDIO_PLAYING = 1
184-
SDL_AUDIO_PAUSED = 2
185-
SDL_AudioStatus = c_int
186205
SDL_GetAudioStatus = _bind("SDL_GetAudioStatus", None, SDL_AudioStatus)
187206
SDL_GetAudioDeviceStatus = _bind("SDL_GetAudioDeviceStatus", [SDL_AudioDeviceID], SDL_AudioStatus)
188207
SDL_PauseAudio = _bind("SDL_PauseAudio", [c_int])
@@ -192,7 +211,6 @@ class SDL_AudioStream(c_void_p):
192211
SDL_FreeWAV = _bind("SDL_FreeWAV", [POINTER(Uint8)])
193212
SDL_BuildAudioCVT = _bind("SDL_BuildAudioCVT", [POINTER(SDL_AudioCVT), SDL_AudioFormat, Uint8, c_int, SDL_AudioFormat, Uint8, c_int], c_int)
194213
SDL_ConvertAudio = _bind("SDL_ConvertAudio", [POINTER(SDL_AudioCVT)], c_int)
195-
SDL_MIX_MAXVOLUME = 128
196214
SDL_MixAudio = _bind("SDL_MixAudio", [POINTER(Uint8), POINTER(Uint8), Uint32, c_int])
197215
SDL_MixAudioFormat = _bind("SDL_MixAudioFormat", [POINTER(Uint8), POINTER(Uint8), SDL_AudioFormat, Uint32, c_int])
198216
SDL_LockAudio = _bind("SDL_LockAudio")

sdl2/dll.py

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -141,12 +141,6 @@ def _findlib(libnames, path=None):
141141
else:
142142
patterns = ["lib{0}.so"]
143143

144-
# On Apple Silicon Macs, search the non-standard Homebrew library path if no other
145-
# path explicitly set
146-
arm_brewpath = "/opt/Homebrew/lib"
147-
if not path and platform == "darwin" and os.path.exists(arm_brewpath):
148-
path = arm_brewpath
149-
150144
# Adding the potential 'd' suffix that is present on the library
151145
# when built in debug configuration
152146
searchfor = libnames + [libname + 'd' for libname in libnames]

sdl2/ext/color.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -454,17 +454,12 @@ def is_rgb_color(v):
454454
False.
455455
456456
"""
457-
try:
458-
if hasattr(v, "r") and hasattr(v, "g") and hasattr(v, "b"):
459-
if 0 <= int(v.r) <= 255 and 0 <= int(v.g) <= 255 and \
460-
0 <= v.b <= 255:
461-
return True
462-
463-
if len(v) >= 3:
464-
if 0 <= int(v[0]) <= 255 and 0 <= int(v[1]) <= 255 and \
465-
0 < int(v[2]) < 255:
466-
return True
457+
if hasattr(v, "r") and hasattr(v, "g") and hasattr(v, "b"):
458+
v = [v.r, v.g, v.b]
459+
if not isiterable(v) or len(v) < 3:
467460
return False
461+
try:
462+
return all([0 <= int(x) <= 255 for x in v[:3]])
468463
except (TypeError, ValueError):
469464
return False
470465

sdl2/ext/image.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ def load_img(path, as_argb=True):
171171
img_surf = surfcopy
172172
if not img_surf:
173173
raise_sdl_err("converting '{0}' to ARGB format".format(fname))
174+
error.SDL_ClearError() # Clear any non-critical errors during loading
174175

175176
return img_surf.contents
176177

sdl2/ext/renderer.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,7 @@ def __init__(self, target, backend=-1, logical_size=None,
363363
raise TypeError("unsupported target type")
364364
if not _renderer:
365365
raise_sdl_err("creating the SDL renderer")
366+
error.SDL_ClearError() # Clear any errors from renderer selection
366367
self._renderer_ref = [_renderer]
367368

368369
self.rendertarget = target

sdl2/render.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,8 @@ class SDL_Texture(c_void_p):
187187
SDL_SetTextureUserData = _bind("SDL_SetTextureUserData", [POINTER(SDL_Texture), c_void_p], c_int, added='2.0.18')
188188
SDL_GetTextureUserData = _bind("SDL_GetTextureUserData", [POINTER(SDL_Texture)], c_void_p, added='2.0.18')
189189
SDL_UpdateTexture = _bind("SDL_UpdateTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), c_void_p, c_int], c_int)
190+
SDL_UpdateYUVTexture = _bind("SDL_UpdateYUVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int)
191+
SDL_UpdateNVTexture = _bind("SDL_UpdateNVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int, added='2.0.16')
190192
SDL_LockTexture = _bind("SDL_LockTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(c_void_p), POINTER(c_int)], c_int)
191193
SDL_LockTextureToSurface = _bind("SDL_LockTextureToSurface", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(POINTER(SDL_Surface))], c_int, added='2.0.12')
192194
SDL_UnlockTexture = _bind("SDL_UnlockTexture", [POINTER(SDL_Texture)])
@@ -195,6 +197,8 @@ class SDL_Texture(c_void_p):
195197
SDL_GetRenderTarget = _bind("SDL_GetRenderTarget", [POINTER(SDL_Renderer)], POINTER(SDL_Texture))
196198
SDL_RenderSetLogicalSize = _bind("SDL_RenderSetLogicalSize", [POINTER(SDL_Renderer), c_int, c_int], c_int)
197199
SDL_RenderGetLogicalSize = _bind("SDL_RenderGetLogicalSize", [POINTER(SDL_Renderer), POINTER(c_int), POINTER(c_int)])
200+
SDL_RenderSetIntegerScale = _bind("SDL_RenderSetIntegerScale", [POINTER(SDL_Renderer), SDL_bool], c_int, added='2.0.5')
201+
SDL_RenderGetIntegerScale = _bind("SDL_RenderGetIntegerScale", [POINTER(SDL_Renderer)], SDL_bool, added='2.0.5')
198202
SDL_RenderSetViewport = _bind("SDL_RenderSetViewport", [POINTER(SDL_Renderer), POINTER(SDL_Rect)], c_int)
199203
SDL_RenderGetViewport = _bind("SDL_RenderGetViewport", [POINTER(SDL_Renderer), POINTER(SDL_Rect)])
200204
SDL_RenderGetClipRect = _bind("SDL_RenderGetClipRect", [POINTER(SDL_Renderer), POINTER(SDL_Rect)])
@@ -204,8 +208,6 @@ class SDL_Texture(c_void_p):
204208
SDL_RenderGetScale = _bind("SDL_RenderGetScale", [POINTER(SDL_Renderer), POINTER(c_float), POINTER(c_float)])
205209
SDL_RenderWindowToLogical = _bind("SDL_RenderWindowToLogical", [POINTER(SDL_Renderer), c_int, c_int, POINTER(c_float), POINTER(c_float)], added='2.0.18')
206210
SDL_RenderLogicalToWindow = _bind("SDL_RenderLogicalToWindow", [POINTER(SDL_Renderer), c_float, c_float, POINTER(c_int), POINTER(c_int)], added='2.0.18')
207-
SDL_RenderGetIntegerScale = _bind("SDL_RenderGetIntegerScale", [POINTER(SDL_Renderer)], SDL_bool, added='2.0.5')
208-
SDL_RenderSetIntegerScale = _bind("SDL_RenderSetIntegerScale", [POINTER(SDL_Renderer), SDL_bool], c_int, added='2.0.5')
209211
SDL_SetRenderDrawColor = _bind("SDL_SetRenderDrawColor", [POINTER(SDL_Renderer), Uint8, Uint8, Uint8, Uint8], c_int)
210212
SDL_GetRenderDrawColor = _bind("SDL_GetRenderDrawColor", [POINTER(SDL_Renderer), POINTER(Uint8), POINTER(Uint8), POINTER(Uint8), POINTER(Uint8)], c_int)
211213
SDL_SetRenderDrawBlendMode = _bind("SDL_SetRenderDrawBlendMode", [POINTER(SDL_Renderer), SDL_BlendMode], c_int)
@@ -240,8 +242,6 @@ class SDL_Texture(c_void_p):
240242
SDL_RenderFlush = _bind("SDL_RenderFlush", [POINTER(SDL_Renderer)], c_int, added='2.0.10')
241243
SDL_GL_BindTexture = _bind("SDL_GL_BindTexture", [POINTER(SDL_Texture), POINTER(c_float), POINTER(c_float)], c_int)
242244
SDL_GL_UnbindTexture = _bind("SDL_GL_UnbindTexture", [POINTER(SDL_Texture)], c_int)
243-
SDL_UpdateYUVTexture = _bind("SDL_UpdateYUVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int)
244-
SDL_UpdateNVTexture = _bind("SDL_UpdateNVTexture", [POINTER(SDL_Texture), POINTER(SDL_Rect), POINTER(Uint8), c_int, POINTER(Uint8), c_int], c_int, added='2.0.16')
245245
SDL_RenderGetMetalLayer = _bind("SDL_RenderGetMetalLayer", [POINTER(SDL_Renderer)], c_void_p, added='2.0.8')
246246
SDL_RenderGetMetalCommandEncoder = _bind("SDL_RenderGetMetalCommandEncoder", [POINTER(SDL_Renderer)], c_void_p, added='2.0.8')
247247
SDL_RenderSetVSync = _bind("SDL_RenderSetVSync", [POINTER(SDL_Renderer), c_int], c_int, added='2.0.18')

0 commit comments

Comments
 (0)