changeset 46174:a1601ff3877c

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
author Yuya Nishihara <yuya@tcha.org>
date Sat, 19 Dec 2020 11:10:18 +0900
parents a9765e0a461d
children a04c03b0678e
files mercurial/utils/procutil.py
diffstat 1 files changed, 11 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- 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: