Skip to content

Commit 3cf92aa

Browse files
committed
Fix everything
1 parent 465a2fc commit 3cf92aa

File tree

5 files changed

+58
-26
lines changed

5 files changed

+58
-26
lines changed

buildconfig/stubs/meson.build

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,13 @@
1+
pg_stub_excludes = ['.flake8']
2+
3+
# SDL3 only!
4+
if sdl_api != 3
5+
pg_stub_excludes += ['_audio.pyi']
6+
endif
7+
18
install_subdir(
29
'pygame',
3-
exclude_files: '.flake8',
10+
exclude_files: pg_stub_excludes,
411
install_dir: pg_dir,
512
strip_directory: true,
613
install_tag: 'pg-tag',

buildconfig/stubs/mypy_allow_list.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,6 @@ pygame\.pypm
2727
pygame\._sdl2\.mixer
2828
pygame\.sysfont.*
2929
pygame\.docs.*
30+
31+
# Remove me when we're checking stubs for SDL3!
32+
pygame._audio

buildconfig/stubs/pygame/_audio.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ from typing import TypeVar
44
from pygame.typing import FileLike
55
from typing_extensions import Buffer
66

7+
# TODO: Support SDL3 stubchecking without failing when on SDL2 builds
8+
# Right now this module is unconditionally skipped in mypy_allow_list.txt
9+
710
def init() -> None: ...
811

912
# def quit() -> None: ...
@@ -52,6 +55,7 @@ class AudioFormat:
5255
def __index__(self) -> int: ...
5356
def __repr__(self) -> str: ...
5457

58+
UNKNOWN: AudioFormat
5559
U8: AudioFormat
5660
S8: AudioFormat
5761
S16LE: AudioFormat

src_c/_base_audio.c

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ adevice_state_dealloc(PGAudioDeviceStateObject *self)
5555
}
5656

5757
static PyMemberDef adevice_state_members[] = {
58-
{"id", Py_T_INT, offsetof(PGAudioDeviceStateObject, devid), Py_READONLY,
58+
{"id", Py_T_UINT, offsetof(PGAudioDeviceStateObject, devid), Py_READONLY,
5959
NULL},
6060
{NULL} /* Sentinel */
6161
};
@@ -127,6 +127,7 @@ pg_audio_get_audio_device_channel_map(PyObject *module, PyObject *arg)
127127
SDL_free(channel_map);
128128
Py_DECREF(item);
129129
Py_DECREF(channel_map_list);
130+
return NULL;
130131
}
131132
}
132133

