changeset 37218:d2bd29dffc6c

server: minor code cleanup Suggested by Yuya after 77f9e95fe3c4, this is mostly using named values for stdio descriptors. The lockfile is also opened in binary mode when reading back content from the child.
author Matt Harbison <matt_harbison@yahoo.com>
date Fri, 30 Mar 2018 23:32:06 -0400
parents 73a60281a861
children b00bd974eef5
files mercurial/server.py
diffstat 1 files changed, 10 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/server.py	Fri Mar 30 23:21:46 2018 -0400
+++ b/mercurial/server.py	Fri Mar 30 23:32:06 2018 -0400
@@ -55,8 +55,8 @@
             fd = os.open(postexecargs['unlink'],
                          os.O_WRONLY | os.O_APPEND | os.O_BINARY)
             try:
-                os.dup2(fd, 1)
-                os.dup2(fd, 2)
+                os.dup2(fd, procutil.stdout.fileno())
+                os.dup2(fd, procutil.stderr.fileno())
             finally:
                 os.close(fd)
 
@@ -94,7 +94,7 @@
                 # If the daemonized process managed to write out an error msg,
                 # report it.
                 if pycompat.iswindows and os.path.exists(lockpath):
-                    with open(lockpath) as log:
+                    with open(lockpath, 'rb') as log:
                         for line in log:
                             procutil.stderr.write(line)
                 raise error.Abort(_('child process failed to start'))
@@ -129,12 +129,14 @@
         if logfile:
             logfilefd = os.open(logfile, os.O_RDWR | os.O_CREAT | os.O_APPEND,
                                 0o666)
-        os.dup2(nullfd, 0)
-        os.dup2(logfilefd, 1)
-        os.dup2(logfilefd, 2)
-        if nullfd not in (0, 1, 2):
+        os.dup2(nullfd, procutil.stdin.fileno())
+        os.dup2(logfilefd, procutil.stdout.fileno())
+        os.dup2(logfilefd, procutil.stderr.fileno())
+        stdio = (procutil.stdin.fileno(), procutil.stdout.fileno(),
+                 procutil.stderr.fileno())
+        if nullfd not in stdio:
             os.close(nullfd)
-        if logfile and logfilefd not in (0, 1, 2):
+        if logfile and logfilefd not in stdio:
             os.close(logfilefd)
 
         # Only unlink after redirecting stdout/stderr, so Windows doesn't