dispatch: move IOError handling and flushing of streams to `dispatch()`
authorPulkit Goyal <7895pulkit@gmail.com>
Thu, 10 Dec 2020 13:51:56 +0530
changeset 46101 49b6910217f9
parent 46100 a771ffc378a8
child 46102 7ce24d3761e8
dispatch: move IOError handling and flushing of streams to `dispatch()` Instead of patching both dispatch code and commandserver code, we directly handle this in `dispatch.dispatch()`. Thanks to Yuya who recommended this.
mercurial/commandserver.py
mercurial/dispatch.py
tests/test-devel-warnings.t
--- a/mercurial/commandserver.py	Wed Dec 09 00:00:19 2020 -0800
+++ b/mercurial/commandserver.py	Thu Dec 10 13:51:56 2020 +0530
@@ -355,18 +355,7 @@
         )
 
         try:
-            err = None
-            try:
-                status = self._dispatchcommand(req)
-            except error.StdioError as e:
-                status = -1
-                err = e
-
-            retval = dispatch.closestdio(req.ui, err)
-            if retval:
-                status = retval
-
-            ret = status & 255
+            ret = self._dispatchcommand(req) & 255
             # If shutdown-on-interrupt is off, it's important to write the
             # result code *after* SIGINT handler removed. If the result code
             # were lost, the client wouldn't be able to continue processing.
--- a/mercurial/dispatch.py	Wed Dec 09 00:00:19 2020 -0800
+++ b/mercurial/dispatch.py	Thu Dec 10 13:51:56 2020 +0530
@@ -104,7 +104,7 @@
                 raise exc
 
 
-def closestdio(ui, err):
+def _flushstdio(ui, err):
     status = None
     # In all cases we try to flush stdio streams.
     if util.safehasattr(ui, b'fout'):
@@ -139,16 +139,8 @@
         initstdio()
         with tracing.log('parse args into request'):
             req = request(pycompat.sysargv[1:])
-        err = None
-        try:
-            status = dispatch(req)
-        except error.StdioError as e:
-            err = e
-            status = -1
 
-        ret = closestdio(req.ui, err)
-        if ret:
-            status = ret
+        status = dispatch(req)
         _silencestdio()
     except KeyboardInterrupt:
         # Catch early/late KeyboardInterrupt as last ditch. Here nothing will
@@ -240,7 +232,21 @@
 
 def dispatch(req):
     """run the command specified in req.args; returns an integer status code"""
-    with tracing.log('dispatch.dispatch'):
+    err = None
+    try:
+        status = _rundispatch(req)
+    except error.StdioError as e:
+        err = e
+        status = -1
+
+    ret = _flushstdio(req.ui, err)
+    if ret:
+        status = ret
+    return status
+
+
+def _rundispatch(req):
+    with tracing.log('dispatch._rundispatch'):
         if req.ferr:
             ferr = req.ferr
         elif req.ui:
--- a/tests/test-devel-warnings.t	Wed Dec 09 00:00:19 2020 -0800
+++ b/tests/test-devel-warnings.t	Thu Dec 10 13:51:56 2020 +0530
@@ -104,6 +104,7 @@
    */hg:* in <module> (glob) (?)
    */mercurial/dispatch.py:* in run (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py* in callcatch (glob)
@@ -120,6 +121,7 @@
    */hg:* in <module> (glob) (?)
    */mercurial/dispatch.py:* in run (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py:* in callcatch (glob)
@@ -142,6 +144,7 @@
    */mercurial/commandserver.py:* in runcommand (glob)
    */mercurial/commandserver.py:* in _dispatchcommand (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py:* in callcatch (glob)
@@ -184,6 +187,7 @@
    */hg:* in <module> (glob) (?)
    */mercurial/dispatch.py:* in run (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py* in callcatch (glob)
@@ -201,6 +205,7 @@
    */hg:* in <module> (glob)
    */mercurial/dispatch.py:* in run (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py:* in callcatch (glob)
@@ -223,6 +228,7 @@
    */mercurial/commandserver.py:* in runcommand (glob)
    */mercurial/commandserver.py:* in _dispatchcommand (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py:* in callcatch (glob)
@@ -247,6 +253,7 @@
    */hg:* in <module> (glob) (?)
    */mercurial/dispatch.py:* in run (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py* in callcatch (glob)
@@ -271,6 +278,7 @@
    */hg:* in <module> (glob)
    */mercurial/dispatch.py:* in run (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py:* in callcatch (glob)
@@ -293,6 +301,7 @@
    */mercurial/commandserver.py:* in runcommand (glob)
    */mercurial/commandserver.py:* in _dispatchcommand (glob)
    */mercurial/dispatch.py:* in dispatch (glob)
+   */mercurial/dispatch.py:* in _rundispatch (glob)
    */mercurial/dispatch.py:* in _runcatch (glob)
    */mercurial/dispatch.py:* in _callcatch (glob)
    */mercurial/scmutil.py:* in callcatch (glob)