浏览代码

get_full_cls_name renamed to qualname

Ask Solem 13 年之前
父节点
当前提交
ab9c14523e

+ 3 - 3
celery/apps/beat.py

@@ -9,7 +9,7 @@ import traceback
 from .. import __version__, platforms
 from .. import beat
 from ..app import app_or_default
-from ..utils import get_full_cls_name, LOG_LEVELS
+from ..utils import LOG_LEVELS, qualname
 from ..utils.timeutils import humanize_seconds
 
 STARTUP_INFO_FMT = """
@@ -104,8 +104,8 @@ class Beat(object):
             "conninfo": self.app.broker_connection().as_uri(),
             "logfile": self.logfile or "[stderr]",
             "loglevel": LOG_LEVELS[self.loglevel],
-            "loader": get_full_cls_name(self.app.loader.__class__),
-            "scheduler": get_full_cls_name(scheduler.__class__),
+            "loader": qualname(self.app.loader),
+            "scheduler": qualname(scheduler),
             "scheduler_info": scheduler.info,
             "hmax_interval": humanize_seconds(beat.max_interval),
             "max_interval": beat.max_interval,

+ 2 - 2
celery/apps/worker.py

@@ -15,7 +15,7 @@ import warnings
 from .. import __version__, platforms, signals
 from ..app import app_or_default
 from ..exceptions import ImproperlyConfigured, SystemTerminate
-from ..utils import get_full_cls_name, isatty, LOG_LEVELS, cry
+from ..utils import isatty, LOG_LEVELS, cry, qualname
 from ..worker import WorkController
 
 try:
@@ -222,7 +222,7 @@ class Worker(object):
             "logfile": self.logfile or "[stderr]",
             "celerybeat": "ON" if self.run_clockservice else "OFF",
             "events": "ON" if self.events else "OFF",
-            "loader": get_full_cls_name(self.loader.__class__),
+            "loader": qualname(self.loader),
             "queues": app.amqp.queues.format(indent=18, indent_first=False),
         }
 

+ 2 - 2
celery/tests/functional/case.py

@@ -13,7 +13,7 @@ from time import time
 
 from celery.exceptions import TimeoutError
 from celery.task.control import ping, flatten_reply, inspect
-from celery.utils import get_full_cls_name
+from celery.utils import qualname
 
 from celery.tests.utils import unittest
 
@@ -84,7 +84,7 @@ class Worker(object):
     def managed(cls, hostname=None, caller=None):
         hostname = hostname or socket.gethostname()
         if caller:
-            hostname = ".".join([get_full_cls_name(caller), hostname])
+            hostname = ".".join([qualname(caller), hostname])
         else:
             hostname += str(cls.next_worker_id())
         worker = cls(hostname)

+ 3 - 2
celery/tests/test_utils/__init__.py

@@ -33,9 +33,10 @@ class test_chunks(unittest.TestCase):
 
 class test_utils(unittest.TestCase):
 
-    def test_get_full_cls_name(self):
+    def test_qualname(self):
         Class = type("Fox", (object, ), {"__module__": "quick.brown"})
-        self.assertEqual(utils.get_full_cls_name(Class), "quick.brown.Fox")
+        self.assertEqual(utils.qualname(Class), "quick.brown.Fox")
+        self.assertEqual(utils.qualname(Class()), "quick.brown.Fox")
 
     def test_is_iterable(self):
         for a in "f", ["f"], ("f", ), {"f": "f"}:

+ 6 - 3
celery/utils/__init__.py

@@ -71,7 +71,7 @@ def deprecated(description=None, deprecation=None, removal=None,
 
         @wraps(fun)
         def __inner(*args, **kwargs):
-            warn_deprecated(description=description or get_full_cls_name(fun),
+            warn_deprecated(description=description or qualname(fun),
                             deprecation=deprecation,
                             removal=removal,
                             alternative=alternative)
@@ -272,8 +272,11 @@ if sys.version_info >= (3, 3):
 else:
 
     def qualname(obj):  # noqa
-        return '.'.join([cls.__module__, cls.__name__])
-get_full_cls_name = qualname
+        if not hasattr(obj, "__name__") and hasattr(obj, "__class__"):
+            return qualname(obj.__class__)
+
+        return '.'.join([obj.__module__, obj.__name__])
+get_full_cls_name = qualname  # XXX Compat
 
 
 def fun_takes_kwargs(fun, kwlist=[]):

+ 3 - 0
celery/utils/encoding.py

@@ -58,6 +58,9 @@ else:
     def from_utf8(s, *args, **kwargs):  # noqa
         return s.encode("utf-8", *args, **kwargs)
 
+    def default_encode(obj):            # noqa
+        return unicode(obj, default_encoding())
+
     str_t = unicode
     bytes_t = str
     ensure_bytes = str_to_bytes