comparison mercurial/dispatch.py @ 46173:a9765e0a461d

dispatch: gate against missing stdout/stderr We do need procutil.stdin/stdout/stderr, but we don't care much for sys.std*. Let's leave them be None as it is the Python 3 way.
author Yuya Nishihara <yuya@tcha.org>
date Fri, 18 Dec 2020 20:14:54 +0900
parents d04c0e494cfe
children 9c9e0b4b2ca7
comparison
equal deleted inserted replaced
46172:d04c0e494cfe 46173:a9765e0a461d
164 # and sys.stderr. They will inherit the line ending normalization settings, 164 # and sys.stderr. They will inherit the line ending normalization settings,
165 # potentially causing e.g. \r\n to be emitted. Since emitting \n should 165 # potentially causing e.g. \r\n to be emitted. Since emitting \n should
166 # "just work," here we change the sys.* streams to disable line ending 166 # "just work," here we change the sys.* streams to disable line ending
167 # normalization, ensuring compatibility with our ui type. 167 # normalization, ensuring compatibility with our ui type.
168 168
169 # write_through is new in Python 3.7. 169 if sys.stdout is not None:
170 kwargs = { 170 # write_through is new in Python 3.7.
171 "newline": "\n", 171 kwargs = {
172 "line_buffering": sys.stdout.line_buffering, 172 "newline": "\n",
173 } 173 "line_buffering": sys.stdout.line_buffering,
174 if util.safehasattr(sys.stdout, "write_through"): 174 }
175 kwargs["write_through"] = sys.stdout.write_through 175 if util.safehasattr(sys.stdout, "write_through"):
176 sys.stdout = io.TextIOWrapper( 176 kwargs["write_through"] = sys.stdout.write_through
177 sys.stdout.buffer, sys.stdout.encoding, sys.stdout.errors, **kwargs 177 sys.stdout = io.TextIOWrapper(
178 ) 178 sys.stdout.buffer,
179 179 sys.stdout.encoding,
180 kwargs = { 180 sys.stdout.errors,
181 "newline": "\n", 181 **kwargs
182 "line_buffering": sys.stderr.line_buffering, 182 )
183 } 183
184 if util.safehasattr(sys.stderr, "write_through"): 184 if sys.stderr is not None:
185 kwargs["write_through"] = sys.stderr.write_through 185 kwargs = {
186 sys.stderr = io.TextIOWrapper( 186 "newline": "\n",
187 sys.stderr.buffer, sys.stderr.encoding, sys.stderr.errors, **kwargs 187 "line_buffering": sys.stderr.line_buffering,
188 ) 188 }
189 if util.safehasattr(sys.stderr, "write_through"):
190 kwargs["write_through"] = sys.stderr.write_through
191 sys.stderr = io.TextIOWrapper(
192 sys.stderr.buffer,
193 sys.stderr.encoding,
194 sys.stderr.errors,
195 **kwargs
196 )
189 197
190 if sys.stdin is not None: 198 if sys.stdin is not None:
191 # No write_through on read-only stream. 199 # No write_through on read-only stream.
192 sys.stdin = io.TextIOWrapper( 200 sys.stdin = io.TextIOWrapper(
193 sys.stdin.buffer, 201 sys.stdin.buffer,
198 line_buffering=sys.stdin.line_buffering, 206 line_buffering=sys.stdin.line_buffering,
199 ) 207 )
200 208
201 def _silencestdio(): 209 def _silencestdio():
202 for fp in (sys.stdout, sys.stderr): 210 for fp in (sys.stdout, sys.stderr):
211 if fp is None:
212 continue
203 # Check if the file is okay 213 # Check if the file is okay
204 try: 214 try:
205 fp.flush() 215 fp.flush()
206 continue 216 continue
207 except IOError: 217 except IOError: