comparison mercurial/ui.py @ 41285:cf8677cd7286

ui: proxy protect/restorestdio() calls to update internal flag It should be better to manage the redirection flag solely by the ui class.
author Yuya Nishihara <yuya@tcha.org>
date Wed, 26 Sep 2018 21:41:52 +0900
parents b0e3f2d7c143
children ff927ecb12f9
comparison
equal deleted inserted replaced
41284:b0e3f2d7c143 41285:cf8677cd7286
1078 def _isatty(self, fh): 1078 def _isatty(self, fh):
1079 if self.configbool('ui', 'nontty'): 1079 if self.configbool('ui', 'nontty'):
1080 return False 1080 return False
1081 return procutil.isatty(fh) 1081 return procutil.isatty(fh)
1082 1082
1083 def protectfinout(self):
1084 """Duplicate ui streams and redirect original if they are stdio
1085
1086 Returns (fin, fout) which point to the original ui fds, but may be
1087 copy of them. The returned streams can be considered "owned" in that
1088 print(), exec(), etc. never reach to them.
1089 """
1090 if self._finoutredirected:
1091 # if already redirected, protectstdio() would just create another
1092 # nullfd pair, which is equivalent to returning self._fin/_fout.
1093 return self._fin, self._fout
1094 fin, fout = procutil.protectstdio(self._fin, self._fout)
1095 self._finoutredirected = (fin, fout) != (self._fin, self._fout)
1096 return fin, fout
1097
1098 def restorefinout(self, fin, fout):
1099 """Restore ui streams from possibly duplicated (fin, fout)"""
1100 if (fin, fout) == (self._fin, self._fout):
1101 return
1102 procutil.restorestdio(self._fin, self._fout, fin, fout)
1103 # protectfinout() won't create more than one duplicated streams,
1104 # so we can just turn the redirection flag off.
1105 self._finoutredirected = False
1106
1083 @contextlib.contextmanager 1107 @contextlib.contextmanager
1084 def protectedfinout(self): 1108 def protectedfinout(self):
1085 """Run code block with protected standard streams""" 1109 """Run code block with protected standard streams"""
1086 fin, fout = procutil.protectstdio(self._fin, self._fout) 1110 fin, fout = self.protectfinout()
1087 try: 1111 try:
1088 yield fin, fout 1112 yield fin, fout
1089 finally: 1113 finally:
1090 procutil.restorestdio(self._fin, self._fout, fin, fout) 1114 self.restorefinout(fin, fout)
1091 1115
1092 def disablepager(self): 1116 def disablepager(self):
1093 self._disablepager = True 1117 self._disablepager = True
1094 1118
1095 def pager(self, command): 1119 def pager(self, command):