I assumed that sys.stdout would refer to the same physical thread as iostreams :: cout works in the same process, but that doesn't seem to be the case. The following code that calls a C ++ function call with a python shell called "write" that writes to cout:
from cStringIO import StringIO import sys orig_stdout = sys.stdout sys.stdout = stringout = StringIO() write("cout") # wrapped C++ function that writes to cout print "-" * 40 print "stdout" sys.stdout = orig_stdout print stringout.getvalue()
immediately writes "cout" to the console, then the separator "---..." and finally, as the return value of stringout.getvalue (), the string "stdout". My intention was to capture in stringout also a string written by cout from C ++. Does anyone know what is going on, and if so, how can I capture what is written in cout in a python string?
Thanks in advance.
source share