ui: move pre/post processes from low-level write()s to _writenobuf()
authorYuya Nishihara <yuya@tcha.org>
Sat, 03 Nov 2018 18:03:09 +0900
changeset 40539 04a9dd8da959
parent 40538 c5e964f75bf7
child 40540 06e841e72523
ui: move pre/post processes from low-level write()s to _writenobuf() This helps adding a dedicated stream for status/error messages. I don't want to add _write*() function per stream.
mercurial/ui.py
--- a/mercurial/ui.py	Sat Nov 03 17:56:17 2018 +0900
+++ b/mercurial/ui.py	Sat Nov 03 18:03:09 2018 +0900
@@ -962,6 +962,8 @@
         # opencode timeblockedsection because this is a critical path
         starttime = util.timer()
         try:
+            if dest is self.ferr and not getattr(self.fout, 'closed', False):
+                self.fout.flush()
             if self._colormode == 'win32':
                 # windows color printing is its own can of crab, defer to
                 # the color module and that is it.
@@ -971,15 +973,22 @@
                     label = opts.get(r'label', '')
                     msg = self.label(msg, label)
                 write(msg)
+            # stderr may be buffered under win32 when redirected to files,
+            # including stdout.
+            if dest is self.ferr and not getattr(self.ferr, 'closed', False):
+                dest.flush()
+        except IOError as err:
+            if (dest is self.ferr
+                and err.errno in (errno.EPIPE, errno.EIO, errno.EBADF)):
+                # no way to report the error, so ignore it
+                return
+            raise error.StdioError(err)
         finally:
             self._blockedtimes['stdio_blocked'] += \
                 (util.timer() - starttime) * 1000
 
     def _write(self, data):
-        try:
-            self.fout.write(data)
-        except IOError as err:
-            raise error.StdioError(err)
+        self.fout.write(data)
 
     def write_err(self, *args, **opts):
         if self._bufferstates and self._bufferstates[-1][0]:
@@ -988,18 +997,7 @@
             self._writenobuf(self.ferr, *args, **opts)
 
     def _write_err(self, data):
-        try:
-            if True:
-                if not getattr(self.fout, 'closed', False):
-                    self.fout.flush()
-                self.ferr.write(data)
-                # stderr may be buffered under win32 when redirected to files,
-                # including stdout.
-                if not getattr(self.ferr, 'closed', False):
-                    self.ferr.flush()
-        except IOError as inst:
-            if inst.errno not in (errno.EPIPE, errno.EIO, errno.EBADF):
-                raise error.StdioError(inst)
+        self.ferr.write(data)
 
     def flush(self):
         # opencode timeblockedsection because this is a critical path