gh-75593: Add support of bytes and path-like paths in wave.open() (GH-140951)

This commit is contained in:
Mark Byrne 2025-11-12 09:20:55 +01:00 committed by GitHub
parent f1b7961ccf
commit 35908265b0
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 4 deletions

View file

@ -1,9 +1,11 @@
import unittest
from test import audiotests
from test import support
from test.support.os_helper import FakePath
import io
import os
import struct
import tempfile
import sys
import wave
@ -206,5 +208,25 @@ def test_open_in_write_raises(self):
self.assertIsNone(cm.unraisable)
class WaveOpen(unittest.TestCase):
def test_open_pathlike(self):
"""It is possible to use `wave.read` and `wave.write` with a path-like object"""
with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
cases = (
FakePath(fp.name),
FakePath(os.fsencode(fp.name)),
os.fsencode(fp.name),
)
for fake_path in cases:
with self.subTest(fake_path):
with wave.open(fake_path, 'wb') as f:
f.setnchannels(1)
f.setsampwidth(2)
f.setframerate(44100)
with wave.open(fake_path, 'rb') as f:
pass
if __name__ == '__main__':
unittest.main()