diff options
| author | Matthias Geier <Matthias.Geier@gmail.com> | 2019-04-03 19:11:33 +0200 |
|---|---|---|
| committer | Matthias Geier <Matthias.Geier@gmail.com> | 2019-04-06 19:35:34 +0200 |
| commit | 2bc3de04015de357db0018dcdb87c1c0adfea8e5 (patch) | |
| tree | eb45341e7202dd684fa1e1ff33399eb4c7529692 /sounddevice.py | |
| parent | 427971c05522cedb5a9d801f80442f5e5bda8bfe (diff) | |
Redirect stderr only during initialization
Diffstat (limited to 'sounddevice.py')
| -rw-r--r-- | sounddevice.py | 50 |
1 files changed, 29 insertions, 21 deletions
diff --git a/sounddevice.py b/sounddevice.py index 4e23e94..c7c1d62 100644 --- a/sounddevice.py +++ b/sounddevice.py @@ -2639,10 +2639,35 @@ def _get_device_id(id_or_query_string, kind, raise_on_error=False): def _initialize(): - """Initialize PortAudio.""" - global _initialized - _check(_lib.Pa_Initialize(), 'Error initializing PortAudio') - _initialized += 1 + """Initialize PortAudio. + + This temporarily forwards messages from stderr to /dev/null + (where supported). + + """ + old_stderr = None + try: + stdio = _ffi.dlopen(None) + 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) def _terminate(): @@ -2667,23 +2692,6 @@ def _exit_handler(): _terminate() -def _ignore_stderr(): - """Try to forward PortAudio messages from stderr to /dev/null.""" - try: - stdio = _ffi.dlopen(None) - devnull = stdio.fopen(_os.devnull.encode(), b'w') - except (OSError, AttributeError): - return - try: - stdio.stderr = devnull - except _ffi.error: - try: - stdio.__stderrp = devnull - except _ffi.error: - stdio.fclose(devnull) - - -_ignore_stderr() _atexit.register(_exit_handler) _initialize() |
