Skip to content

Commit 376c170

Browse files
committed
Merge pull request #150 from bastibe/check-vio-attributes
Factor check for virtual IO attributes into helper function
2 parents 70f396c + 089cf06 commit 376c170

File tree

1 file changed

+13
-6
lines changed

1 file changed

+13
-6
lines changed

soundfile.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,9 +1074,6 @@ def close(self):
10741074

10751075
def _open(self, file, mode_int, closefd):
10761076
"""Call the appropriate sf_open*() function from libsndfile."""
1077-
readable = hasattr(file, 'read') or hasattr(file, 'readinto')
1078-
writeable = hasattr(file, 'write')
1079-
seekable = hasattr(file, 'seek') and hasattr(file, 'tell')
10801077
if isinstance(file, (_unicode, bytes)):
10811078
if _os.path.isfile(file):
10821079
if 'x' in self.mode:
@@ -1093,9 +1090,7 @@ def _open(self, file, mode_int, closefd):
10931090
file_ptr = openfunction(file, mode_int, self._info)
10941091
elif isinstance(file, int):
10951092
file_ptr = _snd.sf_open_fd(file, mode_int, self._info, closefd)
1096-
elif (seekable and
1097-
(readable or mode_int == _snd.SFM_WRITE) and
1098-
(writeable or mode_int == _snd.SFM_READ)):
1093+
elif _has_virtual_io_attrs(file, mode_int):
10991094
file_ptr = _snd.sf_open_virtual(self._init_virtual_io(file),
11001095
mode_int, self._info, _ffi.NULL)
11011096
else:
@@ -1411,3 +1406,15 @@ def _check_format(format_str):
14111406
except KeyError:
14121407
raise ValueError("Unknown format: {0!r}".format(format_str))
14131408
return format_int
1409+
1410+
1411+
def _has_virtual_io_attrs(file, mode_int):
1412+
"""Check if file has all the necessary attributes for virtual IO."""
1413+
readonly = mode_int == _snd.SFM_READ
1414+
writeonly = mode_int == _snd.SFM_WRITE
1415+
return all([
1416+
hasattr(file, 'seek'),
1417+
hasattr(file, 'tell'),
1418+
hasattr(file, 'write') or readonly,
1419+
hasattr(file, 'read') or hasattr(file, 'readinto') or writeonly,
1420+
])

0 commit comments

Comments
 (0)