diff options
author | Zachary Ware <zachary.ware@gmail.com> | 2016-09-05 21:31:21 (GMT) |
---|---|---|
committer | Zachary Ware <zachary.ware@gmail.com> | 2016-09-05 21:31:21 (GMT) |
commit | ae8298bfb78eebc6c1bf914c89ec397121640a94 (patch) | |
tree | 848b6d5390ebab9d35a64322ce585f2dbafa2096 /PC/winsound.c | |
parent | 0e76e67246b4e6e6bb15d04748f40ed96e405d5c (diff) | |
download | cpython-ae8298bfb78eebc6c1bf914c89ec397121640a94.zip cpython-ae8298bfb78eebc6c1bf914c89ec397121640a94.tar.gz cpython-ae8298bfb78eebc6c1bf914c89ec397121640a94.tar.bz2 |
Closes #11620: Fix support for SND_MEMORY in winsound.PlaySound.
Based on a patch by Tim Lesher.
Diffstat (limited to 'PC/winsound.c')
-rw-r--r-- | PC/winsound.c | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/PC/winsound.c b/PC/winsound.c index 6b79d23..000ddd8 100644 --- a/PC/winsound.c +++ b/PC/winsound.c @@ -64,7 +64,7 @@ module winsound /*[clinic input] winsound.PlaySound - sound: Py_UNICODE(accept={str, NoneType}) + sound: object The sound to play; a filename, data, or None. flags: int Flag values, ored together. See module documentation. @@ -74,22 +74,49 @@ A wrapper around the Windows PlaySound API. [clinic start generated code]*/ static PyObject * -winsound_PlaySound_impl(PyObject *module, Py_UNICODE *sound, int flags) -/*[clinic end generated code: output=ec24b3a2b4368378 input=3411b1b7c1f36d93]*/ +winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags) +/*[clinic end generated code: output=49a0fd16a372ebeb input=7bdf637f10201d37]*/ { int ok; - - if (flags & SND_ASYNC && flags & SND_MEMORY) { - /* Sidestep reference counting headache; unfortunately this also - prevent SND_LOOP from memory. */ - PyErr_SetString(PyExc_RuntimeError, - "Cannot play asynchronously from memory"); - return NULL; + wchar_t *wsound; + Py_buffer view = {NULL, NULL}; + + if (sound == Py_None) { + wsound = NULL; + } else if (flags & SND_MEMORY) { + if (flags & SND_ASYNC) { + /* Sidestep reference counting headache; unfortunately this also + prevent SND_LOOP from memory. */ + PyErr_SetString(PyExc_RuntimeError, + "Cannot play asynchronously from memory"); + return NULL; + } + if (PyObject_GetBuffer(sound, &view, PyBUF_SIMPLE) < 0) { + return NULL; + } + wsound = (wchar_t *)view.buf; + } else { + if (!PyUnicode_Check(sound)) { + PyErr_Format(PyExc_TypeError, + "'sound' must be str or None, not '%s'", + Py_TYPE(sound)->tp_name); + return NULL; + } + wsound = PyUnicode_AsWideCharString(sound, NULL); + if (wsound == NULL) { + return NULL; + } } + Py_BEGIN_ALLOW_THREADS - ok = PlaySoundW(sound, NULL, flags); + ok = PlaySoundW(wsound, NULL, flags); Py_END_ALLOW_THREADS + if (view.obj) { + PyBuffer_Release(&view); + } else if (sound != Py_None) { + PyMem_Free(wsound); + } if (!ok) { PyErr_SetString(PyExc_RuntimeError, "Failed to play sound"); return NULL; |