question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

sounddevice silently redirects stderr to /dev/null

See original GitHub issue

This 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:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
pytaunaycommented, Feb 4, 2019

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.

And why is import readline changing the behavior?

Probably because readline overrides the built-in input command to parse input but I have not checked that yet.

Except out-of-memory errors, what could that possibly be?

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.

As I see it, that’s the price I’ll have to pay for not being annoyed by PortAudio’s useless messages all the time. I’m willing to pay that price.

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.

0reactions
mgeiercommented, Sep 25, 2019

Since #176 (and since the latest release 0.3.14), the sounddevice module only redirects stderr 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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

BASH Shell Redirect Output and Errors To /dev/null
Explains how to redirect output (stdout) and errors (stderr) to /dev/null under UNIX / Linux / BSD shell scripting or cron jobs.
Read more >
zsh - How to redirect stderr to /dev/null?
Here 2 things annoy me: ls -a $PWD/*/bin/activate 2>&1 /dev/null. The order of redirection is not the right (you redirect the errors in...
Read more >
python-sounddevice
sounddevice._initialize(). Initialize PortAudio. This temporarily forwards messages from stderr to /dev/null (where supported). In most cases ...
Read more >
ALSA warnings when running audiodevinfo.cc tests
FWIW, the sounddevice python library decided to redirect stderr while ... In Octave, I diverted the error pointer to /dev/null to silence the...
Read more >
Python Examples of subprocess.DEVNULL
CalledProcessError as e: return HttpResponseBadRequest(e.stderr) # restart mopidy to apply audio device change ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found