Skip to content

Commit 3590826

Browse files
authored
pythongh-75593: Add support of bytes and path-like paths in wave.open() (pythonGH-140951)
1 parent f1b7961 commit 3590826

File tree

4 files changed

+33
-4
lines changed

4 files changed

+33
-4
lines changed

Doc/library/wave.rst

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ The :mod:`wave` module defines the following function and exception:
2525

2626
.. function:: open(file, mode=None)
2727

28-
If *file* is a string, open the file by that name, otherwise treat it as a
29-
file-like object. *mode* can be:
28+
If *file* is a string, a :term:`path-like object` or a
29+
:term:`bytes-like object` open the file by that name, otherwise treat it as
30+
a file-like object. *mode* can be:
3031

3132
``'rb'``
3233
Read only mode.
@@ -52,6 +53,10 @@ The :mod:`wave` module defines the following function and exception:
5253
.. versionchanged:: 3.4
5354
Added support for unseekable files.
5455

56+
.. versionchanged:: 3.15
57+
Added support for :term:`path-like objects <path-like object>`
58+
and :term:`bytes-like objects <bytes-like object>`.
59+
5560
.. exception:: Error
5661

5762
An error raised when something is impossible because it violates the WAV

Lib/test/test_wave.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
import unittest
22
from test import audiotests
33
from test import support
4+
from test.support.os_helper import FakePath
45
import io
56
import os
67
import struct
8+
import tempfile
79
import sys
810
import wave
911

@@ -206,5 +208,25 @@ def test_open_in_write_raises(self):
206208
self.assertIsNone(cm.unraisable)
207209

208210

211+
class WaveOpen(unittest.TestCase):
212+
def test_open_pathlike(self):
213+
"""It is possible to use `wave.read` and `wave.write` with a path-like object"""
214+
with tempfile.NamedTemporaryFile(delete_on_close=False) as fp:
215+
cases = (
216+
FakePath(fp.name),
217+
FakePath(os.fsencode(fp.name)),
218+
os.fsencode(fp.name),
219+
)
220+
for fake_path in cases:
221+
with self.subTest(fake_path):
222+
with wave.open(fake_path, 'wb') as f:
223+
f.setnchannels(1)
224+
f.setsampwidth(2)
225+
f.setframerate(44100)
226+
227+
with wave.open(fake_path, 'rb') as f:
228+
pass
229+
230+
209231
if __name__ == '__main__':
210232
unittest.main()

Lib/wave.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969

7070
from collections import namedtuple
7171
import builtins
72+
import os
7273
import struct
7374
import sys
7475

@@ -274,7 +275,7 @@ def initfp(self, file):
274275

275276
def __init__(self, f):
276277
self._i_opened_the_file = None
277-
if isinstance(f, str):
278+
if isinstance(f, (bytes, str, os.PathLike)):
278279
f = builtins.open(f, 'rb')
279280
self._i_opened_the_file = f
280281
# else, assume it is an open file object already
@@ -431,7 +432,7 @@ class Wave_write:
431432

432433
def __init__(self, f):
433434
self._i_opened_the_file = None
434-
if isinstance(f, str):
435+
if isinstance(f, (bytes, str, os.PathLike)):
435436
f = builtins.open(f, 'wb')
436437
self._i_opened_the_file = f
437438
try:
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Add support of :term:`path-like objects <path-like object>` and :term:`bytes-like objects <bytes-like object>` in :func:`wave.open`.

0 commit comments

Comments
 (0)