@@ -72,30 +72,52 @@ PyDoc_STRVAR(sound_module_doc,
7272static PyObject *
7373sound_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