diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2022-11-17 20:19:43 +0100 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2022-11-17 20:19:43 +0100 |
| commit | 397eac5ac43d1b0335dc1a377cfe1c4a8818cd5e (patch) | |
| tree | 28b844b2dbd478a6bb6ee94854f20ff70d8c21b4 | |
| parent | 841e0a1b2ea2367476248e5f0fc6878154949417 (diff) | |
Redirect stderr with os.dup2() instead of CFFI calls
| -rw-r--r-- | sounddevice.py | 23 | ||||
| -rw-r--r-- | sounddevice_build.py | 8 |
2 files changed, 8 insertions, 23 deletions
diff --git a/sounddevice.py b/sounddevice.py index a0b723b..643d2fe 100644 --- a/sounddevice.py +++ b/sounddevice.py @@ -2823,29 +2823,22 @@ def _initialize(): is automatically called with the ``import sounddevice`` statement. """ - old_stderr = None + devnull = None + old_stderr = 2 # To appease Pyright try: - stdio = _ffi.dlopen(None) + old_stderr = _os.dup(2) + devnull = open(_os.devnull, 'w') + _os.dup2(devnull.fileno(), 2) except OSError: pass - else: - for stderr_name in 'stderr', '__stderrp': - try: - old_stderr = getattr(stdio, stderr_name) - except _ffi.error: - continue - else: - devnull = stdio.fopen(_os.devnull.encode(), b'w') - setattr(stdio, stderr_name, devnull) - break try: _check(_lib.Pa_Initialize(), 'Error initializing PortAudio') global _initialized _initialized += 1 finally: - if old_stderr is not None: - setattr(stdio, stderr_name, old_stderr) - stdio.fclose(devnull) + if devnull is not None: + _os.dup2(old_stderr, 2) + devnull.close() def _terminate(): diff --git a/sounddevice_build.py b/sounddevice_build.py index 70ecbd8..7befb06 100644 --- a/sounddevice_build.py +++ b/sounddevice_build.py @@ -311,13 +311,5 @@ typedef struct PaWasapiStreamInfo int PaWasapi_IsLoopback( PaDeviceIndex device ); """) -ffibuilder.cdef(""" - /* from stdio.h */ - FILE* fopen(const char* path, const char* mode); - int fclose(FILE* fp); - extern FILE* stderr; /* GNU C library */ - extern FILE* __stderrp; /* macOS */ -""") - if __name__ == '__main__': ffibuilder.compile(verbose=True) |
