diff mercurial/hook.py @ 20548:5bd6a9fec103

hooks: for python hooks, consistently use __name__ etc as name, not the repr There is no reason to expose unnecessary Python implementation details and memory locations, also not in debug mode. readablefunc was already creating a nice name - we move that functionality up and reuse it. We consider having a __call__ and being types.FunctionType sufficiently similar and unify these two to just using the existing check for __call__.
author Mads Kiilerich <madski@unity3d.com>
date Sat, 15 Feb 2014 01:23:12 +0100
parents 9d9f8ccffead
children b009dd135aa0
line wrap: on
line diff
--- a/mercurial/hook.py	Sat Feb 15 01:23:12 2014 +0100
+++ b/mercurial/hook.py	Sat Feb 15 01:23:12 2014 +0100
@@ -6,7 +6,7 @@
 # GNU General Public License version 2 or any later version.
 
 from i18n import _
-import os, sys, time, types
+import os, sys, time
 import extensions, util, demandimport
 
 def _pythonhook(ui, repo, name, hname, funcname, args, throw):
@@ -19,8 +19,10 @@
     unmodified commands (e.g. mercurial.commands.update) can
     be run as hooks without wrappers to convert return values.'''
 
-    obj = funcname
-    if not util.safehasattr(obj, '__call__'):
+    if util.safehasattr(funcname, '__call__'):
+        obj = funcname
+        funcname = obj.__module__ + "." + obj.__name__
+    else:
         d = funcname.rfind('.')
         if d == -1:
             raise util.Abort(_('%s hook is invalid ("%s" not in '
@@ -101,11 +103,8 @@
     finally:
         sys.stdout, sys.stderr, sys.stdin = old
         duration = time.time() - starttime
-        readablefunc = funcname
-        if isinstance(funcname, types.FunctionType):
-            readablefunc = funcname.__module__ + "." + funcname.__name__
         ui.log('pythonhook', 'pythonhook-%s: %s finished in %0.2f seconds\n',
-               name, readablefunc, duration)
+               name, funcname, duration)
     if r:
         if throw:
             raise util.Abort(_('%s hook failed') % hname)