mirror of
https://github.com/python/cpython
synced 2024-10-04 19:08:12 +00:00
gh-91061: also accept pathlib.Path for winsound.PlaySound (#91489)
Fixes #91061 Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
e39cd76561
commit
9bc616cb4c
|
@ -1,6 +1,7 @@
|
||||||
# Ridiculously simple test of the winsound module for Windows.
|
# Ridiculously simple test of the winsound module for Windows.
|
||||||
|
|
||||||
import functools
|
import functools
|
||||||
|
import pathlib
|
||||||
import time
|
import time
|
||||||
import unittest
|
import unittest
|
||||||
|
|
||||||
|
@ -84,6 +85,13 @@ def test_keyword_args(self):
|
||||||
safe_MessageBeep(type=winsound.MB_OK)
|
safe_MessageBeep(type=winsound.MB_OK)
|
||||||
|
|
||||||
|
|
||||||
|
# A class for testing winsound when the given path resolves
|
||||||
|
# to bytes rather than str.
|
||||||
|
class BytesPath(pathlib.WindowsPath):
|
||||||
|
def __fspath__(self):
|
||||||
|
return bytes(super().__fspath__(), 'UTF-8')
|
||||||
|
|
||||||
|
|
||||||
class PlaySoundTest(unittest.TestCase):
|
class PlaySoundTest(unittest.TestCase):
|
||||||
|
|
||||||
def test_errors(self):
|
def test_errors(self):
|
||||||
|
@ -116,6 +124,20 @@ def test_snd_filename(self):
|
||||||
fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
|
fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
|
||||||
safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
|
safe_PlaySound(fn, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
|
||||||
|
|
||||||
|
def test_snd_filepath(self):
|
||||||
|
fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
|
||||||
|
path = pathlib.Path(fn)
|
||||||
|
safe_PlaySound(path, winsound.SND_FILENAME | winsound.SND_NODEFAULT)
|
||||||
|
|
||||||
|
def test_snd_filepath_as_bytes(self):
|
||||||
|
fn = support.findfile('pluck-pcm8.wav', subdir='audiodata')
|
||||||
|
self.assertRaises(
|
||||||
|
TypeError,
|
||||||
|
winsound.PlaySound,
|
||||||
|
BytesPath(fn),
|
||||||
|
winsound.SND_FILENAME | winsound.SND_NODEFAULT
|
||||||
|
)
|
||||||
|
|
||||||
def test_aliases(self):
|
def test_aliases(self):
|
||||||
aliases = [
|
aliases = [
|
||||||
"SystemAsterisk",
|
"SystemAsterisk",
|
||||||
|
|
|
@ -0,0 +1 @@
|
||||||
|
Accept os.PathLike for the argument to winsound.PlaySound
|
|
@ -94,17 +94,25 @@ winsound_PlaySound_impl(PyObject *module, PyObject *sound, int flags)
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
wsound = (wchar_t *)view.buf;
|
wsound = (wchar_t *)view.buf;
|
||||||
|
} else if (PyBytes_Check(sound)) {
|
||||||
|
PyErr_Format(PyExc_TypeError,
|
||||||
|
"'sound' must be str, os.PathLike, or None, not '%s'",
|
||||||
|
Py_TYPE(sound)->tp_name);
|
||||||
|
return NULL;
|
||||||
} else {
|
} else {
|
||||||
if (!PyUnicode_Check(sound)) {
|
PyObject *obj = PyOS_FSPath(sound);
|
||||||
|
// Either <obj> is unicode/bytes/NULL, or a helpful message
|
||||||
|
// has been surfaced to the user about how they gave a non-path.
|
||||||
|
if (obj == NULL) return NULL;
|
||||||
|
if (PyBytes_Check(obj)) {
|
||||||
PyErr_Format(PyExc_TypeError,
|
PyErr_Format(PyExc_TypeError,
|
||||||
"'sound' must be str or None, not '%s'",
|
"'sound' must resolve to str, not bytes");
|
||||||
Py_TYPE(sound)->tp_name);
|
Py_DECREF(obj);
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
wsound = PyUnicode_AsWideCharString(sound, NULL);
|
|
||||||
if (wsound == NULL) {
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
wsound = PyUnicode_AsWideCharString(obj, NULL);
|
||||||
|
Py_DECREF(obj);
|
||||||
|
if (wsound == NULL) return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue