When calling a program from the command line, I can pipe the output to grep
to select the lines I want to see, e.g.
printf "hello\ngood day\nfarewell\n" | grep day
I am in search for the same kind of line selection, but for a C library called from Python. Consider the following example:
import os
# Function which emulate a C library call
def call_library():
os.system('printf "hello\ngood day\nfarewell\n"')
# Pure Python stuff
print('hello from Python')
# C library stuff
call_library()
When running this Python code, I want the output of the C part to be grep
'ed for the string 'day'
, making the output of the code
hello from Python
good day
So far I has fiddled around with redirection of stdout
, using the methods described here and here. I am able to make the C output vanish completely, or save it to a str
and print it out later (which is what the two links are mainly concerned with). I am not however able to select which lines get printed based on its content. Importantly, I want the output in real time while the C library is being called, so I cannot just redirect stdout
to some buffer and do some processing on this buffer after the fact.
The solution need only to work with Python 3.x on Linux. If in addition to line selection, the solution makes it possible for line editing, that would be even greater.
I think the following should be possible, but I do not know how to set it up
Redirect
stdout
to a "file" in memory.Spawn a new thread which constantly reads from this file, does the selection based on line content, and writes the wanted lines to the screen, i.e. the original destination of
stdout
.Call the C library
Join the two threads back together and redirect
stdout
back to its original destination (the screen).
I do not have a firm enough grasp of file descriptors and the like to be able to do this, nor to even know if this is the best way of doing it.
Edit
Note that the solution cannot simply re-implement the code in call_library
. The code must call call_library
, totally agnostic to the actual code which then gets executed.