Mercurial > hg
comparison mercurial/profiling.py @ 32805:2b0fc56840d0
profile: use explicit logic to control file closing
We make the decision to close 'fp' more explicit instead of relying on the
implication of other variable. This makes the overall logic more robust.
author | Pierre-Yves David <pierre-yves.david@octobus.net> |
---|---|
date | Mon, 12 Jun 2017 17:14:56 +0200 |
parents | c0b2c8f25ad9 |
children | 3a4c677cbd6e |
comparison
equal
deleted
inserted
replaced
32804:c0b2c8f25ad9 | 32805:2b0fc56840d0 |
---|---|
149 """ | 149 """ |
150 def __init__(self, ui, enabled=True): | 150 def __init__(self, ui, enabled=True): |
151 self._ui = ui | 151 self._ui = ui |
152 self._output = None | 152 self._output = None |
153 self._fp = None | 153 self._fp = None |
154 self._fpdoclose = True | |
154 self._profiler = None | 155 self._profiler = None |
155 self._enabled = enabled | 156 self._enabled = enabled |
156 self._entered = False | 157 self._entered = False |
157 self._started = False | 158 self._started = False |
158 | 159 |
191 self._fp = util.stringio() | 192 self._fp = util.stringio() |
192 elif self._output: | 193 elif self._output: |
193 path = self._ui.expandpath(self._output) | 194 path = self._ui.expandpath(self._output) |
194 self._fp = open(path, 'wb') | 195 self._fp = open(path, 'wb') |
195 else: | 196 else: |
197 self._fpdoclose = False | |
196 self._fp = self._ui.ferr | 198 self._fp = self._ui.ferr |
197 | 199 |
198 if proffn is not None: | 200 if proffn is not None: |
199 pass | 201 pass |
200 elif profiler == 'ls': | 202 elif profiler == 'ls': |
219 val = val.replace('%', '%%') | 221 val = val.replace('%', '%%') |
220 self._ui.log('profile', val) | 222 self._ui.log('profile', val) |
221 self._closefp() | 223 self._closefp() |
222 | 224 |
223 def _closefp(self): | 225 def _closefp(self): |
224 self._fp.close() | 226 if self._fpdoclose and self._fp is not None: |
227 self._fp.close() |