# HG changeset patch # User Yuya Nishihara # Date 1608343818 -32400 # Node ID a1601ff3877cd2df4640a12422e7d438c04789e5 # Parent a9765e0a461dc02b1b56f9ed4d3c6001a73d0b8c procutil: introduce pseudo file object that just raises EBADF This should be safer than closing underlying fd as the fd may be reused. On Python 2, closed sys.stdin could be redirected to a random file having fd=0, but we'd be better not copying this behavior. Only readinto() and write() are implemented according to the following ABC table. fileno() is not implemented since fd=0/1/2 may be assigned later to other files. https://docs.python.org/3/library/io.html#class-hierarchy diff -r a9765e0a461d -r a1601ff3877c mercurial/utils/procutil.py --- a/mercurial/utils/procutil.py Fri Dec 18 20:14:54 2020 +0900 +++ b/mercurial/utils/procutil.py Sat Dec 19 11:10:18 2020 +0900 @@ -50,6 +50,16 @@ return False +class BadFile(io.RawIOBase): + """Dummy file object to simulate closed stdio behavior""" + + def readinto(self, b): + raise IOError(errno.EBADF, 'Bad file descriptor') + + def write(self, b): + raise IOError(errno.EBADF, 'Bad file descriptor') + + class LineBufferedWrapper(object): def __init__(self, orig): self.orig = orig @@ -129,8 +139,7 @@ if sys.stdin: stdin = sys.stdin.buffer else: - stdin = open(os.devnull, 'rb') - os.close(stdin.fileno()) + stdin = BadFile() stdout = _make_write_all(sys.stdout.buffer) stderr = _make_write_all(sys.stderr.buffer) if pycompat.iswindows: