Anonymous avatar Anonymous committed 745d0f1

Merged revisions 86283 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k

........
r86283 | hirokazu.yamamoto | 2010-11-07 18:23:15 +0900 | 1 line

Issue #6317: Now winsound.PlaySound can accept non ascii filename.
........

Comments (0)

Files changed (2)

 Extension Modules
 -----------------
 
+- Issue #6317: Now winsound.PlaySound can accept non ascii filename.
+
 - Issue #9054: Fix a crash occurring when using the pyexpat module
   with expat version 2.0.1.
 
 static PyObject *
 sound_playsound(PyObject *s, PyObject *args)
 {
+    Py_UNICODE *wsound;
+    PyObject *osound;
     const char *sound;
     int flags;
-    int length;
     int ok;
 
-    if (!PyArg_ParseTuple(args, "z#i:PlaySound", &sound, &length, &flags)) {
+    if (PyArg_ParseTuple(args, "Zi:PlaySound", &wsound, &flags)) {
+        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;
+        }
+        Py_BEGIN_ALLOW_THREADS
+        ok = PlaySoundW(wsound, NULL, flags);
+        Py_END_ALLOW_THREADS
+        if (!ok) {
+            PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
+            return NULL;
+        }
+        Py_INCREF(Py_None);
+        return Py_None;
+    }
+    /* Drop the argument parsing error as narrow strings
+       are also valid. */
+    PyErr_Clear();
+    if (!PyArg_ParseTuple(args, "O&i:PlaySound",
+                          PyUnicode_FSConverter, &osound, &flags))
         return NULL;
-    }
-
     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");
+        Py_DECREF(osound);
         return NULL;
     }
-
+    sound = PyBytes_AsString(osound);
     Py_BEGIN_ALLOW_THREADS
-    ok = PlaySound(sound, NULL, flags);
+    ok = PlaySoundA(sound, NULL, flags);
     Py_END_ALLOW_THREADS
     if (!ok) {
         PyErr_SetString(PyExc_RuntimeError, "Failed to play sound");
+        Py_DECREF(osound);
         return NULL;
     }
-
+    Py_DECREF(osound);
     Py_INCREF(Py_None);
     return Py_None;
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.