Skip to content

Commit 5482b63

Browse files
authored
Merge pull request #2590 from gresm/channel-queue-sound-subclass
Subclassing Sounds + Channels.
2 parents c86d247 + d504072 commit 5482b63

File tree

5 files changed

+25
-7
lines changed

5 files changed

+25
-7
lines changed

buildconfig/stubs/pygame/mixer.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Any, Dict, Optional, Tuple, Union, final, overload
1+
from typing import Any, Dict, Optional, Tuple, Union, overload
22

33
import numpy
44

@@ -70,7 +70,7 @@ class Sound:
7070
def get_length(self) -> float: ...
7171
def get_raw(self) -> bytes: ...
7272

73-
@final
73+
7474
class Channel:
7575
def __init__(self, id: int) -> None: ...
7676
@property

docs/reST/ref/mixer.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,7 @@ change the default buffer by calling :func:`pygame.mixer.pre_init` before
460460

461461
.. versionchanged:: 2.1.4 This class is also available through the ``pygame.Channel``
462462
alias.
463+
.. versionchanged:: 2.4.0 It is now possible to create subclasses of ``pygame.mixer.Channel``
463464

464465
.. attribute:: id
465466

src_c/include/pygame_mixer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,7 @@ typedef struct {
5050
PYGAMEAPI_DEFINE_SLOTS(mixer);
5151

5252
#define pgSound_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(mixer, 0))
53-
54-
#define pgSound_Check(x) ((x)->ob_type == &pgSound_Type)
53+
#define pgSound_Check(x) (PyObject_IsInstance(x, (PyObject *)&pgSound_Type))
5554

5655
#define pgSound_New \
5756
(*(PyObject * (*)(Mix_Chunk *)) PYGAMEAPI_GET_SLOT(mixer, 1))
@@ -60,7 +59,8 @@ PYGAMEAPI_DEFINE_SLOTS(mixer);
6059
(*(PyObject * (*)(PyObject *, PyObject *)) PYGAMEAPI_GET_SLOT(mixer, 2))
6160

6261
#define pgChannel_Type (*(PyTypeObject *)PYGAMEAPI_GET_SLOT(mixer, 3))
63-
#define pgChannel_Check(x) ((x)->ob_type == &pgChannel_Type)
62+
#define pgChannel_Check(x) \
63+
(PyObject_IsInstance(x, (PyObject *)&pgChannel_Type))
6464

6565
#define pgChannel_New (*(PyObject * (*)(int)) PYGAMEAPI_GET_SLOT(mixer, 4))
6666

src_c/mixer.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ static PyObject *
8585
pgSound_New(Mix_Chunk *);
8686
static PyObject *
8787
pgChannel_New(int);
88-
#define pgSound_Check(x) (Py_TYPE(x) == &pgSound_Type)
89-
#define pgChannel_Check(x) (Py_TYPE(x) == &pgChannel_Type)
88+
#define pgSound_Check(x) (PyObject_IsInstance(x, (PyObject *)&pgSound_Type))
89+
#define pgChannel_Check(x) \
90+
(PyObject_IsInstance(x, (PyObject *)&pgChannel_Type))
9091

9192
static int
9293
snd_getbuffer(PyObject *, Py_buffer *, int);
@@ -1373,6 +1374,7 @@ static PyTypeObject pgChannel_Type = {
13731374
PyVarObject_HEAD_INIT(NULL, 0).tp_name = "pygame.mixer.Channel",
13741375
.tp_basicsize = sizeof(pgChannelObject),
13751376
.tp_dealloc = channel_dealloc,
1377+
.tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE),
13761378
.tp_doc = DOC_MIXER_CHANNEL,
13771379
.tp_methods = channel_methods,
13781380
.tp_init = (initproc)channel_init,

test/mixer_test.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -934,6 +934,10 @@ def test_id_getter(self):
934934
self.assertEqual(ch1.id, 1)
935935
self.assertEqual(ch2.id, 2)
936936

937+
def test_subclass(self):
938+
class MyChannel(mixer.Channel):
939+
pass
940+
937941

938942
class ChannelInteractiveTest(unittest.TestCase):
939943
__tags__ = ["interactive"]
@@ -1264,6 +1268,17 @@ def __init__(self, file):
12641268
except Exception:
12651269
self.fail("This should not raise an exception.")
12661270

1271+
channel = mixer.Channel(0)
1272+
try:
1273+
channel.play(correct)
1274+
except Exception:
1275+
self.fail("This should not raise an exception.")
1276+
1277+
self.assertIsInstance(channel.get_sound(), CorrectSublass)
1278+
self.assertIs(channel.get_sound(), correct)
1279+
1280+
channel.stop()
1281+
12671282
def test_incorrect_subclassing(self):
12681283
class IncorrectSuclass(mixer.Sound):
12691284
def __init__(self):

0 commit comments

Comments
 (0)