[repo threads] Add several safety when looking for a callable name. stable
authorPierre-Yves David <pierre-yves.david@logilab.fr>
Fri, 01 Oct 2010 17:03:01 +0200
branchstable
changeset 6381 c9eed5037223
parent 6375 df4fd2a1b0e7
child 6382 4efd0f07fd53
[repo threads] Add several safety when looking for a callable name. We lookup __name__ attribute, then func_name attribute. object repr is a last fallback
server/utils.py
--- a/server/utils.py	Fri Oct 01 11:09:25 2010 +0200
+++ b/server/utils.py	Fri Oct 01 17:03:01 2010 +0200
@@ -117,6 +117,14 @@
     sconfig.input_config(inputlevel=inputlevel)
     return sconfig
 
+_MARKER=object()
+def func_name(func):
+    name = getattr(func, '__name__', _MARKER)
+    if name is _MARKER:
+        name = getattr(func, 'func_name', _MARKER)
+    if name is _MARKER:
+        name = repr(func)
+    return name
 
 class LoopTask(object):
     """threaded task restarting itself once executed"""
@@ -124,7 +132,7 @@
         if interval <= 0:
             raise ValueError('Loop task interval must be > 0 '
                              '(current value: %f for %s)' % \
-                             (interval, func.__name__))
+                             (interval, func_name(func)))
         self.interval = interval
         def auto_restart_func(self=self, func=func, args=args):
             try:
@@ -132,7 +140,7 @@
             finally:
                 self.start()
         self.func = auto_restart_func
-        self.name = func.__name__
+        self.name = func_name(func)
 
     def __str__(self):
         return '%s (%s seconds)' % (self.name, self.interval)
@@ -162,7 +170,7 @@
                 self.running_threads.remove(self)
         Thread.__init__(self, target=auto_remove_func)
         self.running_threads = running_threads
-        self._name = target.__name__
+        self._name = func_name(target)
 
     def start(self):
         self.running_threads.append(self)