Skip to content

Commit e8e1e1e

Browse files
committed
adds support for file system path protocol (PEP 519)
1 parent 73d7330 commit e8e1e1e

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

soundfile.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,9 @@ def __init__(self, file, mode='r', samplerate=None, channels=None,
614614
>>> assert myfile.closed
615615
616616
"""
617+
# resolve PathLike objects (see PEP519 for details):
618+
# can be replaced with _os.fspath(file) for Python >= 3.6
619+
file = file.__fspath__() if hasattr(file, '__fspath__') else file
617620
self._name = file
618621
if mode is None:
619622
mode = getattr(file, 'mode', None)

tests/test_pysoundfile.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@
2121
filename_new = 'tests/delme.please'
2222

2323

24-
open_variants = 'name', 'fd', 'obj'
25-
24+
if sys.version_info >= (3, 6):
25+
import pathlib
26+
open_variants = 'name', 'fd', 'obj', 'pathlib'
27+
else:
28+
open_variants = 'name', 'fd', 'obj'
2629

2730
xfail_from_buffer = pytest.mark.xfail(cffi.__version_info__ < (0, 9),
2831
reason="from_buffer() since CFFI 0.9")
@@ -31,6 +34,8 @@
3134
def _file_existing(request, filename, fdarg, objarg=None):
3235
if request.param == 'name':
3336
return filename
37+
if request.param == 'pathlib':
38+
return pathlib.Path(filename)
3439
elif request.param == 'fd':
3540
fd = os.open(filename, fdarg)
3641

@@ -660,7 +665,8 @@ def test_seek_in_rplus_mode(sf_stereo_rplus):
660665

661666
@pytest.mark.parametrize("use_default", [True, False])
662667
def test_truncate(file_stereo_rplus, use_default):
663-
if isinstance(file_stereo_rplus, (str, int)):
668+
if (isinstance(file_stereo_rplus, (str, int))
669+
or hasattr(file_stereo_rplus, '__fspath__')):
664670
with sf.SoundFile(file_stereo_rplus, 'r+', closefd=False) as f:
665671
if use_default:
666672
f.seek(2)

0 commit comments

Comments
 (0)