Mercurial > hg
comparison mercurial/profiling.py @ 44192:d6d4170882cd
profiling: flush stdout before writing profile to stderr
On py3, stdout and stderr appear to be buffered and this causes my command's
output to be intermixed with the profiling output.
Differential Revision: https://phab.mercurial-scm.org/D8024
author | Kyle Lippincott <spectral@google.com> |
---|---|
date | Mon, 27 Jan 2020 18:16:05 -0800 |
parents | 882e633ac92c |
children | 5a6a1cd21f09 |
comparison
equal
deleted
inserted
replaced
44191:732098027b34 | 44192:d6d4170882cd |
---|---|
184 def __init__(self, ui, enabled=True): | 184 def __init__(self, ui, enabled=True): |
185 self._ui = ui | 185 self._ui = ui |
186 self._output = None | 186 self._output = None |
187 self._fp = None | 187 self._fp = None |
188 self._fpdoclose = True | 188 self._fpdoclose = True |
189 self._flushfp = None | |
189 self._profiler = None | 190 self._profiler = None |
190 self._enabled = enabled | 191 self._enabled = enabled |
191 self._entered = False | 192 self._entered = False |
192 self._started = False | 193 self._started = False |
193 | 194 |
244 self._fpdoclose = False | 245 self._fpdoclose = False |
245 self._fp = uifp(self._ui) | 246 self._fp = uifp(self._ui) |
246 else: | 247 else: |
247 self._fpdoclose = False | 248 self._fpdoclose = False |
248 self._fp = self._ui.ferr | 249 self._fp = self._ui.ferr |
250 # Ensure we've flushed fout before writing to ferr. | |
251 self._flushfp = self._ui.fout | |
249 | 252 |
250 if proffn is not None: | 253 if proffn is not None: |
251 pass | 254 pass |
252 elif profiler == b'ls': | 255 elif profiler == b'ls': |
253 proffn = lsprofile | 256 proffn = lsprofile |
263 raise | 266 raise |
264 | 267 |
265 def __exit__(self, exception_type, exception_value, traceback): | 268 def __exit__(self, exception_type, exception_value, traceback): |
266 propagate = None | 269 propagate = None |
267 if self._profiler is not None: | 270 if self._profiler is not None: |
271 self._uiflush() | |
268 propagate = self._profiler.__exit__( | 272 propagate = self._profiler.__exit__( |
269 exception_type, exception_value, traceback | 273 exception_type, exception_value, traceback |
270 ) | 274 ) |
271 if self._output == b'blackbox': | 275 if self._output == b'blackbox': |
272 val = b'Profile:\n%s' % self._fp.getvalue() | 276 val = b'Profile:\n%s' % self._fp.getvalue() |
278 return propagate | 282 return propagate |
279 | 283 |
280 def _closefp(self): | 284 def _closefp(self): |
281 if self._fpdoclose and self._fp is not None: | 285 if self._fpdoclose and self._fp is not None: |
282 self._fp.close() | 286 self._fp.close() |
287 | |
288 def _uiflush(self): | |
289 if self._flushfp: | |
290 self._flushfp.flush() |