I have a wrapper to redirect outputs when I call python python-wrapped C++.
The basic idea is to use dup
and dup2
, which are the only ways I've been able to catch the printf
outputs from the C++. The wrapper works fine with no calls to flush()
as long as I'm running the job interactively, but when I send the job to a TORQUE batch I get the unwelcome outputs again.
My understanding, in part from this question, is that some well-placed flush()
calls should fix this, but where exactly do they need to go? Should I flush the buffer before dup'ing to the tempfile? Before dup'ing back? Both?
The wrapper I'm using is as follows:
class Filter(object):
"""
Workaround filter for annoying and worthless errors.
"""
def __init__(self, veto_words={'ClassTable'}):
self.veto_words = set(veto_words)
self.temp = tempfile.NamedTemporaryFile()
def __enter__(self):
sys.stdout.flush() # <--- NEEDED?
sys.stderr.flush() # <--- NEEDED?
self.old_out, self.old_err = os.dup(1), os.dup(2)
os.dup2(self.temp.fileno(), 1)
os.dup2(self.temp.fileno(), 2)
def __exit__(self, exe_type, exe_val, tb):
sys.stdout.flush() # <--- NEEDED?
sys.stderr.flush() # <--- NEEDED?
os.dup2(self.old_out, 1)
os.dup2(self.old_err, 2)
self.temp.seek(0)
for line in self.temp:
veto = set(line.split()) & self.veto_words
if not veto:
sys.stderr.write(line)