@@ -250,6 +251,7 @@ pg_audio_open_audio_device(PyObject *module, PyObject *const *args,
250251
(PGAudioDeviceStateObject *)adevice_state_type->tp_alloc(
251252
adevice_state_type, 0);
252253
if (device == NULL) {
254+
SDL_CloseAudioDevice(logical_id);
253255
return NULL;
254256
}
255257
device->devid = logical_id;
@@ -331,6 +333,11 @@ pg_audio_create_audio_stream(PyObject *module, PyObject *const *args,
331333
PGAudioStreamStateObject *stream_state =
332334
(PGAudioStreamStateObject *)astream_state_type->tp_alloc(
333335
astream_state_type, 0);
336+
337+
if (stream_state == NULL) {
338+
SDL_DestroyAudioStream(stream);
339+
return NULL;
340+
}
334341
stream_state->stream = stream;
335342

336343
return (PyObject *)stream_state;
@@ -476,19 +483,19 @@ pg_audio_put_audio_stream_data(PyObject *module, PyObject *const *args,
476483
}
477484

478485
void *buf;
479-
int len;
486+
Py_ssize_t len;
480487

481-
if (PyBytes_AsStringAndSize(bytes, (char **)&buf, (Py_ssize_t *)&len) !=
482-
0) {
488+
if (PyBytes_AsStringAndSize(bytes, (char **)&buf, &len) != 0) {
483489
Py_DECREF(bytes);
484490
return NULL;
485491
}
486492

487-
if (!SDL_PutAudioStreamData(stream, buf, len)) {
493+
if (!SDL_PutAudioStreamData(stream, buf, (int)len)) {
488494
Py_DECREF(bytes);
489495
return RAISE(pgExc_SDLError, SDL_GetError());
490496
}
491497

498+
Py_DECREF(bytes);
492499
Py_RETURN_NONE;
493500
}
494501

@@ -762,8 +769,8 @@ pg_audio_get_drivers(PyObject *module, PyObject *_null)
762769
for (int i = 0; i < num_drivers; i++) {
763770
driver = SDL_GetAudioDriver(i);
764771
if (driver == NULL) {
765-
return RAISE(pgExc_SDLError, SDL_GetError());
766772
Py_DECREF(driver_list);
773+
return RAISE(pgExc_SDLError, SDL_GetError());
767774
}
768775
item = PyUnicode_FromString(driver);
769776
if (item == NULL) {
@@ -863,13 +870,13 @@ pg_audio_load_wav(PyObject *module, PyObject *arg)
863870
return RAISE(pgExc_SDLError, SDL_GetError());
864871
}
865872

866-
PyObject *bytes = PyBytes_FromStringAndSize(audio_buf, audio_len);
873+
PyObject *bytes = PyBytes_FromStringAndSize((char *)audio_buf, audio_len);
867874
SDL_free(audio_buf);
868875
if (bytes == NULL) {
869876
return NULL;
870877
}
871878

872-
return Py_BuildValue("Oiii", bytes, spec.format, spec.channels, spec.freq);
879+
return Py_BuildValue("Niii", bytes, spec.format, spec.channels, spec.freq);
873880
}
874881

875882
static PyObject *
@@ -882,6 +889,9 @@ pg_audio_get_default_playback_device_state(PyObject *module, PyObject *_null)
882889
PGAudioDeviceStateObject *device =
883890
(PGAudioDeviceStateObject *)adevice_state_type->tp_alloc(
884891
adevice_state_type, 0);
892+
if (device == NULL) {
893+
return NULL;
894+
}
885895
device->devid = SDL_AUDIO_DEVICE_DEFAULT_PLAYBACK;
886896

887897
return (PyObject *)device;
@@ -897,6 +907,9 @@ pg_audio_get_default_recording_device_state(PyObject *module, PyObject *_null)
897907
PGAudioDeviceStateObject *device =
898908
(PGAudioDeviceStateObject *)adevice_state_type->tp_alloc(
899909
adevice_state_type, 0);
910+
if (device == NULL) {
911+
return NULL;
912+
}
900913
device->devid = SDL_AUDIO_DEVICE_DEFAULT_RECORDING;
901914

902915
return (PyObject *)device;
@@ -1028,23 +1041,25 @@ pg_audio_exec(PyObject *module)
10281041
audio_state *state = GET_STATE(module);
10291042
state->audio_initialized = false;
10301043

1031-
PyObject *audio_device_state_type =
1044+
state->audio_device_state_type =
10321045
PyType_FromModuleAndSpec(module, &adevice_state_spec, NULL);
1033-
if (PyModule_AddObjectRef(module, "AudioDeviceState",
1034-
audio_device_state_type) < 0) {
1046+
if (state->audio_device_state_type == NULL) {
1047+
return -1;
1048+
}
1049+
if (PyModule_AddType(module,
1050+
(PyTypeObject *)state->audio_device_state_type) < 0) {
10351051
return -1;
10361052
}
10371053

1038-
state->audio_device_state_type = audio_device_state_type;
1039-
1040-
PyObject *audio_stream_state_type =
1054+
state->audio_stream_state_type =
10411055
PyType_FromModuleAndSpec(module, &astream_state_spec, NULL);
1042-
if (PyModule_AddObjectRef(module, "AudioStreamState",
1043-
audio_stream_state_type) < 0) {
1056+
if (state->audio_stream_state_type == NULL) {
1057+
return -1;
1058+
}
1059+
if (PyModule_AddType(module,
1060+
(PyTypeObject *)state->audio_stream_state_type) < 0) {
10441061
return -1;
10451062
}
1046-
1047-
state->audio_stream_state_type = audio_stream_state_type;
10481063

10491064
return 0;
10501065
}

src_py/_audio.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,14 @@ class AudioFormat:
1919
_MASK_BIG_ENDIAN = 1 << 12
2020
_MASK_SIGNED = 1 << 15
2121

22-
format_num_to_instance = dict()
22+
# For internal use going from integers to AudioFormat instances
23+
_format_num_to_instance = dict()
2324

2425
def __init__(self, name: str, value: int) -> None:
2526
self._name = name
2627
self._value = value
2728

28-
AudioFormat.format_num_to_instance[value] = self
29+
AudioFormat._format_num_to_instance[value] = self
2930

3031
@property
3132
def bitsize(self) -> int:
@@ -64,7 +65,7 @@ def name(self) -> str:
6465
return self._name
6566

6667
@property
67-
def silence_value(self) -> int:
68+
def silence_value(self) -> bytes:
6869
return _base_audio.get_silence_value_for_format(self._value)
6970

7071
# TODO maybe unnecessary?
@@ -337,7 +338,9 @@ def frequency_ratio(self) -> float:
337338
@frequency_ratio.setter
338339
def frequency_ratio(self, value: float) -> None:
339340
if value <= 0 or value > 10:
340-
raise ValueError("Frequency ratio must be > 0 or <= 10.")
341+
raise ValueError(
342+
"Frequency ratio must be between 0 and 10. (0 < ratio <= 10)"
343+
)
341344

342345
_base_audio.set_audio_stream_frequency_ratio(self._state, value)
343346

@@ -392,7 +395,7 @@ def _create_audio_device(dev_state) -> AudioDevice:
392395
def _audio_spec_from_ints(
393396
format_num: int, channels: int, frequency: int
394397
) -> AudioSpec:
395-
format_inst = AudioFormat.format_num_to_instance[format_num]
398+
format_inst = AudioFormat._format_num_to_instance[format_num]
396399
return AudioSpec(format_inst, channels, frequency)
397400

398401

@@ -415,10 +418,10 @@ def get_recording_devices() -> list[AudioDevice]:
415418

416419
def load_wav(file: FileLike) -> tuple[AudioSpec, bytes]:
417420
audio_bytes, format_num, channels, frequency = _base_audio.load_wav(file)
418-
return [
421+
return (
419422
_internals._audio_spec_from_ints(format_num, channels, frequency),
420423
audio_bytes,
421-
]
424+
)
422425

423426

424427
DEFAULT_PLAYBACK_DEVICE = _internals._create_audio_device(

0 commit comments

Comments
 (0)