Skip to content

Commit 6de06ef

Browse files
committed
cast FFI strings
1 parent 3c8f6b7 commit 6de06ef

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

sounddevice.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,7 @@ def query_devices(device=None, kind=None):
572572
if not info:
573573
raise PortAudioError(f'Error querying device {device}')
574574
assert info.structVersion == 2
575-
name_bytes = _ffi.string(info.name)
575+
name_bytes = _ffi_string(info.name)
576576
try:
577577
# We don't know beforehand if DirectSound and MME device names use
578578
# 'utf-8' or 'mbcs' encoding. Let's try 'utf-8' first, because it more
@@ -652,7 +652,7 @@ def query_hostapis(index=None):
652652
raise PortAudioError(f'Error querying host API {index}')
653653
assert info.structVersion == 1
654654
return {
655-
'name': _ffi.string(info.name).decode(),
655+
'name': _ffi_string(info.name).decode(),
656656
'devices': [_lib.Pa_HostApiDeviceIndexToDeviceIndex(index, i)
657657
for i in range(info.deviceCount)],
658658
'default_input_device': info.defaultInputDevice,
@@ -722,7 +722,7 @@ def get_portaudio_version():
722722
(1899, 'PortAudio V19-devel (built Feb 15 2014 23:28:00)')
723723
724724
"""
725-
return _lib.Pa_GetVersion(), _ffi.string(_lib.Pa_GetVersionText()).decode()
725+
return _lib.Pa_GetVersion(), _ffi_string(_lib.Pa_GetVersionText()).decode()
726726

727727

728728
class _StreamBase:
@@ -2658,6 +2658,10 @@ def wait(self, ignore_errors=True):
26582658
return self.status if self.status else None
26592659

26602660

2661+
def _ffi_string(cdata):
2662+
return _t.cast(bytes, _ffi.string(cdata))
2663+
2664+
26612665
def _remove_self(d):
26622666
"""Return a copy of d without the 'self' entry."""
26632667
d = d.copy()
@@ -2788,7 +2792,7 @@ def _check(err, msg=''):
27882792
if err >= 0:
27892793
return err
27902794

2791-
errormsg = _ffi.string(_lib.Pa_GetErrorText(err)).decode()
2795+
errormsg = _ffi_string(_lib.Pa_GetErrorText(err)).decode()
27922796
if msg:
27932797
errormsg = f'{msg}: {errormsg}'
27942798

@@ -2799,7 +2803,7 @@ def _check(err, msg=''):
27992803
# in scenarios where multiple APIs are being used simultaneously.
28002804
info = _lib.Pa_GetLastHostErrorInfo()
28012805
host_api = _lib.Pa_HostApiTypeIdToHostApiIndex(info.hostApiType)
2802-
hosterror_text = _ffi.string(info.errorText).decode()
2806+
hosterror_text = _ffi_string(info.errorText).decode()
28032807
hosterror_info = host_api, info.errorCode, hosterror_text
28042808
raise PortAudioError(errormsg, err, hosterror_info)
28052809

0 commit comments

Comments
 (0)