def captureOutput(self, func, *args, **kwargs):
pass
sys.stdout.flush()
sys.stderr.flush()
(outfd, fn) = tempfile.mkstemp()
fout = os.fdopen(outfd, 'r')
os.unlink(fn)
(errfd, fn) = tempfile.mkstemp()
ferr = os.fdopen(errfd, 'r')
os.unlink(fn)
try:
oldstdout = os.dup(sys.stdout.fileno())
oldstderr = os.dup(sys.stderr.fileno())
os.dup2(outfd, sys.stdout.fileno())
os.dup2(errfd, sys.stderr.fileno())
try:
ret = func(*args, **kwargs)
finally:
sys.stderr.flush()
sys.stdout.flush()
os.dup2(oldstdout, sys.stdout.fileno())
os.close(oldstdout)
os.dup2(oldstderr, sys.stderr.fileno())
os.close(oldstderr)
os.lseek(outfd, 0, 0)
out = fout.read()
os.lseek(errfd, 0, 0)
err = ferr.read()
finally:
fout.close()
ferr.close()
return ret, out, err
When running this code, I get an error:
AttributeError: StringIO instance has no attribute 'fileno'
Why am I getting this error and how can I correct it?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…