You could intercept stdout
before being written to from your C
code, then process the output value.
import sys
import StringIO
buffer = StringIO.StringIO()
# redirect stdout to a buffer
sys.stdout = buffer
# call the c code with ctypes
# process the buffer
# recover the old stdout
sys.stdout = sys.__stdout__
However, it would be easier and nicer to pass a buffer to the C code, and instead of printf
-ing the output values you would write them in the provided buffer.
Or, better yet, you could pass byref
a c_char_p
, allocate memory for it inside the C
code, update the buffer with the output value then use the buffer in Python
. Don't forget to deallocate the memory (you should make a ctypes
wrapper for the free
function).