[PATCH 3 of 5] histedit: do not close stdin

Jun Wu quark at fb.com
Tue Mar 15 05:58:34 EDT 2016


# HG changeset patch
# User Jun Wu <quark at fb.com>
# Date 1458002553 0
#      Tue Mar 15 00:42:33 2016 +0000
# Node ID c769f8196ccfdfa546862b0c4f608b78ca80a1c7
# Parent  cab663c8ba601c8ed617d18344ed8e232f86284b
histedit: do not close stdin

Closing stdin is unexpected by chgserver and is not a good idea generally.
This patch refactors related code a bit and make sure stdin is not closed.
It will make chg much happier on test-histedit*.t.

diff --git a/hgext/histedit.py b/hgext/histedit.py
--- a/hgext/histedit.py
+++ b/hgext/histedit.py
@@ -998,6 +998,13 @@
         return goaleditplan
     return goalnew
 
+def _readfile(path):
+    if path == '-':
+        return sys.stdin.read()
+    else:
+        with open(path, 'rb') as f:
+            return f.read()
+
 def _validateargs(ui, repo, state, freeargs, opts, goal, rules, revs):
     # TODO only abort if we try to histedit mq patches, not just
     # blanket if mq patches are applied somewhere
@@ -1190,12 +1197,7 @@
                                  node.short(state.topmost))
         rules = ruleeditor(repo, ui, state.actions, comment)
     else:
-        if rules == '-':
-            f = sys.stdin
-        else:
-            f = open(rules)
-        rules = f.read()
-        f.close()
+        rules = _readfile(rules)
     actions = parserules(rules, state)
     ctxs = [repo[act.nodetoverify()] \
             for act in state.actions if act.nodetoverify()]
@@ -1236,12 +1238,7 @@
         actions = [pick(state, r) for r in revs]
         rules = ruleeditor(repo, ui, actions, comment)
     else:
-        if rules == '-':
-            f = sys.stdin
-        else:
-            f = open(rules)
-        rules = f.read()
-        f.close()
+        rules = _readfile(rules)
     actions = parserules(rules, state)
     warnverifyactions(ui, repo, actions, state, ctxs)
 


More information about the Mercurial-devel mailing list