How to Capture ctypes stderr or Supress It?

I am trying to use the python bindings for Radare2 found here:

These are based on ctypes and the compiled libraries from radare2. These libraries output text to stderr even under normal usage. This type of c-level stream cannot be redirected using the standard library contextlib.redirect_stderr().

According to the following guide, there is an alternative recipe to make a contextmanager based way to redirect the output from ctypes correctly. However, this way requires sys.stdout.close() which conflicts with Jupyter. If this is run in Jupyter, the cell enters what appears to be an infinite loop.

The native Jupyter cell magic, %%capture also does not work to redirect stderr from ctypes.

Am I stuck between a rock and a hard place, or is there another way that this can work in Jupyter?

I made wurlitzer for exactly this purpose, based on that same blog post. It can be used as an IPython extension with %load_ext wurlitzer in which case it will capture C-level output and forward it to the Jupyter-wrapped sys.stdout in every cell.


Wurlitzer is a work of art. Where do I send the check?