radareorg / radare2-bindings

Bindings of the r2 api for Valabind and friends
GNU Lesser General Public License v3.0
130 stars 92 forks source link

Python Bindings stderr #228

Closed utkonos closed 2 years ago

utkonos commented 2 years ago

I am having trouble capturing stderr when using the Python bindings. I would like to capture the stderr output to a variable, but if that is not possible, at least make the calls quiet. I know how to do this using r2pipe with the -2 flag.

How can one implement the equivalent of the -2 flag using the Python bindings here?

Also, it is possible to capture this output, what is the best way to do that?

image

I have tried using contextlib.redirect_stderr() and it fails.

The problem appears to be that the output is a c-level stream. I tried the following, but it also doesn't work properly: https://eli.thegreenplace.net/2015/redirecting-all-kinds-of-stdout-in-python/

utkonos commented 2 years ago

The problem with that recipe for redirecting stdout/stderr from ctypes is that it uses sys.stdout.close() which conflicts with Jupyter. It would work in standard Python scripts.

wtdcode commented 2 years ago

It's possible to disable all output by e scr.prompt=false. I'm not sure whether it's something you would like.

utkonos commented 2 years ago

I found a fix!

The following is an elegant context manager for doing exactly what I need: https://pypi.org/project/wurlitzer/

It is a work of art.

image