File content will be messed up if stdout
and stderr
are full-buffered and redirected to the same file by the shell.
Are there ways to check whether the underlying files/pipes/terminals of file pointers/descriptors/handles are the same in C++ and Python?
Buffering should be disabled only if stdout
and stderr
are redirected to the same file.
EDIT:
Thanks for isedev's comment.
That idea also works in Python 3.4.
def same_file(file1, file2):
stat1, stat2 = os.fstat(file1.fileno()), os.fstat(file2.fileno())
return (stat1.st_dev == stat2.st_dev) and (stat1.st_ino == stat2.st_ino)
This doesn't work for pipe/terminal of course.