You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
winsound.PlaySound should accept pathlib.Path instances #91061
The library function winsound.PlaySound takes a sound parameter. One of the valid arguments is a path to a WAV filename.
Since Python 3.4, paths can be cleanly represented with PathLib.Path instances.
However, if you pass a Path instance to PlaySound, it responds with:
TypeError: 'sound' must be str or None, not 'WindowsPath'
This can be quickly fixed in the caller by passing str(the_path_instance) instead, but it would be cleaner if PlaySound accepted Python's preferred method of representing paths.
PlaySound() is implemented by the C function winsound_PlaySound_impl() in "PC/winsound.c". To support pathlike objects, it could use soundname = PyOS_FSPath(sound), and require soundname to be a Unicode string via PyUnicode_Check(soundname). Or it could more generically support any buffer or pathlike object via PyUnicode_FSDecoder(sound, &soundname). This call decodes byte strings using the filesystem encoding, which is UTF-8 unless legacy ANSI mode is enabled.
I'm interested in trying this issue. What's an appropriate way to test the fix in CI? I'm assuming manual testing will be straightforward (just build the binary and have a simple script that tries to use a PathLib.Path instead of a string)
Activity
Julian-O commentedon Mar 3, 2022
The library function winsound.PlaySound takes a sound parameter. One of the valid arguments is a path to a WAV filename.
Since Python 3.4, paths can be cleanly represented with PathLib.Path instances.
However, if you pass a Path instance to PlaySound, it responds with:
TypeError: 'sound' must be str or None, not 'WindowsPath'
This can be quickly fixed in the caller by passing str(the_path_instance) instead, but it would be cleaner if PlaySound accepted Python's preferred method of representing paths.
eryksun commentedon Mar 3, 2022
PlaySound() is implemented by the C function winsound_PlaySound_impl() in "PC/winsound.c". To support pathlike objects, it could use
soundname = PyOS_FSPath(sound), and require soundname to be a Unicode string via PyUnicode_Check(soundname). Or it could more generically support any buffer or pathlike object via PyUnicode_FSDecoder(sound, &soundname). This call decodes byte strings using the filesystem encoding, which is UTF-8 unless legacy ANSI mode is enabled.moribellamy commentedon Apr 12, 2022
I'm interested in trying this issue. What's an appropriate way to test the fix in CI? I'm assuming manual testing will be straightforward (just build the binary and have a simple script that tries to use a PathLib.Path instead of a string)
zware commentedon Apr 12, 2022
About the best you can do is replicate this test:
cpython/Lib/test/test_winsound.py
Lines 115 to 117 in f33e2c8
pathlib.Path(fn)instead of justfn.pythongh-91061: also accept pathlib.Path for winsound.PlaySound
moribellamy commentedon Apr 13, 2022
Thanks all. My first ever python PR is out. Looking forward to learning how to do things properly :)
pythongh-91061: also accept pathlib.Path for winsound.PlaySound (pyth…