Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c08c9bc

Browse files
author
Hirokazu Yamamoto
committed
Issue #6317: Now winsound.PlaySound can accept non ascii filename.
1 parent 2e598fa commit c08c9bc

2 files changed

Lines changed: 31 additions & 7 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,8 @@ Library
251251
Extensions
252252
----------
253253

254+
- Issue #6317: Now winsound.PlaySound can accept non ascii filename.
255+
254256
- Issue #9377: Use Unicode API for gethostname on Windows.
255257

256258
- Issue #10143: Update "os.pathconf" values.

PC/winsound.c

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -72,30 +72,52 @@ PyDoc_STRVAR(sound_module_doc,
7272
static PyObject *
7373
sound_playsound(PyObject *s, PyObject *args)
7474
{
75+
Py_UNICODE *wsound;
76+
PyObject *osound;
7577
const char *sound;
7678
int flags;
77-
int length;
7879
int ok;
7980

80-
if (!PyArg_ParseTuple(args, "z#i:PlaySound", &sound, &length, &flags)) {
81-
return NULL;
81+
if (PyArg_ParseTuple(args, "Zi:PlaySound", &wsound, &flags)) {
82+
if (flags & SND_ASYNC && flags & SND_MEMORY) {
83+
/* Sidestep reference counting headache; unfortunately this also
84+
prevent SND_LOOP from memory. */
85+
PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory");
86+
return NULL;
87+
}
88+
Py_BEGIN_ALLOW_THREADS
89+
ok = PlaySoundW(wsound, NULL, flags);
90+
Py_END_ALLOW_THREADS
91+
if (!ok) {
92+
PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
93+
return NULL;
94+
}
95+
Py_INCREF(Py_None);
96+
return Py_None;
8297
}
83-
98+
/* Drop the argument parsing error as narrow strings
99+
are also valid. */
100+
PyErr_Clear();
101+
if (!PyArg_ParseTuple(args, "O&i:PlaySound",
102+
PyUnicode_FSConverter, &osound, &flags))
103+
return NULL;
84104
if (flags & SND_ASYNC && flags & SND_MEMORY) {
85105
/* Sidestep reference counting headache; unfortunately this also
86106
prevent SND_LOOP from memory. */
87107
PyErr_SetString(PyExc_RuntimeError, "Cannot play asynchronously from memory");
108+
Py_DECREF(osound);
88109
return NULL;
89110
}
90-
111+
sound = PyBytes_AsString(osound);
91112
Py_BEGIN_ALLOW_THREADS
92-
ok = PlaySound(sound, NULL, flags);
113+
ok = PlaySoundA(sound, NULL, flags);
93114
Py_END_ALLOW_THREADS
94115
if (!ok) {
95116
PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
117+
Py_DECREF(osound);
96118
return NULL;
97119
}
98-
120+
Py_DECREF(osound);
99121
Py_INCREF(Py_None);
100122
return Py_None;
101123
}

0 commit comments

Comments
 (0)