Skip to content

Commit 12c9952

Browse files
committed
Fixed refcounting (hopefully) and addressed other review comments from coderabbit
1 parent 88a9a13 commit 12c9952

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

src_c/mixer.c

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,6 @@ endsound_callback(int channel)
299299
PyGILState_STATE gstate = PyGILState_Ensure();
300300
int channelnum;
301301
Mix_Chunk *sound = pgSound_AsChunk(channeldata[channel].queue);
302-
Py_XDECREF(channeldata[channel].sound);
303302
channeldata[channel].sound = channeldata[channel].queue;
304303
channeldata[channel].queue = NULL;
305304
PyGILState_Release(gstate);
@@ -310,7 +309,6 @@ endsound_callback(int channel)
310309
}
311310
else {
312311
PyGILState_STATE gstate = PyGILState_Ensure();
313-
Py_XDECREF(channeldata[channel].sound);
314312
channeldata[channel].sound = NULL;
315313
PyGILState_Release(gstate);
316314
Mix_GroupChannel(channel, -1);
@@ -806,32 +804,46 @@ snd_get_raw(PyObject *self, PyObject *_null)
806804
static PyObject *
807805
snd_copy(PyObject *self, PyObject *_null)
808806
{
807+
PG_DECLARE_EXCEPTION_SAVER
809808
MIXER_INIT_CHECK();
810809

811810
pgSoundObject *newSound =
812-
(pgSoundObject *)pgSound_Type.tp_new(&pgSound_Type, NULL, NULL);
811+
(pgSoundObject *)Py_TYPE(self)->tp_new(&pgSound_Type, NULL, NULL);
813812

814813
PyObject *kwargs = PyDict_New();
815814
PyObject *key = PyUnicode_FromString("buffer");
816815
PyObject *bytes = snd_get_raw(self, NULL);
816+
if (bytes == NULL) {
817+
// exception set already by PyBytes_FromStringAndSize
818+
PG_SAVE_EXCEPTION
819+
Py_DECREF(key);
820+
Py_DECREF(kwargs);
821+
PG_UNSAVE_EXCEPTION
822+
return NULL;
823+
}
824+
817825
if (PyDict_SetItem(kwargs, key, bytes) < 0) {
818826
// exception set already
827+
PG_SAVE_EXCEPTION
819828
Py_DECREF(key);
820829
Py_DECREF(bytes);
821830
Py_DECREF(kwargs);
831+
PG_UNSAVE_EXCEPTION
822832
return NULL;
823833
}
824834
Py_DECREF(key);
825835
Py_DECREF(bytes);
826836

827837
if (sound_init((PyObject *)newSound, NULL, kwargs) != 0) {
838+
PG_SAVE_EXCEPTION
828839
Py_DECREF(kwargs);
829840
Py_DECREF(newSound);
830-
return RAISE(pgExc_SDLError, "Failed to initialize copied sound");
841+
PG_UNSAVE_EXCEPTION
842+
// Exception set by sound_init
843+
return NULL;
831844
}
832845

833846
Py_DECREF(kwargs);
834-
Py_INCREF(newSound);
835847
return (PyObject *)newSound;
836848
}
837849

test/mixer_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ def test_snd_copy(self):
13471347
filename = example_path(os.path.join("data", f))
13481348
try:
13491349
sound = mixer.Sound(file=filename)
1350-
except pygame.error as e:
1350+
except pygame.error:
13511351
continue
13521352
sound_copy = sound.copy()
13531353
self.assertEqual(sound.get_length(), sound_copy.get_length())

0 commit comments

Comments
 (0)