sounddevice silently redirects stderr to /dev/null
See original GitHub issueThis is originally in reference to an issue in the psychopy Sound backend which uses sounddevice (see https://github.com/psychopy/psychopy/issues/2230)
sounddevice
has a few lines of code that were added to remove the standard error output for convenience (pull request #12 in response to issues #11 and #10):
2670 def _ignore_stderr():
2671 """Try to forward PortAudio messages from stderr to /dev/null."""
2672 try:
2673 stdio = _ffi.dlopen(None)
2674 devnull = stdio.fopen(_os.devnull.encode(), b'w')
2675 except (OSError, AttributeError):
2676 return
2677 try:
2678 stdio.stderr = devnull
2679 except _ffi.error:
2680 try:
2681 stdio.__stderrp = devnull
2682 except _ffi.error:
2683 stdio.fclose(devnull)
2684
2685
2686 _ignore_stderr()
This redirects all of the standard error output to /dev/null and subsequently breaks built-in functions that use stderr display (e.g. “input”). This occurs in non-interactive sessions only (probably because of the way the CPython backend handles display with a prompt in interactive sessions).
mwe.py:
#!/usr/bin/python3
# This will display "Enter data" on the terminal
t = input("Enter data\n")
import sounddevice
# This does not
t = input("Enter data\n")
python3 mwe.py
will display “Enter data” the first time, but not the second time after sounddevice is imported. Commenting line 2686 in sounddevice.py gets rid of the issue.
stderr should not be redefined as /dev/null internally. I would suggest removing the _ignore_stderr
function. For those who do not like stderr output, just use piping externally to redirect stderr to /dev/null or a log file.
Piping example:
#!/usr/bin/python3
# This will display "Enter data" on the terminal
t = input("Enter data\n")
print("Test")
python3 pipe.py 2> /dev/null
will display only “Test”. “Enter data” gets redirected to /dev/null.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Top GitHub Comments
A pure Python context manager allows you to redirect stderr without messing with the C backend. I put together a solution based on https://stackoverflow.com/questions/4675728/redirect-stdout-to-a-file-in-python for which I will submit a pull request. It suppresses the warnings that show up during the initialization of ALSA and friends but it can technically be extended to other.
Probably because readline overrides the built-in
input
command to parse input but I have not checked that yet.I want to emphasize again that while there may not be many errors raised by the C backend of Python, they may be serious. There are errors for: out of memory, overflow, parsing, input/output, file descriptor, memory allocation, gzip, etc.
In effect this is messing with your car electronics and then removing the lightbulb behind the check engine light because driving with it on is annoying. You could be missing a simple reminder that the oil change is due, or a more important transmission issue. The errors are still there, they just don’t show up anymore; if not addressed it may result in additional problems. I get warning messages are aggravating (I do tend to ignore a bunch of them when compiling stuff) but in this context they can hide more important things coming from the backend of Python.
Since #176 (and since the latest release 0.3.14), the
sounddevice
module only redirectsstderr
during initialization.This redirection is still not optional. If somebody wants to work on making it optional, please open a new issue or pull request!