]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-75593: Add support of bytes and path-like paths in wave.open() (GH-140951)
authorMark Byrne <31762852+mbyrnepr2@users.noreply.github.com>
Wed, 12 Nov 2025 08:20:55 +0000 (09:20 +0100)
committerGitHub <noreply@github.com>
Wed, 12 Nov 2025 08:20:55 +0000 (10:20 +0200)
Doc/library/wave.rst
Lib/test/test_wave.py
Lib/wave.py
Misc/NEWS.d/next/Library/2025-11-04-12-16-13.gh-issue-75593.EFVhKR.rst [new file with mode: 0644]

index a3f5bfd5e2f99cdc3d36c1e8594cc35f5907b72d..7ff2c97992c4e33c9517a10010fb1e7e5e409114 100644 (file)
@@ -25,8 +25,9 @@ The :mod:`wave` module defines the following function and exception:
 
 .. function:: open(file, mode=None)
 
-   If *file* is a string, open the file by that name, otherwise treat it as a
-   file-like object.  *mode* can be:
+   If *file* is a string, a :term:`path-like object` or a
+   :term:`bytes-like object` open the file by that name, otherwise treat it as
+   a file-like object.  *mode* can be:
 
    ``'rb'``
       Read only mode.
@@ -52,6 +53,10 @@ The :mod:`wave` module defines the following function and exception:
    .. versionchanged:: 3.4
       Added support for unseekable files.
 
+   .. versionchanged:: 3.15
+      Added support for :term:`path-like objects <path-like object>`
+      and :term:`bytes-like objects <bytes-like object>`.
+
 .. exception:: Error
 
    An error raised when something is impossible because it violates the WAV
index 226b1aa84bd73c34e5bfab224a809457fa68cbd8..4c21f16553775cc0b1e77d9f128acbd609433f0d 100644 (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 @@ class WaveLowLevelTest(unittest.TestCase):
             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()
index 5af745e2217ec3b6082a418117857f3671d117ef..056bd6aab7ffa3c3220554eaae9bbe3631afa24d 100644 (file)
@@ -69,6 +69,7 @@ is destroyed.
 
 from collections import namedtuple
 import builtins
+import os
 import struct
 import sys
 
@@ -274,7 +275,7 @@ class Wave_read:
 
     def __init__(self, f):
         self._i_opened_the_file = None
-        if isinstance(f, str):
+        if isinstance(f, (bytes, str, os.PathLike)):
             f = builtins.open(f, 'rb')
             self._i_opened_the_file = f
         # else, assume it is an open file object already
@@ -431,7 +432,7 @@ class Wave_write:
 
     def __init__(self, f):
         self._i_opened_the_file = None
-        if isinstance(f, str):
+        if isinstance(f, (bytes, str, os.PathLike)):
             f = builtins.open(f, 'wb')
             self._i_opened_the_file = f
         try:
diff --git a/Misc/NEWS.d/next/Library/2025-11-04-12-16-13.gh-issue-75593.EFVhKR.rst b/Misc/NEWS.d/next/Library/2025-11-04-12-16-13.gh-issue-75593.EFVhKR.rst
new file mode 100644 (file)
index 0000000..9a31af9
--- /dev/null
@@ -0,0 +1 @@
+Add support of :term:`path-like objects <path-like object>` and :term:`bytes-like objects <bytes-like object>` in :func:`wave.open`.