Selaa lähdekoodia

maint: callback name more semantic

wangweimin 5 vuotta sitten
vanhempi
säilyke
f9189df636
2 muutettua tiedostoa jossa 10 lisäystä ja 3 poistoa
  1. 2 2
      pywebio/session/threadbased.py
  2. 8 1
      pywebio/utils.py

+ 2 - 2
pywebio/session/threadbased.py

@@ -7,7 +7,7 @@ from functools import wraps
 
 from .base import AbstractSession
 from ..exceptions import SessionNotFoundException, SessionClosedException, SessionException
-from ..utils import random_str, LimitedSizeQueue, isgeneratorfunction, iscoroutinefunction, catch_exp_call
+from ..utils import random_str, LimitedSizeQueue, isgeneratorfunction, iscoroutinefunction, catch_exp_call, get_function_name
 
 logger = logging.getLogger(__name__)
 
@@ -275,7 +275,7 @@ class ThreadBasedSession(AbstractSession):
             "not coroutine function or generator function. ")
 
         self._activate_callback_env()
-        callback_id = 'CB-%s-%s' % (getattr(callback, '__name__', ''), random_str(10))
+        callback_id = 'CB-%s-%s' % (get_function_name(callback, 'callback'), random_str(10))
         self.callbacks[callback_id] = (callback, serial_mode)
         return callback_id
 

+ 8 - 1
pywebio/utils.py

@@ -5,11 +5,12 @@ import queue
 import random
 import socket
 import string
-import time
 from collections import OrderedDict
 from contextlib import closing
 from os.path import abspath, dirname
 
+import time
+
 project_dir = dirname(abspath(__file__))
 
 STATIC_PATH = '%s/html' % project_dir
@@ -40,6 +41,12 @@ def isgeneratorfunction(object):
     return inspect.isgeneratorfunction(object)
 
 
+def get_function_name(func, default=None):
+    while isinstance(func, functools.partial):
+        func = func.func
+    return getattr(func, '__name__', default)
+
+
 class LimitedSizeQueue(queue.Queue):
     """
     有限大小的队列