Mercurial > public > mercurial-scm > hg
comparison mercurial/dispatch.py @ 29784:e3501546f7e4
profiling: add a context manager that no-ops if profiling isn't enabled
And refactor dispatch.py to use it. As you can see, the resulting code
is much simpler.
I was tempted to inline _runcommand as part of writing this series.
However, a number of extensions wrap _runcommand. So keeping it around
is necessary (extensions can't easily wrap runcommand because it calls
hooks before and after command execution).
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Sun, 14 Aug 2016 17:51:12 -0700 |
parents | 5d44197c208b |
children | 61a4cdc98307 |
comparison
equal
deleted
inserted
replaced
29783:5d44197c208b | 29784:e3501546f7e4 |
---|---|
896 finally: | 896 finally: |
897 if repo and repo != req.repo: | 897 if repo and repo != req.repo: |
898 repo.close() | 898 repo.close() |
899 | 899 |
900 def _runcommand(ui, options, cmd, cmdfunc): | 900 def _runcommand(ui, options, cmd, cmdfunc): |
901 """Enables the profiler if applicable. | 901 """Run a command function, possibly with profiling enabled.""" |
902 | 902 with profiling.maybeprofile(ui): |
903 ``profiling.enabled`` - boolean config that enables or disables profiling | |
904 """ | |
905 def checkargs(): | |
906 try: | 903 try: |
907 return cmdfunc() | 904 return cmdfunc() |
908 except error.SignatureError: | 905 except error.SignatureError: |
909 raise error.CommandError(cmd, _("invalid arguments")) | 906 raise error.CommandError(cmd, _('invalid arguments')) |
910 | |
911 if ui.configbool('profiling', 'enabled'): | |
912 with profiling.profile(ui): | |
913 return checkargs() | |
914 else: | |
915 return checkargs() | |
916 | 907 |
917 def _exceptionwarning(ui): | 908 def _exceptionwarning(ui): |
918 """Produce a warning message for the current active exception""" | 909 """Produce a warning message for the current active exception""" |
919 | 910 |
920 # For compatibility checking, we discard the portion of the hg | 911 # For compatibility checking, we discard the portion of the hg |