Comments
Patch
@@ -893,31 +893,22 @@ def _dispatch(req):
try:
return runcommand(lui, repo, cmd, fullargs, ui, options, d,
cmdpats, cmdoptions)
finally:
if repo and repo != req.repo:
repo.close()
def _runcommand(ui, options, cmd, cmdfunc):
- """Enables the profiler if applicable.
-
- ``profiling.enabled`` - boolean config that enables or disables profiling
- """
- def checkargs():
+ """Run a command function, possibly with profiling enabled."""
+ with profiling.maybeprofile(ui):
try:
return cmdfunc()
except error.SignatureError:
- raise error.CommandError(cmd, _("invalid arguments"))
-
- if ui.configbool('profiling', 'enabled'):
- with profiling.profile(ui):
- return checkargs()
- else:
- return checkargs()
+ raise error.CommandError(cmd, _('invalid arguments'))
def _exceptionwarning(ui):
"""Produce a warning message for the current active exception"""
# For compatibility checking, we discard the portion of the hg
# version after the + on the assumption that if a "normal
# user" is running a build with a + in it the packager
# probably built from fairly close to a tag and anyone with a
@@ -137,8 +137,25 @@ def profile(ui):
if output:
if output == 'blackbox':
val = 'Profile:\n%s' % fp.getvalue()
# ui.log treats the input as a format string,
# so we need to escape any % signs.
val = val.replace('%', '%%')
ui.log('profile', val)
fp.close()
+
+@contextlib.contextmanager
+def maybeprofile(ui):
+ """Profile if enabled, else do nothing.
+
+ This context manager can be used to optionally profile if profiling
+ is enabled. Otherwise, it does nothing.
+
+ The purpose of this context manager is to make calling code simpler:
+ just use a single code path for calling into code you may want to profile
+ and this function determines whether to start profiling.
+ """
+ if ui.configbool('profiling', 'enabled'):
+ with profile(ui):
+ yield
+ else:
+ yield