[PATCH 1 of 6] profile: upgrade the "profile" context manager to a full class
Yuya Nishihara
yuya at tcha.org
Mon Jun 12 13:33:30 UTC 2017
On Fri, 09 Jun 2017 13:32:57 +0100, Pierre-Yves David wrote:
> # HG changeset patch
> # User Pierre-Yves David <pierre-yves.david at octobus.net>
> # Date 1496882328 -3600
> # Thu Jun 08 01:38:48 2017 +0100
> # Node ID a3a4b52464f3dbaf533f62bca7512f42c6a6d449
> # Parent 3ef319e9505f376775c91b2ab7d89ac9ac4343e9
> # EXP-Topic profile
> # Available At https://www.mercurial-scm.org/repo/users/marmoute/mercurial/
> # hg pull https://www.mercurial-scm.org/repo/users/marmoute/mercurial/ -r a3a4b52464f3
> profile: upgrade the "profile" context manager to a full class
Generally looks good. Queued, thanks.
> + def __enter__(self):
> + profiler = encoding.environ.get('HGPROF')
> + proffn = None
> + if profiler is None:
> + profiler = self._ui.config('profiling', 'type', default='stat')
> + if profiler not in ('ls', 'stat', 'flame'):
> + # try load profiler from extension with the same name
> + proffn = _loadprofiler(self._ui, profiler)
> + if proffn is None:
> + self._ui.warn(_("unrecognized profiler '%s' - ignored\n")
> + % profiler)
> + profiler = 'stat'
>
> - if output == 'blackbox':
> - fp = util.stringio()
> - elif output:
> - path = ui.expandpath(output)
> - fp = open(path, 'wb')
> - else:
> - fp = ui.ferr
> + self._output = self._ui.config('profiling', 'output')
>
> - try:
> + if self._output == 'blackbox':
> + self._fp = util.stringio()
> + elif self._output:
> + path = self._ui.expandpath(self._output)
> + self._fp = open(path, 'wb')
> + else:
> + self._fp = self._ui.ferr
> +
> if proffn is not None:
> pass
> elif profiler == 'ls':
> @@ -179,18 +185,21 @@ def profile(ui):
> else:
> proffn = statprofile
>
> - with proffn(ui, fp):
> - yield
> + self._profiler = proffn(self._ui, self._fp)
> + self._profiler.__enter__()
Nit: Perhaps the output file should be closed if the initialization of
self._profiler failed.
More information about the Mercurial-devel
mailing list