Patchwork [2,of,2] server: minor code cleanup

login
register
mail settings
Submitter Matt Harbison
Date March 31, 2018, 3:39 a.m.
Message ID <f1f70b2610da96054c67.1522467592@Envy>
Download mbox | patch
Permalink /patch/30049/
State Accepted
Headers show

Comments

Matt Harbison - March 31, 2018, 3:39 a.m.
# HG changeset patch
# User Matt Harbison <matt_harbison@yahoo.com>
# Date 1522467126 14400
#      Fri Mar 30 23:32:06 2018 -0400
# Node ID f1f70b2610da96054c6750122092adbb24e7aea8
# Parent  6e77d0a68122a6513521c94c4b8256b7e6da6f93
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.

Patch

diff --git a/mercurial/server.py b/mercurial/server.py
--- a/mercurial/server.py
+++ b/mercurial/server.py
@@ -55,8 +55,8 @@  def runservice(opts, parentfn=None, init
             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 @@  def runservice(opts, parentfn=None, init
                 # 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 @@  def runservice(opts, parentfn=None, init
         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