Mercurial > public > mercurial-scm > hg
comparison mercurial/dispatch.py @ 46101:49b6910217f9
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.
author | Pulkit Goyal <7895pulkit@gmail.com> |
---|---|
date | Thu, 10 Dec 2020 13:51:56 +0530 |
parents | ac9de799d390 |
children | db5dddb38f5b |
comparison
equal
deleted
inserted
replaced
46100:a771ffc378a8 | 46101:49b6910217f9 |
---|---|
102 finally: | 102 finally: |
103 if exc is not None: | 103 if exc is not None: |
104 raise exc | 104 raise exc |
105 | 105 |
106 | 106 |
107 def closestdio(ui, err): | 107 def _flushstdio(ui, err): |
108 status = None | 108 status = None |
109 # In all cases we try to flush stdio streams. | 109 # In all cases we try to flush stdio streams. |
110 if util.safehasattr(ui, b'fout'): | 110 if util.safehasattr(ui, b'fout'): |
111 assert ui is not None # help pytype | 111 assert ui is not None # help pytype |
112 assert ui.fout is not None # help pytype | 112 assert ui.fout is not None # help pytype |
137 """run the command in sys.argv""" | 137 """run the command in sys.argv""" |
138 try: | 138 try: |
139 initstdio() | 139 initstdio() |
140 with tracing.log('parse args into request'): | 140 with tracing.log('parse args into request'): |
141 req = request(pycompat.sysargv[1:]) | 141 req = request(pycompat.sysargv[1:]) |
142 err = None | 142 |
143 try: | 143 status = dispatch(req) |
144 status = dispatch(req) | |
145 except error.StdioError as e: | |
146 err = e | |
147 status = -1 | |
148 | |
149 ret = closestdio(req.ui, err) | |
150 if ret: | |
151 status = ret | |
152 _silencestdio() | 144 _silencestdio() |
153 except KeyboardInterrupt: | 145 except KeyboardInterrupt: |
154 # Catch early/late KeyboardInterrupt as last ditch. Here nothing will | 146 # Catch early/late KeyboardInterrupt as last ditch. Here nothing will |
155 # be printed to console to avoid another IOError/KeyboardInterrupt. | 147 # be printed to console to avoid another IOError/KeyboardInterrupt. |
156 status = -1 | 148 status = -1 |
238 return b' '.join(procutil.shellquote(a) for a in args) | 230 return b' '.join(procutil.shellquote(a) for a in args) |
239 | 231 |
240 | 232 |
241 def dispatch(req): | 233 def dispatch(req): |
242 """run the command specified in req.args; returns an integer status code""" | 234 """run the command specified in req.args; returns an integer status code""" |
243 with tracing.log('dispatch.dispatch'): | 235 err = None |
236 try: | |
237 status = _rundispatch(req) | |
238 except error.StdioError as e: | |
239 err = e | |
240 status = -1 | |
241 | |
242 ret = _flushstdio(req.ui, err) | |
243 if ret: | |
244 status = ret | |
245 return status | |
246 | |
247 | |
248 def _rundispatch(req): | |
249 with tracing.log('dispatch._rundispatch'): | |
244 if req.ferr: | 250 if req.ferr: |
245 ferr = req.ferr | 251 ferr = req.ferr |
246 elif req.ui: | 252 elif req.ui: |
247 ferr = req.ui.ferr | 253 ferr = req.ui.ferr |
248 else: | 254 else: |