Skip to content

Commit eae8383

Browse files
committed
Review comments from bot, good :bot:
1 parent 9f82126 commit eae8383

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

buildconfig/stubs/pygame/mixer.pyi

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ from pygame.event import Event
55
from pygame.typing import FileLike
66
from typing_extensions import (
77
Buffer, # collections.abc 3.12
8+
Self,
89
deprecated, # added in 3.13
910
)
1011

@@ -72,8 +73,8 @@ class Sound:
7273
def get_num_channels(self) -> int: ...
7374
def get_length(self) -> float: ...
7475
def get_raw(self) -> bytes: ...
75-
def copy(self) -> Sound: ...
76-
def __copy__(self) -> Sound: ...
76+
def copy(self) -> Self: ...
77+
def __copy__(self) -> Self: ...
7778

7879
class Channel:
7980
def __init__(self, id: int) -> None: ...

src_c/mixer.c

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -816,33 +816,49 @@ snd_copy(PyObject *self, PyObject *_null)
816816
pgSoundObject *newSound =
817817
(pgSoundObject *)Py_TYPE(self)->tp_new(Py_TYPE(self), NULL, NULL);
818818

819-
PyObject *kwargs = PyDict_New();
820-
PyObject *key = PyUnicode_FromString("buffer");
819+
if (!newSound) {
820+
if (!PyErr_Occurred()) {
821+
PyErr_SetString(PyExc_MemoryError,
822+
"Failed to create new Sound object for copy");
823+
}
824+
return NULL;
825+
}
826+
827+
PyObject *dict = PyDict_New();
828+
if (!dict) {
829+
if (!PyErr_Occurred()) {
830+
PyErr_SetString(PyExc_MemoryError,
831+
"Failed to create internal dictionary to create "
832+
"copy of Sound");
833+
}
834+
Py_DECREF(newSound);
835+
return NULL;
836+
}
837+
821838
PyObject *bytes = snd_get_raw(self, NULL);
822839
if (bytes == NULL) {
823840
// exception set already by PyBytes_FromStringAndSize
824841
PG_SAVE_EXCEPTION
825-
Py_DECREF(key);
826-
Py_DECREF(kwargs);
842+
Py_DECREF(dict);
843+
Py_DECREF(newSound);
827844
PG_UNSAVE_EXCEPTION
828845
return NULL;
829846
}
830847

831-
if (PyDict_SetItem(kwargs, key, bytes) < 0) {
848+
if (PyDict_SetItemString(dict, "buffer", bytes) < 0) {
832849
// exception set already
833850
PG_SAVE_EXCEPTION
834-
Py_DECREF(key);
835851
Py_DECREF(bytes);
836-
Py_DECREF(kwargs);
852+
Py_DECREF(dict);
853+
Py_DECREF(newSound);
837854
PG_UNSAVE_EXCEPTION
838855
return NULL;
839856
}
840-
Py_DECREF(key);
841857
Py_DECREF(bytes);
842858

843-
if (sound_init((PyObject *)newSound, NULL, kwargs) != 0) {
859+
if (sound_init((PyObject *)newSound, NULL, dict) != 0) {
844860
PG_SAVE_EXCEPTION
845-
Py_DECREF(kwargs);
861+
Py_DECREF(dict);
846862
Py_DECREF(newSound);
847863
PG_UNSAVE_EXCEPTION
848864
// Exception set by sound_init
@@ -858,7 +874,7 @@ snd_copy(PyObject *self, PyObject *_null)
858874
Mix_VolumeChunk(dst, vol);
859875
}
860876

861-
Py_DECREF(kwargs);
877+
Py_DECREF(dict);
862878
return (PyObject *)newSound;
863879
}
864880

test/mixer_test.py

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import copy
12
import os
23
import pathlib
34
import platform
@@ -1348,6 +1349,8 @@ def __init__(self, *args, **kwargs):
13481349
new_volumes = [0.2, 0.3, 0.7, 1.0, 0.1]
13491350
if pygame.mixer.get_sdl_mixer_version() >= (2, 6, 0):
13501351
filenames.append("house_lo.mp3")
1352+
old_volumes.append(0.9)
1353+
new_volumes.append(0.5)
13511354

13521355
for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes):
13531356
filename = example_path(os.path.join("data", f))
@@ -1377,6 +1380,35 @@ def __init__(self, *args, **kwargs):
13771380
sound_copy.play()
13781381
self.assertEqual(sound_copy.get_num_channels(), 1)
13791382

1383+
# Test __copy__
1384+
for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes):
1385+
filename = example_path(os.path.join("data", f))
1386+
try:
1387+
sound = mixer.Sound(file=filename)
1388+
sound.set_volume(old_vol)
1389+
except pygame.error:
1390+
continue
1391+
sound_copy = copy.copy(sound)
1392+
self.assertEqual(sound.get_length(), sound_copy.get_length())
1393+
self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels())
1394+
self.assertEqual(sound.get_volume(), sound_copy.get_volume())
1395+
self.assertEqual(sound.get_raw(), sound_copy.get_raw())
1396+
1397+
sound.set_volume(new_vol)
1398+
self.assertNotEqual(sound.get_volume(), sound_copy.get_volume())
1399+
1400+
del sound
1401+
1402+
# Test on the copy for playable sounds
1403+
channel = sound_copy.play()
1404+
if channel is None:
1405+
continue
1406+
self.assertTrue(channel.get_busy())
1407+
sound_copy.stop()
1408+
self.assertFalse(channel.get_busy())
1409+
sound_copy.play()
1410+
self.assertEqual(sound_copy.get_num_channels(), 1)
1411+
13801412
# Test copying a subclass of Sound
13811413
for f, old_vol, new_vol in zip(filenames, old_volumes, new_volumes):
13821414
filename = example_path(os.path.join("data", f))
@@ -1386,7 +1418,7 @@ def __init__(self, *args, **kwargs):
13861418
except pygame.error:
13871419
continue
13881420
sound_copy = sound.copy()
1389-
self.assertTrue(sound_copy, SubSound)
1421+
self.assertIsInstance(sound_copy, SubSound)
13901422
self.assertEqual(sound.get_length(), sound_copy.get_length())
13911423
self.assertEqual(sound.get_num_channels(), sound_copy.get_num_channels())
13921424
self.assertEqual(sound.get_volume(), sound_copy.get_volume())

0 commit comments

Comments
 (0)